-
Notifications
You must be signed in to change notification settings - Fork 14
/
funcsStrings.go
229 lines (199 loc) · 4.67 KB
/
funcsStrings.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package stdlib
import (
"fmt"
"rare/pkg/humanize"
"strconv"
"strings"
. "rare/pkg/expressions" //lint:ignore ST1001 Legacy
)
// {prefix string prefix}
func kfPrefix(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 2 {
return stageLiteral(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
val := args[0](context)
contains := args[1](context)
if strings.HasPrefix(val, contains) {
return val
}
return ""
})
}
// {suffix string suffix}
func kfSuffix(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 2 {
return stageLiteral(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
val := args[0](context)
contains := args[1](context)
if strings.HasSuffix(val, contains) {
return val
}
return ""
})
}
func kfUpper(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 1 {
return stageLiteral(ErrorArgCount)
}
return func(context KeyBuilderContext) string {
return strings.ToUpper(args[0](context))
}
}
func kfLower(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 1 {
return stageLiteral(ErrorArgCount)
}
return func(context KeyBuilderContext) string {
return strings.ToLower(args[0](context))
}
}
// {substr {0} }
func kfSubstr(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 3 {
return stageLiteral(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
s := args[0](context)
lenS := len(s)
if lenS == 0 {
return ""
}
left, err1 := strconv.Atoi(args[1](context))
length, err2 := strconv.Atoi(args[2](context))
if err1 != nil || err2 != nil {
return ErrorType
}
if length < 0 {
length = 0
}
if left < 0 { // negative number wrap-around
left += lenS
if left < 0 {
left = 0
}
} else if left > lenS {
left = lenS
}
right := left + length
if right > lenS {
right = lenS
}
return s[left:right]
})
}
// {select {0} 1}
func kfSelect(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 2 {
return stageLiteral(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
s := args[0](context)
idx, err := strconv.Atoi(args[1](context))
if err != nil {
return ErrorType
}
return selectField(s, idx)
})
}
func selectField(s string, idx int) string {
currIdx := 0
wordStart := 0
inDelim := false
quoted := false
for i, c := range s {
if (quoted && c == '"') || (!quoted && (c == ' ' || c == '\t' || c == '\n' || c == '\x00')) {
if currIdx == idx {
return s[wordStart:i]
}
inDelim = true
quoted = false
} else if c == '"' {
quoted = !quoted
} else if inDelim {
wordStart = i
currIdx++
inDelim = false
}
}
if currIdx == idx {
return s[wordStart:]
}
return ""
}
// {format str args...}
// just like fmt.Sprintf
func kfFormat(args []KeyBuilderStage) KeyBuilderStage {
if len(args) < 1 {
return stageLiteral(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
format := args[0](context)
printArgs := make([]interface{}, len(args)-1)
for idx, stage := range args[1:] {
printArgs[idx] = stage(context)
}
return fmt.Sprintf(format, printArgs...)
})
}
func kfHumanizeInt(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 1 {
return stageLiteral(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
val, err := strconv.Atoi(args[0](context))
if err != nil {
return ErrorType
}
return humanize.Hi(val)
})
}
func kfHumanizeFloat(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 1 {
return stageLiteral(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
val, err := strconv.ParseFloat(args[0](context), 64)
if err != nil {
return ErrorType
}
return humanize.Hf(val)
})
}
func kfBytesize(args []KeyBuilderStage) KeyBuilderStage {
if len(args) < 1 {
return stageLiteral(ErrorArgCount)
}
precision, err := strconv.Atoi(EvalStageIndexOrDefault(args, 1, "0"))
if err != nil {
return stageLiteral(ErrorType)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
val, err := strconv.ParseUint(args[0](context), 10, 64)
if err != nil {
return ErrorType
}
return humanize.AlwaysByteSize(val, precision)
})
}
func kfJoin(delim rune) KeyBuilderFunction {
return func(args []KeyBuilderStage) KeyBuilderStage {
if len(args) == 0 {
return stageLiteral("")
}
if len(args) == 1 {
return args[0]
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
var sb strings.Builder
sb.WriteString(args[0](context))
for _, arg := range args[1:] {
sb.WriteRune(delim)
sb.WriteString(arg(context))
}
return sb.String()
})
}
}