s/asTable/toTable/

toTable is more consistent with toSeq
This commit is contained in:
Flaviu Tamas 2015-01-18 12:29:10 -05:00
commit d62b41fa1c
3 changed files with 10 additions and 10 deletions

View file

@ -101,7 +101,7 @@ is inclusive.
- `"abc".match(re"abc").captureBounds[-1] == 0..3` - `"abc".match(re"abc").captureBounds[-1] == 0..3`
`match: string` :: the full text of the match. `match: string` :: the full text of the match.
`matchBounds: Slice[int]` :: the bounds of the match, as in `captureBounds[]` `matchBounds: Slice[int]` :: the bounds of the match, as in `captureBounds[]`
`(captureBounds|captures).asTable` :: returns a table with each named capture `(captureBounds|captures).toTable` :: returns a table with each named capture
as a key. as a key.
`(captureBounds|capture).toSeq` :: returns all the captures by their number. `(captureBounds|capture).toSeq` :: returns all the captures by their number.

View file

@ -137,7 +137,7 @@ proc `[]`*(pattern: Captures, name: string): string =
let pattern = RegexMatch(pattern) let pattern = RegexMatch(pattern)
return pattern.captures[pattern.pattern.captureNameToId.fget(name)] return pattern.captures[pattern.pattern.captureNameToId.fget(name)]
template asTableImpl(cond: bool): stmt {.immediate, dirty.} = template toTableImpl(cond: bool): stmt {.immediate, dirty.} =
for key in RegexMatch(pattern).pattern.captureNameId.keys: for key in RegexMatch(pattern).pattern.captureNameId.keys:
let nextVal = pattern[key] let nextVal = pattern[key]
if cond: if cond:
@ -145,16 +145,16 @@ template asTableImpl(cond: bool): stmt {.immediate, dirty.} =
else: else:
result[key] = nextVal result[key] = nextVal
proc asTable*(pattern: Captures, default: string = nil): Table[string, string] = proc toTable*(pattern: Captures, default: string = nil): Table[string, string] =
## Gets all the named captures and returns them ## Gets all the named captures and returns them
result = initTable[string, string]() result = initTable[string, string]()
asTableImpl(nextVal == nil) toTableImpl(nextVal == nil)
proc asTable*(pattern: CaptureBounds, default = None[Slice[int]]()): proc toTable*(pattern: CaptureBounds, default = None[Slice[int]]()):
Table[string, Option[Slice[int]]] = Table[string, Option[Slice[int]]] =
## Gets all the named captures and returns them ## Gets all the named captures and returns them
result = initTable[string, Option[Slice[int]]]() result = initTable[string, Option[Slice[int]]]()
asTableImpl(nextVal.isNone) toTableImpl(nextVal.isNone)
template itemsImpl(cond: bool): stmt {.immediate, dirty.} = template itemsImpl(cond: bool): stmt {.immediate, dirty.} =
for i in 0 .. <RegexMatch(pattern).pattern.captureCount: for i in 0 .. <RegexMatch(pattern).pattern.captureCount:

View file

@ -41,12 +41,12 @@ suite "captures":
test "named capture table": test "named capture table":
let ex1 = "foo".find(initRegex("(?<foo>foo)(?<bar>bar)?")) let ex1 = "foo".find(initRegex("(?<foo>foo)(?<bar>bar)?"))
check(ex1.captures.asTable == {"foo" : "foo", "bar" : nil}.toTable()) check(ex1.captures.toTable == {"foo" : "foo", "bar" : nil}.toTable())
check(ex1.captureBounds.asTable == {"foo" : Some(0..3), "bar" : None[Slice[int]]()}.toTable()) check(ex1.captureBounds.toTable == {"foo" : Some(0..3), "bar" : None[Slice[int]]()}.toTable())
check(ex1.captures.asTable("") == {"foo" : "foo", "bar" : ""}.toTable()) check(ex1.captures.toTable("") == {"foo" : "foo", "bar" : ""}.toTable())
let ex2 = "foobar".find(initRegex("(?<foo>foo)(?<bar>bar)?")) let ex2 = "foobar".find(initRegex("(?<foo>foo)(?<bar>bar)?"))
check(ex2.captures.asTable == {"foo" : "foo", "bar" : "bar"}.toTable()) check(ex2.captures.toTable == {"foo" : "foo", "bar" : "bar"}.toTable())
test "capture sequence": test "capture sequence":
let ex1 = "foo".find(initRegex("(?<foo>foo)(?<bar>bar)?")) let ex1 = "foo".find(initRegex("(?<foo>foo)(?<bar>bar)?"))