[pypy-svn] r21048 - in pypy/dist/pypy: annotation rpython rpython/test

sanxiyn at codespeak.net sanxiyn at codespeak.net
Sun Dec 11 16:23:39 CET 2005


Author: sanxiyn
Date: Sun Dec 11 16:23:34 2005
New Revision: 21048

Modified:
   pypy/dist/pypy/annotation/unaryop.py
   pypy/dist/pypy/rpython/rstr.py
   pypy/dist/pypy/rpython/test/test_rstr.py
Log:
str.{strip,lstrip,rstrip} on RPython


Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Sun Dec 11 16:23:34 2005
@@ -368,6 +368,15 @@
     def method_rfind(str, frag, start=None, end=None):
         return SomeInteger()
 
+    def method_strip(str, chr):
+        return SomeString()
+
+    def method_lstrip(str, chr):
+        return SomeString()
+
+    def method_rstrip(str, chr):
+        return SomeString()
+
     def method_join(str, s_list):
         getbookkeeper().count("str_join", str)
         s_item = s_list.listdef.read_item()

Modified: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/rstr.py	(original)
+++ pypy/dist/pypy/rpython/rstr.py	Sun Dec 11 16:23:34 2005
@@ -138,6 +138,19 @@
     def rtype_method_rfind(self, hop):
         return self.rtype_method_find(hop, reverse=True)
 
+    def rtype_method_strip(_, hop, left=True, right=True):
+        v_str = hop.inputarg(string_repr, arg=0)
+        v_char = hop.inputarg(char_repr, arg=1)
+        v_left = hop.inputconst(Bool, left)
+        v_right = hop.inputconst(Bool, right)
+        return hop.gendirectcall(ll_strip, v_str, v_char, v_left, v_right)
+
+    def rtype_method_lstrip(self, hop):
+        return self.rtype_method_strip(hop, left=True, right=False)
+
+    def rtype_method_rstrip(self, hop):
+        return self.rtype_method_strip(hop, left=False, right=True)
+
     def rtype_method_upper(_, hop):
         v_str, = hop.inputargs(string_repr)
         hop.exception_cannot_occur()
@@ -822,6 +835,28 @@
 
 emptystr = string_repr.convert_const("")
 
+def ll_strip(s, ch, left, right):
+    s_len = len(s.chars)
+    if s_len == 0:
+        return emptystr
+    lpos = 0
+    rpos = s_len - 1
+    if left:
+        while lpos < rpos and s.chars[lpos] == ch:
+            lpos += 1
+    if right:
+        while lpos < rpos and s.chars[rpos] == ch:
+            rpos -= 1
+    r_len = rpos - lpos + 1
+    result = malloc(STR, r_len)
+    i = 0
+    j = lpos
+    while i < r_len:
+        result.chars[i] = s.chars[j]
+        i += 1
+        j += 1
+    return result
+
 def ll_upper(s):
     s_chars = s.chars
     s_len = len(s_chars)

Modified: pypy/dist/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rstr.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rstr.py	Sun Dec 11 16:23:34 2005
@@ -262,6 +262,20 @@
         res = interpret(fn, [ch])
         assert res == fn(ch)
 
+def test_strip():
+    def both():
+        return '!ab!'.strip('!')
+    def left():
+        return '!ab!'.lstrip('!')
+    def right():
+        return '!ab!'.rstrip('!')
+    res = interpret(both, [])
+    assert ''.join(res.chars) == 'ab'
+    res = interpret(left, [])
+    assert ''.join(res.chars) == 'ab!'
+    res = interpret(right, [])
+    assert ''.join(res.chars) == '!ab'
+
 def test_upper():
     strings = ['', ' ', 'upper', 'UpPeR', ',uppEr,']
     for i in range(256): strings.append(chr(i))



More information about the Pypy-commit mailing list