-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringSlice.go
361 lines (320 loc) · 8.4 KB
/
StringSlice.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
package describe
import "strings"
// TypeStringSlice []string function collections struct
type TypeStringSlice struct {
Obj []string
}
// StringSlice get *TypeStringSlice
func StringSlice(obj ...[]string) *TypeStringSlice {
if len(obj) > 0 {
if obj[0] != nil {
return &TypeStringSlice{Obj: obj[0]}
}
}
return &TypeStringSlice{Obj: []string{}}
}
// Copy get copy of type
func (ss *TypeStringSlice) Copy() *TypeStringSlice {
newSlice := make([]string, len(ss.Obj))
copy(newSlice, ss.Obj)
return &TypeStringSlice{Obj: newSlice}
}
// IndexOf get index of s, return -1 if not found
func (ss *TypeStringSlice) IndexOf(s string) int {
for i, v := range ss.Obj {
if v == s {
return i
}
}
return -1
}
// ElmAt return element of slice at position in describe.Type. Panic if slice is empty or out of range.
func (ss *TypeStringSlice) ElmAt(i int) *TypeString {
if ss.Empty() {
PanicErr(ErrSliceIsEmpty)
}
if i >= len(ss.Obj) {
PanicErr(ErrOutOfRange)
}
return String(ss.Obj[i]).SetSliceIndex(i)
}
// ElmTrim trim all elements in slice
func (ss *TypeStringSlice) ElmTrim(cutset string) *TypeStringSlice {
for i, elm := range ss.Obj {
ss.Obj[i] = String(elm).Trim(cutset).Get()
}
return ss
}
// ElmTrimLeft trim all elements in slice
func (ss *TypeStringSlice) ElmTrimLeft(cutset string) *TypeStringSlice {
for i, elm := range ss.Obj {
ss.Obj[i] = String(elm).TrimLeft(cutset).Get()
}
return ss
}
// ElmTrimRight trim all elements in slice
func (ss *TypeStringSlice) ElmTrimRight(cutset string) *TypeStringSlice {
for i, elm := range ss.Obj {
ss.Obj[i] = String(elm).TrimRight(cutset).Get()
}
return ss
}
// ElmTrimPrefix trim all elements in slice
func (ss *TypeStringSlice) ElmTrimPrefix(prefix string) *TypeStringSlice {
for i, elm := range ss.Obj {
ss.Obj[i] = String(elm).TrimPrefix(prefix).Get()
}
return ss
}
// ElmTrimSuffix trim all elements in slice
func (ss *TypeStringSlice) ElmTrimSuffix(suffix string) *TypeStringSlice {
for i, elm := range ss.Obj {
ss.Obj[i] = String(elm).TrimSuffix(suffix).Get()
}
return ss
}
// ElmTrimSpace trim space all elements in slice
func (ss *TypeStringSlice) ElmTrimSpace() *TypeStringSlice {
for i, elm := range ss.Obj {
ss.Obj[i] = String(elm).TrimSpace().Get()
}
return ss
}
// ElmWrapBy wrap all elements in slice
func (ss *TypeStringSlice) ElmWrapBy(wrapper string) *TypeStringSlice {
for i, elm := range ss.Obj {
ss.Obj[i] = String(elm).WrapBy(wrapper).Get()
}
return ss
}
// Get return object slice
func (ss *TypeStringSlice) Get() []string {
return ss.Obj
}
// Len return len(slice)
func (ss *TypeStringSlice) Len() int {
return len(ss.Obj)
}
// Push push new elements into slice. Return new slice
func (ss *TypeStringSlice) Push(elm string, more ...string) *TypeStringSlice {
// cp := append(append(append([]string{}, ss.Obj...), elm), more...)
// return StringSlice(cp)
ss.Obj = append(append(ss.Obj, elm), more...)
return ss
}
// Shift shift elements into slice. Return new slice
func (ss *TypeStringSlice) Shift(elm string, more ...string) *TypeStringSlice {
// cp := append(append([]string{elm}, more...), ss.Obj...)
ss.Obj = append(append([]string{elm}, more...), ss.Obj...)
return ss
}
// Empty is empty slice or not
func (ss *TypeStringSlice) Empty() bool {
return len(ss.Obj) == 0
}
// From return slice[i:], if out of range return empty slice
func (ss *TypeStringSlice) From(i int) *TypeStringSlice {
if i > ss.Len() {
i = ss.Len()
}
return StringSlice(ss.Obj[i:])
}
// To return slice[:i], if out of range return slice[:last]
func (ss *TypeStringSlice) To(i int) *TypeStringSlice {
if i > ss.Len() {
i = ss.Len()
}
return StringSlice(ss.Obj[:i])
}
// Range return slice[i:j], panic when out of range or i > j
func (ss *TypeStringSlice) Range(i int, j int) *TypeStringSlice {
if i < 0 || j < 0 || i > j {
PanicErr(ErrRangeIndexInvalid)
}
if i > ss.Len() {
PanicErr(ErrOutOfRange)
}
if j > ss.Len() {
PanicErr(ErrOutOfRange)
}
return StringSlice(ss.Obj[i:j])
}
// Trim remove target element from both sides of slice
func (ss *TypeStringSlice) Trim(elm string) *TypeStringSlice {
var start, end int
for start = 0; start < len(ss.Obj); start++ {
if ss.Obj[start] != elm {
break
}
}
for end = len(ss.Obj) - 1; end >= 0; end-- {
if ss.Obj[end] != elm {
break
}
}
return StringSlice(ss.Obj[start : end+1])
}
// TrimSpace remove empty string from both sides of slice
func (ss *TypeStringSlice) TrimSpace() *TypeStringSlice {
return ss.Trim("")
}
// Same whether it is totally same as target slice
func (ss *TypeStringSlice) Same(target []string) bool {
if len(ss.Obj) != len(target) {
return false
}
for i, v := range ss.Obj {
if target[i] != v {
return false
}
}
return true
}
// Has whether slice has all elements
func (ss *TypeStringSlice) Has(elms ...string) bool {
for _, elm := range elms {
has := false
for _, v := range ss.Obj {
if v == elm {
has = true
break
}
}
if !has {
return false
}
}
return true
}
// NotHave whether slice doesn't have any of elements
func (ss *TypeStringSlice) NotHave(elms ...string) bool {
// for _, elm := range elms {
// notHave := true
// for _, v := range ss.Obj {
// if v == elm {
// notHave = false
// break
// }
// }
// if !notHave {
// return false
// }
// }
// return true
return !ss.Has(elms...)
}
// First return first element of slice in describe.Type. Panic if slice is empty.
func (ss *TypeStringSlice) First() *TypeString {
if ss.Empty() {
PanicErr(ErrSliceIsEmpty)
}
return String(ss.Obj[0])
}
// Find find first element in describe.Type when func passed. Return nil if not found.
func (ss *TypeStringSlice) Find(f func(*TypeString) bool) *TypeString {
for _, v := range ss.Obj {
sv := String(v)
if f(sv) {
return sv
}
}
return nil
}
// FindHasPrefix find first string has prefix. Return nil if not found.
func (ss *TypeStringSlice) FindHasPrefix(prefix string) *TypeString {
for _, v := range ss.Obj {
sv := String(v)
if sv.HasPrefix(prefix) {
return sv
}
}
return nil
}
// FindHasSuffix find first string has prefix. Return nil if not found.
func (ss *TypeStringSlice) FindHasSuffix(suffix string) *TypeString {
for _, v := range ss.Obj {
sv := String(v)
if sv.HasSuffix(suffix) {
return sv
}
}
return nil
}
// FilterBy get new slice of string which pass the func
func (ss *TypeStringSlice) FilterBy(filter func(*TypeString) bool) *TypeStringSlice {
newSS := StringSlice(nil)
for _, v := range ss.Obj {
if filter(String(v)) {
newSS.Push(v)
}
}
return newSS
}
// FilterByPrefix get new slice of string which has prefix
func (ss *TypeStringSlice) FilterByPrefix(prefix string) *TypeStringSlice {
return ss.FilterBy(func(s *TypeString) bool {
return s.HasPrefix(prefix)
})
}
// FilterBySuffix get new slice of string which has prefix
func (ss *TypeStringSlice) FilterBySuffix(suffix string) *TypeStringSlice {
return ss.FilterBy(func(s *TypeString) bool {
return s.HasSuffix(suffix)
})
}
// Last return last element of slice in describe.Type. Panic if slice is empty.
func (ss *TypeStringSlice) Last() *TypeString {
if ss.Empty() {
PanicErr(ErrSliceIsEmpty)
}
return String(ss.Obj[len(ss.Obj)-1])
}
// Set set element at position of slice. Panic if index is invalid.
func (ss *TypeStringSlice) Set(i int, s string) *TypeStringSlice {
// cp := ss.Copy()
// cp.Obj[i] = s
// return cp
ss.Obj[i] = s
return ss
}
// Join join slice. retrun TypeString
func (ss *TypeStringSlice) Join(sep string) *TypeString {
return String(strings.Join(ss.Obj, sep))
}
// Map update elements by func, return a new slice
func (ss *TypeStringSlice) Map(mapper func(*TypeString) *TypeString) *TypeStringSlice {
cp := ss.Copy()
for i, v := range cp.Obj {
cp.Obj[i] = mapper(String(v)).Get()
}
return cp
}
// Delete delete elm at i
func (ss *TypeStringSlice) Delete(i int) *TypeStringSlice {
if i < ss.Len() {
ss.Obj = append(ss.Obj[:i], ss.Obj[i+1:]...)
}
return ss
}
// DeleteSame delete elm same with s
func (ss *TypeStringSlice) DeleteSame(s ...string) *TypeStringSlice {
for _, v := range s {
if i := ss.IndexOf(v); i != -1 {
ss.Delete(i)
}
}
return ss
}
// Include return if slice has one of elements
func (ss *TypeStringSlice) Include(elms ...string) bool {
for _, elm := range elms {
if ss.Has(elm) {
return true
}
}
return false
}
// NotInclude return if slice does not include any of elements
func (ss *TypeStringSlice) NotInclude(elms ...string) bool {
return !ss.Include(elms...)
}