[pypy-commit] pypy bytearray-refactor: Refactor capitalize() method into base class.
Aaron Iles
noreply at buildbot.pypy.org
Wed Mar 21 12:20:36 CET 2012
Author: Aaron Iles <aaron.iles at gmail.com>
Branch: bytearray-refactor
Changeset: r53856:111c05553722
Date: 2012-03-21 21:32 +1100
http://bitbucket.org/pypy/pypy/changeset/111c05553722/
Log: Refactor capitalize() method into base class.
diff --git a/pypy/objspace/std/abstractstring.py b/pypy/objspace/std/abstractstring.py
--- a/pypy/objspace/std/abstractstring.py
+++ b/pypy/objspace/std/abstractstring.py
@@ -28,6 +28,9 @@
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)
@@ -165,6 +168,20 @@
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)
diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -332,6 +332,15 @@
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
@@ -490,11 +499,6 @@
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,
diff --git a/pypy/objspace/std/ropeobject.py b/pypy/objspace/std/ropeobject.py
--- a/pypy/objspace/std/ropeobject.py
+++ b/pypy/objspace/std/ropeobject.py
@@ -135,30 +135,7 @@
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
diff --git a/pypy/objspace/std/ropeunicodeobject.py b/pypy/objspace/std/ropeunicodeobject.py
--- a/pypy/objspace/std/ropeunicodeobject.py
+++ b/pypy/objspace/std/ropeunicodeobject.py
@@ -91,6 +91,13 @@
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']
@@ -105,7 +112,7 @@
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()
@@ -395,18 +402,6 @@
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()
@@ -434,6 +429,9 @@
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()
diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -154,25 +154,7 @@
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
diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -437,16 +437,6 @@
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:
@@ -463,6 +453,9 @@
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)
More information about the pypy-commit
mailing list