diff --git a/src/nre.nim b/src/nre.nim index 76a45532bc..fbfe7deb10 100644 --- a/src/nre.nim +++ b/src/nre.nim @@ -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 diff --git a/test/find.nim b/test/find.nim index c138c2b28e..549fd6ecd9 100644 --- a/test/find.nim +++ b/test/find.nim @@ -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")) == @["", ""]) check("слово слово".findAll(re(r"\b", "uW")) == @["", "", "", ""]) diff --git a/test/replace.nim b/test/replace.nim index 4e0b6e3726..516fd43280 100644 --- a/test/replace.nim +++ b/test/replace.nim @@ -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":