mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 05:50:30 +00:00
Modify pegs.nim such that no match will return nil
An empty match will return "" A zero-length match will return nil Add test cases Add news information
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
------------------
|
||||
|
||||
Reference in New Issue
Block a user