From 5713eb64fe53a1ae5c0b64c2a11cd4b6c5d869df Mon Sep 17 00:00:00 2001 From: Daniel Kang Date: Sat, 2 Feb 2019 23:11:02 -0800 Subject: [PATCH] Fix a slicing expression bug: allow low-index == len(a) --- runtime/vm.go | 6 +++--- runtime/vm_string_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/runtime/vm.go b/runtime/vm.go index 34ff212..cc1990b 100644 --- a/runtime/vm.go +++ b/runtime/vm.go @@ -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 { diff --git a/runtime/vm_string_test.go b/runtime/vm_string_test.go index d3e7985..76611ff 100644 --- a/runtime/vm_string_test.go +++ b/runtime/vm_string_test.go @@ -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])