/
arr.go
75 lines (65 loc) · 1.88 KB
/
arr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2019 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package vm
import (
"fmt"
"sort"
"strings"
"github.com/gentee/gentee/core"
)
// AssignAddºArr appends one array to another one
func AssignAddºArr(dest interface{}, src interface{}) (interface{}, error) {
for _, item := range src.(*core.Array).Data {
dest.(*core.Array).Data = append(dest.(*core.Array).Data, item)
}
return dest, nil
}
// AssignAddºArrAny appends an item to array
func AssignAddºArrAny(arr interface{}, value interface{}) (interface{}, error) {
arr.(*core.Array).Data = append(arr.(*core.Array).Data, value)
return arr, nil
}
// boolºArr converts array to bool
func boolºArr(val *core.Array) int64 {
if len(val.Data) == 0 {
return 0
}
return 1
}
// JoinºArrStr concatenates the elements of a to create a single string.
func JoinºArrStr(value *core.Array, sep string) string {
tmp := make([]string, len(value.Data))
for i, item := range value.Data {
tmp[i] = item.(string)
}
return strings.Join(tmp, sep)
}
// ReverseºArr reverses an array
func ReverseºArr(arr *core.Array) *core.Array {
for i, j := 0, len(arr.Data)-1; i < j; i, j = i+1, j-1 {
arr.Data[i], arr.Data[j] = arr.Data[j], arr.Data[i]
}
return arr
}
// SliceºArr extracts some consecutive elements from within an array.
func SliceºArr(rt *Runtime, arr *core.Array, start, end int64) (*core.Array, error) {
ret := core.NewArray()
if start < 0 || end > int64(len(arr.Data)) {
return ret, fmt.Errorf(ErrorText(ErrInvalidParam))
}
if end == 0 {
end = int64(len(arr.Data))
}
for ; start < end; start++ {
var ptr interface{}
CopyVar(rt, &ptr, arr.Data[start])
ret.Data = append(ret.Data, ptr)
}
return ret, nil
}
// SortºArr sorts an array of strings
func SortºArr(value *core.Array) *core.Array {
sort.Sort(value)
return value
}