-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathstrings.go
57 lines (43 loc) · 945 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
56
57
package jsonlogic
import (
"bytes"
"strings"
"github.com/diegoholiveira/jsonlogic/v3/internal/typing"
)
func substr(values any) any {
parsed := values.([]any)
runes := []rune(typing.ToString(parsed[0]))
from := int(typing.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(typing.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 any) any {
if typing.IsString(values) {
return values
}
var s bytes.Buffer
for _, text := range values.([]any) {
s.WriteString(typing.ToString(text))
}
return strings.TrimSpace(s.String())
}