Author: Vladimir Kryachko <[email protected]>
Branch: remove-string-smm
Changeset: r62513:25d514ae998b
Date: 2013-03-19 16:37 -0700
http://bitbucket.org/pypy/pypy/changeset/25d514ae998b/

Log:    Add str.rjust and str.join methods

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
@@ -8,7 +8,7 @@
     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
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
@@ -11,7 +11,7 @@
     str_isalnum, str_isdigit, str_isspace, str_istitle,
     str_upper, str_lower, str_title, str_swapcase, str_capitalize,
     str_expandtabs, str_center, str_zfill,
-    str_join, str_split, str_rsplit, str_partition, str_rpartition,
+    str_split, str_rsplit, str_partition, str_rpartition,
     str_splitlines, str_translate)
 from pypy.objspace.std.listtype import (
     list_append, list_extend)
@@ -20,6 +20,7 @@
 
 
 str_ljust = SMM('ljust', 3, defaults=(' ',))
+str_join = SMM('join', 2, defaults=(None,-1))
 
 bytearray_insert  = SMM('insert', 3,
                     doc="B.insert(index, int) -> None\n\n"
diff --git a/pypy/objspace/std/contiguousstring.py 
b/pypy/objspace/std/contiguousstring.py
--- a/pypy/objspace/std/contiguousstring.py
+++ b/pypy/objspace/std/contiguousstring.py
@@ -1,6 +1,8 @@
 """Common methods for string types (bytes and unicode)"""
 
-from pypy.interpreter.error import OperationError
+from rpython.rlib import jit
+from pypy.interpreter.error import OperationError, operationerrfmt
+from rpython.rlib.rstring import StringBuilder, split
 
 
 class StringMethods(object):
@@ -18,3 +20,65 @@
             u_self += d * fillchar
 
         return space.wrap(u_self)
+
+    def rjust(self, space, arg, fillchar=' '):
+        u_self = self._value
+        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:
+            fillchar = fillchar[0]    # annotator hint: it's a single character
+            u_self = d * fillchar + u_self
+
+        return space.wrap(u_self)
+
+    def join(self, space, w_list):
+        l = space.listview_str(w_list)
+        if l is not None:
+            if len(l) == 1:
+                return space.wrap(l[0])
+            return space.wrap(self._value.join(l))
+        list_w = space.listview(w_list)
+        size = len(list_w)
+
+        if size == 0:
+            return space.wrap("")
+
+        if size == 1:
+            w_s = list_w[0]
+            # only one item,  return it if it's not a subclass of str
+            if (space.is_w(space.type(w_s), space.w_str) or
+                space.is_w(space.type(w_s), space.w_unicode)):
+                return w_s
+
+        return _str_join_many_items(space, self, list_w, size)
+
+
[email protected]_inside_iff(lambda space, w_self, list_w, size:
+                     jit.loop_unrolling_heuristic(list_w, size))
+def _str_join_many_items(space, w_self, list_w, size):
+    self = w_self._value
+    reslen = len(self) * (size - 1)
+    for i in range(size):
+        w_s = list_w[i]
+        if not space.isinstance_w(w_s, space.w_str):
+            if space.isinstance_w(w_s, space.w_unicode):
+                # we need to rebuild w_list here, because the original
+                # w_list might be an iterable which we already consumed
+                w_list = space.newlist(list_w)
+                w_u = space.call_function(space.w_unicode, w_self)
+                return space.call_method(w_u, "join", w_list)
+            raise operationerrfmt(
+                space.w_TypeError,
+                "sequence item %d: expected string, %s "
+                "found", i, space.type(w_s).getname(space))
+        reslen += len(space.str_w(w_s))
+
+    sb = StringBuilder(reslen)
+    for i in range(size):
+        if self and i != 0:
+            sb.append(self)
+        sb.append(space.str_w(list_w[i]))
+    return space.wrap(sb.build())
\ No newline at end of file
diff --git a/pypy/objspace/std/stringinterface.py 
b/pypy/objspace/std/stringinterface.py
--- a/pypy/objspace/std/stringinterface.py
+++ b/pypy/objspace/std/stringinterface.py
@@ -4,6 +4,7 @@
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from rpython.rlib.objectmodel import compute_unique_id
+from pypy.interpreter.error import OperationError
 
 
 class W_AbstractStringObject(W_Object):
@@ -39,6 +40,16 @@
 
 
 class StringInterface(object):
+    @unwrap_spec(w_self=W_Root)
+    def join(w_self, space, w_list):
+        """S.join(sequence) -> string
+
+        Return a string which is
+        the concatenation of the strings in the sequence.
+        The separator between elements is S."""
+        assert isinstance(w_self, W_AbstractStringObject)
+        return w_self.join(space, w_list)
+
     @unwrap_spec(w_self=W_Root, arg=int, fillchar=str)
     def ljust(w_self, space, arg, fillchar=' '):
         """S.ljust(width[, fillchar]) -> string
@@ -49,8 +60,20 @@
         assert isinstance(w_self, W_AbstractStringObject)
         return w_self.ljust(space, arg, fillchar)
 
+    @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)"""
+        assert isinstance(w_self, W_AbstractStringObject)
+        return w_self.rjust(space, arg, fillchar)
+
 
 def string_interface_methods():
     return dict((name, interp2app(method)) for
                 name, method in StringInterface.__dict__.items()
                 if not name.startswith('_'))
+
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
@@ -348,69 +348,6 @@
 str_rsplit__String_String_ANY = 
make_rsplit_with_delim('str_rsplit__String_String_ANY',
                                                        sliced)
 
-def str_join__String_ANY(space, w_self, w_list):
-    l = space.listview_str(w_list)
-    if l is not None:
-        if len(l) == 1:
-            return space.wrap(l[0])
-        return space.wrap(w_self._value.join(l))
-    list_w = space.listview(w_list)
-    size = len(list_w)
-
-    if size == 0:
-        return W_StringObject.EMPTY
-
-    if size == 1:
-        w_s = list_w[0]
-        # only one item,  return it if it's not a subclass of str
-        if (space.is_w(space.type(w_s), space.w_str) or
-            space.is_w(space.type(w_s), space.w_unicode)):
-            return w_s
-
-    return _str_join_many_items(space, w_self, list_w, size)
-
[email protected]_inside_iff(lambda space, w_self, list_w, size:
-                     jit.loop_unrolling_heuristic(list_w, size))
-def _str_join_many_items(space, w_self, list_w, size):
-    self = w_self._value
-    reslen = len(self) * (size - 1)
-    for i in range(size):
-        w_s = list_w[i]
-        if not space.isinstance_w(w_s, space.w_str):
-            if space.isinstance_w(w_s, space.w_unicode):
-                # we need to rebuild w_list here, because the original
-                # w_list might be an iterable which we already consumed
-                w_list = space.newlist(list_w)
-                w_u = space.call_function(space.w_unicode, w_self)
-                return space.call_method(w_u, "join", w_list)
-            raise operationerrfmt(
-                space.w_TypeError,
-                "sequence item %d: expected string, %s "
-                "found", i, space.type(w_s).getname(space))
-        reslen += len(space.str_w(w_s))
-
-    sb = StringBuilder(reslen)
-    for i in range(size):
-        if self and i != 0:
-            sb.append(self)
-        sb.append(space.str_w(list_w[i]))
-    return space.wrap(sb.build())
-
-def str_rjust__String_ANY_ANY(space, w_self, w_arg, w_fillchar):
-    u_arg = space.int_w(w_arg)
-    u_self = w_self._value
-    fillchar = space.str_w(w_fillchar)
-    if len(fillchar) != 1:
-        raise OperationError(space.w_TypeError,
-            space.wrap("rjust() argument 2 must be a single character"))
-
-    d = u_arg - len(u_self)
-    if d>0:
-        fillchar = fillchar[0]    # annotator hint: it's a single character
-        u_self = d * fillchar + u_self
-
-    return space.wrap(u_self)
-
 
 @specialize.arg(4)
 def _convert_idx_params(space, w_self, w_start, w_end, upper_bound=False):
diff --git a/pypy/objspace/std/stringtype.py b/pypy/objspace/std/stringtype.py
--- a/pypy/objspace/std/stringtype.py
+++ b/pypy/objspace/std/stringtype.py
@@ -46,10 +46,6 @@
     else:
         return wrapstr(space, str1 + str2)
 
-str_join    = SMM('join', 2,
-                  doc='S.join(sequence) -> string\n\nReturn a string which is'
-                      ' the concatenation of the strings in the\nsequence. '
-                      ' The separator between elements is S.')
 str_split   = SMM('split', 3, defaults=(None,-1),
                   doc='S.split([sep [,maxsplit]]) -> list of strings\n\nReturn'
                       ' a list of the words in the string S, using sep as'
@@ -96,11 +92,6 @@
                      doc='S.isalnum() -> bool\n\nReturn True if all characters'
                          ' in S are alphanumeric\nand there is at least one'
                          ' character in S, False otherwise.')
-str_rjust      = SMM('rjust', 3, defaults=(' ',),
-                     doc='S.rjust(width[, fillchar]) -> string\n\nReturn S'
-                         ' right justified in a string of length width.'
-                         ' Padding is\ndone using the specified fill character'
-                         ' (default is a space)')
 str_upper      = SMM('upper', 1,
                      doc='S.upper() -> string\n\nReturn a copy of the string S'
                          ' converted to uppercase.')
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to