Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r59131:ec104e524331
Date: 2012-11-28 23:35 +0100
http://bitbucket.org/pypy/pypy/changeset/ec104e524331/

Log:    cpyext: add PyUnicode_CompareWithASCIIString

diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -2316,15 +2316,6 @@
     raise NotImplementedError
     
 
-@cpython_api([PyObject, rffi.CCHARP], rffi.INT_real, error=-1)
-def PyUnicode_CompareWithASCIIString(space, uni, string):
-    """Compare a unicode object, uni, with string and return -1, 0, 1 for less
-    than, equal, and greater than, respectively. It is best to pass only
-    ASCII-encoded strings, but the function interprets the input string as
-    ISO-8859-1 if it contains non-ASCII characters"."""
-    raise NotImplementedError
-    
-
 @cpython_api([PyObject, PyObject, rffi.INT_real], PyObject)
 def PyUnicode_RichCompare(space, left, right, op):
     """Rich compare two unicode strings and return one of the following:
diff --git a/pypy/module/cpyext/test/test_unicodeobject.py 
b/pypy/module/cpyext/test/test_unicodeobject.py
--- a/pypy/module/cpyext/test/test_unicodeobject.py
+++ b/pypy/module/cpyext/test/test_unicodeobject.py
@@ -131,6 +131,30 @@
         res = module.aswidecharstring("Caf\xe9")
         assert res == ("Caf\xe9\0", 4)
 
+    def test_CompareWithASCIIString(self):
+        module = self.import_extension('foo', [
+            ("compare", "METH_VARARGS",
+             '''
+             PyObject *uni;
+             const char* s;
+             int res;
+
+             if (!PyArg_ParseTuple(args, "Uy", &uni, &s))
+                 return NULL;
+
+             res = PyUnicode_CompareWithASCIIString(uni, s);
+             return PyLong_FromLong(res);
+             ''')])
+        assert module.compare("abc", b"abc") == 0
+        assert module.compare("abd", b"abc") == 1
+        assert module.compare("abb", b"abc") == -1
+        assert module.compare("caf\xe9", b"caf\xe9") == 0
+        assert module.compare("abc", b"ab") == 1
+        assert module.compare("ab\0", b"ab") == 1
+        assert module.compare("ab", b"abc") == -1
+        assert module.compare("", b"abc") == -1
+        assert module.compare("abc", b"") == 1
+
 
 class TestUnicode(BaseApiTest):
     def test_unicodeobject(self, space, api):
diff --git a/pypy/module/cpyext/unicodeobject.py 
b/pypy/module/cpyext/unicodeobject.py
--- a/pypy/module/cpyext/unicodeobject.py
+++ b/pypy/module/cpyext/unicodeobject.py
@@ -748,6 +748,31 @@
         return 1
     return 0
 
+@cpython_api([PyObject, CONST_STRING], rffi.INT_real, error=CANNOT_FAIL)
+def PyUnicode_CompareWithASCIIString(space, w_uni, string):
+    """Compare a unicode object, uni, with string and return -1, 0, 1 for less
+    than, equal, and greater than, respectively. It is best to pass only
+    ASCII-encoded strings, but the function interprets the input string as
+    ISO-8859-1 if it contains non-ASCII characters."""
+    uni = space.unicode_w(w_uni)
+    i = 0
+    # Compare Unicode string and source character set string
+    while i < len(uni) and string[i] != '\0':
+        u = ord(uni[i])
+        s = ord(string[i])
+        if u != s:
+            if u < s:
+                return -1
+            else:
+                return 1
+        i += 1
+    if i < len(uni):
+        return 1  # uni is longer
+    if string[i] != '\0':
+        return -1  # str is longer
+    return 0
+    
+
 @cpython_api([rffi.CWCHARP, rffi.CWCHARP, Py_ssize_t], lltype.Void)
 def Py_UNICODE_COPY(space, target, source, length):
     """Roughly equivalent to memcpy() only the base size is Py_UNICODE
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to