Fix skipping an empty match at the end

This commit is contained in:
Oleh Prypin
2015-04-12 15:18:16 +03:00
parent ec1758509c
commit 02c6e7306f
3 changed files with 5 additions and 4 deletions

View File

@@ -496,6 +496,9 @@ iterator findIter*(str: string, pattern: Regex, start = 0, endpos = int.high): R
if match.isNone:
# either the end of the input or the string
# cannot be split here
if offset >= strlen:
break
if matchesCrLf and offset < (str.len - 1) and
str[offset] == '\r' and str[offset + 1] == '\l':
# if PCRE treats CrLf as newline, skip both at the same time
@@ -511,9 +514,6 @@ iterator findIter*(str: string, pattern: Regex, start = 0, endpos = int.high): R
yield match.get
if offset >= strlen:
# do while
break
proc find*(str: string, pattern: Regex, start = 0, endpos = int.high): Option[RegexMatch] =
## Finds the given pattern in the string between the end and start

View File

@@ -19,6 +19,7 @@ suite "find":
test "len 0 find":
check("".findAll(re"\ ") == newSeq[string]())
check("".findAll(re"") == @[""])
check("abc".findAll(re"") == @["", "", "", ""])
check("word word".findAll(re"\b") == @["", "", "", ""])
check("word\r\lword".findAll(re(r"$", "m<anycrlf>")) == @["", ""])
check("слово слово".findAll(re(r"\b", "uW")) == @["", "", "", ""])

View File

@@ -4,7 +4,7 @@ import unittest
suite "replace":
test "replace with 0-length strings":
check("".replace(re"1", proc (v: RegexMatch): string = "1") == "")
check(" ".replace(re"", proc (v: RegexMatch): string = "1") == "1 ")
check(" ".replace(re"", proc (v: RegexMatch): string = "1") == "1 1")
check("".replace(re"", proc (v: RegexMatch): string = "1") == "1")
test "regular replace":