fix #9437(fix re.replace wrong behaviour) (#17546)
* fix nim js cmp fails at CT * fix
This commit is contained in:
parent
f65f760dee
commit
6b97889f44
2 changed files with 24 additions and 5 deletions
|
|
@ -199,6 +199,18 @@ proc findBounds*(s: string, pattern: Regex,
|
||||||
## `(-1,0)` is returned.
|
## `(-1,0)` is returned.
|
||||||
result = findBounds(cstring(s), pattern, matches, start, s.len)
|
result = findBounds(cstring(s), pattern, matches, start, s.len)
|
||||||
|
|
||||||
|
proc findBoundsImpl(buf: cstring, pattern: Regex,
|
||||||
|
start = 0, bufSize = 0, flags = 0): tuple[first, last: int] =
|
||||||
|
var rtarray = initRtArray[cint](3)
|
||||||
|
let rawMatches = rtarray.getRawData
|
||||||
|
let res = pcre.exec(pattern.h, pattern.e, buf, bufSize.cint, start.cint, flags.int32,
|
||||||
|
cast[ptr cint](rawMatches), 3)
|
||||||
|
|
||||||
|
if res < 0'i32:
|
||||||
|
result = (-1, 0)
|
||||||
|
else:
|
||||||
|
result = (int(rawMatches[0]), int(rawMatches[1]-1))
|
||||||
|
|
||||||
proc findBounds*(buf: cstring, pattern: Regex,
|
proc findBounds*(buf: cstring, pattern: Regex,
|
||||||
start = 0, bufSize: int): tuple[first, last: int] =
|
start = 0, bufSize: int): tuple[first, last: int] =
|
||||||
## returns the `first` and `last` position of `pattern` in `buf`,
|
## returns the `first` and `last` position of `pattern` in `buf`,
|
||||||
|
|
@ -433,12 +445,16 @@ proc replace*(s: string, sub: Regex, by = ""): string =
|
||||||
doAssert "var1=key; var2=key2".replace(re"(\w+)=(\w+)", "?") == "?; ?"
|
doAssert "var1=key; var2=key2".replace(re"(\w+)=(\w+)", "?") == "?; ?"
|
||||||
result = ""
|
result = ""
|
||||||
var prev = 0
|
var prev = 0
|
||||||
|
var flags = int32(0)
|
||||||
while prev < s.len:
|
while prev < s.len:
|
||||||
var match = findBounds(s, sub, prev)
|
var match = findBoundsImpl(s.cstring, sub, prev, s.len, flags)
|
||||||
|
flags = 0
|
||||||
if match.first < 0: break
|
if match.first < 0: break
|
||||||
add(result, substr(s, prev, match.first-1))
|
add(result, substr(s, prev, match.first-1))
|
||||||
add(result, by)
|
add(result, by)
|
||||||
if match.last + 1 == prev: break
|
if match.first > match.last:
|
||||||
|
# 0-len match
|
||||||
|
flags = pcre.NOTEMPTY_ATSTART
|
||||||
prev = match.last + 1
|
prev = match.last + 1
|
||||||
add(result, substr(s, prev))
|
add(result, substr(s, prev))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -99,10 +99,13 @@ proc testAll() =
|
||||||
accum.add($x)
|
accum.add($x)
|
||||||
doAssert(accum == @["a","b","c"])
|
doAssert(accum == @["a","b","c"])
|
||||||
|
|
||||||
block:
|
block: # bug #9306
|
||||||
# bug #9306
|
|
||||||
doAssert replace("bar", re"^", "foo") == "foobar"
|
doAssert replace("bar", re"^", "foo") == "foobar"
|
||||||
doAssert replace("foo", re"", "-") == "-foo"
|
|
||||||
doAssert replace("foo", re"$", "bar") == "foobar"
|
doAssert replace("foo", re"$", "bar") == "foobar"
|
||||||
|
|
||||||
|
|
||||||
|
block: # bug #9437
|
||||||
|
doAssert replace("foo", re"", "-") == "-f-o-o-"
|
||||||
|
doAssert replace("ooo", re"o", "-") == "---"
|
||||||
|
|
||||||
testAll()
|
testAll()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue