-
Notifications
You must be signed in to change notification settings - Fork 4
/
strings.go
439 lines (391 loc) · 9.76 KB
/
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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Blue Oak Model License
// license that can be found in the LICENSE file.
package funcmap
import (
"errors"
"net/url"
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
// String functions from Hugo
// https://github.com/gohugoio/hugo/blob/7f47b99ea9a7ba7759c4f9424fedd4591e6da497/tpl/strings/strings.go
// TODO (bradrydzewski) add Join function
// TODO (bradrydzewski) add Substr function
// Append returns a slice of the string s, with append appended.
func Append(s, append interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
as, err := toStringE(append)
if err != nil {
return "", err
}
return ss + as, nil
}
// Prepend returns a slice of the string s, prepended with prepend.
func Prepend(s, prepend interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
ps, err := toStringE(prepend)
if err != nil {
return "", err
}
return ps + ss, nil
}
// Chomp returns a copy of s with all trailing newline
// characters removed.
func Chomp(s interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
return strings.TrimRight(ss, "\r\n"), nil
}
// Contains reports whether substr is within s.
func Contains(s, substr interface{}) (bool, error) {
ss, err := toStringE(s)
if err != nil {
return false, err
}
su, err := toStringE(substr)
if err != nil {
return false, err
}
return strings.Contains(ss, su), nil
}
// ContainsAny reports whether any Unicode code points in
// chars are within s.
func ContainsAny(s, chars interface{}) (bool, error) {
ss, err := toStringE(s)
if err != nil {
return false, err
}
sc, err := toStringE(chars)
if err != nil {
return false, err
}
return strings.ContainsAny(ss, sc), nil
}
// FindRE returns a list of strings that match the regular expression. By default all matches
// will be included. The number of matches can be limited with an optional third parameter.
func FindRE(expr string, content interface{}, limit ...interface{}) ([]string, error) {
re, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
conv, err := toStringE(content)
if err != nil {
return nil, err
}
if len(limit) == 0 {
return re.FindAllString(conv, -1), nil
}
lim, err := toIntE(limit[0])
if err != nil {
return nil, err
}
return re.FindAllString(conv, lim), nil
}
// FirstUpper returns a string with the first character as upper case.
func FirstUpper(s interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
return firstUpper(ss), nil
}
// HasPrefix tests whether the string s begins with prefix.
func HasPrefix(s, prefix interface{}) (bool, error) {
s1, err := toStringE(s)
if err != nil {
return false, err
}
s2, err := toStringE(prefix)
if err != nil {
return false, err
}
return strings.HasPrefix(s1, s2), nil
}
// HasSuffix tests whether the string s ends with suffix.
func HasSuffix(s, suffix interface{}) (bool, error) {
s1, err := toStringE(s)
if err != nil {
return false, err
}
s2, err := toStringE(suffix)
if err != nil {
return false, err
}
return strings.HasSuffix(s1, s2), nil
}
// PadLeft returns a slice of the string s, prefixed with
// n copies of padding.
func PadLeft(s, padding, n interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
sp, err := toStringE(padding)
if err != nil {
return "", err
}
sn, err := toIntE(n)
if err != nil {
return "", err
}
for i := 0; i < sn; i++ {
ss = sp + ss
}
return ss, err
}
// PadRight returns a slice of the string s, suffixed with
// n instances of the padding string.
func PadRight(s, padding, n interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
sp, err := toStringE(padding)
if err != nil {
return "", err
}
sn, err := toIntE(n)
if err != nil {
return "", err
}
for i := 0; i < sn; i++ {
ss = ss + sp
}
return ss, err
}
// Repeat returns a new string consisting of count copies
// of the string s.
func Repeat(s, count interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
sn, err := toIntE(count)
if err != nil {
return "", err
}
return strings.Repeat(ss, sn), nil
}
// Replace returns a copy of the string s with instances of
// old replaced by new.
func Replace(s, old, new interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
so, err := toStringE(old)
if err != nil {
return "", err
}
no, err := toStringE(new)
if err != nil {
return "", err
}
return strings.Replace(ss, so, no, -1), nil
}
// ReplaceRE returns a copy of s, replacing all matches of the regular
// expression pattern with the replacement text repl.
func ReplaceRE(pattern, repl, s interface{}) (string, error) {
sp, err := toStringE(pattern)
if err != nil {
return "", err
}
sr, err := toStringE(repl)
if err != nil {
return "", err
}
ss, err := toStringE(s)
if err != nil {
return "", err
}
re, err := regexp.Compile(sp)
if err != nil {
return "", err
}
return re.ReplaceAllString(ss, sr), nil
}
// SliceString slices a string by specifying a half-open range with
// two indices, start and end. 1 and 4 creates a slice including elements 1 through 3.
// The end index can be omitted, it defaults to the string's length.
func SliceString(a interface{}, startEnd ...interface{}) (string, error) {
aStr, err := toStringE(a)
if err != nil {
return "", err
}
var argStart, argEnd int
argNum := len(startEnd)
if argNum > 0 {
if argStart, err = toIntE(startEnd[0]); err != nil {
return "", errors.New("start argument must be integer")
}
}
if argNum > 1 {
if argEnd, err = toIntE(startEnd[1]); err != nil {
return "", errors.New("end argument must be integer")
}
}
if argNum > 2 {
return "", errors.New("too many arguments")
}
asRunes := []rune(aStr)
if argNum > 0 && (argStart < 0 || argStart >= len(asRunes)) {
return "", errors.New("slice bounds out of range")
}
if argNum == 2 {
if argEnd < 0 || argEnd > len(asRunes) {
return "", errors.New("slice bounds out of range")
}
return string(asRunes[argStart:argEnd]), nil
} else if argNum == 1 {
return string(asRunes[argStart:]), nil
} else {
return string(asRunes[:]), nil
}
}
// Split slices s into all substrings separated by sep and
// returns a slice of the substrings between those separators.
func Split(s interface{}, sep string) ([]string, error) {
ss, err := toStringE(s)
if err != nil {
return nil, err
}
return strings.Split(ss, sep), nil
}
// SplitN slices s into substrings separated by sep and returns
// a slice of the substrings between those separators.
func SplitN(s, sep, n interface{}) ([]string, error) {
s1, err := toStringE(s)
if err != nil {
return nil, err
}
s2, err := toStringE(sep)
if err != nil {
return nil, err
}
no, err := toIntE(n)
if err != nil {
return nil, err
}
return strings.SplitN(s1, s2, no), nil
}
// ToLower returns a copy of the input s with all Unicode letters
// mapped to their lower case.
func ToLower(s interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
return strings.ToLower(ss), nil
}
// ToTitle returns a copy of the input s with all Unicode letters
// mapped to their title case.
func ToTitle(s interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
return strings.ToTitle(ss), nil
}
// ToUpper returns a copy of the input s with all Unicode letters
// mapped to their upper case.
func ToUpper(s interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
return strings.ToUpper(ss), nil
}
// TrimLeft returns a slice of the string s with all leading Unicode
// code points contained in cutset removed.
func TrimLeft(s, cutset interface{}) (string, error) {
s1, err := toStringE(s)
if err != nil {
return "", err
}
s2, err := toStringE(cutset)
if err != nil {
return "", err
}
return strings.TrimLeft(s1, s2), nil
}
// TrimRight returns a slice of the string s, with all trailing
// Unicode code points contained in cutset removed.
func TrimRight(s, cutset interface{}) (string, error) {
s1, err := toStringE(s)
if err != nil {
return "", err
}
s2, err := toStringE(cutset)
if err != nil {
return "", err
}
return strings.TrimRight(s1, s2), nil
}
// TrimPrefix returns a slice of the string s, with all trailing
// Unicode code points contained in cutset removed.
func TrimPrefix(s, prefix interface{}) (string, error) {
s1, err := toStringE(s)
if err != nil {
return "", err
}
s2, err := toStringE(prefix)
if err != nil {
return "", err
}
return strings.TrimPrefix(s1, s2), nil
}
// TrimSuffix returns s without the provided trailing suffix
// string. If s doesn't end with suffix, s is returned unchanged.
func TrimSuffix(s, suffix interface{}) (string, error) {
s1, err := toStringE(s)
if err != nil {
return "", err
}
s2, err := toStringE(suffix)
if err != nil {
return "", err
}
return strings.TrimSuffix(s1, s2), nil
}
// Trim returns a slice of the string s, with all leading
// and trailing white space removed, as defined by Unicode.
func Trim(s interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
return strings.TrimSpace(ss), nil
}
// Urlize returns a copy of the input s that is sanities so
// it can be safely placed inside a URL path segment, with
// spaces converted to hypens.
func Urlize(s interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
ss = strings.TrimSpace(ss)
ss = strings.Replace(ss, " ", "-", -1)
return url.QueryEscape(ss), nil
}
//
// helpers
//
func firstUpper(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToUpper(r)) + s[n:]
}