[pypy-svn] r40050 - pypy/branch/rope-branch2/pypy/objspace/std

cfbolz at codespeak.net cfbolz at codespeak.net
Wed Mar 7 21:41:08 CET 2007


Author: cfbolz
Date: Wed Mar  7 21:41:07 2007
New Revision: 40050

Modified:
   pypy/branch/rope-branch2/pypy/objspace/std/ropeobject.py
Log:
implement startswith and endswith with tuple arguments


Modified: pypy/branch/rope-branch2/pypy/objspace/std/ropeobject.py
==============================================================================
--- pypy/branch/rope-branch2/pypy/objspace/std/ropeobject.py	(original)
+++ pypy/branch/rope-branch2/pypy/objspace/std/ropeobject.py	Wed Mar  7 21:41:07 2007
@@ -31,6 +31,11 @@
 
 W_RopeObject.empty = W_RopeObject(rope.LiteralStringNode(""))
 
+def rope_w(space, w_str):
+    if isinstance(w_str, W_RopeObject):
+        return w_str._node
+    return rope.LiteralStringNode(space.str_w(w_str))
+
 registerimplementation(W_RopeObject)
 
 class W_RopeIterObject(W_Object):
@@ -347,7 +352,6 @@
 
 def str_join__Rope_ANY(space, w_self, w_list):
     list_w = space.unpackiterable(w_list)
-    str_w = space.str_w
     if list_w:
         self = w_self._node
         l = []
@@ -652,43 +656,69 @@
         i += 1
     return wrapint(space, i)
 
-def str_endswith__Rope_Rope_ANY_ANY(space, w_self, w_suffix, w_start, w_end):
-    (self, suffix, start, end) = _convert_idx_params(space, w_self,
-                                                     w_suffix, w_start, w_end)
+def ropeendswith(self, suffix, start, end):
     if suffix.length() == 0:
-        return space.w_True
+        return True
     if self.length() == 0:
-        return space.w_False
+        return False
     begin = end - suffix.length()
     if begin < start:
-        return space.w_False
+        return False
     iter1 = rope.SeekableCharIterator(self)
     iter1.seekforward(begin)
     iter2 = rope.CharIterator(suffix)
     for i in range(suffix.length()):
         if iter1.next() != iter2.next():
-            return space.w_False
-    return space.w_True
-    
-def str_startswith__Rope_Rope_ANY_ANY(space, w_self, w_prefix, w_start, w_end):
-    (self, prefix, start, end) = _convert_idx_params(space, w_self,
-                                                     w_prefix, w_start, w_end)
+            return False
+    return True
+
+
+def str_endswith__Rope_Rope_ANY_ANY(space, w_self, w_suffix, w_start, w_end):
+    (self, suffix, start, end) = _convert_idx_params(space, w_self,
+                                                     w_suffix, w_start, w_end)
+    return space.newbool(ropeendswith(self, suffix, start, end))
+
+def str_endswith__Rope_Tuple_ANY_ANY(space, w_self, w_suffixes, w_start, w_end):
+    (self, _, start, end) = _convert_idx_params(space, w_self,
+                                                  space.wrap(''), w_start, w_end)
+    for w_suffix in space.unpacktuple(w_suffixes):
+        suffix = rope_w(space, w_suffix) 
+        if ropeendswith(self, suffix, start, end):
+            return space.w_True
+    return space.w_False
+
+def ropestartswith(self, prefix, start, end):
     if prefix.length() == 0:
-        return space.w_True
+        return True
     if self.length() == 0:
-        return space.w_False
+        return False
     stop = start + prefix.length()
     if stop > end:
-        return space.w_False
+        return False
     iter1 = rope.SeekableCharIterator(self)
     iter1.seekforward(start)
     iter2 = rope.CharIterator(prefix)
     for i in range(prefix.length()):
         if iter1.next() != iter2.next():
-            return space.w_False
-    return space.w_True
-    
+            return False
+    return True
+  
+
+def str_startswith__Rope_Rope_ANY_ANY(space, w_self, w_prefix, w_start, w_end):
+    (self, prefix, start, end) = _convert_idx_params(space, w_self,
+                                                     w_prefix, w_start, w_end)
+    return space.newbool(ropestartswith(self, prefix, start, end))
     
+def str_startswith__Rope_Tuple_ANY_ANY(space, w_self, w_prefixes, w_start, w_end):
+    (self, _, start, end) = _convert_idx_params(space, w_self, space.wrap(''),
+                                                  w_start, w_end)
+    for w_prefix in space.unpacktuple(w_prefixes):
+        prefix = rope_w(space, w_prefix)
+        if ropestartswith(self, prefix, start, end):
+            return space.w_True
+    return space.w_False
+ 
+
 def _tabindent(node, tabsize):
     "calculates distance after the token to the next tabstop"
     # XXX implement reverse char iterator



More information about the Pypy-commit mailing list