Merge branch 'master' of github.com:mitsuhiko/markupsafe
diff --git a/CHANGES b/CHANGES
index 21a7e06..ec33090 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,12 @@
 MarkupSafe Changelog
 ====================
 
+Version 0.13
+------------
+
+- Do not attempt to compile extension for PyPy or Jython.
+- Work around some 64bit Windows issues.
+
 Version 0.12
 ------------
 
diff --git a/markupsafe/_speedups.c b/markupsafe/_speedups.c
index 2a09b42..f349feb 100644
--- a/markupsafe/_speedups.c
+++ b/markupsafe/_speedups.c
@@ -64,9 +64,9 @@
 
 	/* First we need to figure out how long the escaped string will be */
 	while (*(inp) || inp < inp_end) {
-		if (*inp < ESCAPED_CHARS_TABLE_SIZE && escaped_chars_delta_len[*inp]) {
+		if (*inp < ESCAPED_CHARS_TABLE_SIZE) {
 			delta += escaped_chars_delta_len[*inp];
-			++erepl;
+			erepl += !!escaped_chars_delta_len[*inp];
 		}
 		++inp;
 	}
diff --git a/setup.py b/setup.py
index 0e092a0..6a516f9 100644
--- a/setup.py
+++ b/setup.py
@@ -9,6 +9,9 @@
 # fail safe compilation shamelessly stolen from the simplejson
 # setup.py file.  Original author: Bob Ippolito
 
+is_jython = 'java' in sys.platform
+is_pypy = hasattr(sys, 'pypy_version_info')
+
 
 speedups = Feature(
     'optional C speed-enhancement module',
@@ -97,21 +100,29 @@
     )
 
 
-try:
-    run_setup(True)
-except BuildFailed:
-    LINE = '=' * 74
-    BUILD_EXT_WARNING = 'WARNING: The C extension could not be compiled, speedups are not enabled.'
+def try_building_extension():
+    try:
+        run_setup(True)
+    except BuildFailed:
+        LINE = '=' * 74
+        BUILD_EXT_WARNING = 'WARNING: The C extension could not be ' \
+                            'compiled, speedups are not enabled.'
 
-    echo(LINE)
-    echo(BUILD_EXT_WARNING)
-    echo('Failure information, if any, is above.')
-    echo('Retrying the build without the C extension now.')
-    echo()
+        echo(LINE)
+        echo(BUILD_EXT_WARNING)
+        echo('Failure information, if any, is above.')
+        echo('Retrying the build without the C extension now.')
+        echo()
 
-    run_setup(False)
+        run_setup(False)
 
-    echo(LINE)
-    echo(BUILD_EXT_WARNING)
-    echo('Plain-Python installation succeeded.')
-    echo(LINE)
+        echo(LINE)
+        echo(BUILD_EXT_WARNING)
+        echo('Plain-Python installation succeeded.')
+        echo(LINE)
+
+
+if not (is_pypy or is_jython):
+    try_building_extension()
+else:
+    run_setpu(False)