MCPcopy Index your code
hub / github.com/expr-lang/expr / Slice

Function Slice

vm/runtime/runtime.go:164–207  ·  view source on GitHub ↗
(array, from, to any)

Source from the content-addressed store, hash-verified

162}
163
164func Slice(array, from, to any) any {
165 v := reflect.ValueOf(array)
166
167 switch v.Kind() {
168 case reflect.Array, reflect.Slice, reflect.String:
169 length := v.Len()
170 a, b := ToInt(from), ToInt(to)
171 if a < 0 {
172 a = length + a
173 }
174 if a < 0 {
175 a = 0
176 }
177 if b < 0 {
178 b = length + b
179 }
180 if b < 0 {
181 b = 0
182 }
183 if b > length {
184 b = length
185 }
186 if a > b {
187 a = b
188 }
189 if v.Kind() == reflect.Array && !v.CanAddr() {
190 newValue := reflect.New(v.Type()).Elem()
191 newValue.Set(v)
192 v = newValue
193 }
194 value := v.Slice(a, b)
195 if value.IsValid() {
196 return value.Interface()
197 }
198
199 case reflect.Ptr:
200 value := v.Elem()
201 if value.IsValid() {
202 return Slice(value.Interface(), from, to)
203 }
204
205 }
206 panic(fmt.Sprintf("cannot slice %v", from))
207}
208
209func In(needle any, array any) bool {
210 if array == nil {

Callers 1

RunMethod · 0.92

Calls 6

ToIntFunction · 0.85
ElemMethod · 0.80
SetMethod · 0.80
SprintfMethod · 0.80
TypeMethod · 0.65
LenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…