[pypy-svn] rev 677 - in pypy/trunk/src/pypy/objspace/std: . test

tomek at codespeak.net tomek at codespeak.net
Thu May 29 16:10:52 CEST 2003


Author: tomek
Date: Thu May 29 16:10:52 2003
New Revision: 677

Modified:
   pypy/trunk/src/pypy/objspace/std/stringobject.py
   pypy/trunk/src/pypy/objspace/std/test/test_stringobject.py
Log:
rich_compare in stringobject.py


Modified: pypy/trunk/src/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/stringobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/stringobject.py	Thu May 29 16:10:52 2003
@@ -110,6 +110,70 @@
 
 StdObjSpace.is_true.register(str_is_true, W_StringObject)
 
+
+EQ = 1
+LE = 2
+GE = 3
+GT = 4
+LT = 5
+NE = 6
+
+def string_richcompare(space, w_str1, w_str2, op):
+    str1 = w_str1._value
+    str2 = w_str2._value
+
+    if space.is_(str1, str2):
+        if op == EQ or op == LE or op == GE:
+            return space.w_True
+        elif op == GT or op == LT or op == NE:
+            return space.w_False
+    else:
+        if op == EQ:
+            if val1.len == val2.len:
+                for i in range(val1.len):
+                    if ord(val1.charat(i)) != ord(val2.charat(i)):
+                        return space.w_False
+                return space.w_True
+            else:
+                return space.w_False
+        else:
+            if val1.len > val2.len:
+                min_len = val2.len
+            else:
+                min_len = val1.len
+
+            idx = 0
+            if (min_len > 0):
+                while (c == 0) and (idx < min_len):
+                    c = ord(val1.charat[idx]) - ord(val2.charat[idx])
+                    idx = idx + 1
+            else:
+                c = 0
+
+        if (c == 0):
+            if val1.len < val2.len:
+                c = -1
+            elif val1.len > val2.leb:
+                c = 1
+            else:
+                c = 0
+
+        if op == LT:
+            return space.newbool(c < 0)
+        elif op == LE:
+            return space.newbool(c <= 0)
+        elif op == NE:
+            return space.newbool(c != 0)
+        elif op == GT:
+            return space.newbook(c > 0)
+        elif op == GE:
+            return space.newbool(c >= 0)
+        else:
+            raise NotImplemented
+
+
+
+
 def str_str_lt(space, w_str1, w_str2):
     i = w_str1._value.value()
     j = w_str2._value.value()

Modified: pypy/trunk/src/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_stringobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_stringobject.py	Thu May 29 16:10:52 2003
@@ -1,6 +1,11 @@
 import testsupport
+<<<<<<< .mine
+from pypy.objspace.std.stringobject import string_richcompare, W_StringObject, EQ, LT, GT, NE, LE, GE
+from pypy.objspace.std.objspace import StdObjSpace
+=======
 #from pypy.objspace.std.stringobject import W_StringObject
 #from pypy.objspace.std.objspace import StdObjSpace
+>>>>>>> .r676
 
 
 class TestW_StringObject(testsupport.TestCase):


More information about the Pypy-commit mailing list