[prev in list] [next in list] [prev in thread] [next in thread]
List: pypy-svn
Subject: [pypy-commit] pypy default: also support the split variant where the separator is not given
From: cfbolz <noreply () buildbot ! pypy ! org>
Date: 2013-08-31 10:14:55
Message-ID: 20130831101455.7C6151C00D8 () cobra ! cs ! uni-duesseldorf ! de
[Download RAW message or body]
Author: Carl Friedrich Bolz <cfbolz@gmx.de>
Branch:
Changeset: r66707:d5214fcde251
Date: 2013-08-31 11:14 +0100
http://bitbucket.org/pypy/pypy/changeset/d5214fcde251/
Log: also support the split variant where the separator is not given
diff --git a/rpython/rlib/rstring.py b/rpython/rlib/rstring.py
--- a/rpython/rlib/rstring.py
+++ b/rpython/rlib/rstring.py
@@ -14,7 +14,37 @@
# -------------- public API for string functions -----------------------
@specialize.argtype(0)
-def split(value, by, maxsplit=-1):
+def split(value, by=None, maxsplit=-1):
+ if by is None:
+ length = len(value)
+ i = 0
+ res = []
+ while True:
+ # find the beginning of the next word
+ print i
+ while i < length:
+ if not value[i].isspace():
+ break # found
+ i += 1
+ else:
+ break # end of string, finished
+
+ # find the end of the word
+ if maxsplit == 0:
+ j = length # take all the rest of the string
+ else:
+ j = i + 1
+ while j < length and not value[j].isspace():
+ j += 1
+ maxsplit -= 1 # NB. if it's already < 0, it stays < 0
+
+ # the word is value[i:j]
+ res.append(value[i:j])
+
+ # continue to look from the character following the space after the word
+ i = j + 1
+ return res
+
if isinstance(value, str):
assert isinstance(by, str)
else:
diff --git a/rpython/rlib/test/test_rstring.py b/rpython/rlib/test/test_rstring.py
--- a/rpython/rlib/test/test_rstring.py
+++ b/rpython/rlib/test/test_rstring.py
@@ -16,6 +16,11 @@
assert split('endcase test', 'test') == ['endcase ', '']
py.test.raises(ValueError, split, 'abc', '')
+def test_split_None():
+ assert split("") == []
+ assert split(' a\ta\na b') == ['a', 'a', 'a', 'b']
+ assert split(" a a ", maxsplit=1) == ['a', 'a ']
+
def test_split_unicode():
assert split(u"", u'x') == [u'']
assert split(u"a", u"a", 1) == [u'', u'']
@@ -168,6 +173,7 @@
def fn():
res = True
res = res and split('a//b//c//d', '//') == ['a', 'b', 'c', 'd']
+ res = res and split(' a\ta\na b') == ['a', 'a', 'a', 'b']
res = res and split('a//b//c//d', '//', 2) == ['a', 'b', 'c//d']
res = res and split(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
res = res and split(u'endcase test', u'test') == [u'endcase ', u'']
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic