[pypy-svn] r48386 - pypy/branch/pypy-rpython-unicode/rpython/lltypesystem

fijal at codespeak.net fijal at codespeak.net
Thu Nov 8 01:43:15 CET 2007


Author: fijal
Date: Thu Nov  8 01:43:13 2007
New Revision: 48386

Modified:
   pypy/branch/pypy-rpython-unicode/rpython/lltypesystem/rstr.py
Log:
First round of simplifications and getting rid of ugly typeOf switch


Modified: pypy/branch/pypy-rpython-unicode/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/rpython/lltypesystem/rstr.py	(original)
+++ pypy/branch/pypy-rpython-unicode/rpython/lltypesystem/rstr.py	Thu Nov  8 01:43:13 2007
@@ -41,13 +41,21 @@
 mallocstr = new_malloc(STR)
 mallocunicode = new_malloc(UNICODE)
 
+def emptystrfun():
+    return emptystr
+
+def emptyunicodefun():
+    return emptyunicode
+
 STR.become(GcStruct('rpy_string', ('hash',  Signed),
                     ('chars', Array(Char, hints={'immutable': True,
                                                  'isrpystring': True})),
-                    adtmeths={'malloc':staticAdtMethod(mallocstr)}))
+                    adtmeths={'malloc' : staticAdtMethod(mallocstr),
+                              'empty'  : staticAdtMethod(emptystrfun)}))
 UNICODE.become(GcStruct('rpy_unicode', ('hash', Signed),
                         ('chars', Array(UniChar, hints={'immutable': True})),
-                        adtmeths={'malloc':staticAdtMethod(mallocunicode)}
+                        adtmeths={'malloc' : staticAdtMethod(mallocunicode),
+                                  'empty'  : staticAdtMethod(emptyunicodefun)}
                         ))
 SIGNED_ARRAY = GcArray(Signed)
 CONST_STR_CACHE = WeakValueDictionary()
@@ -243,10 +251,7 @@
     def ll_strip(s, ch, left, right):
         s_len = len(s.chars)
         if s_len == 0:
-            if typeOf(s).TO == STR:
-                return emptystr
-            else:
-                return emptyunicode
+            return s.empty()
         lpos = 0
         rpos = s_len - 1
         if left:
@@ -256,11 +261,7 @@
             while lpos < rpos and s.chars[rpos] == ch:
                 rpos -= 1
         r_len = rpos - lpos + 1
-        if typeOf(s).TO == STR:
-            malloc = mallocstr
-        else:
-            malloc = mallocunicode
-        result = malloc(r_len)
+        result = s.malloc(r_len)
         i = 0
         j = lpos
         while i < r_len:
@@ -273,16 +274,9 @@
         s_chars = s.chars
         s_len = len(s_chars)
         if s_len == 0:
-            if typeOf(s).TO == STR:
-                return emptystr
-            else:
-                return emptyunicode
+            return s.empty()
         i = 0
-        if typeOf(s).TO == STR:
-            malloc = mallocstr
-        else:
-            malloc = mallocunicode
-        result = malloc(s_len)
+        result = s.malloc(s_len)
         while i < s_len:
             ch = s_chars[i]
             if 'a' <= ch <= 'z':
@@ -295,16 +289,9 @@
         s_chars = s.chars
         s_len = len(s_chars)
         if s_len == 0:
-            if typeOf(s).TO == STR:
-                return emptystr
-            else:
-                return emptyunicode
+            return s.empty()
         i = 0
-        if typeOf(s).TO == STR:
-            malloc = mallocstr
-        else:
-            malloc = mallocunicode
-        result = malloc(s_len)
+        result = s.malloc(s_len)
         while i < s_len:
             ch = s_chars[i]
             if 'A' <= ch <= 'Z':
@@ -318,20 +305,13 @@
         s_len = len(s_chars)
         num_items = length
         if num_items == 0:
-            if typeOf(s).TO == STR:
-                return emptystr
-            else:
-                return emptyunicode
+            return s.empty()
         itemslen = 0
         i = 0
         while i < num_items:
             itemslen += len(items[i].chars)
             i += 1
-        if typeOf(s).TO == STR:
-            malloc = mallocstr
-        else:
-            malloc = mallocunicode
-        result = malloc(itemslen + s_len * (num_items - 1))
+        result = s.malloc(itemslen + s_len * (num_items - 1))
         res_chars = result.chars
         res_index = 0
         i = 0
@@ -616,11 +596,7 @@
 
     def ll_stringslice_startonly(s1, start):
         len1 = len(s1.chars)
-        if typeOf(s1).TO == STR:
-            malloc = mallocstr
-        else:
-            malloc = mallocunicode
-        newstr = malloc(len1 - start)
+        newstr = s1.malloc(len1 - start)
         j = 0
         while start < len1:
             newstr.chars[j] = s1.chars[start]
@@ -635,11 +611,7 @@
             if start == 0:
                 return s1
             stop = len(s1.chars)
-        if typeOf(s1).TO == STR:
-            malloc = mallocstr
-        else:
-            malloc = mallocunicode
-        newstr = malloc(stop - start)
+        newstr = s1.malloc(stop - start)
         j = 0
         while start < stop:
             newstr.chars[j] = s1.chars[start]
@@ -649,11 +621,7 @@
 
     def ll_stringslice_minusone(s1):
         newlen = len(s1.chars) - 1
-        if typeOf(s1).TO == STR:
-            malloc = mallocstr
-        else:
-            malloc = mallocunicode
-        newstr = malloc(newlen)
+        newstr = s1.malloc(newlen)
         j = 0
         while j < newlen:
             newstr.chars[j] = s1.chars[j]
@@ -674,13 +642,9 @@
         i = 0
         j = 0
         resindex = 0
-        if typeOf(s).TO == STR:
-            malloc = mallocstr
-        else:
-            malloc = mallocunicode
         while j < strlen:
             if chars[j] == c:
-                item = items[resindex] = malloc(j - i)
+                item = items[resindex] = s.malloc(j - i)
                 newchars = item.chars
                 k = i
                 while k < j:
@@ -689,7 +653,7 @@
                 resindex += 1
                 i = j + 1
             j += 1
-        item = items[resindex] = malloc(j - i)
+        item = items[resindex] = s.malloc(j - i)
         newchars = item.chars
         k = i
         while k < j:
@@ -701,11 +665,7 @@
 
     def ll_replace_chr_chr(s, c1, c2):
         length = len(s.chars)
-        if typeOf(s).TO == STR:
-            malloc = mallocstr
-        else:
-            malloc = mallocunicode
-        newstr = malloc(length)
+        newstr = s.malloc(length)
         src = s.chars
         dst = newstr.chars
         j = 0



More information about the Pypy-commit mailing list