Commit 954accec authored by Guido van Rossum's avatar Guido van Rossum

In string.split(), honor maxsplit (if > 0).

In string.splitfields(), ignore maxsplit if <= 0, rather than ignoring
maxsplit=0 but effectively treating negative numbers the same as
maxsplit=1.  Also made the test for maxsplit slightly more efficient
(set it to the length of the string when <= 0 so the test for its
presence can be omitted from the loop).
parent b8beff75
...@@ -74,11 +74,17 @@ def split(s, sep=None, maxsplit=0): ...@@ -74,11 +74,17 @@ def split(s, sep=None, maxsplit=0):
if sep is not None: return splitfields(s, sep, maxsplit) if sep is not None: return splitfields(s, sep, maxsplit)
res = [] res = []
i, n = 0, len(s) i, n = 0, len(s)
if maxsplit <= 0: maxsplit = n
count = 0
while i < n: while i < n:
while i < n and s[i] in whitespace: i = i+1 while i < n and s[i] in whitespace: i = i+1
if i == n: break if i == n: break
if count >= maxsplit:
res.append(s[i:])
break
j = i j = i
while j < n and s[j] not in whitespace: j = j+1 while j < n and s[j] not in whitespace: j = j+1
count = count + 1
res.append(s[i:j]) res.append(s[i:j])
i = j i = j
return res return res
...@@ -93,6 +99,7 @@ def splitfields(s, sep=None, maxsplit=0): ...@@ -93,6 +99,7 @@ def splitfields(s, sep=None, maxsplit=0):
if nsep == 0: if nsep == 0:
return [s] return [s]
ns = len(s) ns = len(s)
if maxsplit <= 0: maxsplit = ns
i = j = 0 i = j = 0
count = 0 count = 0
while j+nsep <= ns: while j+nsep <= ns:
...@@ -100,8 +107,7 @@ def splitfields(s, sep=None, maxsplit=0): ...@@ -100,8 +107,7 @@ def splitfields(s, sep=None, maxsplit=0):
count = count + 1 count = count + 1
res.append(s[i:j]) res.append(s[i:j])
i = j = j + nsep i = j = j + nsep
if (maxsplit and (count >= maxsplit)): if count >= maxsplit: break
break
else: else:
j = j + 1 j = j + 1
......
...@@ -74,11 +74,17 @@ def split(s, sep=None, maxsplit=0): ...@@ -74,11 +74,17 @@ def split(s, sep=None, maxsplit=0):
if sep is not None: return splitfields(s, sep, maxsplit) if sep is not None: return splitfields(s, sep, maxsplit)
res = [] res = []
i, n = 0, len(s) i, n = 0, len(s)
if maxsplit <= 0: maxsplit = n
count = 0
while i < n: while i < n:
while i < n and s[i] in whitespace: i = i+1 while i < n and s[i] in whitespace: i = i+1
if i == n: break if i == n: break
if count >= maxsplit:
res.append(s[i:])
break
j = i j = i
while j < n and s[j] not in whitespace: j = j+1 while j < n and s[j] not in whitespace: j = j+1
count = count + 1
res.append(s[i:j]) res.append(s[i:j])
i = j i = j
return res return res
...@@ -93,6 +99,7 @@ def splitfields(s, sep=None, maxsplit=0): ...@@ -93,6 +99,7 @@ def splitfields(s, sep=None, maxsplit=0):
if nsep == 0: if nsep == 0:
return [s] return [s]
ns = len(s) ns = len(s)
if maxsplit <= 0: maxsplit = ns
i = j = 0 i = j = 0
count = 0 count = 0
while j+nsep <= ns: while j+nsep <= ns:
...@@ -100,8 +107,7 @@ def splitfields(s, sep=None, maxsplit=0): ...@@ -100,8 +107,7 @@ def splitfields(s, sep=None, maxsplit=0):
count = count + 1 count = count + 1
res.append(s[i:j]) res.append(s[i:j])
i = j = j + nsep i = j = j + nsep
if (maxsplit and (count >= maxsplit)): if count >= maxsplit: break
break
else: else:
j = j + 1 j = j + 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment