Commit 4cd5cdbc authored by Armin Rigo's avatar Armin Rigo

str.find(), str.rfind(), str.index(), str.rindex()

parent ee139b2b
......@@ -775,6 +775,34 @@ class W_BytesObject(W_AbstractBytesObject):
return self_as_uni.descr_count(space, w_sub, w_start, w_end)
return self._StringMethods_descr_count(space, w_sub, w_start, w_end)
_StringMethods_descr_find = descr_find
def descr_find(self, space, w_sub, w_start=None, w_end=None):
if space.isinstance_w(w_sub, space.w_unicode):
self_as_uni = unicode_from_encoded_object(space, self, None, None)
return self_as_uni.descr_find(space, w_sub, w_start, w_end)
return self._StringMethods_descr_find(space, w_sub, w_start, w_end)
_StringMethods_descr_rfind = descr_rfind
def descr_rfind(self, space, w_sub, w_start=None, w_end=None):
if space.isinstance_w(w_sub, space.w_unicode):
self_as_uni = unicode_from_encoded_object(space, self, None, None)
return self_as_uni.descr_rfind(space, w_sub, w_start, w_end)
return self._StringMethods_descr_rfind(space, w_sub, w_start, w_end)
_StringMethods_descr_index = descr_index
def descr_index(self, space, w_sub, w_start=None, w_end=None):
if space.isinstance_w(w_sub, space.w_unicode):
self_as_uni = unicode_from_encoded_object(space, self, None, None)
return self_as_uni.descr_index(space, w_sub, w_start, w_end)
return self._StringMethods_descr_index(space, w_sub, w_start, w_end)
_StringMethods_descr_rindex = descr_rindex
def descr_rindex(self, space, w_sub, w_start=None, w_end=None):
if space.isinstance_w(w_sub, space.w_unicode):
self_as_uni = unicode_from_encoded_object(space, self, None, None)
return self_as_uni.descr_rindex(space, w_sub, w_start, w_end)
return self._StringMethods_descr_rindex(space, w_sub, w_start, w_end)
def _join_return_one(self, space, w_obj):
return (space.is_w(space.type(w_obj), space.w_str) or
space.is_w(space.type(w_obj), space.w_unicode))
......
......@@ -675,6 +675,16 @@ class AppTestUnicodeString:
def test_rfind_corner_case(self):
assert u'abc'.rfind('', 4) == -1
def test_find_index_str_unicode(self):
assert 'abcdefghiabc'.find(u'bc') == 1
assert 'abcdefghiabc'.rfind(u'abc') == 9
raises(UnicodeDecodeError, '\x80'.find, u'')
raises(UnicodeDecodeError, '\x80'.rfind, u'')
assert 'abcdefghiabc'.index(u'bc') == 1
assert 'abcdefghiabc'.rindex(u'abc') == 9
raises(UnicodeDecodeError, '\x80'.index, u'')
raises(UnicodeDecodeError, '\x80'.rindex, u'')
def test_count(self):
assert u"".count(u"x") ==0
assert u"".count(u"") ==1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment