fixes #9557
This commit is contained in:
parent
cf3d206b9e
commit
e653121633
1 changed files with 34 additions and 32 deletions
|
|
@ -1525,6 +1525,8 @@ proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} =
|
||||||
## backwards to 0.
|
## backwards to 0.
|
||||||
##
|
##
|
||||||
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
|
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
|
||||||
|
if sub.len == 0:
|
||||||
|
return -1
|
||||||
let realStart = if start == -1: s.len else: start
|
let realStart = if start == -1: s.len else: start
|
||||||
for i in countdown(realStart-sub.len, 0):
|
for i in countdown(realStart-sub.len, 0):
|
||||||
for j in 0..sub.len-1:
|
for j in 0..sub.len-1:
|
||||||
|
|
@ -1632,11 +1634,7 @@ proc replace*(s, sub: string, by = ""): string {.noSideEffect,
|
||||||
result = ""
|
result = ""
|
||||||
let subLen = sub.len
|
let subLen = sub.len
|
||||||
if subLen == 0:
|
if subLen == 0:
|
||||||
for c in s:
|
result = s
|
||||||
add result, by
|
|
||||||
add result, c
|
|
||||||
add result, by
|
|
||||||
return
|
|
||||||
elif subLen == 1:
|
elif subLen == 1:
|
||||||
# when the pattern is a single char, we use a faster
|
# when the pattern is a single char, we use a faster
|
||||||
# char-based search that doesn't need a skip table:
|
# char-based search that doesn't need a skip table:
|
||||||
|
|
@ -1691,7 +1689,8 @@ proc replaceWord*(s, sub: string, by = ""): string {.noSideEffect,
|
||||||
initSkipTable(a, sub)
|
initSkipTable(a, sub)
|
||||||
var i = 0
|
var i = 0
|
||||||
let last = s.high
|
let last = s.high
|
||||||
let sublen = max(sub.len, 1)
|
let sublen = sub.len
|
||||||
|
if sublen > 0:
|
||||||
while true:
|
while true:
|
||||||
var j = find(a, s, sub, i, last)
|
var j = find(a, s, sub, i, last)
|
||||||
if j < 0: break
|
if j < 0: break
|
||||||
|
|
@ -1722,15 +1721,18 @@ proc multiReplace*(s: string, replacements: varargs[(string, string)]): string {
|
||||||
result = newStringOfCap(s.len)
|
result = newStringOfCap(s.len)
|
||||||
var i = 0
|
var i = 0
|
||||||
var fastChk: set[char] = {}
|
var fastChk: set[char] = {}
|
||||||
for tup in replacements: fastChk.incl(tup[0][0]) # Include first character of all replacements
|
for sub, by in replacements.items:
|
||||||
|
if sub.len > 0:
|
||||||
|
# Include first character of all replacements
|
||||||
|
fastChk.incl sub[0]
|
||||||
while i < s.len:
|
while i < s.len:
|
||||||
block sIteration:
|
block sIteration:
|
||||||
# Assume most chars in s are not candidates for any replacement operation
|
# Assume most chars in s are not candidates for any replacement operation
|
||||||
if s[i] in fastChk:
|
if s[i] in fastChk:
|
||||||
for tup in replacements:
|
for sub, by in replacements.items:
|
||||||
if s.continuesWith(tup[0], i):
|
if sub.len > 0 and s.continuesWith(sub[0], i):
|
||||||
add result, tup[1]
|
add result, by
|
||||||
inc(i, tup[0].len)
|
inc(i, sub.len)
|
||||||
break sIteration
|
break sIteration
|
||||||
# No matching replacement found
|
# No matching replacement found
|
||||||
# copy current character from s
|
# copy current character from s
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue