Expose the pattern string, rename some procs
This commit is contained in:
parent
d7dbf7e011
commit
fb51221aa8
2 changed files with 12 additions and 11 deletions
20
src/nre.nim
20
src/nre.nim
|
|
@ -10,7 +10,7 @@ import optional_t
|
||||||
# Type definitions {{{
|
# Type definitions {{{
|
||||||
type
|
type
|
||||||
Regex* = ref object
|
Regex* = ref object
|
||||||
pattern: string # not nil
|
pattern*: string # not nil
|
||||||
pcreObj: ptr pcre.Pcre # not nil
|
pcreObj: ptr pcre.Pcre # not nil
|
||||||
pcreExtra: ptr pcre.ExtraData ## nil
|
pcreExtra: ptr pcre.ExtraData ## nil
|
||||||
|
|
||||||
|
|
@ -43,18 +43,20 @@ 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 {{{
|
# Regex 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)
|
||||||
|
|
||||||
proc captureNames*(self: Regex): seq[string] =
|
proc captureNameId*(self: Regex): Table[string, int] =
|
||||||
result = @[]
|
## Returns a map from named capture groups to their numerical
|
||||||
for key in self.captureNameToId.keys:
|
## identifier
|
||||||
result.add(key)
|
return self.captureNameToId
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# Capture accessors {{{
|
||||||
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)
|
||||||
|
|
@ -107,7 +109,7 @@ proc `[]`*(self: Captures, name: string): string =
|
||||||
return self.captures[self.pattern.captureNameToId.fget(name)]
|
return self.captures[self.pattern.captureNameToId.fget(name)]
|
||||||
|
|
||||||
template asTableImpl(cond: bool): stmt {.immediate, dirty.} =
|
template asTableImpl(cond: bool): stmt {.immediate, dirty.} =
|
||||||
for key in RegexMatch(self).pattern.captureNames:
|
for key in RegexMatch(self).pattern.captureNameId.keys:
|
||||||
let nextVal = self[key]
|
let nextVal = self[key]
|
||||||
if cond:
|
if cond:
|
||||||
result[key] = default
|
result[key] = default
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,7 @@ suite "captures":
|
||||||
test "capture count":
|
test "capture count":
|
||||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?")
|
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?")
|
||||||
check(ex1.captureCount == 2)
|
check(ex1.captureCount == 2)
|
||||||
# Don't have sets, do this :<
|
check(ex1.captureNameId == {"foo" : 0, "bar" : 1}.toTable())
|
||||||
check(ex1.captureNames == @["foo", "bar"] or ex1.captureNames == @["bar", "foo"])
|
|
||||||
|
|
||||||
test "named capture table":
|
test "named capture table":
|
||||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").match("foo").get
|
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").match("foo").get
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue