[pypy-commit] pypy refactor-str-types: Implement capitalize() method.

Manuel Jacob noreply at buildbot.pypy.org
Tue Jun 4 22:28:19 CEST 2013


Author: Manuel Jacob
Branch: refactor-str-types
Changeset: r64782:cfd9859a607d
Date: 2013-06-04 21:03 +0200
http://bitbucket.org/pypy/pypy/changeset/cfd9859a607d/

Log:	Implement capitalize() method.

diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -96,6 +96,20 @@
 
     _builder = StringBuilder
 
+    def _upper(self, ch):
+        if ch.islower():
+            o = ord(ch) - 32
+            return chr(o)
+        else:
+            return ch
+
+    def _lower(self, ch):
+        if ch.isupper():
+            o = ord(ch) + 32
+            return chr(o)
+        else:
+            return ch
+
     @staticmethod
     @unwrap_spec(w_object = WrappedDefault(""))
     def descr_new(space, w_stringtype, w_object):
diff --git a/pypy/objspace/std/stringmethods.py b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -27,20 +27,6 @@
     def _val(self):
         raise NotImplementedError
 
-    def _upper(self, ch):
-        if ch.islower():
-            o = ord(ch) - 32
-            return chr(o)
-        else:
-            return ch
-
-    def _lower(self, ch):
-        if ch.isupper():
-            o = ord(ch) + 32
-            return chr(o)
-        else:
-            return ch
-
     def _sliced(self, space, s, start, stop, orig_obj):
         assert start >= 0
         assert stop >= 0
@@ -174,27 +160,14 @@
             return self._sliced(space, selfvalue, start, stop, self)
 
     def descr_capitalize(self, space):
-        # XXX just to pass the test
-        return self._new(self._val().capitalize())
+        value = self._val()
+        if len(value) == 0:
+            return self.EMPTY
 
-        input = self._value
-        builder = self._builder(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)
-
+        builder = self._builder(len(value))
+        builder.append(self._upper(value[0]))
+        for i in range(1, len(value)):
+            builder.append(self._lower(value[i]))
         return space.wrap(builder.build())
 
     @unwrap_spec(width=int, w_fillchar=WrappedDefault(' '))
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
@@ -89,6 +89,12 @@
 
     _builder = UnicodeBuilder
 
+    def _upper(self, ch):
+        return unichr(unicodedb.toupper(ord(ch)))
+
+    def _lower(self, ch):
+        return unichr(unicodedb.tolower(ord(ch)))
+
     def descr_repr(self, space):
         chars = self._value
         size = len(chars)


More information about the pypy-commit mailing list