summary refs log tree commit diff stats
path: root/lib/impure
diff options
context:
space:
mode:
authorFlaviu Tamas <tamasflaviu@gmail.com>2014-10-26 09:31:21 -0400
committerFlaviu Tamas <tamasflaviu@gmail.com>2014-10-29 17:54:43 -0400
commit218cb7587a351c9a29a7de5961f168a5cdbb5a70 (patch)
tree24cf05e50d80cf6358d4fcb261d1747340b52571 /lib/impure
parentbc3464ede77bba781928b7b0f425373a520077a4 (diff)
downloadNim-218cb7587a351c9a29a7de5961f168a5cdbb5a70.tar.gz
re module returns nil on failed captures
- tests included
- news.txt updated
Diffstat (limited to 'lib/impure')
-rw-r--r--lib/impure/re.nim16
1 files changed, 13 insertions, 3 deletions
diff --git a/lib/impure/re.nim b/lib/impure/re.nim
index fc9ed3ce0..6e3a69c62 100644
--- a/lib/impure/re.nim
+++ b/lib/impure/re.nim
@@ -92,7 +92,7 @@ proc matchOrFind(s: string, pattern: Regex, matches: var openArray[string],
     var a = rawMatches[i * 2]
     var b = rawMatches[i * 2 + 1]
     if a >= 0'i32: matches[i-1] = substr(s, int(a), int(b)-1)
-    else: matches[i-1] = ""
+    else: matches[i-1] = nil
   return rawMatches[1] - rawMatches[0]
   
 proc findBounds*(s: string, pattern: Regex, matches: var openArray[string],
@@ -110,7 +110,7 @@ proc findBounds*(s: string, pattern: Regex, matches: var openArray[string],
     var a = rawMatches[i * 2]
     var b = rawMatches[i * 2 + 1]
     if a >= 0'i32: matches[i-1] = substr(s, int(a), int(b)-1)
-    else: matches[i-1] = ""
+    else: matches[i-1] = nil
   return (rawMatches[0].int, rawMatches[1].int - 1)
   
 proc findBounds*(s: string, pattern: Regex, 
@@ -190,7 +190,7 @@ proc find*(s: string, pattern: Regex, matches: var openArray[string],
     var a = rawMatches[i * 2]
     var b = rawMatches[i * 2 + 1]
     if a >= 0'i32: matches[i-1] = substr(s, int(a), int(b)-1)
-    else: matches[i-1] = ""
+    else: matches[i-1] = nil
   return rawMatches[0]
 
 proc find*(s: string, pattern: Regex, start = 0): int =
@@ -310,6 +310,8 @@ proc replacef*(s: string, sub: Regex, by: string): string =
   while true:
     var match = findBounds(s, sub, caps, prev)
     if match.first < 0: break
+    assert result != nil
+    assert s != nil
     add(result, substr(s, prev, match.first-1))
     addf(result, by, caps)
     prev = match.last + 1
@@ -450,6 +452,14 @@ when isMainModule:
     assert matches[1] == "abc"
   else:
     assert false
+  
+  if "abc" =~ re"(cba)?.*":
+    assert matches[0] == nil
+  else: assert false
+
+  if "abc" =~ re"().*":
+    assert matches[0] == ""
+  else: assert false
     
   assert "var1=key; var2=key2".endsWith(re"\w+=\w+")
   assert("var1=key; var2=key2".replacef(re"(\w+)=(\w+)", "$1<-$2$2") ==