Fix a slicing expression bug: allow low-index == len(a)

This commit is contained in:
Daniel Kang 2019-02-02 23:11:02 -08:00
parent eda0495f30
commit 5713eb64fe
2 changed files with 4 additions and 4 deletions

View file

@ -644,7 +644,7 @@ func (v *VM) Run() error {
case *objects.Array:
numElements := int64(len(left.Value))
if lowIdx < 0 || lowIdx >= numElements {
if lowIdx < 0 || lowIdx > numElements {
return fmt.Errorf("index out of bounds: %d", lowIdx)
}
if highIdx < 0 {
@ -669,7 +669,7 @@ func (v *VM) Run() error {
case *objects.ImmutableArray:
numElements := int64(len(left.Value))
if lowIdx < 0 || lowIdx >= numElements {
if lowIdx < 0 || lowIdx > numElements {
return fmt.Errorf("index out of bounds: %d", lowIdx)
}
if highIdx < 0 {
@ -694,7 +694,7 @@ func (v *VM) Run() error {
case *objects.String:
numElements := int64(len(left.Value))
if lowIdx < 0 || lowIdx >= numElements {
if lowIdx < 0 || lowIdx > numElements {
return fmt.Errorf("index out of bounds: %d", lowIdx)
}
if highIdx < 0 {

View file

@ -28,7 +28,7 @@ func TestString(t *testing.T) {
expectError(t, fmt.Sprintf("%s[%d]", strStr, strLen))
// slice operator
for low := 0; low < strLen; low++ {
for low := 0; low <= strLen; low++ {
for high := low; high <= strLen; high++ {
expect(t, fmt.Sprintf("out = %s[%d:%d]", strStr, low, high), str[low:high])
expect(t, fmt.Sprintf("out = %s[0 + %d : 0 + %d]", strStr, low, high), str[low:high])