[pypy-svn] r27238 - in pypy/dist/pypy/rpython: ootypesystem test

antocuni at codespeak.net antocuni at codespeak.net
Mon May 15 15:44:37 CEST 2006


Author: antocuni
Date: Mon May 15 15:44:29 2006
New Revision: 27238

Modified:
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/rpython/ootypesystem/rstr.py
   pypy/dist/pypy/rpython/test/test_rstr.py
Log:
Added support for slices to ootypesystem rstr.



Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Mon May 15 15:44:29 2006
@@ -289,6 +289,7 @@
             "ll_strip": Meth([Char, Bool, Bool], self.SELFTYPE_T),
             "ll_upper": Meth([], self.SELFTYPE_T),
             "ll_lower": Meth([], self.SELFTYPE_T),
+            "ll_substring": Meth([Signed, Signed], self.SELFTYPE_T), # ll_substring(start, count)
             })
         self._setup_methods(generic_types)
 
@@ -867,6 +868,9 @@
         # NOT_RPYTHON
         return make_string(self._str.lower())
 
+    def ll_substring(self, start, count):
+        # NOT_RPYTHON
+        return make_string(self._str[start:start+count])
 
 class _null_string(_null_mixin(_string), _string):
     def __init__(self, STRING):

Modified: pypy/dist/pypy/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rstr.py	Mon May 15 15:44:29 2006
@@ -129,6 +129,19 @@
             i += 1
         return buf.ll_build()
 
+    def ll_stringslice_startonly(s, start):
+        return s.ll_substring(start, s.ll_strlen() - start)
+
+    def ll_stringslice(s, slice):
+        start = slice.start
+        stop = slice.stop
+        length = s.ll_strlen()        
+        if stop > length:
+            stop = length
+        return s.ll_substring(start, stop-start)
+
+    def ll_stringslice_minusone(s):
+        return s.ll_substring(0, s.ll_strlen()-1)
 
 def add_helpers():
     dic = {}

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	Mon May 15 15:44:29 2006
@@ -357,6 +357,25 @@
                 res = self.interpret(fn, [i,j])
                 assert self.ll_to_string(res) == fn(i, j)
 
+    def test_str_slice(self):
+        def fn():
+            s = 'hello'
+            s1 = s[:3]
+            s2 = s[3:]
+            s3 = s[3:10]
+            return s1+s2 == s and s2+s1 == 'lohel' and s1+s3 == s
+        res = self.interpret(fn, ())
+        assert res
+
+    def test_str_slice_minusone(self):
+        def fn():
+            s = 'hello'
+            z = 'h'
+            return s[:-1]+z[:-1]
+        res = self.interpret(fn, ())
+        assert self.ll_to_string(res) == 'hell'
+
+
 def test_parse_fmt():
     assert LLHelpers.parse_fmt_string('a') == ['a']
     assert LLHelpers.parse_fmt_string('%s') == [('s',)]
@@ -413,25 +432,6 @@
     res = interpret(percentS, ['D'])
     assert ''.join(res.chars) == 'before D after'
 
-def test_str_slice():
-    def fn():
-        s = 'hello'
-        s1 = s[:3]
-        s2 = s[3:]
-        s3 = s[3:10]
-        return s1+s2 == s and s2+s1 == 'lohel' and s1+s3 == s
-    res = interpret(fn, ())
-    assert res
-
-def test_str_slice_minusone():
-    def fn():
-        s = 'hello'
-        z = 'h'
-        return s[:-1]+z[:-1]
-    res = interpret(fn, ())
-    assert ''.join(res.chars) == 'hell'
-
-
 def test_strformat_instance():
     class C:
         pass



More information about the Pypy-commit mailing list