Author: Aaron Iles <[email protected]>
Branch: bytearray-refactor
Changeset: r53866:da00b35c920c
Date: 2012-03-21 22:46 +1100
http://bitbucket.org/pypy/pypy/changeset/da00b35c920c/
Log: Refactor out title() 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
@@ -26,7 +26,7 @@
w_self._isupper, w_self._islower)
def istitle(w_self, space):
- return w_self._title(space)
+ return w_self._istitle(space)
def capitalize(w_self, space):
return w_self._capitalize(space)
@@ -37,6 +37,9 @@
def swapcase(w_self, space):
return w_self._transform(space, w_self._swapcase)
+ def title(w_self, space):
+ return w_self._title(space)
+
def upper(w_self, space):
return w_self._transform(space, w_self._upper)
@@ -147,7 +150,7 @@
status = True
return space.newbool(status)
- def _title(w_self, space):
+ def _istitle(w_self, space):
input = w_self.unwrap(space)
cased = False
previous_is_cased = False
@@ -182,6 +185,24 @@
bd.append(ch)
return w_self.construct(space, bd.build())
+ def _title(w_self, space):
+ sz = w_self.length(space)
+ if sz == 0:
+ return w_self
+ it = w_self.iterator(space)
+ bd = w_self.builder(space, sz)
+ pv = ' '
+ for i in range(sz):
+ ch = it.nextchar()
+ if not w_self._isalpha(pv):
+ ch = w_self._upper(ch)
+ bd.append(ch)
+ else:
+ ch = w_self._lower(ch)
+ bd.append(ch)
+ pv = 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,14 +332,11 @@
def str_swapcase__Bytearray(space, w_self):
return w_self.swapcase(space)
-def str_capitalize__Bytearray(space, w_bytearray):
+def str_capitalize__Bytearray(space, w_self):
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_title__Bytearray(space, w_self):
+ return w_self.title(space)
def str_count__Bytearray_Int_ANY_ANY(space, w_bytearray, w_char, w_start,
w_stop):
char = w_char.intval
@@ -494,11 +491,6 @@
w_str2, w_max)
return String2Bytearray(space, w_res)
-def str_title__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_title__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
@@ -138,22 +138,7 @@
return w_self.capitalize(space)
def str_title__Rope(space, w_self):
- node = w_self._node
- length = node.length()
- buffer = [' '] * length
- prev_letter = ' '
-
- iter = rope.ItemIterator(node)
- for pos in range(0, length):
- ch = iter.nextchar()
- if not prev_letter.isalpha():
- buffer[pos] = w_self._upper(ch)
- else:
- buffer[pos] = w_self._lower(ch)
-
- prev_letter = buffer[pos]
-
- return W_RopeObject(rope.rope_from_charlist(buffer))
+ return w_self.title(space)
def str_split__Rope_None_ANY(space, w_self, w_none, w_maxsplit=-1):
selfnode = 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
@@ -403,22 +403,7 @@
unicode_from_string(space, w_chars))
def unicode_title__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)
-
- previous_is_cased = False
- for i in range(input.length()):
- unichar = iter.nextint()
- if previous_is_cased:
- result[i] = unichr(unicodedb.tolower(unichar))
- else:
- result[i] = unichr(unicodedb.totitle(unichar))
- previous_is_cased = unicodedb.iscased(unichar)
- return W_RopeUnicodeObject(rope.rope_from_unicharlist(result))
+ return w_self.title(space)
def unicode_lower__RopeUnicode(space, w_self):
return w_self.lower(space)
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
@@ -128,20 +128,12 @@
return w_self.isspace(space)
def str_islower__String(space, w_self):
- """Return True if all cased characters in S are lowercase and there is
-at least one cased character in S, False otherwise."""
return w_self.islower(space)
def str_isupper__String(space, w_self):
- """Return True if all cased characters in S are uppercase and there is
-at least one cased character in S, False otherwise."""
return w_self.isupper(space)
def str_istitle__String(space, w_self):
- """Return True if S is a titlecased string and there is at least one
-character in S, i.e. uppercase characters may only follow uncased
-characters and lowercase characters only cased ones. Return False
-otherwise."""
return w_self.istitle(space)
def str_lower__String(space, w_self):
@@ -157,22 +149,7 @@
return w_self.capitalize(space)
def str_title__String(space, w_self):
- input = w_self._value
- builder = StringBuilder(len(input))
- prev_letter=' '
-
- for pos in range(len(input)):
- ch = input[pos]
- if not prev_letter.isalpha():
- ch = w_self._upper(ch)
- builder.append(ch)
- else:
- ch = w_self._lower(ch)
- builder.append(ch)
-
- prev_letter = ch
-
- return space.wrap(builder.build())
+ return w_self.title(space)
def str_split__String_None_ANY(space, w_self, w_none, w_maxsplit=-1):
maxsplit = space.int_w(w_maxsplit)
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
@@ -438,20 +438,7 @@
unicode_rstrip__Unicode_Rope = unicode_rstrip__Unicode_String
def unicode_title__Unicode(space, w_self):
- input = w_self._value
- if len(input) == 0:
- return w_self
- builder = UnicodeBuilder(len(input))
-
- previous_is_cased = False
- for i in range(len(input)):
- unichar = ord(input[i])
- if previous_is_cased:
- builder.append(unichr(unicodedb.tolower(unichar)))
- else:
- builder.append(unichr(unicodedb.totitle(unichar)))
- previous_is_cased = unicodedb.iscased(unichar)
- return W_UnicodeObject(builder.build())
+ return w_self.title(space)
def unicode_capitalize__Unicode(space, w_self):
return w_self.capitalize(space)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit