forked from google/agi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
265 lines (239 loc) · 6.67 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
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package template
import (
"fmt"
"reflect"
"strings"
"unicode"
"github.com/google/gapid/core/text/cases"
)
type stringList []string
// String returns the concatenation of all the string segments with no
// separator.
func (l stringList) String() string {
return strings.Join([]string(l), "")
}
// stringify transforms the input parameters into a string list. Arrays and
// slices are flattened into a sequential list of strings.
func stringify(v ...interface{}) stringList {
out := stringList{}
for _, v := range v {
switch v := v.(type) {
case nil:
case string:
out = append(out, v)
case []string:
out = append(out, v...)
case stringList:
out = append(out, v...)
default:
switch reflect.TypeOf(v).Kind() {
case reflect.Array, reflect.Slice:
v := reflect.ValueOf(v)
for i, c := 0, v.Len(); i < c; i++ {
out = append(out, stringify(v.Index(i).Interface())...)
}
default:
out = append(out, fmt.Sprintf("%v", v))
}
}
}
// Filter out any empty strings
count := 0
for _, s := range out {
if len(s) > 0 {
out[count] = s
count++
}
}
return out[:count]
}
// Strings returns the arguments as a string list.
func (Functions) Strings(v ...interface{}) stringList {
return stringify(v...)
}
// JoinWith returns the concatenation of all the string segments with the
// specified separator.
func (Functions) JoinWith(sep string, v ...interface{}) string {
l := stringify(v...)
return strings.Join([]string(l), sep)
}
// SplitOn slices each string segement into all substrings separated by sep.
// The returned stringList will not contain any occurances of sep.
func (Functions) SplitOn(sep string, v ...interface{}) stringList {
l := stringify(v...)
out := stringList{}
for _, s := range l {
for _, v := range strings.Split(s, sep) {
if len(v) > 0 {
out = append(out, v)
}
}
}
return out
}
// SplitEOL slices each string segement into all substrings separated by an end
// of line delimiter (\n or \r\n).
// The returned stringList will not contain any new lines.
func (f Functions) SplitEOL(v ...interface{}) stringList {
return f.SplitOn("\n", f.SplitOn("\r\n", v...))
}
// SplitUpperCase slices each string segment before and after each upper-case
// rune.
func (Functions) SplitUpperCase(v ...interface{}) stringList {
l := stringify(v...)
out := stringList{}
for _, s := range l {
str := ""
for _, r := range s {
if unicode.IsUpper(r) {
if len(str) > 0 {
out = append(out, str)
str = ""
}
out = append(out, string(r))
} else {
str += string(r)
}
}
if len(str) > 0 {
out = append(out, str)
}
}
return out
}
// SplitPascalCase slices each string segment at each transition from an letter
// rune to a upper-case letter rune.
func (Functions) SplitPascalCase(v ...interface{}) stringList {
out := stringList{}
for _, s := range stringify(v...) {
out = append(out, cases.Pascal(s)...)
}
return out
}
// Title capitalizes each letter of each string segment.
func (Functions) Title(v ...interface{}) stringList {
l := stringify(v...)
return stringList(cases.Words(l).Title())
}
// Untitle lower-cases each letter of each string segment.
func (Functions) Untitle(v ...interface{}) stringList {
l := stringify(v...)
return stringList(cases.Words(l).Untitle())
}
// Lower lower-cases all letters of each string segment.
func (Functions) Lower(v ...interface{}) stringList {
l := stringify(v...)
return stringList(cases.Words(l).ToLower())
}
// Upper upper-cases all letters of each string segment.
func (Functions) Upper(v ...interface{}) stringList {
l := stringify(v...)
return stringList(cases.Words(l).ToUpper())
}
// Contains returns true if any string segment contains substr.
func (Functions) Contains(substr string, v ...interface{}) bool {
l := stringify(v...)
for _, s := range l {
if strings.Contains(s, substr) {
return true
}
}
return false
}
// HasPrefix tests whether the string s begins with prefix.
func (Functions) HasPrefix(s string, prefix string) bool {
return strings.HasPrefix(s, prefix)
}
// HasSuffix tests whether the string s ends with suffix.
func (Functions) HasSuffix(s string, suffix string) bool {
return strings.HasSuffix(s, suffix)
}
// Replace any occurance of old with new in the string segments.
func (Functions) Replace(old string, new string, v ...interface{}) stringList {
l := stringify(v...)
out := stringList{}
for _, s := range l {
s = strings.Replace(s, old, new, -1)
if len(s) > 0 {
out = append(out, s)
}
}
return out
}
// TrimLeft returns from with the all leading Unicode code points contained
// in cutset removed.
func (Functions) TrimLeft(cutset string, from ...interface{}) stringList {
l := stringify(from...)
out := stringList{}
for _, s := range l {
s = strings.TrimLeft(s, cutset)
if len(s) > 0 {
out = append(out, s)
}
}
return out
}
// TrimRight returns from with the all trailing Unicode code points contained
// in cutset removed.
func (Functions) TrimRight(cutset string, from ...interface{}) stringList {
l := stringify(from...)
out := stringList{}
for _, s := range l {
s = strings.TrimRight(s, cutset)
if len(s) > 0 {
out = append(out, s)
}
}
return out
}
// TrimPrefix returns from with without the provided leading prefix string.
func (Functions) TrimPrefix(prefix string, from ...interface{}) stringList {
l := stringify(from...)
out := stringList{}
for _, s := range l {
s = strings.TrimPrefix(s, prefix)
if len(s) > 0 {
out = append(out, s)
}
}
return out
}
// TrimSuffix returns from without the provided trailing suffix string.
func (Functions) TrimSuffix(suffix string, from ...interface{}) stringList {
l := stringify(from...)
out := stringList{}
for _, s := range l {
s = strings.TrimSuffix(s, suffix)
if len(s) > 0 {
out = append(out, s)
}
}
return out
}
// FilterOut returns from with all occurances of v removed.
func (Functions) FilterOut(v, from stringList) stringList {
m := make(map[string]struct{}, len(v))
for _, s := range v {
m[s] = struct{}{}
}
out := make(stringList, 0, len(from))
for _, s := range from {
if _, found := m[s]; !found {
out = append(out, s)
}
}
return out
}