[pypy-svn] pypy default: The 'maxsplit' argument of str.split(sep, maxsplit=-1) is now RPython.
amauryfa
commits-noreply at bitbucket.org
Wed Mar 30 19:53:58 CEST 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch:
Changeset: r43024:12df7babf225
Date: 2011-03-29 14:58 +0200
http://bitbucket.org/pypy/pypy/changeset/12df7babf225/
Log: The 'maxsplit' argument of str.split(sep, maxsplit=-1) is now
RPython.
diff --git a/pypy/rpython/test/test_rstr.py b/pypy/rpython/test/test_rstr.py
--- a/pypy/rpython/test/test_rstr.py
+++ b/pypy/rpython/test/test_rstr.py
@@ -590,6 +590,21 @@
res = self.interpret(fn, [i])
assert res == fn(i)
+ def test_split_limit(self):
+ const = self.const
+ def fn(i, j):
+ s = [const(''), const('0.1.2.4.8'), const('.1.2'), const('1.2.'), const('.1.2.4.')][i]
+ l = s.split(const('.'), j)
+ sum = 0
+ for num in l:
+ if len(num):
+ sum += ord(num[0]) - ord(const('0')[0])
+ return sum + len(l) * 100
+ for i in range(5):
+ for j in range(4):
+ res = self.interpret(fn, [i, j])
+ assert res == fn(i, j)
+
def test_contains(self):
const = self.const
constchar = self.constchar
diff --git a/pypy/rpython/rstr.py b/pypy/rpython/rstr.py
--- a/pypy/rpython/rstr.py
+++ b/pypy/rpython/rstr.py
@@ -221,14 +221,18 @@
def rtype_method_split(self, hop):
rstr = hop.args_r[0].repr
- v_str, v_chr = hop.inputargs(rstr.repr, rstr.char_repr)
+ if hop.nb_args == 3:
+ v_str, v_chr, v_max = hop.inputargs(rstr.repr, rstr.char_repr, Signed)
+ else:
+ v_str, v_chr = hop.inputargs(rstr.repr, rstr.char_repr)
+ v_max = hop.inputconst(Signed, -1)
try:
list_type = hop.r_result.lowleveltype.TO
except AttributeError:
list_type = hop.r_result.lowleveltype
cLIST = hop.inputconst(Void, list_type)
hop.exception_cannot_occur()
- return hop.gendirectcall(self.ll.ll_split_chr, cLIST, v_str, v_chr)
+ return hop.gendirectcall(self.ll.ll_split_chr, cLIST, v_str, v_chr, v_max)
def rtype_method_replace(self, hop):
rstr = hop.args_r[0].repr
diff --git a/pypy/rpython/ootypesystem/ootype.py b/pypy/rpython/ootypesystem/ootype.py
--- a/pypy/rpython/ootypesystem/ootype.py
+++ b/pypy/rpython/ootypesystem/ootype.py
@@ -443,7 +443,7 @@
"ll_upper": Meth([], self.SELFTYPE_T),
"ll_lower": Meth([], self.SELFTYPE_T),
"ll_substring": Meth([Signed, Signed], self.SELFTYPE_T), # ll_substring(start, count)
- "ll_split_chr": Meth([self.CHAR], Array(self.SELFTYPE_T)), # XXX this is not pure!
+ "ll_split_chr": Meth([self.CHAR, Signed], Array(self.SELFTYPE_T)), # XXX this is not pure!
"ll_contains": Meth([self.CHAR], Bool),
"ll_replace_chr_chr": Meth([self.CHAR, self.CHAR], self.SELFTYPE_T),
})
@@ -1480,9 +1480,9 @@
# NOT_RPYTHON
return self.make_string(self._str[start:start+count])
- def ll_split_chr(self, ch):
+ def ll_split_chr(self, ch, max):
# NOT_RPYTHON
- l = [self.make_string(s) for s in self._str.split(ch)]
+ l = [self.make_string(s) for s in self._str.split(ch, max)]
res = _array(Array(self._TYPE), len(l))
res._array[:] = l
return res
diff --git a/pypy/translator/cli/src/pypylib.cs b/pypy/translator/cli/src/pypylib.cs
--- a/pypy/translator/cli/src/pypylib.cs
+++ b/pypy/translator/cli/src/pypylib.cs
@@ -717,9 +717,12 @@
return s.Substring(start, count);
}
- public static string[] ll_split_chr(string s, char ch)
+ public static string[] ll_split_chr(string s, char ch, int max)
{
- return s.Split(ch);
+ if (max < 0)
+ return s.Split(ch);
+ else
+ return s.Split(new Char[] {ch}, max + 1);
}
public static bool ll_contains(string s, char ch)
diff --git a/pypy/rpython/lltypesystem/rstr.py b/pypy/rpython/lltypesystem/rstr.py
--- a/pypy/rpython/lltypesystem/rstr.py
+++ b/pypy/rpython/lltypesystem/rstr.py
@@ -722,26 +722,35 @@
newlen = len(s1.chars) - 1
return LLHelpers._ll_stringslice(s1, 0, newlen)
- def ll_split_chr(LIST, s, c):
+ def ll_split_chr(LIST, s, c, max):
chars = s.chars
strlen = len(chars)
count = 1
i = 0
+ if max == 0:
+ i = strlen
while i < strlen:
if chars[i] == c:
count += 1
+ if max >= 0 and count > max:
+ break
i += 1
res = LIST.ll_newlist(count)
items = res.ll_items()
i = 0
j = 0
resindex = 0
+ if max == 0:
+ j = strlen
while j < strlen:
if chars[j] == c:
item = items[resindex] = s.malloc(j - i)
item.copy_contents(s, item, i, 0, j - i)
resindex += 1
i = j + 1
+ if max >= 0 and resindex >= max:
+ j = strlen
+ break
j += 1
item = items[resindex] = s.malloc(j - i)
item.copy_contents(s, item, i, 0, j - i)
diff --git a/pypy/annotation/unaryop.py b/pypy/annotation/unaryop.py
--- a/pypy/annotation/unaryop.py
+++ b/pypy/annotation/unaryop.py
@@ -499,7 +499,7 @@
def getanyitem(str):
return str.basecharclass()
- def method_split(str, patt): # XXX
+ def method_split(str, patt, max=-1):
getbookkeeper().count("str_split", str, patt)
return getbookkeeper().newlist(str.basestringclass())
diff --git a/pypy/rpython/ootypesystem/rstr.py b/pypy/rpython/ootypesystem/rstr.py
--- a/pypy/rpython/ootypesystem/rstr.py
+++ b/pypy/rpython/ootypesystem/rstr.py
@@ -211,8 +211,8 @@
def ll_stringslice_minusone(s):
return s.ll_substring(0, s.ll_strlen()-1)
- def ll_split_chr(RESULT, s, c):
- return RESULT.ll_convert_from_array(s.ll_split_chr(c))
+ def ll_split_chr(RESULT, s, c, max):
+ return RESULT.ll_convert_from_array(s.ll_split_chr(c, max))
def ll_int(s, base):
if not 2 <= base <= 36:
diff --git a/pypy/translator/jvm/src/pypy/PyPy.java b/pypy/translator/jvm/src/pypy/PyPy.java
--- a/pypy/translator/jvm/src/pypy/PyPy.java
+++ b/pypy/translator/jvm/src/pypy/PyPy.java
@@ -746,11 +746,13 @@
return str.substring(start, end);
}
- public static Object[] ll_split_chr(String str, char c) {
+ public static Object[] ll_split_chr(String str, char c, int max) {
ArrayList list = new ArrayList();
int lastidx = 0, idx = 0;
while ((idx = str.indexOf(c, lastidx)) != -1)
{
+ if (max >= 0 && list.size() >= max)
+ break;
String sub = str.substring(lastidx, idx);
list.add(sub);
lastidx = idx+1;
More information about the Pypy-commit
mailing list