mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-08 05:53:22 +00:00
s/asTable/toTable/
toTable is more consistent with toSeq
This commit is contained in:
@@ -101,7 +101,7 @@ is inclusive.
|
||||
- `"abc".match(re"abc").captureBounds[-1] == 0..3`
|
||||
`match: string` :: the full text of the match.
|
||||
`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.
|
||||
`(captureBounds|capture).toSeq` :: returns all the captures by their number.
|
||||
|
||||
|
||||
10
src/nre.nim
10
src/nre.nim
@@ -137,7 +137,7 @@ proc `[]`*(pattern: Captures, name: string): string =
|
||||
let pattern = RegexMatch(pattern)
|
||||
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:
|
||||
let nextVal = pattern[key]
|
||||
if cond:
|
||||
@@ -145,16 +145,16 @@ template asTableImpl(cond: bool): stmt {.immediate, dirty.} =
|
||||
else:
|
||||
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
|
||||
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]]] =
|
||||
## Gets all the named captures and returns them
|
||||
result = initTable[string, Option[Slice[int]]]()
|
||||
asTableImpl(nextVal.isNone)
|
||||
toTableImpl(nextVal.isNone)
|
||||
|
||||
template itemsImpl(cond: bool): stmt {.immediate, dirty.} =
|
||||
for i in 0 .. <RegexMatch(pattern).pattern.captureCount:
|
||||
|
||||
@@ -41,12 +41,12 @@ suite "captures":
|
||||
|
||||
test "named capture table":
|
||||
let ex1 = "foo".find(initRegex("(?<foo>foo)(?<bar>bar)?"))
|
||||
check(ex1.captures.asTable == {"foo" : "foo", "bar" : nil}.toTable())
|
||||
check(ex1.captureBounds.asTable == {"foo" : Some(0..3), "bar" : None[Slice[int]]()}.toTable())
|
||||
check(ex1.captures.asTable("") == {"foo" : "foo", "bar" : ""}.toTable())
|
||||
check(ex1.captures.toTable == {"foo" : "foo", "bar" : nil}.toTable())
|
||||
check(ex1.captureBounds.toTable == {"foo" : Some(0..3), "bar" : None[Slice[int]]()}.toTable())
|
||||
check(ex1.captures.toTable("") == {"foo" : "foo", "bar" : ""}.toTable())
|
||||
|
||||
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":
|
||||
let ex1 = "foo".find(initRegex("(?<foo>foo)(?<bar>bar)?"))
|
||||
|
||||
Reference in New Issue
Block a user