summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFlaviu Tamas <tamasflaviu@gmail.com>2015-01-16 19:11:28 -0500
committerFlaviu Tamas <tamasflaviu@gmail.com>2015-01-16 19:11:28 -0500
commit1a5401ebcffdfa2e90888c1ce4e9126d993c67e8 (patch)
tree609c549632de3244488212356dcd89b035d64069
parentca3e72ee27be61ea5d89357867906da9cd0b2f05 (diff)
downloadNim-1a5401ebcffdfa2e90888c1ce4e9126d993c67e8.tar.gz
s/match/find/g
The implementation needs to match the API after all ;)
-rw-r--r--src/nre.nim15
-rw-r--r--test/captures.nim28
2 files changed, 20 insertions, 23 deletions
diff --git a/src/nre.nim b/src/nre.nim
index 3c1645456..eb0210084 100644
--- a/src/nre.nim
+++ b/src/nre.nim
@@ -317,11 +317,7 @@ proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): Reg
     raise newException(AssertionError, "Internal error: errno " & $execRet)
 
 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)
+  return str.matchImpl(pattern, start, endpos, pcre.ANCHORED)
 
 iterator findIter*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMatch =
   # see pcredemo for explaination
@@ -364,10 +360,11 @@ iterator findIter*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMa
       yield currentMatch
 
 proc find*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMatch =
-  for match in str.findIter(pattern, start, endpos):
-    return match
-
-  return nil
+  ## 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)
 
 proc findAll*(str: string, pattern: Regex, start = 0, endpos = -1): seq[RegexMatch] =
   accumulateResult(str.findIter(pattern, start, endpos))
diff --git a/test/captures.nim b/test/captures.nim
index 9e7698f6b..15bdca7d9 100644
--- a/test/captures.nim
+++ b/test/captures.nim
@@ -8,29 +8,29 @@ suite "captures":
 
   test "capture bounds are correct":
     let ex1 = initRegex("([0-9])")
-    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)
+    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])")
-    check("824".match(ex2).captureBounds[0].get == 0 .. 0)
-    check("824".match(ex2).captureBounds[10].get == 0 .. 1)
+    check("824".find(ex2).captureBounds[0].get == 0 .. 0)
+    check("824".find(ex2).captureBounds[10].get == 0 .. 1)
 
     let ex3 = initRegex("([0-9]+)")
-    check("824".match(ex3).captureBounds[0].get == 0 .. 3)
+    check("824".find(ex3).captureBounds[0].get == 0 .. 3)
 
   test "named captures":
-    let ex1 = "foobar".match(initRegex("(?<foo>foo)(?<bar>bar)"))
+    let ex1 = "foobar".find(initRegex("(?<foo>foo)(?<bar>bar)"))
     check(ex1.captures["foo"] == "foo")
     check(ex1.captures["bar"] == "bar")
 
-    let ex2 = "foo".match(initRegex("(?<foo>foo)(?<bar>bar)?"))
+    let ex2 = "foo".find(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)?"))
+    let ex1 = "foo".find(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)?"))
+    let ex1 = "foo".find(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)?"))
+    let ex2 = "foobar".find(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)?"))
+    let ex1 = "foo".find(initRegex("(?<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".match(initRegex("(?<foo>foo)(?<bar>bar)?"))
+    let ex2 = "foobar".find(initRegex("(?<foo>foo)(?<bar>bar)?"))
     check(ex2.captures.toSeq == @["foo", "bar"])