summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorFlaviu Tamas <tamasflaviu@gmail.com>2014-10-21 16:49:29 -0400
committerFlaviu Tamas <tamasflaviu@gmail.com>2014-10-29 17:54:43 -0400
commitbc3464ede77bba781928b7b0f425373a520077a4 (patch)
tree1ca1d9e09a4211c4c86be0426d1b07f84db2bd3a /lib/pure
parent73ff0432dc374057d817c95be074737b82c3024c (diff)
downloadNim-bc3464ede77bba781928b7b0f425373a520077a4.tar.gz
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
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/pegs.nim18
1 files changed, 17 insertions, 1 deletions
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