summary refs log tree commit diff stats
path: root/src/nre.nim
diff options
context:
space:
mode:
authorFlaviu Tamas <tamasflaviu@gmail.com>2015-01-10 19:10:43 -0500
committerFlaviu Tamas <tamasflaviu@gmail.com>2015-01-10 19:10:43 -0500
commit0f4b142c757f7d46071d0cbff959d5c1002d0c05 (patch)
tree3b7bcf5ec7c2a417e230e8b3cba188f651a67e5a /src/nre.nim
parentcc33942d893b339f87976008b06bc55b8a36d5f0 (diff)
downloadNim-0f4b142c757f7d46071d0cbff959d5c1002d0c05.tar.gz
Implement toTable(Captures), toSeq(Captures)
Diffstat (limited to 'src/nre.nim')
-rw-r--r--src/nre.nim35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/nre.nim b/src/nre.nim
index 5a5cbaa45..64ed8c75d 100644
--- a/src/nre.nim
+++ b/src/nre.nim
@@ -2,6 +2,7 @@ import private.pcre as pcre
 import private.util
 import tables
 import unsigned
+from future import lc, `[]`
 from strutils import toLower, `%`
 from math import ceil
 import optional_t
@@ -102,6 +103,40 @@ proc `[]`*(self: Captures, name: string): string =
   ## Will fail with KeyError if `name` is not a real named capture
   let self = RegexMatch(self)
   return self.captures[self.pattern.captureNameToId.fget(name)]
+
+template asTableImpl(cond: bool): stmt {.immediate, dirty.} =
+  for key in RegexMatch(self).pattern.captureNames:
+    let nextVal = self[key]
+    if cond:
+      result[key] = default
+    else:
+      result[key] = nextVal
+
+proc asTable*(self: Captures, default: string = nil): Table[string, string] =
+  ## Gets all the named captures and returns them
+  result = initTable[string, string]()
+  asTableImpl(nextVal == nil)
+
+proc asTable*(self: CaptureBounds, default = None[Slice[int]]()):
+    Table[string, Option[Slice[int]]] =
+  ## Gets all the named captures and returns them
+  result = initTable[string, Option[Slice[int]]]()
+  asTableImpl(nextVal.isNone)
+
+template asSeqImpl(cond: bool): stmt {.immediate, dirty.} =
+  result = @[]
+  for i in 0 .. <RegexMatch(self).pattern.captureCount:
+    let nextVal = self[i]
+    if cond:
+      result.add(default)
+    else:
+      result.add(nextVal)
+
+proc asSeq*(self: CaptureBounds, default = None[Slice[int]]()): seq[Option[Slice[int]]] =
+  asSeqImpl(nextVal.isNone)
+
+proc asSeq*(self: Captures, default: string = nil): seq[string] =
+  asSeqImpl(nextVal == nil)
 # }}}
 
 # Creation & Destruction {{{