summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/string.py12
-rw-r--r--Lib/test/string_tests.py20
2 files changed, 32 insertions, 0 deletions
diff --git a/Lib/string.py b/Lib/string.py
index 0a77f46..bc10c20 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -121,6 +121,18 @@ def split(s, sep=None, maxsplit=-1):
return s.split(sep, maxsplit)
splitfields = split
+# Split a string into a list of space/tab-separated words
+def rsplit(s, sep=None, maxsplit=-1):
+ """rsplit(s [,sep [,maxsplit]]) -> list of strings
+
+ Return a list of the words in the string s, using sep as the
+ delimiter string, starting at the end of the string and working
+ to the front. If maxsplit is given, at most maxsplit splits are
+ done. If sep is not specified or is None, any whitespace string
+ is a separator.
+ """
+ return s.rsplit(sep, maxsplit)
+
# Join fields with optional separator
def join(words, sep = ' '):
"""join(list [,sep]) -> string
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 236c577..c3e53ad 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -189,6 +189,26 @@ class CommonTest(unittest.TestCase):
self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
+ def test_rsplit(self):
+ self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
+ 'this is the rsplit function', 'rsplit')
+ self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
+ self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
+ self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
+ self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
+ self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
+ self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
+ self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
+ self.checkequal(['a, b, c', 'd'], 'a, b, c, d', 'rsplit', ', ', 1)
+ self.checkequal(['a, b', 'c', 'd'], 'a, b, c, d', 'rsplit', ', ', 2)
+ self.checkequal(['a', 'b', 'c', 'd'], 'a, b, c, d', 'rsplit', ', ', 3)
+ self.checkequal(['a', 'b', 'c', 'd'], 'a, b, c, d', 'rsplit', ', ', 4)
+ self.checkequal(['a, b, c, d'], 'a, b, c, d', 'rsplit', ', ', 0)
+ self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
+ self.checkequal(['a\x00b', 'c'], 'a\x00b\x00c', 'rsplit', '\x00', 1)
+ self.checkequal(['', ''], 'abcd', 'rsplit', 'abcd')
+ self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2)
+
def test_strip(self):
self.checkequal('hello', ' hello ', 'strip')
self.checkequal('hello ', ' hello ', 'lstrip')