clamp start idx to length for out of bound
This commit is contained in:
@@ -3,6 +3,7 @@ package selector
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -262,33 +263,35 @@ func resolveSliceIndices(slice []int64, length int64) (start int64, end int64) {
|
|||||||
|
|
||||||
start, end = slice[0], slice[1]
|
start, end = slice[0], slice[1]
|
||||||
|
|
||||||
// clamp start index
|
// adjust boundaries
|
||||||
|
switch {
|
||||||
|
case slice[0] == math.MinInt:
|
||||||
|
start = 0
|
||||||
|
case slice[0] < 0:
|
||||||
|
start = length + slice[0]
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case slice[1] == math.MaxInt:
|
||||||
|
end = length
|
||||||
|
case slice[1] < 0:
|
||||||
|
end = length + slice[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// backward iteration is not allowed, shortcut to an empty result
|
||||||
|
if start >= end {
|
||||||
|
start, end = 0, 0
|
||||||
|
}
|
||||||
|
// clamp out of bound
|
||||||
if start < 0 {
|
if start < 0 {
|
||||||
start = length + start
|
start = 0
|
||||||
if start < 0 {
|
|
||||||
start = 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if start > length {
|
if start > length {
|
||||||
start = length
|
start = length
|
||||||
}
|
}
|
||||||
|
|
||||||
// clamp end index
|
|
||||||
if end < 0 {
|
|
||||||
end = length + end
|
|
||||||
if end < 0 {
|
|
||||||
end = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if end > length {
|
if end > length {
|
||||||
end = length
|
end = length
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle backward slicing
|
|
||||||
if start > end {
|
|
||||||
start, end = 0, 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return start, end
|
return start, end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user