-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
12 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Araq
Author
Member
|
||
doAssert replace("foo", re"$", "bar") == "foobar" | ||
|
This second test is not correct. The replacement should match with "-f-o-o-". Looks like it's treating "" re the same as "^"?