Commit ee139b2b authored by Armin Rigo's avatar Armin Rigo

str.count()

parent 037c8b44
......@@ -768,6 +768,13 @@ class W_BytesObject(W_AbstractBytesObject):
return self_as_uni.descr_rstrip(space, w_chars)
return self._StringMethods_descr_rstrip(space, w_chars)
_StringMethods_descr_count = descr_count
def descr_count(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_count(space, w_sub, w_start, w_end)
return self._StringMethods_descr_count(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))
......
......@@ -680,13 +680,23 @@ class AppTestUnicodeString:
assert u"".count(u"") ==1
assert u"Python".count(u"") ==7
assert u"ab aaba".count(u"ab") ==2
assert 'aaa'.count('a') == 3
assert 'aaa'.count('b') == 0
assert 'aaa'.count('a', -1) == 1
assert 'aaa'.count('a', -10) == 3
assert 'aaa'.count('a', 0, -1) == 2
assert 'aaa'.count('a', 0, -10) == 0
assert 'ababa'.count('aba') == 1
assert u'aaa'.count(u'a') == 3
assert u'aaa'.count(u'b') == 0
assert u'aaa'.count(u'a', -1) == 1
assert u'aaa'.count(u'a', -10) == 3
assert u'aaa'.count(u'a', 0, -1) == 2
assert u'aaa'.count(u'a', 0, -10) == 0
assert u'ababa'.count(u'aba') == 1
def test_count_str_unicode(self):
assert 'aaa'.count(u'a') == 3
assert 'aaa'.count(u'b') == 0
assert 'aaa'.count(u'a', -1) == 1
assert 'aaa'.count(u'a', -10) == 3
assert 'aaa'.count(u'a', 0, -1) == 2
assert 'aaa'.count(u'a', 0, -10) == 0
assert 'ababa'.count(u'aba') == 1
raises(UnicodeDecodeError, '\x80'.count, u'')
def test_swapcase(self):
assert u'\xe4\xc4\xdf'.swapcase() == u'\xc4\xe4\xdf'
......
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