[pypy-svn] r14064 - in pypy/branch/dist-2.4.1/pypy/objspace/std: . test

ignas at codespeak.net ignas at codespeak.net
Fri Jul 1 20:49:02 CEST 2005


Author: ignas
Date: Fri Jul  1 20:49:00 2005
New Revision: 14064

Modified:
   pypy/branch/dist-2.4.1/pypy/objspace/std/stringobject.py
   pypy/branch/dist-2.4.1/pypy/objspace/std/stringtype.py
   pypy/branch/dist-2.4.1/pypy/objspace/std/test/test_stringobject.py
Log:
Add rsplit method to a string type as in python 2.4.1


Modified: pypy/branch/dist-2.4.1/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/objspace/std/stringobject.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/objspace/std/stringobject.py	Fri Jul  1 20:49:00 2005
@@ -291,6 +291,70 @@
 
     return W_ListObject(w_self.space, res_w)
 
+def str_rsplit__String_None_ANY(space, w_self, w_none, w_maxsplit=-1):
+    res = []
+    inword = 0
+    value = w_self._value
+    maxsplit = space.int_w(w_maxsplit)
+
+    for i in range(len(value)-1, -1, -1):
+        ch = value[i]
+        if _isspace(ch):
+            if inword:
+                inword = 0
+        else:
+            if inword:
+                ch = ch + res[-1]
+                res[-1] = ch
+            else:
+                if maxsplit > -1:
+                    if maxsplit == 0:
+                        res.append(value[:i+1])
+                        break
+                    maxsplit = maxsplit - 1
+                res.append(ch)
+                inword = 1
+
+    res_w = [None] * len(res)
+    for i in range(len(res)):
+        res_w[i] = W_StringObject(space, res[i])
+    res_w.reverse()
+    return W_ListObject(space, res_w)
+
+def str_rsplit__String_String_ANY(space, w_self, w_by, w_maxsplit=-1):
+    res_w = []
+    value = w_self._value
+    end = len(value)
+    by = w_by._value
+    bylen = len(by)
+    if bylen == 0:
+        raise OperationError(space.w_ValueError, space.wrap("empty separator"))
+    maxsplit = space.int_w(w_maxsplit)
+
+    #if maxsplit is default, then you have no limit
+    #of the length of the resulting array
+    if maxsplit == -1:
+        splitcount = 1
+    else:
+        splitcount = maxsplit
+
+    while splitcount:
+        next = _find(value, by, 0, end, -1)
+        #next = value.rfind(by, end)    #of course we cannot use 
+                                        #the find method, 
+        if next < 0:
+            break
+        res_w.append(W_StringObject(space, value[next+bylen:end]))
+        end = next
+        #decrese the counter only then, when
+        #we don't have default maxsplit
+        if maxsplit > -1:
+            splitcount = splitcount - 1
+
+    res_w.append(W_StringObject(space, value[:end]))
+    res_w.reverse()
+    return W_ListObject(w_self.space, res_w)
+
 def str_join__String_ANY(space, w_self, w_list):
     list = space.unpackiterable(w_list)
     str_w = space.str_w

Modified: pypy/branch/dist-2.4.1/pypy/objspace/std/stringtype.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/objspace/std/stringtype.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/objspace/std/stringtype.py	Fri Jul  1 20:49:00 2005
@@ -5,6 +5,7 @@
 
 str_join    = MultiMethod('join', 2)
 str_split   = MultiMethod('split', 3, defaults=(None,-1))
+str_rsplit  = MultiMethod('rsplit', 3, defaults=(None,-1))
 str_isdigit    = MultiMethod('isdigit', 1)
 str_isalpha    = MultiMethod('isalpha', 1)
 str_isspace    = MultiMethod('isspace', 1)

Modified: pypy/branch/dist-2.4.1/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/objspace/std/test/test_stringobject.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/objspace/std/test/test_stringobject.py	Fri Jul  1 20:49:00 2005
@@ -142,6 +142,29 @@
         assert 'endcase test'.split('test') == ['endcase ', '']
         raises(ValueError, 'abc'.split, '')
 
+    def test_rsplit(self):
+        assert "".rsplit() == []
+        assert " ".rsplit() == []
+        assert "a".rsplit() == ['a']
+        assert "a".rsplit("a", 1) == ['', '']
+        assert " ".rsplit(" ", 1) == ['', '']
+        assert "aa".rsplit("a", 2) == ['', '', '']
+        assert " a ".rsplit() == ['a']
+        assert "a b c".rsplit() == ['a','b','c']
+        assert 'this is the rsplit function'.rsplit() == ['this', 'is', 'the', 'rsplit', 'function']
+        assert 'a|b|c|d'.rsplit('|') == ['a', 'b', 'c', 'd']
+        assert 'a|b|c|d'.rsplit('|', 2) == ['a|b', 'c', 'd']
+        assert 'a b c d'.rsplit(None, 1) == ['a b c', 'd']
+        assert 'a b c d'.rsplit(None, 2) == ['a b', 'c', 'd']
+        assert 'a b c d'.rsplit(None, 3) == ['a', 'b', 'c', 'd']
+        assert 'a b c d'.rsplit(None, 4) == ['a', 'b', 'c', 'd']
+        assert 'a b c d'.rsplit(None, 0) == ['a b c d']
+        assert 'a  b  c  d'.rsplit(None, 2) == ['a  b', 'c', 'd']
+        assert 'a b c d '.rsplit() == ['a', 'b', 'c', 'd']
+        assert 'a//b//c//d'.rsplit('//') == ['a', 'b', 'c', 'd']
+        assert 'endcase test'.rsplit('test') == ['endcase ', '']
+        raises(ValueError, 'abc'.rsplit, '')
+
     def test_split_splitchar(self):
         assert "/a/b/c".split('/') == ['','a','b','c']
 



More information about the Pypy-commit mailing list