[pypy-commit] pypy remove-string-smm: add rjust

vkryachko noreply at buildbot.pypy.org
Tue Mar 19 23:18:17 CET 2013


Author: Vladimir Kryachko <v.kryachko at gmail.com>
Branch: remove-string-smm
Changeset: r62510:e268e40ce2ee
Date: 2013-03-19 15:18 -0700
http://bitbucket.org/pypy/pypy/changeset/e268e40ce2ee/

Log:	add rjust

diff --git a/pypy/objspace/std/bytearrayinterface.py b/pypy/objspace/std/bytearrayinterface.py
--- a/pypy/objspace/std/bytearrayinterface.py
+++ b/pypy/objspace/std/bytearrayinterface.py
@@ -1,13 +1,14 @@
 from pypy.objspace.std.model import W_Object
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.objspace.std.inttype import wrapint
 
 
 class W_AbstractBytearrayObject(W_Object):
     pass
 
 
-class ByteArrayInterface(object) :
+class BytearrayInterface(object) :
     @unwrap_spec(w_self=W_Root, arg=int, fillchar=str)
     def ljust(w_self, space, arg, fillchar=' '):
         """S.ljust(width[, fillchar]) -> string
@@ -15,8 +16,8 @@
         Return S left justified in a string of length width. Padding
         is done using the specified fill character (default is a space).
         """
+        assert isinstance(w_self, W_AbstractBytearrayObject)
         u_self = w_self.data
-        assert isinstance(w_self, W_AbstractBytearrayObject)
         if len(fillchar) != 1:
             raise OperationError(space.w_TypeError,
                 space.wrap("ljust() argument 2 must be a single character"))
@@ -33,8 +34,34 @@
 
         return space.newbytearray(lst)
 
+    @unwrap_spec(w_self=W_Root, arg=int, fillchar=str)
+    def rjust(w_self, space, arg, fillchar=' '):
+        """S.rjust(width[, fillchar]) -> string
+
+        Return S right justified in a string of length width. Padding
+        is done using the specified fill character (default is a space).
+        """
+        u_self = w_self.data
+        assert isinstance(w_self, W_AbstractBytearrayObject)
+        if len(fillchar) != 1:
+            raise OperationError(space.w_TypeError,
+                space.wrap("rjust() argument 2 must be a single character"))
+
+        d = arg - len(u_self)
+        if d > 0:
+            lst = [0] * max(arg, len(u_self))
+            fillchar = fillchar[0]    # annotator hint: it's a single character
+            for i in range(d):
+                lst[i] = fillchar
+            lst[len(u_self)-1:] = u_self
+        else:
+            lst = u_self.data[:]
+
+        return space.newbytearray(lst)
+
 
 def bytearray_interface_methods():
+    """Convenience function which collects all BytearrayInterface methods into a dict"""
     return dict((name, interp2app(method)) for
-                name, method in ByteArrayInterface.__dict__.items()
+                name, method in BytearrayInterface.__dict__.items()
                 if not name.startswith('_'))
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
@@ -482,12 +482,6 @@
     w_res = stringobject.str_capitalize__String(space, w_str)
     return String2Bytearray(space, w_res)
 
-def str_rjust__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
-    w_str = str__Bytearray(space, w_bytearray)
-    w_res = stringobject.str_rjust__String_ANY_ANY(space, w_str, w_width,
-                                                   w_fillchar)
-    return String2Bytearray(space, w_res)
-
 def str_center__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
     w_str = str__Bytearray(space, w_bytearray)
     w_res = stringobject.str_center__String_ANY_ANY(space, w_str, w_width,
diff --git a/pypy/objspace/std/bytearraytype.py b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -10,7 +10,7 @@
     str_startswith, str_endswith, str_islower, str_isupper, str_isalpha,
     str_isalnum, str_isdigit, str_isspace, str_istitle,
     str_upper, str_lower, str_title, str_swapcase, str_capitalize,
-    str_expandtabs, str_rjust, str_center, str_zfill,
+    str_expandtabs, str_center, str_zfill,
     str_join, str_split, str_rsplit, str_partition, str_rpartition,
     str_splitlines, str_translate)
 from pypy.objspace.std.listtype import (


More information about the pypy-commit mailing list