[pypy-svn] r48510 - in pypy/branch/unicode-objspace/pypy/objspace/std: . test

fijal at codespeak.net fijal at codespeak.net
Sat Nov 10 16:48:04 CET 2007


Author: fijal
Date: Sat Nov 10 16:48:04 2007
New Revision: 48510

Modified:
   pypy/branch/unicode-objspace/pypy/objspace/std/formatting.py
   pypy/branch/unicode-objspace/pypy/objspace/std/test/test_unicodeobject.py
   pypy/branch/unicode-objspace/pypy/objspace/std/unicodeobject.py
Log:
* Slightly factor out unicode_startswith and unicode_endswith
* Fix formatting


Modified: pypy/branch/unicode-objspace/pypy/objspace/std/formatting.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/objspace/std/formatting.py	(original)
+++ pypy/branch/unicode-objspace/pypy/objspace/std/formatting.py	Sat Nov 10 16:48:04 2007
@@ -439,7 +439,7 @@
         else:
             return space.wrap(''.join(result))
     else:
-        fmt = space.unicode_w(w_fmt)
+        fmt = space.unichars_w(w_fmt)
     formatter = UnicodeFormatter(space, fmt, values_w, w_valuedict)
     result = formatter.format()
     return space.wrap(u''.join(result))

Modified: pypy/branch/unicode-objspace/pypy/objspace/std/test/test_unicodeobject.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/objspace/std/test/test_unicodeobject.py	(original)
+++ pypy/branch/unicode-objspace/pypy/objspace/std/test/test_unicodeobject.py	Sat Nov 10 16:48:04 2007
@@ -370,3 +370,6 @@
         assert unicode('x', 'utf-8') == u'x'
         assert unicode('x', 'utf-8', 'strict') == u'x'
         
+    def test_unicode_startswith_tuple(self):
+        assert u'xxx'.startswith(('x', 'y', 'z'), 0)
+        assert u'xxx'.endswith(('x', 'y', 'z'), 0)

Modified: pypy/branch/unicode-objspace/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/branch/unicode-objspace/pypy/objspace/std/unicodeobject.py	Sat Nov 10 16:48:04 2007
@@ -407,40 +407,48 @@
 
     return (self, start, end)
 
-def unicode_endswith__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
-    self, start, end = _convert_idx_params(space, w_self, w_start, w_end)
-    substr = w_substr._value
+def _check_startswith_substring(str, substr, start, end):
     substr_len = len(substr)
     
     if end - start < substr_len:
-        return space.w_False # substring is too long
+        return False # substring is too long
+    
+    for i in range(substr_len):
+        if str[start + i] != substr[i]:
+            return False
+    return True    
+
+def _check_endswith_substring(str, substr, start, end):
+    substr_len = len(substr)
+
+    if end - start < substr_len:
+        return False # substring is too long
     start = end - substr_len
     for i in range(substr_len):
-        if self[start + i] != substr[i]:
-            return space.w_False
-    return space.w_True
+        if str[start + i] != substr[i]:
+            return False
+    return True
+
+def unicode_endswith__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
+    self, start, end = _convert_idx_params(space, w_self, w_start, w_end)
+    substr = w_substr._value
+    return space.wrap(_check_endswith_substring(self, substr, start, end))
 
 def unicode_startswith__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
     self, start, end = _convert_idx_params(space, w_self, w_start, w_end)
+    # XXX this stuff can be waaay better for ootypebased backends if
+    #     we re-use more of our rpython machinery (ie implement startswith
+    #     with additional parameters as rpython)
 
     substr = w_substr._value
-    substr_len = len(substr)
-    
-    if end - start < substr_len:
-        return space.w_False # substring is too long
-    
-    for i in range(substr_len):
-        if self[start + i] != substr[i]:
-            return space.w_False
-    return space.w_True
-
+    return space.wrap(_check_startswith_substring(self, substr, start, end))
 
 def unicode_startswith__Unicode_Tuple_ANY_ANY(space, w_unistr, w_prefixes,
                                               w_start, w_end):
     unistr, start, end = _convert_idx_params(space, w_unistr, w_start, w_end)
     for w_prefix in space.unpacktuple(w_prefixes):
         prefix = space.unicode_w(w_prefix)
-        if unistr.startswith(prefix, start, end):
+        if _check_startswith_substring(unistr, prefix, start, end):
             return space.w_True
     return space.w_False
 
@@ -449,7 +457,7 @@
     unistr, start, end = _convert_idx_params(space, w_unistr, w_start, w_end)
     for w_suffix in space.unpacktuple(w_suffixes):
         suffix = space.unicode_w(w_suffix)
-        if unistr.endswith(suffix, start, end):
+        if _check_endswith_substring(unistr, suffix, start, end):
             return space.w_True
     return space.w_False
 



More information about the Pypy-commit mailing list