parent
4b5cecd902
commit
0ff4b2ba7e
2 changed files with 13 additions and 4 deletions
|
|
@ -556,19 +556,22 @@ iterator split*(s: string, sep: Regex; maxsplit = -1): string =
|
||||||
@["", "this", "is", "an", "example", ""]
|
@["", "this", "is", "an", "example", ""]
|
||||||
var last = 0
|
var last = 0
|
||||||
var splits = maxsplit
|
var splits = maxsplit
|
||||||
var x: int
|
var x = -1
|
||||||
|
if len(s) == 0:
|
||||||
|
last = 1
|
||||||
|
if matchLen(s, sep, 0) == 0:
|
||||||
|
x = 0
|
||||||
while last <= len(s):
|
while last <= len(s):
|
||||||
var first = last
|
var first = last
|
||||||
var sepLen = 1
|
var sepLen = 1
|
||||||
|
if x == 0:
|
||||||
|
inc(last)
|
||||||
while last < len(s):
|
while last < len(s):
|
||||||
x = matchLen(s, sep, last)
|
x = matchLen(s, sep, last)
|
||||||
if x >= 0:
|
if x >= 0:
|
||||||
sepLen = x
|
sepLen = x
|
||||||
break
|
break
|
||||||
inc(last)
|
inc(last)
|
||||||
if x == 0:
|
|
||||||
if last >= len(s): break
|
|
||||||
inc last
|
|
||||||
if splits == 0: last = len(s)
|
if splits == 0: last = len(s)
|
||||||
yield substr(s, first, last-1)
|
yield substr(s, first, last-1)
|
||||||
if splits == 0: break
|
if splits == 0: break
|
||||||
|
|
|
||||||
|
|
@ -108,4 +108,10 @@ proc testAll() =
|
||||||
doAssert replace("foo", re"", "-") == "-f-o-o-"
|
doAssert replace("foo", re"", "-") == "-f-o-o-"
|
||||||
doAssert replace("ooo", re"o", "-") == "---"
|
doAssert replace("ooo", re"o", "-") == "---"
|
||||||
|
|
||||||
|
block: # bug #14468
|
||||||
|
accum = @[]
|
||||||
|
for word in split("this is an example", re"\b"):
|
||||||
|
accum.add(word)
|
||||||
|
doAssert(accum == @["this", " ", "is", " ", "an", " ", "example"])
|
||||||
|
|
||||||
testAll()
|
testAll()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue