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

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


Author: Manuel Jacob
Branch: refactor-str-types
Changeset: r64783:6a4af8ab46ec
Date: 2013-06-04 21:17 +0200
http://bitbucket.org/pypy/pypy/changeset/6a4af8ab46ec/

Log:	Implement swapcase() 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,27 @@
 
     _builder = StringBuilder
 
+    def _isupper(self, ch):
+        return ch.isupper()
+
+    def _islower(self, ch):
+        return ch.islower()
+
+    def _istitle(self, ch):
+        return ch.istitle()
+
+    def _isspace(self, ch):
+        return ch.isspace()
+
+    def _isalpha(self, ch):
+        return ch.isalpha()
+
+    def _isalnum(self, ch):
+        return ch.isalnum()
+
+    def _isdigit(self, ch):
+        return ch.isdigit()
+
     def _upper(self, ch):
         if ch.islower():
             o = ord(ch) - 32
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
@@ -9,12 +9,6 @@
 from rpython.rlib.rstring import split
 
 
-_isspace = lambda c: c.isspace()
-_isdigit = lambda c: c.isdigit()
-_isalpha = lambda c: c.isalpha()
-_isalnum = lambda c: c.isalnum()
-
-
 class StringMethods(object):
     _mixin_ = True
 
@@ -296,13 +290,13 @@
         return space.w_True
 
     def descr_isalnum(self, space):
-        return self._is_generic(space, _isalnum)
+        return self._is_generic(space, self._isalnum)
 
     def descr_isalpha(self, space):
-        return self._is_generic(space, _isalpha)
+        return self._is_generic(space, self._isalpha)
 
     def descr_isdigit(self, space):
-        return self._is_generic(space, _isdigit)
+        return self._is_generic(space, self._isdigit)
 
     def descr_islower(self, space):
         v = self._value
@@ -318,7 +312,7 @@
         return space.newbool(cased)
 
     def descr_isspace(self, space):
-        return self._is_generic(space, _isspace)
+        return self._is_generic(space, self._isspace)
 
     def descr_istitle(self, space):
         input = self._value
@@ -769,19 +763,14 @@
         return self._strip(space, w_chars, left=0, right=1)
 
     def descr_swapcase(self, space):
-        # XXX just to pass the test
-        return space.wrap(self._val().swapcase())
-
-        selfvalue = self._value
+        selfvalue = self._val()
         builder = self._builder(len(selfvalue))
         for i in range(len(selfvalue)):
             ch = selfvalue[i]
-            if ch.isupper():
-                o = ord(ch) + 32
-                builder.append(chr(o))
-            elif ch.islower():
-                o = ord(ch) - 32
-                builder.append(chr(o))
+            if self._isupper(ch):
+                builder.append(self._lower(ch))
+            elif self._islower(ch):
+                builder.append(self._upper(ch))
             else:
                 builder.append(ch)
         return space.wrap(builder.build())
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,27 @@
 
     _builder = UnicodeBuilder
 
+    def _isupper(self, ch):
+        return ch.isupper()
+
+    def _islower(self, ch):
+        return ch.islower()
+
+    def _istitle(self, ch):
+        return ch.istitle()
+
+    def _isspace(self, ch):
+        return ch.isspace()
+
+    def _isalpha(self, ch):
+        return ch.isalpha()
+
+    def _isalnum(self, ch):
+        return ch.isalnum()
+
+    def _isdigit(self, ch):
+        return ch.isdigit()
+
     def _upper(self, ch):
         return unichr(unicodedb.toupper(ord(ch)))
 


More information about the pypy-commit mailing list