Fix a slicing expression bug: allow low-index == len(a)
This commit is contained in:
parent
eda0495f30
commit
5713eb64fe
2 changed files with 4 additions and 4 deletions
|
@ -644,7 +644,7 @@ func (v *VM) Run() error {
|
||||||
case *objects.Array:
|
case *objects.Array:
|
||||||
numElements := int64(len(left.Value))
|
numElements := int64(len(left.Value))
|
||||||
|
|
||||||
if lowIdx < 0 || lowIdx >= numElements {
|
if lowIdx < 0 || lowIdx > numElements {
|
||||||
return fmt.Errorf("index out of bounds: %d", lowIdx)
|
return fmt.Errorf("index out of bounds: %d", lowIdx)
|
||||||
}
|
}
|
||||||
if highIdx < 0 {
|
if highIdx < 0 {
|
||||||
|
@ -669,7 +669,7 @@ func (v *VM) Run() error {
|
||||||
case *objects.ImmutableArray:
|
case *objects.ImmutableArray:
|
||||||
numElements := int64(len(left.Value))
|
numElements := int64(len(left.Value))
|
||||||
|
|
||||||
if lowIdx < 0 || lowIdx >= numElements {
|
if lowIdx < 0 || lowIdx > numElements {
|
||||||
return fmt.Errorf("index out of bounds: %d", lowIdx)
|
return fmt.Errorf("index out of bounds: %d", lowIdx)
|
||||||
}
|
}
|
||||||
if highIdx < 0 {
|
if highIdx < 0 {
|
||||||
|
@ -694,7 +694,7 @@ func (v *VM) Run() error {
|
||||||
case *objects.String:
|
case *objects.String:
|
||||||
numElements := int64(len(left.Value))
|
numElements := int64(len(left.Value))
|
||||||
|
|
||||||
if lowIdx < 0 || lowIdx >= numElements {
|
if lowIdx < 0 || lowIdx > numElements {
|
||||||
return fmt.Errorf("index out of bounds: %d", lowIdx)
|
return fmt.Errorf("index out of bounds: %d", lowIdx)
|
||||||
}
|
}
|
||||||
if highIdx < 0 {
|
if highIdx < 0 {
|
||||||
|
|
|
@ -28,7 +28,7 @@ func TestString(t *testing.T) {
|
||||||
expectError(t, fmt.Sprintf("%s[%d]", strStr, strLen))
|
expectError(t, fmt.Sprintf("%s[%d]", strStr, strLen))
|
||||||
|
|
||||||
// slice operator
|
// slice operator
|
||||||
for low := 0; low < strLen; low++ {
|
for low := 0; low <= strLen; low++ {
|
||||||
for high := low; high <= strLen; high++ {
|
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[%d:%d]", strStr, low, high), str[low:high])
|
||||||
expect(t, fmt.Sprintf("out = %s[0 + %d : 0 + %d]", strStr, low, high), str[low:high])
|
expect(t, fmt.Sprintf("out = %s[0 + %d : 0 + %d]", strStr, low, high), str[low:high])
|
||||||
|
|
Loading…
Reference in a new issue