Add access to capture count and names
This commit is contained in:
parent
d2c20a32ed
commit
48c29ac905
2 changed files with 12 additions and 1 deletions
|
|
@ -96,13 +96,18 @@ proc getinfo[T](self: Regex, opt: cint): T =
|
||||||
# XXX Error message that doesn't expose implementation details
|
# XXX Error message that doesn't expose implementation details
|
||||||
raise newException(FieldError, "Invalid getinfo for $1, errno $2" % [$opt, $retcode])
|
raise newException(FieldError, "Invalid getinfo for $1, errno $2" % [$opt, $retcode])
|
||||||
|
|
||||||
|
# Capture accessors {{{
|
||||||
proc captureCount(self: Regex): int =
|
proc captureCount(self: Regex): int =
|
||||||
## get the maximum number of captures
|
## get the maximum number of captures
|
||||||
##
|
##
|
||||||
## Does not return the number of captured captures
|
## Does not return the number of captured captures
|
||||||
return getinfo[int](self, pcre.INFO_CAPTURECOUNT)
|
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 captureBounds*(self: RegexMatch): CaptureBounds = return CaptureBounds(self)
|
||||||
|
|
||||||
proc captures*(self: RegexMatch): Captures = return Captures(self)
|
proc captures*(self: RegexMatch): Captures = return Captures(self)
|
||||||
|
|
|
||||||
|
|
@ -33,3 +33,9 @@ suite "captures":
|
||||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").exec("foo").get
|
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").exec("foo").get
|
||||||
check(ex1.captureBounds["foo"] == Some(0..3))
|
check(ex1.captureBounds["foo"] == Some(0..3))
|
||||||
check(ex1.captureBounds["bar"] == None[Slice[int]]())
|
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"])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue