forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
198 lines (161 loc) · 3.75 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
package stringx
import (
"errors"
"unicode"
"github.com/wuhc1010/go-zero/core/lang"
)
var (
// ErrInvalidStartPosition is an error that indicates the start position is invalid.
ErrInvalidStartPosition = errors.New("start position is invalid")
// ErrInvalidStopPosition is an error that indicates the stop position is invalid.
ErrInvalidStopPosition = errors.New("stop position is invalid")
)
// Contains checks if str is in list.
func Contains(list []string, str string) bool {
for _, each := range list {
if each == str {
return true
}
}
return false
}
// Filter filters chars from s with given filter function.
func Filter(s string, filter func(r rune) bool) string {
var n int
chars := []rune(s)
for i, x := range chars {
if n < i {
chars[n] = x
}
if !filter(x) {
n++
}
}
return string(chars[:n])
}
// FirstN returns first n runes from s.
func FirstN(s string, n int, ellipsis ...string) string {
var i int
for j := range s {
if i == n {
ret := s[:j]
for _, each := range ellipsis {
ret += each
}
return ret
}
i++
}
return s
}
// HasEmpty checks if there are empty strings in args.
func HasEmpty(args ...string) bool {
for _, arg := range args {
if len(arg) == 0 {
return true
}
}
return false
}
// Join joins any number of elements into a single string, separating them with given sep.
// Empty elements are ignored. However, if the argument list is empty or all its elements are empty,
// Join returns an empty string.
func Join(sep byte, elem ...string) string {
var size int
for _, e := range elem {
size += len(e)
}
if size == 0 {
return ""
}
buf := make([]byte, 0, size+len(elem)-1)
for _, e := range elem {
if len(e) == 0 {
continue
}
if len(buf) > 0 {
buf = append(buf, sep)
}
buf = append(buf, e...)
}
return string(buf)
}
// NotEmpty checks if all strings are not empty in args.
func NotEmpty(args ...string) bool {
return !HasEmpty(args...)
}
// Remove removes given strs from strings.
func Remove(strings []string, strs ...string) []string {
out := append([]string(nil), strings...)
for _, str := range strs {
var n int
for _, v := range out {
if v != str {
out[n] = v
n++
}
}
out = out[:n]
}
return out
}
// Reverse reverses s.
func Reverse(s string) string {
runes := []rune(s)
for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
runes[from], runes[to] = runes[to], runes[from]
}
return string(runes)
}
// Substr returns runes between start and stop [start, stop)
// regardless of the chars are ascii or utf8.
func Substr(str string, start, stop int) (string, error) {
rs := []rune(str)
length := len(rs)
if start < 0 || start > length {
return "", ErrInvalidStartPosition
}
if stop < 0 || stop > length {
return "", ErrInvalidStopPosition
}
return string(rs[start:stop]), nil
}
// TakeOne returns valid string if not empty or later one.
func TakeOne(valid, or string) string {
if len(valid) > 0 {
return valid
}
return or
}
// TakeWithPriority returns the first not empty result from fns.
func TakeWithPriority(fns ...func() string) string {
for _, fn := range fns {
val := fn()
if len(val) > 0 {
return val
}
}
return ""
}
// ToCamelCase returns the string that converts the first letter to lowercase.
func ToCamelCase(s string) string {
for i, v := range s {
return string(unicode.ToLower(v)) + s[i+1:]
}
return ""
}
// Union merges the strings in first and second.
func Union(first, second []string) []string {
set := make(map[string]lang.PlaceholderType)
for _, each := range first {
set[each] = lang.Placeholder
}
for _, each := range second {
set[each] = lang.Placeholder
}
merged := make([]string, 0, len(set))
for k := range set {
merged = append(merged, k)
}
return merged
}