Author: Carl Friedrich Bolz <[email protected]>
Branch: 
Changeset: r64903:005d786754eb
Date: 2013-06-13 22:32 +0200
http://bitbucket.org/pypy/pypy/changeset/005d786754eb/

Log:    make rstring.split and rsplit work for unicode strings too

diff --git a/rpython/rlib/rstring.py b/rpython/rlib/rstring.py
--- a/rpython/rlib/rstring.py
+++ b/rpython/rlib/rstring.py
@@ -3,14 +3,17 @@
 
 from rpython.annotator.model import (SomeObject, SomeString, s_None, SomeChar,
     SomeInteger, SomeUnicodeCodePoint, SomeUnicodeString, SomePtr, SomePBC)
-from rpython.rlib.objectmodel import newlist_hint
+from rpython.rlib.objectmodel import newlist_hint, specialize
 from rpython.rlib.rarithmetic import ovfcheck
 from rpython.rtyper.extregistry import ExtRegistryEntry
 from rpython.tool.pairtype import pairtype
 
 
 # -------------- public API for string functions -----------------------
+
[email protected](0)
 def split(value, by, maxsplit=-1):
+    assert type(value) == type(by)
     bylen = len(by)
     if bylen == 0:
         raise ValueError("empty separator")
@@ -32,7 +35,9 @@
     return res
 
 
[email protected](0)
 def rsplit(value, by, maxsplit=-1):
+    assert type(value) == type(by)
     if maxsplit > 0:
         res = newlist_hint(min(maxsplit + 1, len(value)))
     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
@@ -13,6 +13,17 @@
     assert split('endcase test', 'test') == ['endcase ', '']
     py.test.raises(ValueError, split, 'abc', '')
 
+def test_split_unicode():
+    assert split(u"", u'x') == [u'']
+    assert split(u"a", u"a", 1) == [u'', u'']
+    assert split(u" ", u" ", 1) == [u'', u'']
+    assert split(u"aa", u"a", 2) == [u'', u'', u'']
+    assert split(u'a|b|c|d', u'|') == [u'a', u'b', u'c', u'd']
+    assert split(u'a|b|c|d', u'|', 2) == [u'a', u'b', u'c|d']
+    assert split(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
+    assert split(u'endcase test', u'test') == [u'endcase ', u'']
+    py.test.raises(ValueError, split, u'abc', u'')
+
 def test_rsplit():
     assert rsplit("a", "a", 1) == ['', '']
     assert rsplit(" ", " ", 1) == ['', '']
@@ -23,6 +34,16 @@
     assert rsplit('endcase test', 'test') == ['endcase ', '']
     py.test.raises(ValueError, rsplit, "abc", '')
 
+def test_rsplit_unicode():
+    assert rsplit(u"a", u"a", 1) == [u'', u'']
+    assert rsplit(u" ", u" ", 1) == [u'', u'']
+    assert rsplit(u"aa", u"a", 2) == [u'', u'', u'']
+    assert rsplit(u'a|b|c|d', u'|') == [u'a', u'b', u'c', u'd']
+    assert rsplit(u'a|b|c|d', u'|', 2) == [u'a|b', u'c', u'd']
+    assert rsplit(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
+    assert rsplit(u'endcase test', u'test') == [u'endcase ', u'']
+    py.test.raises(ValueError, rsplit, u"abc", u'')
+
 def test_string_builder():
     s = StringBuilder()
     s.append("a")
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to