Correctly handle [..=-1] list indexing

Fixes #1518
This commit is contained in:
Kurtis Rader 2022-05-01 18:43:44 -07:00 committed by Qi Xiao
parent a57275564b
commit 7bae8f2df7
2 changed files with 7 additions and 1 deletions

View File

@ -129,7 +129,11 @@ func parseIndexString(s string, n int) (slice bool, i int, j int, err error) {
}
if sep == "..=" {
// TODO: Handle j == MaxInt-1
j++
if j == -1 { // subtle corner case that is same as no high value
j = n
} else {
j++
}
}
}
// Two numbers

View File

@ -34,6 +34,7 @@ func TestIndex(t *testing.T) {
Args("abc", "1..=").Rets("bc", nil),
Args("abc", "..=1").Rets("ab", nil),
Args("abc", "..=").Rets("abc", nil),
Args("abc", "..=-1").Rets("abc", nil),
// String slices not at rune boundary.
Args("你好", "2..").Rets(Any, errIndexNotAtRuneBoundary),
Args("你好", "..2").Rets(Any, errIndexNotAtRuneBoundary),
@ -92,6 +93,7 @@ func TestIndex(t *testing.T) {
Args(li4, "..=-2").Rets(eq(MakeList("foo", "bar", "lorem")), nil),
Args(li4, "3..=").Rets(eq(MakeList("ipsum")), nil),
Args(li4, "..=").Rets(eq(li4), nil),
Args(li4, "..=-1").Rets(eq(li4), nil),
// Slice index out of range.
Args(li4, "-5..1").Rets(nil, errs.OutOfRange{