2018-03-02 07:15:51 +08:00
|
|
|
package vals
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-04-12 01:43:52 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/errs"
|
2018-03-02 07:15:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-07-05 10:00:29 +08:00
|
|
|
errIndexMustBeInteger = errors.New("index must be integer")
|
2018-03-02 07:15:51 +08:00
|
|
|
)
|
|
|
|
|
2022-03-20 23:50:25 +08:00
|
|
|
func indexList(l List, rawIndex any) (any, error) {
|
2018-03-02 07:15:51 +08:00
|
|
|
index, err := ConvertListIndex(rawIndex, l.Len())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if index.Slice {
|
|
|
|
return l.SubVector(index.Lower, index.Upper), nil
|
|
|
|
}
|
|
|
|
// Bounds are already checked.
|
|
|
|
value, _ := l.Index(index.Lower)
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListIndex represents a (converted) list index.
|
|
|
|
type ListIndex struct {
|
|
|
|
Slice bool
|
|
|
|
Lower int
|
|
|
|
Upper int
|
|
|
|
}
|
|
|
|
|
2020-04-12 01:43:52 +08:00
|
|
|
func adjustAndCheckIndex(i, n int, includeN bool) (int, error) {
|
|
|
|
if i < 0 {
|
|
|
|
if i < -n {
|
|
|
|
return 0, negIndexOutOfRange(strconv.Itoa(i), n)
|
|
|
|
}
|
|
|
|
return i + n, nil
|
|
|
|
}
|
|
|
|
if includeN {
|
|
|
|
if i > n {
|
|
|
|
return 0, posIndexOutOfRange(strconv.Itoa(i), n+1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if i >= n {
|
|
|
|
return 0, posIndexOutOfRange(strconv.Itoa(i), n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2018-03-02 07:15:51 +08:00
|
|
|
// ConvertListIndex parses a list index, check whether it is valid, and returns
|
|
|
|
// the converted structure.
|
2022-03-20 23:50:25 +08:00
|
|
|
func ConvertListIndex(rawIndex any, n int) (*ListIndex, error) {
|
2019-04-28 04:00:25 +08:00
|
|
|
switch rawIndex := rawIndex.(type) {
|
2021-04-04 20:37:38 +08:00
|
|
|
case int:
|
|
|
|
index, err := adjustAndCheckIndex(rawIndex, n, false)
|
2020-04-12 01:43:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-01-03 07:03:26 +08:00
|
|
|
}
|
2019-04-28 04:00:25 +08:00
|
|
|
return &ListIndex{false, index, 0}, nil
|
|
|
|
case string:
|
2020-08-16 05:52:50 +08:00
|
|
|
slice, i, j, err := parseIndexString(rawIndex, n)
|
2019-04-28 04:00:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-12 01:43:52 +08:00
|
|
|
if !slice {
|
|
|
|
i, err = adjustAndCheckIndex(i, n, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-28 04:00:25 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-12 01:43:52 +08:00
|
|
|
i, err = adjustAndCheckIndex(i, n, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
j0 := j
|
|
|
|
j, err = adjustAndCheckIndex(j, n, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if j < i {
|
|
|
|
if j0 < 0 {
|
|
|
|
return nil, errs.OutOfRange{
|
2021-06-16 01:48:24 +08:00
|
|
|
What: "negative slice upper index",
|
2020-08-19 04:23:26 +08:00
|
|
|
ValidLow: strconv.Itoa(i - n), ValidHigh: "-1",
|
|
|
|
Actual: strconv.Itoa(j0)}
|
2020-04-12 01:43:52 +08:00
|
|
|
}
|
2020-08-01 12:12:42 +08:00
|
|
|
return nil, errs.OutOfRange{
|
2021-06-16 01:48:24 +08:00
|
|
|
What: "slice upper index",
|
2020-08-19 04:23:26 +08:00
|
|
|
ValidLow: strconv.Itoa(i), ValidHigh: strconv.Itoa(n),
|
|
|
|
Actual: strconv.Itoa(j0)}
|
2019-04-28 04:00:25 +08:00
|
|
|
}
|
2018-03-02 07:15:51 +08:00
|
|
|
}
|
2019-04-28 04:00:25 +08:00
|
|
|
return &ListIndex{slice, i, j}, nil
|
|
|
|
default:
|
|
|
|
return nil, errIndexMustBeInteger
|
2018-03-02 07:15:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 05:52:50 +08:00
|
|
|
// Index = Number |
|
2022-08-08 05:30:11 +08:00
|
|
|
//
|
2023-02-27 04:49:31 +08:00
|
|
|
// Number ( '..' | '..=' ) Number
|
2020-08-16 05:52:50 +08:00
|
|
|
func parseIndexString(s string, n int) (slice bool, i int, j int, err error) {
|
|
|
|
low, sep, high := splitIndexString(s)
|
|
|
|
if sep == "" {
|
2018-03-02 07:15:51 +08:00
|
|
|
// A single number
|
2020-04-12 01:43:52 +08:00
|
|
|
i, err := atoi(s, n)
|
2018-03-02 07:15:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, 0, 0, err
|
|
|
|
}
|
|
|
|
return false, i, 0, nil
|
|
|
|
}
|
2020-08-16 05:52:50 +08:00
|
|
|
if low == "" {
|
2018-03-02 07:15:51 +08:00
|
|
|
i = 0
|
|
|
|
} else {
|
2021-06-16 01:48:24 +08:00
|
|
|
i, err = atoi(low, n+1)
|
2018-03-02 07:15:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, 0, 0, err
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 05:52:50 +08:00
|
|
|
if high == "" {
|
2018-03-02 07:15:51 +08:00
|
|
|
j = n
|
|
|
|
} else {
|
2021-06-16 01:48:24 +08:00
|
|
|
j, err = atoi(high, n+1)
|
2018-03-02 07:15:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, 0, 0, err
|
|
|
|
}
|
2020-08-16 05:52:50 +08:00
|
|
|
if sep == "..=" {
|
2021-06-16 01:48:24 +08:00
|
|
|
// TODO: Handle j == MaxInt-1
|
2022-05-02 09:43:44 +08:00
|
|
|
if j == -1 { // subtle corner case that is same as no high value
|
|
|
|
j = n
|
|
|
|
} else {
|
|
|
|
j++
|
|
|
|
}
|
2020-08-16 05:52:50 +08:00
|
|
|
}
|
2018-03-02 07:15:51 +08:00
|
|
|
}
|
|
|
|
// Two numbers
|
|
|
|
return true, i, j, nil
|
|
|
|
}
|
|
|
|
|
2020-08-16 05:52:50 +08:00
|
|
|
func splitIndexString(s string) (low, sep, high string) {
|
|
|
|
if i := strings.Index(s, "..="); i >= 0 {
|
|
|
|
return s[:i], "..=", s[i+3:]
|
|
|
|
}
|
|
|
|
if i := strings.Index(s, ".."); i >= 0 {
|
|
|
|
return s[:i], "..", s[i+2:]
|
|
|
|
}
|
|
|
|
return s, "", ""
|
|
|
|
}
|
|
|
|
|
2018-03-02 07:15:51 +08:00
|
|
|
// atoi is a wrapper around strconv.Atoi, converting strconv.ErrRange to
|
2020-04-12 01:43:52 +08:00
|
|
|
// errs.OutOfRange.
|
|
|
|
func atoi(a string, n int) (int, error) {
|
2018-03-02 07:15:51 +08:00
|
|
|
i, err := strconv.Atoi(a)
|
|
|
|
if err != nil {
|
|
|
|
if err.(*strconv.NumError).Err == strconv.ErrRange {
|
2020-04-12 01:43:52 +08:00
|
|
|
if i < 0 {
|
|
|
|
return 0, negIndexOutOfRange(a, n)
|
|
|
|
}
|
|
|
|
return 0, posIndexOutOfRange(a, n)
|
2018-03-02 07:15:51 +08:00
|
|
|
}
|
2019-04-28 04:00:25 +08:00
|
|
|
return 0, errIndexMustBeInteger
|
2018-03-02 07:15:51 +08:00
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
2020-04-12 01:43:52 +08:00
|
|
|
|
|
|
|
func posIndexOutOfRange(index string, n int) errs.OutOfRange {
|
|
|
|
return errs.OutOfRange{
|
2021-06-16 01:48:24 +08:00
|
|
|
What: "index",
|
2020-08-19 04:23:26 +08:00
|
|
|
ValidLow: "0", ValidHigh: strconv.Itoa(n - 1), Actual: index}
|
2020-04-12 01:43:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func negIndexOutOfRange(index string, n int) errs.OutOfRange {
|
|
|
|
return errs.OutOfRange{
|
2021-06-16 01:48:24 +08:00
|
|
|
What: "negative index",
|
2020-08-19 04:23:26 +08:00
|
|
|
ValidLow: strconv.Itoa(-n), ValidHigh: "-1", Actual: index}
|
2020-04-12 01:43:52 +08:00
|
|
|
}
|