-
Notifications
You must be signed in to change notification settings - Fork 8
/
texts.go
288 lines (263 loc) · 5.35 KB
/
texts.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
package texts
import (
"strings"
"unicode"
"unicode/utf8"
"unsafe"
)
// Join is a fast-version of `strings.Join` function.
func Join(sep string, a ...string) string {
switch len(a) {
case 0:
return ""
case 1:
return a[0]
case 2:
// Special case for common small values.
// Remove if golang.org/issue/6714 is fixed
if sep == "" {
return a[0] + a[1]
}
return a[0] + sep + a[1]
case 3:
// Special case for common small values.
// Remove if golang.org/issue/6714 is fixed
if sep == "" {
return a[0] + a[1] + a[2]
}
return a[0] + sep + a[1] + sep + a[2]
}
var n int
if sep != "" {
n = len(sep) * (len(a) - 1)
}
for i := 0; i < len(a); i++ {
n += len(a[i])
}
buf := make([]byte, n)
bp := copy(buf, a[0])
if sep == "" {
for _, s := range a[1:] {
bp += copy(buf[bp:], s)
}
} else {
for _, s := range a[1:] {
bp += copy(buf[bp:], sep)
bp += copy(buf[bp:], s)
}
}
return *(*string)(unsafe.Pointer(&buf))
//return string(buf)
}
// JoinRepeat if sep = "", should use strings.Repeat instead
func JoinRepeat(s, sep string, count int) string {
switch count {
case 0:
return ""
case 1:
return s
case 2:
return s + sep + s
case 3:
return s + sep + s + sep + s
}
n := (len(s)+len(sep))*(count-1) + len(s)
buf := make([]byte, n)
bp := copy(buf, s)
for i := 1; i < count; i++ {
bp += copy(buf[bp:], sep)
bp += copy(buf[bp:], s)
}
return *(*string)(unsafe.Pointer(&buf))
}
func Concat(a ...string) string {
return Join("", a...)
}
func Contains(a []string, s string) bool {
for _, v := range a {
if v == s {
return true
}
}
return false
}
func PadLeft(s string, c byte, w int) string {
count := w - len(s)
if count > 0 {
return strings.Repeat(string(c), count) + s
}
return s
}
func PadRight(s string, c byte, w int) string {
count := w - len(s)
if count > 0 {
return s + strings.Repeat(string(c), count)
}
return s
}
func PadCenter(s string, c byte, w int) string {
count := w - len(s)
if count > 0 {
left := count / 2
return strings.Repeat(string(c), left) + s + strings.Repeat(string(c), count-left)
}
return s
}
func CutLeft(s string, w int) string {
if len(s) > w {
return s[len(s)-w:]
}
return s
}
func CutRight(s string, w int) string {
if len(s) > w {
return s[:w]
}
return s
}
type NameStyle int
const (
Pascal NameStyle = iota
Camel
Upper
Lower
)
func Rename(name string, style NameStyle) string {
words := splitName(name)
switch style {
case Lower:
for i := 0; i < len(words); i++ {
words[i] = strings.ToLower(words[i])
}
return strings.Join(words, "_")
case Upper:
for i := 0; i < len(words); i++ {
words[i] = strings.ToUpper(words[i])
}
return strings.Join(words, "_")
case Pascal:
for i := 0; i < len(words); i++ {
words[i] = strings.ToUpper(words[i][:1]) + strings.ToLower(words[i][1:])
}
return Concat(words...)
case Camel:
words[0] = strings.ToLower(words[0])
for i := 1; i < len(words); i++ {
words[i] = strings.ToUpper(words[i][:1]) + strings.ToLower(words[i][1:])
}
return Concat(words...)
default:
return name
}
}
func IsUpper(s string) bool {
for i := 0; i < len(s); i++ {
b := s[i]
if b < 'A' || b > 'Z' {
return false
}
}
return true
}
func IsLower(s string) bool {
for i := 0; i < len(s); i++ {
b := s[i]
if b < 'a' || b > 'z' {
return false
}
}
return true
}
func splitName(name string) (words []string) {
var (
start = 0
last byte
)
for i := 0; i < len(name); i++ {
b := name[i]
switch {
case b == '_':
words = append(words, name[start:i])
start, last = i+1, 0
continue
case b >= 'A' && b <= 'Z': // upper char
if last != 0 && (last < 'A' || last > 'Z') { // last is lower char
words = append(words, name[start:i])
start = i
}
default:
if (last >= 'A' && last <= 'Z') && start != i-1 { // last is upper char
words = append(words, name[start:i-1])
start = i - 1
}
}
last = b
}
words = append(words, name[start:])
return
}
// CompareFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under Unicode case-folding.
func CompareFold(s, t string) int {
for s != "" && t != "" {
// Extract first rune from each string.
var sr, tr rune
if s[0] < utf8.RuneSelf {
sr, s = rune(s[0]), s[1:]
} else {
r, size := utf8.DecodeRuneInString(s)
sr, s = r, s[size:]
}
if t[0] < utf8.RuneSelf {
tr, t = rune(t[0]), t[1:]
} else {
r, size := utf8.DecodeRuneInString(t)
tr, t = r, t[size:]
}
// If they match, keep going; if not, return false.
// Easy case.
if tr == sr {
continue
}
// Make sr < tr to simplify what follows.
result := 1
if tr < sr {
result = -result
tr, sr = sr, tr
}
// Fast check for ASCII.
if tr < utf8.RuneSelf && 'A' <= sr && sr <= 'Z' {
// ASCII, and sr is upper case. tr must be lower case.
if lower := sr + 'a' - 'A'; tr == lower {
continue
} else if tr < lower {
return result
} else if tr > lower {
return -result
}
}
// General case. SimpleFold(x) returns the next equivalent rune > x
// or wraps around to smaller values.
r := unicode.SimpleFold(sr)
for r != sr && r < tr {
r = unicode.SimpleFold(r)
}
if r == tr {
continue
}
if tr < r {
return result
}
if tr > r {
return -result
}
}
// One string is empty. Are both?
if s == "" && t != "" {
return -1
}
if s != "" && t == "" {
return 1
}
return 0
}