summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFlaviu Tamas <tamasflaviu@gmail.com>2015-01-18 13:04:56 -0500
committerFlaviu Tamas <tamasflaviu@gmail.com>2015-01-18 13:04:56 -0500
commitf141737b9f677fadfd24a7e84bed6d3f9102c780 (patch)
tree72da159f7cde45fc8df08b1b8fad6ac26769e125
parentca299504d1dc3a23968c4ddf33fd64aa3f1a9422 (diff)
downloadNim-f141737b9f677fadfd24a7e84bed6d3f9102c780.tar.gz
Remove initRegex
-rw-r--r--README.asciidoc2
-rw-r--r--src/nre.nim15
-rw-r--r--test/captures.nim24
-rw-r--r--test/find.nim4
-rw-r--r--test/init.nim18
-rw-r--r--test/split.nim8
6 files changed, 35 insertions, 36 deletions
diff --git a/README.asciidoc b/README.asciidoc
index 54afe8dcf..38adab639 100644
--- a/README.asciidoc
+++ b/README.asciidoc
@@ -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.
diff --git a/src/nre.nim b/src/nre.nim
index 6cc9bfddf..539a91005 100644
--- a/src/nre.nim
+++ b/src/nre.nim
@@ -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 =
diff --git a/test/captures.nim b/test/captures.nim
index bccbc0993..31bf1c6a2 100644
--- a/test/captures.nim
+++ b/test/captures.nim
@@ -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"])
 
diff --git a/test/find.nim b/test/find.nim
index 06f4d22cc..ddbfcdfdb 100644
--- a/test/find.nim
+++ b/test/find.nim
@@ -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])
 
diff --git a/test/init.nim b/test/init.nim
index 9bf7c3c42..105e6a19e 100644
--- a/test/init.nim
+++ b/test/init.nim
@@ -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)
diff --git a/test/split.nim b/test/split.nim
index 2dbb835d2..184b5f9e2 100644
--- a/test/split.nim
+++ b/test/split.nim
@@ -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", ""])