Skip to content

Commit 43677ba

Browse files
committed
Advance past zero-width replace_all matches
1 parent 79a7446 commit 43677ba

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

vibes/builtins.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,18 +516,18 @@ func regexReplaceAllWithLimit(re *regexp.Regexp, text string, replacement string
516516
}
517517
lastAppended = loc[1]
518518

519-
if loc[1] > searchStart {
519+
if loc[1] > loc[0] {
520520
searchStart = loc[1]
521521
continue
522522
}
523-
if searchStart >= len(text) {
523+
if loc[1] >= len(text) {
524524
break
525525
}
526-
_, size := utf8.DecodeRuneInString(text[searchStart:])
526+
_, size := utf8.DecodeRuneInString(text[loc[1]:])
527527
if size == 0 {
528528
size = 1
529529
}
530-
searchStart += size
530+
searchStart = loc[1] + size
531531
}
532532

533533
tailLen := len(text) - lastAppended

vibes/runtime_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,7 @@ func TestRegexBuiltins(t *testing.T) {
19551955
replace_one: Regex.replace("ID-12 ID-34", "ID-[0-9]+", "X"),
19561956
replace_all: Regex.replace_all("ID-12 ID-34", "ID-[0-9]+", "X"),
19571957
replace_all_anchor: Regex.replace_all("abc", "^", "X"),
1958+
replace_all_boundary: Regex.replace_all("ab", "\\b", "X"),
19581959
replace_capture: Regex.replace("ID-12 ID-34", "ID-([0-9]+)", "X-$1"),
19591960
replace_boundary: Regex.replace("ab", "\\Bb", "X")
19601961
}
@@ -1988,6 +1989,9 @@ func TestRegexBuiltins(t *testing.T) {
19881989
if !out["replace_all_anchor"].Equal(NewString("Xabc")) {
19891990
t.Fatalf("replace_all_anchor mismatch: %v", out["replace_all_anchor"])
19901991
}
1992+
if !out["replace_all_boundary"].Equal(NewString("XabX")) {
1993+
t.Fatalf("replace_all_boundary mismatch: %v", out["replace_all_boundary"])
1994+
}
19911995
if !out["replace_capture"].Equal(NewString("X-12 ID-34")) {
19921996
t.Fatalf("replace_capture mismatch: %v", out["replace_capture"])
19931997
}

0 commit comments

Comments
 (0)