Issue #7462: Implement the stringlib fast search algorithm for the rfind,

`rindex`, `rsplit` and `rpartition` methods.  Patch by Florent Xicluna.
This commit is contained in:
Antoine Pitrou 2010-01-02 21:12:58 +00:00
parent d3e323215c
commit 5b7139aab4
11 changed files with 148 additions and 149 deletions

View file

@ -230,6 +230,31 @@ class CommonTest(unittest.TestCase):
self.checkraises(TypeError, 'hello', 'rfind')
self.checkraises(TypeError, 'hello', 'rfind', 42)
# For a variety of combinations,
# verify that str.rfind() matches __contains__
# and that the found substring is really at that location
charset = ['', 'a', 'b', 'c']
digits = 5
base = len(charset)
teststrings = set()
for i in xrange(base ** digits):
entry = []
for j in xrange(digits):
i, m = divmod(i, base)
entry.append(charset[m])
teststrings.add(''.join(entry))
teststrings = list(teststrings)
for i in teststrings:
i = self.fixtype(i)
for j in teststrings:
loc = i.rfind(j)
r1 = (loc != -1)
r2 = j in i
if r1 != r2:
self.assertEqual(r1, r2)
if loc != -1:
self.assertEqual(i[loc:loc+len(j)], j)
def test_index(self):
self.checkequal(0, 'abcdefghiabc', 'index', '')
self.checkequal(3, 'abcdefghiabc', 'index', 'def')
@ -686,8 +711,10 @@ class CommonTest(unittest.TestCase):
EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
ba = buffer('a')
bb = buffer('b')
# Silence Py3k warning
with test_support.check_warnings():
ba = buffer('a')
bb = buffer('b')
EQ("bbc", "abc", "replace", ba, bb)
EQ("aac", "abc", "replace", bb, ba)