[pypy-svn] r76124 - in pypy/branch/fast-forward/pypy/rpython: . lltypesystem ootypesystem test

benjamin at codespeak.net benjamin at codespeak.net
Mon Jul 12 05:40:29 CEST 2010


Author: benjamin
Date: Mon Jul 12 05:40:28 2010
New Revision: 76124

Modified:
   pypy/branch/fast-forward/pypy/rpython/lltypesystem/rstr.py
   pypy/branch/fast-forward/pypy/rpython/ootypesystem/rstr.py
   pypy/branch/fast-forward/pypy/rpython/rstr.py
   pypy/branch/fast-forward/pypy/rpython/test/test_rstr.py
Log:
allow chars to be joined into a different type

Modified: pypy/branch/fast-forward/pypy/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rpython/lltypesystem/rstr.py	(original)
+++ pypy/branch/fast-forward/pypy/rpython/lltypesystem/rstr.py	Mon Jul 12 05:40:28 2010
@@ -675,19 +675,21 @@
             i += 1
         return result
 
-    def ll_join_chars(length, chars):
+    def ll_join_chars(length, chars, RES):
         # no need to optimize this, will be replaced by string builder
         # at some point soon
         num_chars = length
-        if typeOf(chars).TO.OF == Char:
+        if RES is StringRepr.lowleveltype:
+            target = Char
             malloc = mallocstr
         else:
+            target = UniChar
             malloc = mallocunicode
         result = malloc(num_chars)
         res_chars = result.chars
         i = 0
         while i < num_chars:
-            res_chars[i] = chars[i]
+            res_chars[i] = cast_primitive(target, chars[i])
             i += 1
         return result
 

Modified: pypy/branch/fast-forward/pypy/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rpython/ootypesystem/rstr.py	(original)
+++ pypy/branch/fast-forward/pypy/rpython/ootypesystem/rstr.py	Mon Jul 12 05:40:28 2010
@@ -169,16 +169,18 @@
             buf.ll_append(lastitem)
         return buf.ll_build()
 
-    def ll_join_chars(length_dummy, lst):
-        if typeOf(lst).ITEM == Char:
+    def ll_join_chars(length_dummy, lst, RES):
+        if RES is ootype.String:
+            target = Char
             buf = ootype.new(ootype.StringBuilder)
         else:
+            target = UniChar
             buf = ootype.new(ootype.UnicodeBuilder)
         length = lst.ll_length()
         buf.ll_allocate(length)
         i = 0
         while i < length:
-            buf.ll_append_char(lst.ll_getitem_fast(i))
+            buf.ll_append_char(cast_primitive(target, lst.ll_getitem_fast(i)))
             i += 1
         return buf.ll_build()
 

Modified: pypy/branch/fast-forward/pypy/rpython/rstr.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rpython/rstr.py	(original)
+++ pypy/branch/fast-forward/pypy/rpython/rstr.py	Mon Jul 12 05:40:28 2010
@@ -190,8 +190,11 @@
         if hop.args_s[0].is_constant() and hop.args_s[0].const == '':
             if r_lst.item_repr == rstr.repr:
                 llfn = self.ll.ll_join_strs
-            elif r_lst.item_repr == rstr.char_repr:
-                llfn = self.ll.ll_join_chars
+            elif (r_lst.item_repr == hop.rtyper.type_system.rstr.char_repr or
+                  r_lst.item_repr == hop.rtyper.type_system.rstr.unichar_repr):
+                v_tp = hop.inputconst(Void, self.lowleveltype)
+                return hop.gendirectcall(self.ll.ll_join_chars, v_length,
+                                         v_items, v_tp)
             else:
                 raise TyperError("''.join() of non-string list: %r" % r_lst)
             return hop.gendirectcall(llfn, v_length, v_items)

Modified: pypy/branch/fast-forward/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rpython/test/test_rstr.py	(original)
+++ pypy/branch/fast-forward/pypy/rpython/test/test_rstr.py	Mon Jul 12 05:40:28 2010
@@ -405,6 +405,13 @@
                 res = self.interpret(fn, [i,j])
                 assert self.ll_to_string(res) == fn(i, j)
 
+        def fn(i):
+            c = ["a", "b", "c"]
+            assert i >= 0
+            return const('').join(c[i:])
+        res = self.interpret(fn, [0])
+        assert self.ll_to_string(res) == const("abc")
+
     def test_str_slice(self):
         const = self.const
         def fn(n):



More information about the Pypy-commit mailing list