Fix flaviut/nre#20
This commit is contained in:
parent
17cace280c
commit
90292754f5
2 changed files with 7 additions and 2 deletions
|
|
@ -586,9 +586,12 @@ proc split*(str: string, pattern: Regex, maxSplit = -1, start = 0): seq[string]
|
||||||
result = @[]
|
result = @[]
|
||||||
var lastIdx = start
|
var lastIdx = start
|
||||||
var splits = 0
|
var splits = 0
|
||||||
var bounds = 0 .. 0
|
var bounds = 0 .. -1
|
||||||
|
var never_ran = true
|
||||||
|
|
||||||
for match in str.findIter(pattern, start = start):
|
for match in str.findIter(pattern, start = start):
|
||||||
|
never_ran = false
|
||||||
|
|
||||||
# bounds are inclusive:
|
# bounds are inclusive:
|
||||||
#
|
#
|
||||||
# 0123456
|
# 0123456
|
||||||
|
|
@ -615,7 +618,8 @@ proc split*(str: string, pattern: Regex, maxSplit = -1, start = 0): seq[string]
|
||||||
# "12".split("\b") would be @["1", "2", ""], but
|
# "12".split("\b") would be @["1", "2", ""], but
|
||||||
# if we skip an empty last match, it's the correct
|
# if we skip an empty last match, it's the correct
|
||||||
# @["1", "2"]
|
# @["1", "2"]
|
||||||
if bounds.a <= bounds.b or bounds.b < str.high:
|
# If matches were never found, then the input string is the result
|
||||||
|
if bounds.a <= bounds.b or bounds.b < str.high or never_ran:
|
||||||
# last match: Each match takes the previous substring,
|
# last match: Each match takes the previous substring,
|
||||||
# but "1 2".split(/ /) needs to return @["1", "2"].
|
# but "1 2".split(/ /) needs to return @["1", "2"].
|
||||||
# This handles "2"
|
# This handles "2"
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ suite "string splitting":
|
||||||
check("1 2".split(re(" ")) == @["1", "2"])
|
check("1 2".split(re(" ")) == @["1", "2"])
|
||||||
check("foo".split(re("foo")) == @["", ""])
|
check("foo".split(re("foo")) == @["", ""])
|
||||||
check("".split(re"foo") == @[""])
|
check("".split(re"foo") == @[""])
|
||||||
|
check("9".split(re"\son\s") == @["9"])
|
||||||
|
|
||||||
test "captured patterns":
|
test "captured patterns":
|
||||||
check("12".split(re"(\d)") == @["", "1", "", "2", ""])
|
check("12".split(re"(\d)") == @["", "1", "", "2", ""])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue