diff --git a/lib/pure/pegs.nim b/lib/pure/pegs.nim index b2d8a2a4f..6eb7dee78 100644 --- a/lib/pure/pegs.nim +++ b/lib/pure/pegs.nim @@ -737,7 +737,12 @@ proc rawMatch*(s: string, p: Peg, start: int, c: var Captures): int {. template fillMatches(s, caps, c: expr) = for k in 0..c.ml-1: - caps[k] = substr(s, c.matches[k][0], c.matches[k][1]) + let startIdx = c.matches[k][0] + let endIdx = c.matches[k][1] + if startIdx != -1: + caps[k] = substr(s, startIdx, endIdx) + else: + caps[k] = nil proc match*(s: string, pattern: Peg, matches: var openArray[string], start = 0): bool {.nosideEffect, rtl, extern: "npegs$1Capture".} = @@ -1767,3 +1772,14 @@ when isMainModule: assert match("prefix/start", peg"^start$", 7) + if "foo" =~ peg"{'a'}?.*": + assert matches[0] == nil + else: assert false + + if "foo" =~ peg"{''}.*": + assert matches[0] == "" + else: assert false + + if "foo" =~ peg"{'foo'}": + assert matches[0] == "foo" + else: assert false diff --git a/web/news.txt b/web/news.txt index c9374558a..c79eb55cc 100644 --- a/web/news.txt +++ b/web/news.txt @@ -20,6 +20,8 @@ News - String case (or any non-ordinal case) statements without 'else' are deprecated. - Recursive tuple types are not allowed anymore. Use ``object`` instead. + - The PEGS module returns ``nil`` instead of ``""`` when an optional capture + fails to match Language Additions ------------------