diff --git a/ext/strings.go b/ext/strings.go index 88b4d7f0..de65421f 100644 --- a/ext/strings.go +++ b/ext/strings.go @@ -607,6 +607,10 @@ func lastIndexOf(str, substr string) (int64, error) { if substr == "" { return int64(len(runes)), nil } + + if len(str) < len(substr) { + return -1, nil + } return lastIndexOfOffset(str, substr, int64(len(runes)-1)) } diff --git a/ext/strings_test.go b/ext/strings_test.go index 3a8adeb0..17be558f 100644 --- a/ext/strings_test.go +++ b/ext/strings_test.go @@ -53,6 +53,7 @@ var stringTests = []struct { {expr: `'hello wello'.indexOf('ello', 6) == 7`}, {expr: `'hello wello'.indexOf('elbo room!!') == -1`}, {expr: `'hello wello'.indexOf('elbo room!!!') == -1`}, + {expr: `''.lastIndexOf('@@') == -1`}, {expr: `'tacocat'.lastIndexOf('') == 7`}, {expr: `'tacocat'.lastIndexOf('at') == 5`}, {expr: `'tacocat'.lastIndexOf('none') == -1`},