summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFlaviu Tamas <tamasflaviu@gmail.com>2015-01-10 16:36:51 -0500
committerFlaviu Tamas <tamasflaviu@gmail.com>2015-01-10 16:39:51 -0500
commit48c29ac9052b4ade187c51063693c3ed2ec27855 (patch)
tree0be01771e079a39b05678eddb33c3b47195f1edd
parentd2c20a32edd8ecb0d2ddaa3cfe39774caedceceb (diff)
downloadNim-48c29ac9052b4ade187c51063693c3ed2ec27855.tar.gz
Add access to capture count and names
-rw-r--r--src/nre.nim7
-rw-r--r--test/captures.nim6
2 files changed, 12 insertions, 1 deletions
diff --git a/src/nre.nim b/src/nre.nim
index 338b26944..e457306d6 100644
--- a/src/nre.nim
+++ b/src/nre.nim
@@ -96,13 +96,18 @@ proc getinfo[T](self: Regex, opt: cint): T =
     # XXX Error message that doesn't expose implementation details
     raise newException(FieldError, "Invalid getinfo for $1, errno $2" % [$opt, $retcode])
 
+# Capture accessors {{{
 proc captureCount(self: Regex): int =
   ## get the maximum number of captures
   ##
   ## Does not return the number of captured captures
   return getinfo[int](self, pcre.INFO_CAPTURECOUNT)
 
-# Capture accessors {{{
+proc captureNames*(self: Regex): seq[string] =
+  result = @[]
+  for key in self.captureNameToId.keys:
+    result.add(key)
+
 proc captureBounds*(self: RegexMatch): CaptureBounds = return CaptureBounds(self)
 
 proc captures*(self: RegexMatch): Captures = return Captures(self)
diff --git a/test/captures.nim b/test/captures.nim
index 91b780ad3..f2699c935 100644
--- a/test/captures.nim
+++ b/test/captures.nim
@@ -33,3 +33,9 @@ suite "captures":
     let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").exec("foo").get
     check(ex1.captureBounds["foo"] == Some(0..3))
     check(ex1.captureBounds["bar"] == None[Slice[int]]())
+
+  test "capture count":
+    let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?")
+    check(ex1.captureCount == 2)
+    # Don't have sets, do this :<
+    check(ex1.captureNames == @["foo", "bar"] or ex1.captureNames == @["bar", "foo"])