-
Notifications
You must be signed in to change notification settings - Fork 28
/
str_funcs.go
317 lines (293 loc) · 10.4 KB
/
str_funcs.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package queryfunctions
import (
"errors"
"fmt"
"html"
"strconv"
"strings"
"unicode"
"unicode/utf8"
ds "v.io/v23/query/engine/datasource"
"v.io/v23/query/engine/internal/conversions"
"v.io/v23/query/engine/internal/queryparser"
"v.io/v23/query/syncql"
"v.io/v23/vdl"
)
func str(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
o := args[0]
if strOp, err := conversions.ConvertValueToString(o); err == nil {
return strOp, nil
}
var c queryparser.Operand
c.Type = queryparser.TypStr
c.Off = o.Off
switch args[0].Type {
case queryparser.TypBigInt:
c.Str = o.BigInt.String()
case queryparser.TypBigRat:
c.Str = o.BigRat.String()
case queryparser.TypBool:
c.Str = strconv.FormatBool(o.Bool)
case queryparser.TypFloat:
c.Str = strconv.FormatFloat(o.Float, 'f', -1, 64)
case queryparser.TypInt:
c.Str = strconv.FormatInt(o.Int, 10)
case queryparser.TypTime:
c.Str = o.Time.Format("Mon Jan 2 15:04:05 -0700 MST 2006")
case queryparser.TypUint:
c.Str = strconv.FormatUint(o.Uint, 10)
case queryparser.TypObject:
// A vdl.TypeObject?
// In this case, TypeObject().Name() is used (rather than Type().Name()
if o.Object.Kind() == vdl.TypeObject {
c.Str = o.Object.TypeObject().Name()
} else {
c.Str = fmt.Sprintf("%v", o.Object)
}
}
return &c, nil
}
func atoi(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
o := args[0]
switch o.Type {
case queryparser.TypStr:
i, err := strconv.ParseInt(o.Str, 10, 64)
if err != nil {
return nil, err
}
return makeIntOp(off, i), nil
default:
return nil, errors.New("cannot convert operand to int")
}
}
func atof(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
o := args[0]
switch o.Type {
case queryparser.TypStr:
f, err := strconv.ParseFloat(o.Str, 64)
if err != nil {
return nil, err
}
return makeFloatOp(off, f), nil
default:
return nil, errors.New("cannot convert operand to float")
}
}
func htmlEscapeFunc(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
strOp, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
return makeStrOp(off, html.EscapeString(strOp.Str)), nil
}
func htmlUnescapeFunc(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
strOp, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
return makeStrOp(off, html.UnescapeString(strOp.Str)), nil
}
func lowerCase(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
strOp, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
return makeStrOp(off, strings.ToLower(strOp.Str)), nil
}
func upperCase(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
strOp, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
return makeStrOp(off, strings.ToUpper(strOp.Str)), nil
}
func typeFunc(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
// If operand is not an object, we can't get a type
if args[0].Type != queryparser.TypObject {
return nil, syncql.ErrorfFunctionTypeInvalidArg(db.GetContext(), "[%v]function 'Type()' cannot get type of argument -- expecting object", args[0].Off)
}
if args[0].Object.Kind() == vdl.TypeObject {
// Believe it or not, Name() doesn't return the name of the type if the type is TypeObject.
return makeStrOp(off, args[0].Object.Type().String()), nil
}
return makeStrOp(off, args[0].Object.Type().Name()), nil
}
func typeFuncFieldCheck(db ds.Database, off int64, args []*queryparser.Operand) error {
// At this point, it is known that there is one arg. Make sure it is of type field
// and is a value field (i.e., it must begin with a v segment).
if args[0].Type != queryparser.TypField || len(args[0].Column.Segments) < 1 || args[0].Column.Segments[0].Value != "v" {
return syncql.ErrorfArgMustBeField(db.GetContext(), "[%v]argument must be a value field (i.e., must begin with 'v')", args[0].Off)
}
return nil
}
// Split splits str (arg[0]) into substrings separated by sep (arg[1]) and returns an
// array of substrings between those separators. If sep is empty, Split splits after each
// UTF-8 sequence.
// e.g., Split("abc.def.ghi", ".") returns a list of "abc", "def", "ghi"
func split(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
strArg, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
sepArg, err := conversions.ConvertValueToString(args[1])
if err != nil {
return nil, err
}
var o queryparser.Operand
o.Off = args[0].Off
o.Type = queryparser.TypObject
o.Object = vdl.ValueOf(strings.Split(strArg.Str, sepArg.Str))
return &o, nil
}
// Sprintf(<format-str>, arg...) string
// Sprintf is golang's Sprintf.
// e.g., Sprintf("The meaning of life is %s.", v.LifeMeaning) returns "The meaning of life is 42."
func sprintf(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
sprintfArgs := []interface{}{}
for _, arg := range args[1:] {
switch arg.Type {
case queryparser.TypBigInt:
sprintfArgs = append(sprintfArgs, arg.BigInt)
case queryparser.TypBigRat:
sprintfArgs = append(sprintfArgs, arg.BigRat)
case queryparser.TypBool:
sprintfArgs = append(sprintfArgs, arg.Bool)
case queryparser.TypFloat:
sprintfArgs = append(sprintfArgs, arg.Float)
case queryparser.TypInt:
sprintfArgs = append(sprintfArgs, arg.Int)
case queryparser.TypStr:
sprintfArgs = append(sprintfArgs, arg.Str)
case queryparser.TypTime:
sprintfArgs = append(sprintfArgs, arg.Time)
case queryparser.TypObject:
sprintfArgs = append(sprintfArgs, arg.Object)
case queryparser.TypUint:
sprintfArgs = append(sprintfArgs, arg.Uint)
default:
sprintfArgs = append(sprintfArgs, nil)
}
}
return makeStrOp(off, fmt.Sprintf(args[0].Str, sprintfArgs...)), nil
}
// StrCat(str1, str2,... string) string
// StrCat returns the concatenation of all the string args.
// e.g., StrCat("abc", ",", "def") returns "abc,def"
func strCat(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
val := ""
for _, arg := range args {
str, err := conversions.ConvertValueToString(arg)
if err != nil {
return nil, err
}
val += str.Str
}
return makeStrOp(off, val), nil
}
// StrIndex(s, sep string) int
// StrIndex returns the index of sep in s, or -1 is sep is not present in s.
// e.g., StrIndex("abc", "bc") returns 1.
func strIndex(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
s, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
sep, err := conversions.ConvertValueToString(args[1])
if err != nil {
return nil, err
}
return makeIntOp(off, int64(strings.Index(s.Str, sep.Str))), nil
}
// StrLastIndex(s, sep string) int
// StrLastIndex returns the index of the last instance of sep in s, or -1 is sep is not present in s.
// e.g., StrLastIndex("abcbc", "bc") returns 3.
func strLastIndex(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
s, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
sep, err := conversions.ConvertValueToString(args[1])
if err != nil {
return nil, err
}
return makeIntOp(off, int64(strings.LastIndex(s.Str, sep.Str))), nil
}
// StrRepeat(s string, count int) int
// StrRepeat returns a new string consisting of count copies of the string s.
// e.g., StrRepeat("abc", 3) returns "abcabcabc".
func strRepeat(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
s, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
count, err := conversions.ConvertValueToInt(args[1])
if err != nil {
return nil, err
}
if count.Int >= 0 {
return makeStrOp(off, strings.Repeat(s.Str, int(count.Int))), nil
}
// golang strings.Repeat doesn't like count < 0
return makeStrOp(off, ""), nil
}
// StrReplace(s, old, new string) string
// StrReplace returns a copy of s with the first instance of old replaced by new.
// e.g., StrReplace("abcdef", "bc", "zzzzz") returns "azzzzzdef".
func strReplace(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
s, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
old, err := conversions.ConvertValueToString(args[1])
if err != nil {
return nil, err
}
new, err := conversions.ConvertValueToString(args[2])
if err != nil {
return nil, err
}
return makeStrOp(off, strings.Replace(s.Str, old.Str, new.Str, 1)), nil
}
// Trim(s string) string
// Trim returns a copy of s with all leading and trailing white space removed, as defined by Unicode.
// e.g., Trim(" abc ") returns "abc".
func trim(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
s, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
return makeStrOp(off, strings.TrimSpace(s.Str)), nil
}
// TrimLeft(s string) string
// TrimLeft returns a copy of s with all leading white space removed, as defined by Unicode.
// e.g., TrimLeft(" abc ") returns "abc ".
func trimLeft(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
s, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
return makeStrOp(off, strings.TrimLeftFunc(s.Str, unicode.IsSpace)), nil
}
// TrimRight(s string) string
// TrimRight returns a copy of s with all leading white space removed, as defined by Unicode.
// e.g., TrimRight(" abc ") returns "abc ".
func trimRight(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
s, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
return makeStrOp(off, strings.TrimRightFunc(s.Str, unicode.IsSpace)), nil
}
// RuneCount(s string) int
// RuneCount returns the number of runes in s.
// e.g., RuneCount("abc") returns 3.
func runeCount(db ds.Database, off int64, args []*queryparser.Operand) (*queryparser.Operand, error) {
s, err := conversions.ConvertValueToString(args[0])
if err != nil {
return nil, err
}
return makeIntOp(off, int64(utf8.RuneCountInString(s.Str))), nil
}