Commits

Anonymous committed 111c055

Refactor capitalize() method into base class.

  • Participants
  • Parent commits 8737c6e
  • Branches bytearray-refactor

Comments (0)

Files changed (6)

pypy/objspace/std/abstractstring.py

     def istitle(w_self, space):
         return w_self._title(space)
 
+    def capitalize(w_self, space):
+        return w_self._capitalize(space)
+
     def lower(w_self, space):
         return w_self._transform(space, w_self._lower)
 
 
         return space.newbool(cased)
 
+    def _capitalize(w_self, space):
+        sz = w_self.length(space)
+        it = w_self.iterator(space)
+        bd = w_self.builder(space, sz)
+        if sz > 0:
+            ch = it.nextchar()
+            ch = w_self._upper(ch)
+            bd.append(ch)
+            for i in range(1, sz):
+                ch = it.nextchar()
+                ch = w_self._lower(ch)
+                bd.append(ch)
+        return w_self.construct(space, bd.build())
+
     @specialize.arg(2)
     def _transform(w_self, space, func):
         sz = w_self.length(space)

pypy/objspace/std/bytearrayobject.py

 def str_swapcase__Bytearray(space, w_self):
     return w_self.swapcase(space)
 
+def str_capitalize__Bytearray(space, w_bytearray):
+    return w_self.capitalize(space)
+
+def str_capitalize__Bytearray(space, w_bytearray):
+    w_str = str__Bytearray(space, w_bytearray)
+    w_res = stringobject.str_capitalize__String(space, w_str)
+    return String2Bytearray(space, w_res)
+
+
 def str_count__Bytearray_Int_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
     char = w_char.intval
     bytearray = w_bytearray.data
     w_res = stringobject.str_title__String(space, w_str)
     return String2Bytearray(space, w_res)
 
-def str_capitalize__Bytearray(space, w_bytearray):
-    w_str = str__Bytearray(space, w_bytearray)
-    w_res = stringobject.str_capitalize__String(space, w_str)
-    return String2Bytearray(space, w_res)
-
 def str_ljust__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
     w_str = str__Bytearray(space, w_bytearray)
     w_res = stringobject.str_ljust__String_ANY_ANY(space, w_str, w_width,

pypy/objspace/std/ropeobject.py

     return w_self.upper(space)
 
 def str_capitalize__Rope(space, w_self):
-    node = w_self._node
-    length = node.length()
-    buffer = [' '] * length
-    if length > 0:
-        iter = rope.ItemIterator(node)
-        ch = iter.nextchar()
-        if ch.islower():
-            o = ord(ch) - 32
-            buffer[0] = chr(o)
-        else:
-            buffer[0] = ch
-
-        for i in range(1, length):
-            ch = iter.nextchar()
-            if ch.isupper():
-                o = ord(ch) + 32
-                buffer[i] = chr(o)
-            else:
-                buffer[i] = ch
-    else:
-        return W_RopeObject.EMPTY
-
-    return W_RopeObject(rope.rope_from_charlist(buffer))
-
+    return w_self.capitalize(space)
 
 def str_title__Rope(space, w_self):
     node = w_self._node

pypy/objspace/std/ropeunicodeobject.py

         return rope.rope_from_unicharlist(self.data)
 
 
+class UnicodeIterator(rope.ItemIterator):
+    """"Iterate over unicode characters from rope iterator"""
+
+    def nextchar(self):
+        return self.nextunichar()
+
+
 class W_RopeUnicodeObject(unicodeobject.W_AbstractUnicodeObject):
     from pypy.objspace.std.unicodetype import unicode_typedef as typedef
     _immutable_fields_ = ['_node']
         return W_RopeUnicodeObject(data)
 
     def iterator(w_self, space):
-        return rope.ItemIterator(w_self._node)
+        return UnicodeIterator(w_self._node)
 
     def length(w_self, space):
         return w_self._node.length()
     return space.call_method(w_self, 'rstrip',
                              unicode_from_string(space, w_chars))
 
-def unicode_capitalize__RopeUnicode(space, w_self):
-    input = w_self._node
-    length = input.length()
-    if length == 0:
-        return w_self
-    result = [u'\0'] * length
-    iter = rope.ItemIterator(input)
-    result[0] = unichr(unicodedb.toupper(iter.nextint()))
-    for i in range(1, length):
-        result[i] = unichr(unicodedb.tolower(iter.nextint()))
-    return W_RopeUnicodeObject(rope.rope_from_unicharlist(result))
-
 def unicode_title__RopeUnicode(space, w_self):
     input = w_self._node
     length = input.length()
 def unicode_swapcase__RopeUnicode(space, w_self):
     return w_self.swapcase(space)
 
+def unicode_capitalize__RopeUnicode(space, w_self):
+    return w_self.capitalize(space)
+
 def _convert_idx_params(space, w_self, w_start, w_end):
     self = w_self._node
     length = w_self._node.length()

pypy/objspace/std/stringobject.py

     return w_self.upper(space)
 
 def str_capitalize__String(space, w_self):
-    input = w_self._value
-    builder = StringBuilder(len(input))
-    if len(input) > 0:
-        ch = input[0]
-        if ch.islower():
-            o = ord(ch) - 32
-            builder.append(chr(o))
-        else:
-            builder.append(ch)
-
-        for i in range(1, len(input)):
-            ch = input[i]
-            if ch.isupper():
-                o = ord(ch) + 32
-                builder.append(chr(o))
-            else:
-                builder.append(ch)
-
-    return space.wrap(builder.build())
+    return w_self.capitalize(space)
 
 def str_title__String(space, w_self):
     input = w_self._value

pypy/objspace/std/unicodeobject.py

 
 unicode_rstrip__Unicode_Rope = unicode_rstrip__Unicode_String
 
-def unicode_capitalize__Unicode(space, w_self):
-    input = w_self._value
-    if len(input) == 0:
-        return W_UnicodeObject.EMPTY
-    builder = UnicodeBuilder(len(input))
-    builder.append(unichr(unicodedb.toupper(ord(input[0]))))
-    for i in range(1, len(input)):
-        builder.append(unichr(unicodedb.tolower(ord(input[i]))))
-    return W_UnicodeObject(builder.build())
-
 def unicode_title__Unicode(space, w_self):
     input = w_self._value
     if len(input) == 0:
         previous_is_cased = unicodedb.iscased(unichar)
     return W_UnicodeObject(builder.build())
 
+def unicode_capitalize__Unicode(space, w_self):
+    return w_self.capitalize(space)
+
 def unicode_lower__Unicode(space, w_self):
     return w_self.lower(space)