summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-01-02 21:40:36 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-01-02 21:40:36 (GMT)
commitda2ecaf3349d564ef0392183d86270eea5cdb439 (patch)
treed30ed807e0e325a492f999576bf19fd370b0dbda /Lib
parent2952148dd246b67ca88a68c44819a208d0d6624a (diff)
downloadcpython-da2ecaf3349d564ef0392183d86270eea5cdb439.zip
cpython-da2ecaf3349d564ef0392183d86270eea5cdb439.tar.gz
cpython-da2ecaf3349d564ef0392183d86270eea5cdb439.tar.bz2
Merged revisions 77241 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r77241 | antoine.pitrou | 2010-01-02 22:12:58 +0100 (sam., 02 janv. 2010) | 4 lines Issue #7462: Implement the stringlib fast search algorithm for the `rfind`, `rindex`, `rsplit` and `rpartition` methods. Patch by Florent Xicluna. ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/collections.py2
-rw-r--r--Lib/test/string_tests.py24
2 files changed, 26 insertions, 0 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index f255919..3e01054 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -767,6 +767,8 @@ class UserString(Sequence):
new = new.data
return self.__class__(self.data.replace(old, new, maxsplit))
def rfind(self, sub, start=0, end=_sys.maxsize):
+ if isinstance(sub, UserString):
+ sub = sub.data
return self.data.rfind(sub, start, end)
def rindex(self, sub, start=0, end=_sys.maxsize):
return self.data.rindex(sub, start, end)
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 85e52f3..ea49e38 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -216,6 +216,30 @@ class BaseTest(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 range(base ** digits):
+ entry = []
+ for j in range(digits):
+ i, m = divmod(i, base)
+ entry.append(charset[m])
+ teststrings.add(''.join(entry))
+ teststrings = [self.fixtype(ts) for ts in teststrings]
+ for i in teststrings:
+ 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')