Rename exec(...) and extend it
This commit is contained in:
parent
4f4a7dfa5b
commit
d7dbf7e011
2 changed files with 20 additions and 16 deletions
|
|
@ -253,7 +253,11 @@ proc initRegex*(pattern: string, options = "Sx"): Regex =
|
|||
result.captureNameToId = result.getNameToNumberTable()
|
||||
# }}}
|
||||
|
||||
proc exec*(self: Regex, str: string, start = 0): Option[RegexMatch] =
|
||||
proc match*(self: Regex, str: string, start = 0, endpos = -1): Option[RegexMatch] =
|
||||
## Returns Some if there is a match between `start` and `endpos`, otherwise
|
||||
## it returns None.
|
||||
##
|
||||
## if `endpos == -1`, then `endpos = str.len`
|
||||
var result: RegexMatch
|
||||
new(result)
|
||||
result.pattern = self
|
||||
|
|
@ -269,7 +273,7 @@ proc exec*(self: Regex, str: string, start = 0): Option[RegexMatch] =
|
|||
let execRet = pcre.exec(self.pcreObj,
|
||||
self.pcreExtra,
|
||||
cstring(str),
|
||||
cint(str.len),
|
||||
cint(max(str.len, endpos)),
|
||||
cint(start),
|
||||
cint(0),
|
||||
cast[ptr cint](addr result.pcreMatchBounds[0]), cint(vecsize))
|
||||
|
|
|
|||
|
|
@ -8,29 +8,29 @@ suite "captures":
|
|||
|
||||
test "capture bounds are correct":
|
||||
let ex1 = initRegex("([0-9])")
|
||||
check(ex1.exec("1 23").get.matchBounds == 0 .. 1)
|
||||
check(ex1.exec("1 23").get.captureBounds[0].get == 0 .. 1)
|
||||
check(ex1.exec("1 23", 1).get.matchBounds == 2 .. 3)
|
||||
check(ex1.exec("1 23", 3).get.matchBounds == 3 .. 4)
|
||||
check(ex1.match("1 23").get.matchBounds == 0 .. 1)
|
||||
check(ex1.match("1 23").get.captureBounds[0].get == 0 .. 1)
|
||||
check(ex1.match("1 23", 1).get.matchBounds == 2 .. 3)
|
||||
check(ex1.match("1 23", 3).get.matchBounds == 3 .. 4)
|
||||
|
||||
let ex2 = initRegex("()()()()()()()()()()([0-9])")
|
||||
check(ex2.exec("824").get.captureBounds[0].get == 0 .. 0)
|
||||
check(ex2.exec("824").get.captureBounds[10].get == 0 .. 1)
|
||||
check(ex2.match("824").get.captureBounds[0].get == 0 .. 0)
|
||||
check(ex2.match("824").get.captureBounds[10].get == 0 .. 1)
|
||||
|
||||
let ex3 = initRegex("([0-9]+)")
|
||||
check(ex3.exec("824").get.captureBounds[0].get == 0 .. 3)
|
||||
check(ex3.match("824").get.captureBounds[0].get == 0 .. 3)
|
||||
|
||||
test "named captures":
|
||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)").exec("foobar").get
|
||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)").match("foobar").get
|
||||
check(ex1.captures["foo"] == "foo")
|
||||
check(ex1.captures["bar"] == "bar")
|
||||
|
||||
let ex2 = initRegex("(?<foo>foo)(?<bar>bar)?").exec("foo").get
|
||||
let ex2 = initRegex("(?<foo>foo)(?<bar>bar)?").match("foo").get
|
||||
check(ex2.captures["foo"] == "foo")
|
||||
check(ex2.captures["bar"] == nil)
|
||||
|
||||
test "named capture bounds":
|
||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").exec("foo").get
|
||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").match("foo").get
|
||||
check(ex1.captureBounds["foo"] == Some(0..3))
|
||||
check(ex1.captureBounds["bar"] == None[Slice[int]]())
|
||||
|
||||
|
|
@ -41,20 +41,20 @@ suite "captures":
|
|||
check(ex1.captureNames == @["foo", "bar"] or ex1.captureNames == @["bar", "foo"])
|
||||
|
||||
test "named capture table":
|
||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").exec("foo").get
|
||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").match("foo").get
|
||||
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())
|
||||
|
||||
let ex2 = initRegex("(?<foo>foo)(?<bar>bar)?").exec("foobar").get
|
||||
let ex2 = initRegex("(?<foo>foo)(?<bar>bar)?").match("foobar").get
|
||||
check(ex2.captures.asTable == {"foo" : "foo", "bar" : "bar"}.toTable())
|
||||
|
||||
test "capture sequence":
|
||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").exec("foo").get
|
||||
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?").match("foo").get
|
||||
check(ex1.captures.asSeq == @["foo", nil])
|
||||
check(ex1.captureBounds.asSeq == @[Some(0..3), None[Slice[int]]()])
|
||||
check(ex1.captures.asSeq("") == @["foo", ""])
|
||||
|
||||
let ex2 = initRegex("(?<foo>foo)(?<bar>bar)?").exec("foobar").get
|
||||
let ex2 = initRegex("(?<foo>foo)(?<bar>bar)?").match("foobar").get
|
||||
check(ex2.captures.asSeq == @["foo", "bar"])
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue