diff options
author | Wim <wim@42.be> | 2021-03-20 22:40:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-20 22:40:23 +0100 |
commit | ee5d9b43b54a3becf3cb4025198f24608d35500d (patch) | |
tree | dd3614db7423da52f5a71da3001e48d1e4195ea1 /vendor/github.com/d5/tengo/v2 | |
parent | 3a8857c8c9efb2c67fb8c175f31d2b9c617b771b (diff) | |
download | matterbridge-msglm-ee5d9b43b54a3becf3cb4025198f24608d35500d.tar.gz matterbridge-msglm-ee5d9b43b54a3becf3cb4025198f24608d35500d.tar.bz2 matterbridge-msglm-ee5d9b43b54a3becf3cb4025198f24608d35500d.zip |
Update vendor (#1414)
Diffstat (limited to 'vendor/github.com/d5/tengo/v2')
-rw-r--r-- | vendor/github.com/d5/tengo/v2/builtins.go | 69 | ||||
-rw-r--r-- | vendor/github.com/d5/tengo/v2/errors.go | 3 |
2 files changed, 72 insertions, 0 deletions
diff --git a/vendor/github.com/d5/tengo/v2/builtins.go b/vendor/github.com/d5/tengo/v2/builtins.go index fcda81cd..b954d072 100644 --- a/vendor/github.com/d5/tengo/v2/builtins.go +++ b/vendor/github.com/d5/tengo/v2/builtins.go @@ -121,6 +121,10 @@ var builtinFuncs = []*BuiltinFunction{ Name: "format", Value: builtinFormat, }, + { + Name: "range", + Value: builtinRange, + }, } // GetAllBuiltinFunctions returns all builtin function objects. @@ -323,6 +327,71 @@ func builtinLen(args ...Object) (Object, error) { } } +//range(start, stop[, step]) +func builtinRange(args ...Object) (Object, error) { + numArgs := len(args) + if numArgs < 2 || numArgs > 3 { + return nil, ErrWrongNumArguments + } + var start, stop, step *Int + + for i, arg := range args { + v, ok := args[i].(*Int) + if !ok { + var name string + switch i { + case 0: + name = "start" + case 1: + name = "stop" + case 2: + name = "step" + } + + return nil, ErrInvalidArgumentType{ + Name: name, + Expected: "int", + Found: arg.TypeName(), + } + } + if i == 2 && v.Value <= 0 { + return nil, ErrInvalidRangeStep + } + switch i { + case 0: + start = v + case 1: + stop = v + case 2: + step = v + } + } + + if step == nil { + step = &Int{Value: int64(1)} + } + + return buildRange(start.Value, stop.Value, step.Value), nil +} + +func buildRange(start, stop, step int64) *Array { + array := &Array{} + if start <= stop { + for i := start; i < stop; i += step { + array.Value = append(array.Value, &Int{ + Value: i, + }) + } + } else { + for i := start; i > stop; i -= step { + array.Value = append(array.Value, &Int{ + Value: i, + }) + } + } + return array +} + func builtinFormat(args ...Object) (Object, error) { numArgs := len(args) if numArgs == 0 { diff --git a/vendor/github.com/d5/tengo/v2/errors.go b/vendor/github.com/d5/tengo/v2/errors.go index a3fd1f3b..8ef610a3 100644 --- a/vendor/github.com/d5/tengo/v2/errors.go +++ b/vendor/github.com/d5/tengo/v2/errors.go @@ -49,6 +49,9 @@ var ( // ErrNotImplemented is an error where an Object has not implemented a // required method. ErrNotImplemented = errors.New("not implemented") + + // ErrInvalidRangeStep is an error where the step parameter is less than or equal to 0 when using builtin range function. + ErrInvalidRangeStep = errors.New("range step must be greater than 0") ) // ErrInvalidArgumentType represents an invalid argument value type error. |