[pypy-svn] r8496 - in pypy/dist/pypy/objspace/std: . test

odie at codespeak.net odie at codespeak.net
Sun Jan 23 17:21:55 CET 2005


Author: odie
Date: Sun Jan 23 17:21:54 2005
New Revision: 8496

Modified:
   pypy/dist/pypy/objspace/std/stringobject.py
   pypy/dist/pypy/objspace/std/stringtype.py
   pypy/dist/pypy/objspace/std/test/test_stringobject.py
Log:
String index, rindex and count method support for int and long.

Modified: pypy/dist/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/stringobject.py	Sun Jan 23 17:21:54 2005
@@ -395,31 +395,28 @@
 def _convert_idx_params(space, w_self, w_sub, w_start, w_end):
     self = w_self._value
     sub = w_sub._value
-    if space.is_true(space.is_(w_start,space.w_None)):
-        start = 0
-    else:
-        start = space.int_w(w_start)
-    if space.is_true(space.is_(w_end,space.w_None)):
-        end = len(self)
-    else:
-        end = space.int_w(w_end)
+    w_start = slicetype.adapt_bound(space, w_start, space.wrap(len(self)))
+    w_end = slicetype.adapt_bound(space, w_end, space.wrap(len(self)))
+
+    start = space.int_w(w_start)
+    end = space.int_w(w_end)
 
     return (self, sub, start, end)
 
 
-def str_find__String_String_ANY_ANY(space, w_self, w_sub, w_start=None, w_end=None):
+def str_find__String_String_ANY_ANY(space, w_self, w_sub, w_start, w_end):
 
     (self, sub, start, end) =  _convert_idx_params(space, w_self, w_sub, w_start, w_end)
     res = _find(self, sub, start, end, 1)
     return space.wrap(res)
 
-def str_rfind__String_String_ANY_ANY(space, w_self, w_sub, w_start=None, w_end=None):
+def str_rfind__String_String_ANY_ANY(space, w_self, w_sub, w_start, w_end):
 
     (self, sub, start, end) =  _convert_idx_params(space, w_self, w_sub, w_start, w_end)
     res = _find(self, sub, start, end, -1)
     return space.wrap(res)
 
-def str_index__String_String_ANY_ANY(space, w_self, w_sub, w_start=None, w_end=None):
+def str_index__String_String_ANY_ANY(space, w_self, w_sub, w_start, w_end):
 
     (self, sub, start, end) =  _convert_idx_params(space, w_self, w_sub, w_start, w_end)
     res = _find(self, sub, start, end, 1)
@@ -431,7 +428,7 @@
     return space.wrap(res)
 
 
-def str_rindex__String_String_ANY_ANY(space, w_self, w_sub, w_start=None, w_end=None):
+def str_rindex__String_String_ANY_ANY(space, w_self, w_sub, w_start, w_end):
 
     (self, sub, start, end) =  _convert_idx_params(space, w_self, w_sub, w_start, w_end)
     res = _find(self, sub, start, end, -1)
@@ -590,26 +587,16 @@
     u_self  = w_self._value
     u_arg   = w_arg._value
 
-    if space.is_true(space.is_(w_start,space.w_None)):
-        u_start = 0
-    else:
-        u_start = space.int_w(w_start)
-
-    if space.is_true(space.is_(w_end,space.w_None)):
-        u_end = len(u_self)
-    else:
-        u_end = space.int_w(w_end)
-        if u_end < 0:
-            u_end += len(u_self)
-    
-    area =  u_self [u_start:u_end]
+    w_start = slicetype.adapt_bound(space, w_start, space.wrap(len(u_self)))
+    w_end = slicetype.adapt_bound(space, w_end, space.wrap(len(u_self)))
+    u_start = space.int_w(w_start)
+    u_end = space.int_w(w_end)
     
     count = 0  
 
-    pos = -1
+    pos = u_start - 1 
     while 1: 
-       pos = _find(area, u_arg, pos+1, u_end, 1)
-       #pos = area.find(u_arg, pos+1, u_end)
+       pos = _find(u_self, u_arg, pos+1, u_end, 1)
        if pos == -1:
           break
        count += 1

Modified: pypy/dist/pypy/objspace/std/stringtype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringtype.py	(original)
+++ pypy/dist/pypy/objspace/std/stringtype.py	Sun Jan 23 17:21:54 2005
@@ -1,6 +1,7 @@
 from pypy.objspace.std.stdtypedef import *
 from pypy.objspace.std.basestringtype import basestring_typedef
 
+from sys import maxint
 
 str_join    = MultiMethod('join', 2)
 str_split   = MultiMethod('split', 3, defaults=(None,-1))
@@ -18,17 +19,17 @@
 str_swapcase   = MultiMethod('swapcase', 1)
 str_capitalize = MultiMethod('capitalize', 1)
 str_title      = MultiMethod('title', 1)
-str_find       = MultiMethod('find', 4, defaults=(None, None))
-str_rfind      = MultiMethod('rfind', 4, defaults=(None, None))
-str_index      = MultiMethod('index', 4, defaults=(None, None))
-str_rindex     = MultiMethod('rindex', 4, defaults=(None, None))
+str_find       = MultiMethod('find', 4, defaults=(0, maxint))
+str_rfind      = MultiMethod('rfind', 4, defaults=(0, maxint))
+str_index      = MultiMethod('index', 4, defaults=(0, maxint))
+str_rindex     = MultiMethod('rindex', 4, defaults=(0, maxint))
 str_replace    = MultiMethod('replace', 4, defaults=(-1,))
 str_zfill      = MultiMethod('zfill', 2)
 str_strip      = MultiMethod('strip',  2, defaults=('', ' '))
 str_rstrip     = MultiMethod('rstrip', 2, defaults=('', ' '))
 str_lstrip     = MultiMethod('lstrip', 2, defaults=('', ' '))
 str_center     = MultiMethod('center', 2, )
-str_count      = MultiMethod('count', 4, defaults=(None,None))      
+str_count      = MultiMethod('count', 4, defaults=(0, maxint))      
 str_endswith   = MultiMethod('endswith', 2)   #[optional arguments not supported now]
 str_expandtabs = MultiMethod('expandtabs', 2, defaults=(8,))
 str_splitlines = MultiMethod('splitlines', 2, defaults=(0,))

Modified: pypy/dist/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_stringobject.py	Sun Jan 23 17:21:54 2005
@@ -324,14 +324,18 @@
         assert 'abcdefghiabc'.find('def', 4) == -1
 
     def test_index(self):
+        from sys import maxint
         assert 'abcdefghiabc'.index('') == 0
         assert 'abcdefghiabc'.index('def') == 3
         assert 'abcdefghiabc'.index('abc') == 0
         assert 'abcdefghiabc'.index('abc', 1) == 9
+        assert 'abcdefghiabc'.index('def', -4*maxint, 4*maxint) == 3
         raises(ValueError, 'abcdefghiabc'.index, 'hib')
         raises(ValueError, 'abcdefghiab'.index, 'abc', 1)
         raises(ValueError, 'abcdefghi'.index, 'ghi', 8)
         raises(ValueError, 'abcdefghi'.index, 'ghi', -1)
+        raises(TypeError, 'abcdefghijklmn'.index, 'abc', 0, 0.0)
+        raises(TypeError, 'abcdefghijklmn'.index, 'abc', -10.0, 30)
 
     def test_rfind(self):
         assert 'abcdefghiabc'.rfind('abc') == 9
@@ -340,15 +344,19 @@
         assert 'abcdefghiabc'.rfind('abcz') == -1
 
     def test_rindex(self):
+        from sys import maxint
         assert 'abcdefghiabc'.rindex('') == 12
         assert 'abcdefghiabc'.rindex('def') == 3
         assert 'abcdefghiabc'.rindex('abc') == 9
         assert 'abcdefghiabc'.rindex('abc', 0, -1) == 0
+        assert 'abcdefghiabc'.rindex('abc', -4*maxint, 4*maxint) == 9
         raises(ValueError, 'abcdefghiabc'.rindex, 'hib')
         raises(ValueError, 'defghiabc'.rindex, 'def', 1)
         raises(ValueError, 'defghiabc'.rindex, 'abc', 0, -1)
         raises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8)
         raises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1)
+        raises(TypeError, 'abcdefghijklmn'.rindex, 'abc', 0, 0.0)
+        raises(TypeError, 'abcdefghijklmn'.rindex, 'abc', -10.0, 30)
 
 
     def test_split_maxsplit(self):



More information about the Pypy-commit mailing list