[pypy-svn] r39914 - in pypy/branch/pypy-2.5/pypy/objspace/std: . test

gbrandl at codespeak.net gbrandl at codespeak.net
Sun Mar 4 19:35:41 CET 2007


Author: gbrandl
Date: Sun Mar  4 19:35:40 2007
New Revision: 39914

Modified:
   pypy/branch/pypy-2.5/pypy/objspace/std/stringobject.py
   pypy/branch/pypy-2.5/pypy/objspace/std/stringtype.py
   pypy/branch/pypy-2.5/pypy/objspace/std/strsliceobject.py
   pypy/branch/pypy-2.5/pypy/objspace/std/test/test_stringobject.py
   pypy/branch/pypy-2.5/pypy/objspace/std/unicodeobject.py
   pypy/branch/pypy-2.5/pypy/objspace/std/unicodetype.py
Log:
Add str.startswith and str.endswith with tuple objects.



Modified: pypy/branch/pypy-2.5/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/stringobject.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/stringobject.py	Sun Mar  4 19:35:40 2007
@@ -11,7 +11,8 @@
 from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.objspace.std.tupleobject import W_TupleObject
 
-from pypy.objspace.std.stringtype import sliced, joined, wrapstr, wrapchar
+from pypy.objspace.std.stringtype import sliced, joined, wrapstr, wrapchar, \
+     stringendswith, stringstartswith
 
 
 class W_StringObject(W_Object):
@@ -342,7 +343,6 @@
         return W_StringObject.EMPTY
 
 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)
@@ -359,7 +359,6 @@
 
 
 def str_ljust__String_ANY_ANY(space, w_self, w_arg, w_fillchar):
-
     u_self = w_self._value
     u_arg = space.int_w(w_arg)
     fillchar = space.str_w(w_fillchar)
@@ -576,26 +575,30 @@
 def str_endswith__String_String_ANY_ANY(space, w_self, w_suffix, w_start, w_end):
     (u_self, suffix, start, end) = _convert_idx_params(space, w_self,
                                                        w_suffix, w_start, w_end)
-    begin = end - len(suffix)
-    if begin < start:
-        return space.w_False
-    for i in range(len(suffix)):
-        if u_self[begin+i] != suffix[i]:
-            return space.w_False
-    return space.w_True
-    
-    
+    return space.newbool(stringendswith(u_self, suffix, start, end))
+
+def str_endswith__String_Tuple_ANY_ANY(space, w_self, w_suffixes, w_start, w_end):
+    (u_self, _, start, end) = _convert_idx_params(space, w_self,
+                                                  space.wrap(''), w_start, w_end)
+    for w_suffix in space.unpacktuple(w_suffixes):
+        suffix = space.str_w(w_suffix) 
+        if stringendswith(u_self, suffix, start, end):
+            return space.w_True
+    return space.w_False
+
 def str_startswith__String_String_ANY_ANY(space, w_self, w_prefix, w_start, w_end):
     (u_self, prefix, start, end) = _convert_idx_params(space, w_self,
                                                        w_prefix, w_start, w_end)
-    stop = start + len(prefix)
-    if stop > end:
-        return space.w_False
-    for i in range(len(prefix)):
-        if u_self[start+i] != prefix[i]:
-            return space.w_False
-    return space.w_True
-    
+    return space.newbool(stringstartswith(u_self, prefix, start, end))
+
+def str_startswith__String_Tuple_ANY_ANY(space, w_self, w_prefixes, w_start, w_end):
+    (u_self, _, start, end) = _convert_idx_params(space, w_self, space.wrap(''),
+                                                  w_start, w_end)
+    for w_prefix in space.unpacktuple(w_prefixes):
+        prefix = space.str_w(w_prefix)
+        if stringstartswith(u_self, prefix, start, end):
+            return space.w_True
+    return space.w_False
     
 def _tabindent(u_token, u_tabsize):
     "calculates distance behind the token to the next tabstop"

Modified: pypy/branch/pypy-2.5/pypy/objspace/std/stringtype.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/stringtype.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/stringtype.py	Sun Mar  4 19:35:40 2007
@@ -28,6 +28,8 @@
         return W_StringObject(c)
 
 def sliced(space, s, start, stop):
+    assert start >= 0
+    assert stop >= 0 
     if space.config.objspace.std.withstrslice:
         from pypy.objspace.std.strsliceobject import W_StringSliceObject
         # XXX heuristic, should be improved!
@@ -268,3 +270,26 @@
 
 str_typedef.custom_hash = True
 str_typedef.registermethods(globals())
+
+# ____________________________________________________________
+
+# Helpers for several string implementations
+
+def stringendswith(u_self, suffix, start, end):
+    begin = end - len(suffix)
+    if begin < start:
+        return False
+    for i in range(len(suffix)):
+        if u_self[begin+i] != suffix[i]:
+            return False
+    return True
+
+def stringstartswith(u_self, prefix, start, end):
+    stop = start + len(prefix)
+    if stop > end:
+        return False
+    for i in range(len(prefix)):
+        if u_self[start+i] != prefix[i]:
+            return False
+    return True
+    

Modified: pypy/branch/pypy-2.5/pypy/objspace/std/strsliceobject.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/strsliceobject.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/strsliceobject.py	Sun Mar  4 19:35:40 2007
@@ -5,13 +5,16 @@
 from pypy.objspace.std import slicetype
 from pypy.objspace.std.inttype import wrapint
 
-from pypy.objspace.std.stringtype import wrapstr, wrapchar, sliced
+from pypy.objspace.std.stringtype import wrapstr, wrapchar, sliced, \
+     stringendswith, stringstartswith
 
 
 class W_StringSliceObject(W_Object):
     from pypy.objspace.std.stringtype import str_typedef as typedef
 
     def __init__(w_self, str, start, stop):
+        assert start >= 0
+        assert stop >= 0 
         w_self.str = str
         w_self.start = start
         w_self.stop = stop
@@ -131,6 +134,34 @@
 
     return space.wrap(res - w_self.start)
 
+def str_endswith__StringSlice_String_ANY_ANY(space, w_self, w_suffix, w_start, w_end):
+    (u_self, suffix, start, end) = _convert_idx_params(space, w_self,
+                                                       w_suffix, w_start, w_end)
+    return space.newbool(stringendswith(u_self, suffix, start, end))
+
+#def str_endswith__StringSlice_Tuple_ANY_ANY(space, w_self, w_suffixes, w_start, w_end):
+#    (u_self, _, start, end) = _convert_idx_params(space, w_self,
+#                                                  space.wrap(''), w_start, w_end)
+#    for w_suffix in space.unpacktuple(w_suffixes):
+#        suffix = space.str_w(w_suffix) 
+#        if stringendswith(u_self, suffix, start, end):
+#            return space.w_True
+#    return space.w_False
+
+def str_startswith__StringSlice_String_ANY_ANY(space, w_self, w_prefix, w_start, w_end):
+    (u_self, prefix, start, end) = _convert_idx_params(space, w_self,
+                                                       w_prefix, w_start, w_end)
+    return space.newbool(stringstartswith(u_self, prefix, start, end))
+
+#def str_startswith__StringSlice_Tuple_ANY_ANY(space, w_self, w_prefixes, w_start, w_end):
+#    (u_self, _, start, end) = _convert_idx_params(space, w_self, space.wrap(''),
+#                                                  w_start, w_end)
+#    for w_prefix in space.unpacktuple(w_prefixes):
+#        prefix = space.str_w(w_prefix)
+#        if stringstartswith(u_self, prefix, start, end):
+#            return space.w_True
+#    return space.w_False
+
 
 def str_w__StringSlice(space, w_str):
     return w_str.force()
@@ -178,5 +209,6 @@
         return w_str
     return W_StringSliceObject(w_str.str, w_str.start, w_str.stop)
 
+
 from pypy.objspace.std import stringtype
 register_all(vars(), stringtype)

Modified: pypy/branch/pypy-2.5/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/test/test_stringobject.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/test/test_stringobject.py	Sun Mar  4 19:35:40 2007
@@ -334,6 +334,18 @@
         assert 'abc'.startswith('bc', 1, 2) is False
         assert 'abc'.startswith('c', -1, 4) is True
 
+    def test_startswith_tuples(self):
+        assert 'hello'.startswith(('he', 'ha'))
+        assert not 'hello'.startswith(('lo', 'llo'))
+        assert 'hello'.startswith(('hellox', 'hello'))
+        assert not 'hello'.startswith(())
+        assert 'helloworld'.startswith(('hellowo', 'rld', 'lowo'), 3)
+        assert not 'helloworld'.startswith(('hellowo', 'ello', 'rld'), 3)
+        assert 'hello'.startswith(('lo', 'he'), 0, -1)
+        assert not 'hello'.startswith(('he', 'hel'), 0, 1)
+        assert 'hello'.startswith(('he', 'hel'), 0, 2)
+        raises(TypeError, 'hello'.startswith, (42,))
+    
     def test_endswith(self):
         assert 'ab'.endswith('ab') is True
         assert 'ab'.endswith('b') is True
@@ -350,7 +362,19 @@
         assert 'abc'.endswith('bc', 1) is True
         assert 'abc'.endswith('bc', 2) is False
         assert 'abc'.endswith('b', -3, -1) is True
-      
+
+    def test_endswith_tuple(self):
+        assert not 'hello'.endswith(('he', 'ha'))
+        assert 'hello'.endswith(('lo', 'llo'))
+        assert 'hello'.endswith(('hellox', 'hello'))
+        assert not 'hello'.endswith(())
+        assert 'helloworld'.endswith(('hellowo', 'rld', 'lowo'), 3)
+        assert not 'helloworld'.endswith(('hellowo', 'ello', 'rld'), 3, -1)
+        assert 'hello'.endswith(('hell', 'ell'), 0, -1)
+        assert not 'hello'.endswith(('he', 'hel'), 0, 1)
+        assert 'hello'.endswith(('he', 'hell'), 0, 4)
+        raises(TypeError, 'hello'.endswith, (42,))
+
     def test_expandtabs(self):
         assert 'abc\rab\tdef\ng\thi'.expandtabs() ==    'abc\rab      def\ng       hi'
         assert 'abc\rab\tdef\ng\thi'.expandtabs(8) ==   'abc\rab      def\ng       hi'

Modified: pypy/branch/pypy-2.5/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/unicodeobject.py	Sun Mar  4 19:35:40 2007
@@ -865,15 +865,32 @@
                         type(retval).__name__)
     return retval
 
-def repr__Unicode(unistr):
-    import _codecs
-    return ''.join(_codecs.unicodeescape_string(unistr,len(unistr),True))
+# XXX: These should probably be written on interplevel 
+
+def unicode_partition__Unicode_Unicode(unistr, unisub):
+    pos = unistr.find(unisub)
+    if pos == -1:
+        return (unistr, u'', u'')
+    else:
+        return (unistr[:pos], unisub, unistr[pos+len(unisub):])
+
+def unicode_rpartition__Unicode_Unicode(unistr, unisub):
+    pos = unistr.rfind(unisub)
+    if pos == -1:
+        return (u'', u'', unistr)
+    else:
+        return (unistr[:pos], unisub, unistr[pos+len(unisub):])
+
+#def unicode_startswith_
 
 ''')
+
+mod__Unicode_ANY = app.interphook('mod__Unicode_ANY')
 unicode_expandtabs__Unicode_ANY = app.interphook('unicode_expandtabs__Unicode_ANY')
 unicode_translate__Unicode_ANY = app.interphook('unicode_translate__Unicode_ANY')
-mod__Unicode_ANY = app.interphook('mod__Unicode_ANY')
 unicode_encode__Unicode_ANY_ANY = app.interphook('unicode_encode__Unicode_ANY_ANY')
+unicode_partition__Unicode_Unicode = app.interphook('unicode_partition__Unicode_Unicode')
+unicode_rpartition__Unicode_Unicode = app.interphook('unicode_rpartition__Unicode_Unicode')
 
 # Move this into the _codecs module as 'unicodeescape_string (Remember to cater for quotes)'
 def repr__Unicode(space, w_unicode):

Modified: pypy/branch/pypy-2.5/pypy/objspace/std/unicodetype.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/unicodetype.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/unicodetype.py	Sun Mar  4 19:35:40 2007
@@ -207,6 +207,19 @@
                              ' string x with zeros on the left, to fill a'
                              ' field\nof the specified width. The string x is'
                              ' never truncated.')
+unicode_partition  = SMM('partition', 2,
+                         doc='S.partition(sep) -> (head, sep, tail)\n\nSearches'
+                         ' for the separator sep in S, and returns the part before'
+                         ' it,\nthe separator itself, and the part after it.  If'
+                         ' the separator is not\nfound, returns S and two empty'
+                         ' strings.')
+unicode_rpartition = SMM('rpartition', 2,
+                     doc='S.rpartition(sep) -> (tail, sep, head)\n\nSearches'
+                         ' for the separator sep in S, starting at the end of S,'
+                         ' and returns\nthe part before it, the separator itself,'
+                         ' and the part after it.  If the\nseparator is not found,'
+                         ' returns two empty strings and S.')
+
 
 # ____________________________________________________________
 



More information about the Pypy-commit mailing list