Skip to content

Commit 845f2a8

Browse files
authored
Fix lastIndexOf behavior against an empty string in strings extension (#1173)
* Fix lastIndexOf behavior against an empty string in strings extension
1 parent ad76382 commit 845f2a8

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

ext/strings.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ func lastIndexOf(str, substr string) (int64, error) {
607607
if substr == "" {
608608
return int64(len(runes)), nil
609609
}
610+
611+
if len(str) < len(substr) {
612+
return -1, nil
613+
}
610614
return lastIndexOfOffset(str, substr, int64(len(runes)-1))
611615
}
612616

ext/strings_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var stringTests = []struct {
5353
{expr: `'hello wello'.indexOf('ello', 6) == 7`},
5454
{expr: `'hello wello'.indexOf('elbo room!!') == -1`},
5555
{expr: `'hello wello'.indexOf('elbo room!!!') == -1`},
56+
{expr: `''.lastIndexOf('@@') == -1`},
5657
{expr: `'tacocat'.lastIndexOf('') == 7`},
5758
{expr: `'tacocat'.lastIndexOf('at') == 5`},
5859
{expr: `'tacocat'.lastIndexOf('none') == -1`},

0 commit comments

Comments
 (0)