summary refs log tree commit diff stats
path: root/lib/impure/re.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-10-14 11:19:45 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-10-14 11:19:51 +0200
commit8955470644972e2d1f2b029650c04505ebacca23 (patch)
tree2fecb489cdcca50633749390a0b44410aaa26702 /lib/impure/re.nim
parentb62328cb7676c6f997a92a3b7245daf34c379fb9 (diff)
downloadNim-8955470644972e2d1f2b029650c04505ebacca23.tar.gz
fixes #9306 properly, fixes #9306
Diffstat (limited to 'lib/impure/re.nim')
-rw-r--r--lib/impure/re.nim14
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/impure/re.nim b/lib/impure/re.nim
index fe2ab426d..b142f58cd 100644
--- a/lib/impure/re.nim
+++ b/lib/impure/re.nim
@@ -364,7 +364,8 @@ iterator findAll*(buf: cstring, pattern: Regex, start = 0, bufSize: int): string
 proc findAll*(s: string, pattern: Regex, start = 0): seq[string] {.inline.} =
   ## returns all matching `substrings` of ``s`` that match ``pattern``.
   ## If it does not match, @[] is returned.
-  accumulateResult(findAll(s, pattern, start))
+  result = @[]
+  for x in findAll(s, pattern, start): result.add x
 
 when not defined(nimhygiene):
   {.pragma: inject.}
@@ -433,6 +434,7 @@ proc replace*(s: string, sub: Regex, by = ""): string =
     if match.first < 0: break
     add(result, substr(s, prev, match.first-1))
     add(result, by)
+    if match.last + 1 == prev: break
     prev = match.last + 1
   add(result, substr(s, prev))
 
@@ -458,6 +460,7 @@ proc replacef*(s: string, sub: Regex, by: string): string =
     if match.first < 0: break
     add(result, substr(s, prev, match.first-1))
     addf(result, by, caps)
+    if match.last + 1 == prev: break
     prev = match.last + 1
   add(result, substr(s, prev))
 
@@ -543,7 +546,8 @@ proc split*(s: string, sep: Regex, maxsplit = -1): seq[string] {.inline.} =
   ## Splits the string ``s`` into a seq of substrings.
   ##
   ## The portion matched by ``sep`` is not returned.
-  accumulateResult(split(s, sep))
+  result = @[]
+  for x in split(s, sep): result.add x
 
 proc escapeRe*(s: string): string =
   ## escapes ``s`` so that it is matched verbatim when used as a regular
@@ -674,3 +678,9 @@ when isMainModule:
       accum.add($x)
     doAssert(accum == @["a","b","c"])
 
+  block:
+    # bug #9306
+    doAssert replace("bar", re"^", "foo") == "foobar"
+    doAssert replace("foo", re"", "-") == "-foo"
+    doAssert replace("foo", re"$", "bar") == "foobar"
+