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

tomek at codespeak.net tomek at codespeak.net
Sun Jun 22 19:53:30 CEST 2003


Author: tomek
Date: Sun Jun 22 19:53:29 2003
New Revision: 966

Modified:
   pypy/trunk/src/pypy/objspace/std/stringobject.py
   pypy/trunk/src/pypy/objspace/std/stringtype.py
   pypy/trunk/src/pypy/objspace/std/test/test_stringobject.py
Log:
I added following methods to the string object: lower, index, find, rindex, rfind


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	Sun Jun 22 19:53:29 2003
@@ -42,8 +42,8 @@
 encode            !Unicode not supported now
 endswith          str_endswith__String_String    [optional arguments not supported now]
 expandtabs        str_expandtabs__String_Int
-find              *Tomek
-index             *Tomek
+find              OK, nur noch tests
+index             OK, nur noch tests
 isalnum           def str_isalnum__String(space, w_self): def _isalnum(ch):
 isalpha           def str_isalpha__String(space, w_self): def _isalpha(ch):
 isdigit           def str_isdigit__String(space, w_self): def _isdigit(ch):
@@ -53,11 +53,11 @@
 isupper           def str_isupper__String(space, w_self): def _isupper(ch):
 join              def str_join__String_ANY(space, w_self, w_list):
 ljust             def str_ljust__String_ANY(space, w_self, w_arg):
-lower
+lower             OK
 lstrip            def str_lstrip__String(space, w_self):
 replace           *Tomek
-rfind             *Tomek
-rindex            *Tomek
+rfind             OK, nur noch tests
+rindex            OK, nur noch tests
 rjust             def str_rjust__String_ANY(space, w_self, w_arg):
 rstrip            def str_rstrip__String(space, w_self):
 split             def str_split__String_None_Int(space, w_self, w_none, w_maxsplit=-1):def str_split__String_String_Int(space, w_self, w_by, w_maxsplit=-1):
@@ -164,11 +164,25 @@
         ch = self[i]
         if _islower(ch):
             o = ord(ch) - 32
-            buf[i] = chr(o)
+            res[i] = chr(o)
         else:
-            buf[i] = ch
+            res[i] = ch
 
     return space.wrap("".join(res))
+
+def str_lower__String(space, w_self):
+    self = space.unwrap(w_self)
+    res = [' '] * len(self)
+    for i in range(len(self)):
+        ch = self[i]
+        if _isupper(ch):
+            o = ord(ch) + 32
+            res[i] = chr(o)
+        else:
+            res[i] = ch
+
+    return space.wrap("".join(res))
+
     
 def str_capitalize__String(space, w_self):
     input = space.unwrap(w_self)
@@ -296,12 +310,12 @@
                 pos = pos + len(item)
             else:
                 for i in range(len(self)):
-                    res[i+pos] = item[i]
-                    pos = pos + len(self)
-    
+                    res[i+pos] = self[i]
+                pos = pos + len(self)
+                 
                 for i in range(len(item)):
                     res[i+pos] = item[i]
-                    pos = pos + len(item)
+                pos = pos + len(item)
 
         return space.wrap("".join(res))
     else:
@@ -334,34 +348,86 @@
     return space.wrap(u_self)
 
 def str_find__String_String_Int_Int(space, w_self, w_sub, w_start=None, w_end=None):
-    start = space.unwrap(w_start)
-    end = space.unwrap(w_end)
 
-    self = space.unwrap(w_self)
-    sub = space.unwrap(w_sub)
+    u = space.unwrap 
+    res = _find(u(w_self), u(w_sub), u(w_start), u(w_end), 1)
+    return space.wrap(res)
+
+def str_rfind__String_String_Int_Int(space, w_self, w_sub, w_start=None, w_end=None):
 
-    if start is None:
+    u = space.unwrap
+    res = _find(u(w_self), u(w_sub), u(w_start), u(w_end), -1)
+    return space.wrap(res)
+
+def str_index__String_String_Int_Int(space, w_self, w_sub, w_start=None, w_end=None):
+
+    u = space.unwrap
+    res = _find(u(w_self), u(w_sub), u(w_start), u(w_end), 1)
+    if res == -1:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("substring not found in string.index"))
+
+    return space.wrap(res)
+
+
+def str_rindex__String_String_Int_Int(space, w_self, w_sub, w_start=None, w_end=None):
+
+    u = space.unwrap
+    res = _find(u(w_self), u(w_sub), u(w_start), u(w_end), -1)
+    if res == -1:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("substring not found in string.rindex"))
+
+    return space.wrap(res)
+
+
+def _find(self, sub, start, end, dir):
+
+    length = len(self)
+
+    #adjust_indicies
+    if (end > length):
+        end = length
+    elif (end < 0):
+        end += length
+    if (end < 0):
+        end = 0
+    if (start < 0):
+        start += length
+    if (start < 0):
         start = 0
 
-    if end is None:
-        end = self.len
+    if dir > 0:
+        if len(sub) == 0 and start < end:
+            return start
+
+        end = end - len(sub) + 1
+
+        for i in range(start, end):
+            match = 1
+            for idx in range(len(sub)):
+                if sub[idx] != self[idx+i]:
+                    match = 0
+                    break
+            if match: 
+                return i
+        return -1
+    else:
+        if len(sub) == 0 and start < end:
+            return last
 
-    maxend = self.len - sub.len
+        end = end - len(sub)
 
-    if end > maxend:
-        end = maxend
+        for j in range(end, start+1, -1):
+            match = 1
+            for idx in range(len(sub)):
+                if sub[idx] != self[idx+j]:
+                    match = 0
+                    break
+            if match:
+                return j
+        return -1        
 
-    if sub.len == 0 and start < end:
-        return start
-
-    for i in range(start, end):
-        match = 1
-        for idx in range(sub.len):
-            if not sub[idx] == self[idx+i]:
-                match = 0
-                break
-        return i
-    return -1
 
 
 def str_strip__String(space, w_self):

Modified: pypy/trunk/src/pypy/objspace/std/stringtype.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/stringtype.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/stringtype.py	Sun Jun 22 19:53:29 2003
@@ -19,11 +19,16 @@
     str_ljust      = MultiMethod('ljust', 2)
     str_rjust      = MultiMethod('rjust', 2)
     str_upper      = MultiMethod('upper', 1)
+    str_lower      = MultiMethod('lower', 1)
     str_capitalize = MultiMethod('capitalize', 1)
     str_title      = MultiMethod('title', 1)
     #XXX we need to have the possibility to specify, if the a parameter
     #was given
-    str_find       = MultiMethod('find', 4, defaults=(0, 0))
+    str_find       = MultiMethod('find', 4, defaults=(None, None))
+    str_rfind      = MultiMethod('rfind', 4, defaults=(None, None))
+    str_index      = MultiMethod('index', 4, defaults=(None, None))
+    str_rindex     = MultiMethod('rindex', 4, defaults=(None, None))
+
     str_strip      = MultiMethod('strip', 1)
     str_rstrip     = MultiMethod('rstrip', 1)
     str_lstrip     = MultiMethod('lstrip', 1)

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	Sun Jun 22 19:53:29 2003
@@ -202,11 +202,24 @@
 
     def test_split_maxsplit(self):
         self.assertEquals("/a/b/c".split('/', 2), ['','a','b/c'])
+        self.assertEquals("a/b/c".split("/"), ['a', 'b', 'c'])
         self.assertEquals(" a ".split(None, 0), ['a '])
         self.assertEquals(" a ".split(None, 1), ['a'])
         self.assertEquals(" a a ".split(" ", 0), [' a a '])
         self.assertEquals(" a a ".split(" ", 1), ['', 'a a '])
 
+    def test_join(self):
+        self.assertEquals(", ".join(['a', 'b', 'c']), "a, b, c")
+        self.assertEquals("".join([]), "")
+        self.assertEquals("-".join(['a', 'b']), 'a-b')
+
+    def test_lower(self):
+        self.assertEquals("aaa AAA".lower(), "aaa aaa")
+        self.assertEquals("".lower(), "")
+
+    def test_upper(self):
+        self.assertEquals("aaa AAA".upper(), "AAA AAA")
+        self.assertEquals("".upper(), "")
 
 if __name__ == '__main__':
     test.main()


More information about the Pypy-commit mailing list