Remove all optional RegexMatch returns

This commit is contained in:
Flaviu Tamas
2015-01-12 21:12:23 -05:00
parent efb4f7d89c
commit aac71d0fc9
3 changed files with 26 additions and 28 deletions

View File

@@ -284,8 +284,7 @@ proc initRegex*(pattern: string, options = "Sx"): Regex =
result.captureNameToId = result.getNameToNumberTable()
# }}}
proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): Option[RegexMatch] =
var result: RegexMatch
proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): RegexMatch =
new(result)
result.pattern = pattern
result.str = str
@@ -306,15 +305,15 @@ proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): Opt
cast[ptr cint](addr result.pcreMatchBounds[0]),
cint(vecsize))
if execRet >= 0:
return Some(result)
return result
elif execRet == pcre.ERROR_NOMATCH:
return None[RegexMatch]()
return nil
else:
raise newException(AssertionError, "Internal error: errno " & $execRet)
proc match*(str: string, pattern: Regex, start = 0, endpos = -1): Option[RegexMatch] =
## Returns Some if there is a match between `start` and `endpos`, otherwise
## it returns None.
proc match*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMatch =
## Returns a `RegexMatch` if there is a match between `start` and `endpos`, otherwise
## it returns nil.
##
## if `endpos == -1`, then `endpos = str.len`
return str.matchImpl(pattern, start, endpos, 0)
@@ -340,9 +339,9 @@ iterator findIter*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMa
flags = pcre.NOTEMPTY_ATSTART or pcre.ANCHORED
let currentMatch = str.matchImpl(pattern, offset, endpos, flags)
previousMatch = currentMatch.get(nil)
previousMatch = currentMatch
if currentMatch.isNone:
if currentMatch == nil:
# either the end of the input or the string
# cannot be split here
offset += 1
@@ -355,16 +354,15 @@ iterator findIter*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMa
# XXX what about invalid unicode?
offset += str.runeLenAt(offset)
else:
let currentMatch = currentMatch.get
offset = currentMatch.matchBounds.b
yield currentMatch
proc find*(str: string, pattern: Regex, start = 0, endpos = -1): Option[RegexMatch] =
proc find*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMatch =
for match in str.findIter(pattern, start, endpos):
return Some(match)
return match
return None[RegexMatch]()
return nil
proc findAll*(str: string, pattern: Regex, start = 0, endpos = -1): seq[RegexMatch] =
accumulateResult(str.findIter(pattern, start, endpos))

View File

@@ -8,29 +8,29 @@ suite "captures":
test "capture bounds are correct":
let ex1 = initRegex("([0-9])")
check("1 23".match(ex1).get.matchBounds == 0 .. 1)
check("1 23".match(ex1).get.captureBounds[0].get == 0 .. 1)
check("1 23".match(ex1, 1).get.matchBounds == 2 .. 3)
check("1 23".match(ex1, 3).get.matchBounds == 3 .. 4)
check("1 23".match(ex1).matchBounds == 0 .. 1)
check("1 23".match(ex1).captureBounds[0].get == 0 .. 1)
check("1 23".match(ex1, 1).matchBounds == 2 .. 3)
check("1 23".match(ex1, 3).matchBounds == 3 .. 4)
let ex2 = initRegex("()()()()()()()()()()([0-9])")
check("824".match(ex2).get.captureBounds[0].get == 0 .. 0)
check("824".match(ex2).get.captureBounds[10].get == 0 .. 1)
check("824".match(ex2).captureBounds[0].get == 0 .. 0)
check("824".match(ex2).captureBounds[10].get == 0 .. 1)
let ex3 = initRegex("([0-9]+)")
check("824".match(ex3).get.captureBounds[0].get == 0 .. 3)
check("824".match(ex3).captureBounds[0].get == 0 .. 3)
test "named captures":
let ex1 = "foobar".match(initRegex("(?<foo>foo)(?<bar>bar)")).get
let ex1 = "foobar".match(initRegex("(?<foo>foo)(?<bar>bar)"))
check(ex1.captures["foo"] == "foo")
check(ex1.captures["bar"] == "bar")
let ex2 = "foo".match(initRegex("(?<foo>foo)(?<bar>bar)?")).get
let ex2 = "foo".match(initRegex("(?<foo>foo)(?<bar>bar)?"))
check(ex2.captures["foo"] == "foo")
check(ex2.captures["bar"] == nil)
test "named capture bounds":
let ex1 = "foo".match(initRegex("(?<foo>foo)(?<bar>bar)?")).get
let ex1 = "foo".match(initRegex("(?<foo>foo)(?<bar>bar)?"))
check(ex1.captureBounds["foo"] == Some(0..3))
check(ex1.captureBounds["bar"] == None[Slice[int]]())
@@ -40,20 +40,20 @@ suite "captures":
check(ex1.captureNameId == {"foo" : 0, "bar" : 1}.toTable())
test "named capture table":
let ex1 = "foo".match(initRegex("(?<foo>foo)(?<bar>bar)?")).get
let ex1 = "foo".match(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())
let ex2 = "foobar".match(initRegex("(?<foo>foo)(?<bar>bar)?")).get
let ex2 = "foobar".match(initRegex("(?<foo>foo)(?<bar>bar)?"))
check(ex2.captures.asTable == {"foo" : "foo", "bar" : "bar"}.toTable())
test "capture sequence":
let ex1 = "foo".match(initRegex("(?<foo>foo)(?<bar>bar)?")).get
let ex1 = "foo".match(initRegex("(?<foo>foo)(?<bar>bar)?"))
check(ex1.captures.asSeq == @["foo", nil])
check(ex1.captureBounds.asSeq == @[Some(0..3), None[Slice[int]]()])
check(ex1.captures.asSeq("") == @["foo", ""])
let ex2 = "foobar".match(initRegex("(?<foo>foo)(?<bar>bar)?")).get
let ex2 = "foobar".match(initRegex("(?<foo>foo)(?<bar>bar)?"))
check(ex2.captures.asSeq == @["foo", "bar"])

View File

@@ -3,7 +3,7 @@ include nre
suite "find":
test "find text":
check("3213a".find(initRegex(r"[a-z]")).get.match == "a")
check("3213a".find(initRegex(r"[a-z]")).match == "a")
check("1 2 3 4 5 6 7 8 ".findAll(initRegex(r" ", "S")).map(
proc (a: RegexMatch): string = a.match
) == @[" ", " ", " ", " ", " ", " ", " ", " "])