Remove initRegex

This commit is contained in:
Flaviu Tamas
2015-01-18 13:04:56 -05:00
parent ca299504d1
commit f141737b9f
6 changed files with 35 additions and 36 deletions

View File

@@ -108,7 +108,7 @@ as a key.
=== Pattern
Represents the pattern that things are matched against, constructed with
`initRegex(string)` or `re(string)`. Examples: `re"foo"`, `re(r"foo # comment",
`re(string, string)`. Examples: `re"foo"`, `re(r"foo # comment",
"x<anycrlf>")`.
`pattern: string` :: the string that was used to create the pattern.

View File

@@ -208,7 +208,7 @@ let Options: Table[string, int] = {
}.toTable
proc tokenizeOptions(opts: string): tuple[flags: int, study: bool] =
result = (0, false)
result = (0, true)
var longOpt: string = nil
for i, c in opts:
@@ -219,17 +219,16 @@ proc tokenizeOptions(opts: string): tuple[flags: int, study: bool] =
if longOpt != nil:
if c == '>':
result.flags = result.flags or Options.fget(longOpt)
if longOpt == "no_study":
result.study = false
else:
result.flags = result.flags or Options.fget(longOpt)
longOpt = nil
else:
longOpt.add(c.toLower)
continue
# }}}
if c == 'S': # handle study
result.study = true
continue
result.flags = result.flags or Options.fget($c)
# }}}
@@ -261,7 +260,7 @@ proc getNameToNumberTable(pattern: Regex): Table[string, int] =
result[name] = num
proc initRegex*(pattern: string, options = "S"): Regex =
proc initRegex(pattern: string, options: string): Regex =
new(result, destroyRegex)
result.pattern = pattern
@@ -286,7 +285,7 @@ proc initRegex*(pattern: string, options = "S"): Regex =
result.captureNameToId = result.getNameToNumberTable()
proc re*(pattern: string, options = "S"): Regex = initRegex(pattern, options)
proc re*(pattern: string, options = ""): Regex = initRegex(pattern, options)
# }}}
proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): RegexMatch =

View File

@@ -3,57 +3,57 @@ include nre
suite "captures":
test "map capture names to numbers":
check(getNameToNumberTable(initRegex("(?<v1>1(?<v2>2(?<v3>3))(?'v4'4))()")) ==
check(getNameToNumberTable(re("(?<v1>1(?<v2>2(?<v3>3))(?'v4'4))()")) ==
{ "v1" : 0, "v2" : 1, "v3" : 2, "v4" : 3 }.toTable())
test "capture bounds are correct":
let ex1 = initRegex("([0-9])")
let ex1 = re("([0-9])")
check("1 23".find(ex1).matchBounds == 0 .. 1)
check("1 23".find(ex1).captureBounds[0].get == 0 .. 1)
check("1 23".find(ex1, 1).matchBounds == 2 .. 3)
check("1 23".find(ex1, 3).matchBounds == 3 .. 4)
let ex2 = initRegex("()()()()()()()()()()([0-9])")
let ex2 = re("()()()()()()()()()()([0-9])")
check("824".find(ex2).captureBounds[0].get == 0 .. 0)
check("824".find(ex2).captureBounds[10].get == 0 .. 1)
let ex3 = initRegex("([0-9]+)")
let ex3 = re("([0-9]+)")
check("824".find(ex3).captureBounds[0].get == 0 .. 3)
test "named captures":
let ex1 = "foobar".find(initRegex("(?<foo>foo)(?<bar>bar)"))
let ex1 = "foobar".find(re("(?<foo>foo)(?<bar>bar)"))
check(ex1.captures["foo"] == "foo")
check(ex1.captures["bar"] == "bar")
let ex2 = "foo".find(initRegex("(?<foo>foo)(?<bar>bar)?"))
let ex2 = "foo".find(re("(?<foo>foo)(?<bar>bar)?"))
check(ex2.captures["foo"] == "foo")
check(ex2.captures["bar"] == nil)
test "named capture bounds":
let ex1 = "foo".find(initRegex("(?<foo>foo)(?<bar>bar)?"))
let ex1 = "foo".find(re("(?<foo>foo)(?<bar>bar)?"))
check(ex1.captureBounds["foo"] == Some(0..3))
check(ex1.captureBounds["bar"] == None[Slice[int]]())
test "capture count":
let ex1 = initRegex("(?<foo>foo)(?<bar>bar)?")
let ex1 = re("(?<foo>foo)(?<bar>bar)?")
check(ex1.captureCount == 2)
check(ex1.captureNameId == {"foo" : 0, "bar" : 1}.toTable())
test "named capture table":
let ex1 = "foo".find(initRegex("(?<foo>foo)(?<bar>bar)?"))
let ex1 = "foo".find(re("(?<foo>foo)(?<bar>bar)?"))
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)?"))
let ex2 = "foobar".find(re("(?<foo>foo)(?<bar>bar)?"))
check(ex2.captures.toTable == {"foo" : "foo", "bar" : "bar"}.toTable())
test "capture sequence":
let ex1 = "foo".find(initRegex("(?<foo>foo)(?<bar>bar)?"))
let ex1 = "foo".find(re("(?<foo>foo)(?<bar>bar)?"))
check(ex1.captures.toSeq == @["foo", nil])
check(ex1.captureBounds.toSeq == @[Some(0..3), None[Slice[int]]()])
check(ex1.captures.toSeq("") == @["foo", ""])
let ex2 = "foobar".find(initRegex("(?<foo>foo)(?<bar>bar)?"))
let ex2 = "foobar".find(re("(?<foo>foo)(?<bar>bar)?"))
check(ex2.captures.toSeq == @["foo", "bar"])

View File

@@ -3,13 +3,13 @@ include nre
suite "find":
test "find text":
check("3213a".find(initRegex(r"[a-z]")).match == "a")
check("3213a".find(re"[a-z]").match == "a")
check("1 2 3 4 5 6 7 8 ".findAll(re" ").map(
proc (a: RegexMatch): string = a.match
) == @[" ", " ", " ", " ", " ", " ", " ", " "])
test "find bounds":
check("1 2 3 4 5 ".findAll(re" ")).map(
check("1 2 3 4 5 ".findAll(re" ").map(
proc (a: RegexMatch): Slice[int] = a.matchBounds
) == @[1..2, 3..4, 5..6, 7..8, 9..10])

View File

@@ -1,24 +1,24 @@
import unittest
include nre
import nre
suite "Test NRE initialization":
test "correct intialization":
check(initRegex("[0-9]+") != nil)
check(initRegex("[0-9]+", "iS") != nil)
check(re("[0-9]+") != nil)
check(re("[0-9]+", "i") != nil)
test "correct options":
expect(SyntaxError): # ValueError would be bad
discard initRegex("[0-9]+",
"89AEfimNsUWXxY<any><anycrlf><cr><crlf><lf><bsr_anycrlf><bsr_unicode><js>")
discard re("[0-9]+",
"89AEfimNsUWXxY<any><anycrlf><cr><crlf><lf><bsr_anycrlf><bsr_unicode><js><no_study>")
test "incorrect options":
expect(KeyError): discard initRegex("[0-9]+", "a")
expect(KeyError): discard initRegex("[0-9]+", "<does_not_exist>")
expect(KeyError): discard re("[0-9]+", "a")
expect(KeyError): discard re("[0-9]+", "<does_not_exist>")
test "invalid regex":
expect(SyntaxError): discard initRegex("[0-9")
expect(SyntaxError): discard re("[0-9")
try:
discard initRegex("[0-9")
discard re("[0-9")
except SyntaxError:
let ex = SyntaxError(getCurrentException())
check(ex.pos == 4)

View File

@@ -3,11 +3,11 @@ include nre
suite "string splitting":
test "splitting strings":
check("12345".split(initRegex("")) == @["1", "2", "3", "4", "5"])
check("12345".split(re("")) == @["1", "2", "3", "4", "5"])
check("1 2 3 4 5 6 ".split(re" ") == @["1", "2", "3", "4", "5", "6", ""])
check("1 2 ".split(initRegex(" ")) == @["1", "", "2", "", ""])
check("1 2".split(initRegex(" ")) == @["1", "2"])
check("foo".split(initRegex("foo")) == @["", ""])
check("1 2 ".split(re(" ")) == @["1", "", "2", "", ""])
check("1 2".split(re(" ")) == @["1", "2"])
check("foo".split(re("foo")) == @["", ""])
test "captured patterns":
check("12".split(re"(\d)") == @["", "1", "", "2", ""])