-
Notifications
You must be signed in to change notification settings - Fork 52
/
strings.go
55 lines (42 loc) · 899 Bytes
/
strings.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
package jsonlogic
import (
"bytes"
"strings"
)
func substr(values interface{}) interface{} {
parsed := values.([]interface{})
runes := []rune(toString(parsed[0]))
from := int(toNumber(parsed[1]))
length := len(runes)
if from < 0 {
from = length + from
}
if from < 0 || from > length {
// case from is still negative, we must stop right now and return the original string
return string(runes)
}
if len(parsed) == 3 {
length = int(toNumber(parsed[2]))
}
var to int
if length < 0 {
length = len(runes) + length
to = length
} else {
to = from + length
}
if to > len(runes) {
to = len(runes)
}
return string(runes[from:to])
}
func concat(values interface{}) interface{} {
if isString(values) {
return values
}
var s bytes.Buffer
for _, text := range values.([]interface{}) {
s.WriteString(toString(text))
}
return strings.TrimSpace(s.String())
}