forked from andeya/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
557 lines (536 loc) · 12.1 KB
/
string.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package goutil
import (
"bytes"
"regexp"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"unsafe"
)
// Indent inserts prefix at the beginning of each line
func Indent(text, prefix string) string {
if len(prefix) == 0 {
return text
}
has := strings.HasSuffix(text, "\n")
text = prefix + strings.Replace(text, "\n", "\n"+prefix, -1)
if has {
return text[:len(text)-len(prefix)]
}
return text
}
// BytesToString convert []byte type to string type.
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// StringToBytes convert string type to []byte type.
// NOTE: panic if modify the member value of the []byte.
func StringToBytes(s string) []byte {
sp := *(*[2]uintptr)(unsafe.Pointer(&s))
bp := [3]uintptr{sp[0], sp[1], sp[1]}
return *(*[]byte)(unsafe.Pointer(&bp))
}
// SnakeString converts the accepted string to a snake string (XxYy to xx_yy)
func SnakeString(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
for _, d := range StringToBytes(s) {
if d >= 'A' && d <= 'Z' {
if j {
data = append(data, '_')
j = false
}
} else if d != '_' {
j = true
}
data = append(data, d)
}
return strings.ToLower(BytesToString(data))
}
// CamelString converts the accepted string to a camel string (xx_yy to XxYy)
func CamelString(s string) string {
data := make([]byte, 0, len(s))
j := false
k := false
num := len(s) - 1
for i := 0; i <= num; i++ {
d := s[i]
if k == false && d >= 'A' && d <= 'Z' {
k = true
}
if d >= 'a' && d <= 'z' && (j || k == false) {
d = d - 32
j = false
k = true
}
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
j = true
continue
}
data = append(data, d)
}
return BytesToString(data[:])
}
// LintCamelString converts the accepted string to a camel string (xx_id to XxID)
// NOTE:
// support common initialisms
func LintCamelString(name string) string {
// Fast path for simple cases: "_" and all lowercase.
if name == "_" {
return "_"
}
runes := []rune(name)
var i int
for k, v := range runes {
if v != '_' {
i = k
runes[k] = unicode.ToUpper(v)
break
}
}
r := string(toInitialisms(runes[i:]))
return r
}
func toInitialisms(runes []rune) []rune {
// Split camelCase at any lower->upper transition, and split on underscores.
// Check each word for common initialisms.
w, i := 0, 0 // index of start of word, scan
for i+1 <= len(runes) {
eow := false // whether we hit the end of a word
if i+1 == len(runes) {
eow = true
} else if runes[i+1] == '_' {
// underscore; shift the remainder forward over any run of underscores
eow = true
n := 1
for i+n+1 < len(runes) && runes[i+n+1] == '_' {
n++
}
// Leave at most one underscore if the underscore is between two digits
if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) {
n--
}
copy(runes[i+1:], runes[i+n+1:])
runes = runes[:len(runes)-n]
} else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
// lower->non-lower
eow = true
}
i++
if !eow {
continue
}
// [w,i) is a word.
word := string(runes[w:i])
if u := strings.ToUpper(word); commonInitialisms[u] {
// Keep consistent case, which is lowercase only at the start.
if w == 0 && unicode.IsLower(runes[w]) {
u = strings.ToLower(u)
}
// All the common initialisms are ASCII,
// so we can replace the bytes exactly.
copy(runes[w:], []rune(u))
} else if w > 0 && strings.ToLower(word) == word {
// already all lowercase, and not the first word, so uppercase the first character.
runes[w] = unicode.ToUpper(runes[w])
}
w = i
}
return runes
}
// commonInitialisms is a set of common initialisms.
// Only add entries that are highly unlikely to be non-initialisms.
// For instance, "ID" is fine (Freudian code is rare), but "AND" is not.
var commonInitialisms = map[string]bool{
"ACL": true,
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JSON": true,
"LHS": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SQL": true,
"SSH": true,
"TCP": true,
"TLS": true,
"TTL": true,
"UDP": true,
"UI": true,
"UID": true,
"UUID": true,
"URI": true,
"URL": true,
"UTF8": true,
"VM": true,
"XML": true,
"XMPP": true,
"XSRF": true,
"XSS": true,
}
var htmlEntityRegexp = regexp.MustCompile(`&#([0-9a-zA-Z]+);*`)
// HTMLEntityToUTF8 converts HTML Unicode to UTF-8.
// e.g.: HTMLEntityToUTF8(`{"info":[["color","ᕸᖹ⁐c;eff;⁐"]]}`, 16)
// => `{"info":[["color","咖啡色|绿色"]]}`
func HTMLEntityToUTF8(str string, base int) string {
a := htmlEntityRegexp.FindAllStringSubmatch(str, -1)
if len(a) == 0 {
return str
}
oldnew := make([]string, 0, len(a)*2)
for _, s := range a {
if i, err := strconv.ParseInt(s[1], base, 32); err == nil {
oldnew = append(oldnew, s[0], string(i))
}
}
r := strings.NewReplacer(oldnew...)
return r.Replace(str)
}
// CodePointToUTF8 converts Unicode Code Point to UTF-8.
// e.g.: CodePointToUTF8(`{"info":[["color","\u5496\u5561\u8272\u7c\u7eff\u8272"]]}`, 16)
// => `{"info":[["color","咖啡色|绿色"]]}`
func CodePointToUTF8(str string, base int) string {
i := 0
if strings.Index(str, `\u`) > 0 {
i = 1
}
strSlice := strings.Split(str, `\u`)
last := len(strSlice) - 1
if len(strSlice[last]) > 4 {
strSlice = append(strSlice, string(strSlice[last][4:]))
strSlice[last] = string(strSlice[last][:4])
}
for ; i <= last; i++ {
if x, err := strconv.ParseInt(strSlice[i], base, 32); err == nil {
strSlice[i] = string(x)
}
}
return strings.Join(strSlice, "")
}
var spaceReplacer = strings.NewReplacer(
" ", " ",
"\n\n", "\n",
"\r\r", "\r",
"\t\t", "\t",
"\r\n\r\n", "\r\n",
" \n", "\n",
"\t\n", "\n",
" \t", "\t",
"\t ", "\t",
"\v\v", "\v",
"\f\f", "\f",
string(0x85)+string(0x85),
string(0x85),
string(0xA0)+string(0xA0),
string(0xA0),
)
// SpaceInOne combines multiple consecutive space characters into one.
func SpaceInOne(s string) string {
var old string
for old != s {
old = s
s = spaceReplacer.Replace(s)
}
return s
}
// StringMarshalJSON converts the string to JSON byte stream.
func StringMarshalJSON(s string, escapeHTML bool) []byte {
a := StringToBytes(s)
var buf = bytes.NewBuffer(make([]byte, 0, 64))
buf.WriteByte('"')
start := 0
for i := 0; i < len(a); {
if b := a[i]; b < utf8.RuneSelf {
if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) {
i++
continue
}
if start < i {
buf.Write(a[start:i])
}
switch b {
case '\\', '"':
buf.WriteByte('\\')
buf.WriteByte(b)
case '\n':
buf.WriteByte('\\')
buf.WriteByte('n')
case '\r':
buf.WriteByte('\\')
buf.WriteByte('r')
case '\t':
buf.WriteByte('\\')
buf.WriteByte('t')
default:
// This encodes bytes < 0x20 except for \t, \n and \r.
// If escapeHTML is set, it also escapes <, >, and &
// because they can lead to security holes when
// user-controlled strings are rendered into JSON
// and served to some browsers.
buf.WriteString(`\u00`)
buf.WriteByte(hexSet[b>>4])
buf.WriteByte(hexSet[b&0xF])
}
i++
start = i
continue
}
c, size := utf8.DecodeRune(a[i:])
if c == utf8.RuneError && size == 1 {
if start < i {
buf.Write(a[start:i])
}
buf.WriteString(`\ufffd`)
i += size
start = i
continue
}
// U+2028 is LINE SEPARATOR.
// U+2029 is PARAGRAPH SEPARATOR.
// They are both technically valid characters in JSON strings,
// but don't work in JSONP, which has to be evaluated as JavaScript,
// and can lead to security holes there. It is valid JSON to
// escape them, so we do so unconditionally.
// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
if c == '\u2028' || c == '\u2029' {
if start < i {
buf.Write(a[start:i])
}
buf.WriteString(`\u202`)
buf.WriteByte(hexSet[c&0xF])
i += size
start = i
continue
}
i += size
}
if start < len(a) {
buf.Write(a[start:])
}
buf.WriteByte('"')
return buf.Bytes()
}
var hexSet = "0123456789abcdef"
// safeSet holds the value true if the ASCII character with the given array
// position can be represented inside a JSON string without any further
// escaping.
//
// All values are true except for the ASCII control characters (0-31), the
// double quote ("), and the backslash character ("\").
var safeSet = [utf8.RuneSelf]bool{
' ': true,
'!': true,
'"': false,
'#': true,
'$': true,
'%': true,
'&': true,
'\'': true,
'(': true,
')': true,
'*': true,
'+': true,
',': true,
'-': true,
'.': true,
'/': true,
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
':': true,
';': true,
'<': true,
'=': true,
'>': true,
'?': true,
'@': true,
'A': true,
'B': true,
'C': true,
'D': true,
'E': true,
'F': true,
'G': true,
'H': true,
'I': true,
'J': true,
'K': true,
'L': true,
'M': true,
'N': true,
'O': true,
'P': true,
'Q': true,
'R': true,
'S': true,
'T': true,
'U': true,
'V': true,
'W': true,
'X': true,
'Y': true,
'Z': true,
'[': true,
'\\': false,
']': true,
'^': true,
'_': true,
'`': true,
'a': true,
'b': true,
'c': true,
'd': true,
'e': true,
'f': true,
'g': true,
'h': true,
'i': true,
'j': true,
'k': true,
'l': true,
'm': true,
'n': true,
'o': true,
'p': true,
'q': true,
'r': true,
's': true,
't': true,
'u': true,
'v': true,
'w': true,
'x': true,
'y': true,
'z': true,
'{': true,
'|': true,
'}': true,
'~': true,
'\u007f': true,
}
// htmlSafeSet holds the value true if the ASCII character with the given
// array position can be safely represented inside a JSON string, embedded
// inside of HTML <script> tags, without any additional escaping.
//
// All values are true except for the ASCII control characters (0-31), the
// double quote ("), the backslash character ("\"), HTML opening and closing
// tags ("<" and ">"), and the ampersand ("&").
var htmlSafeSet = [utf8.RuneSelf]bool{
' ': true,
'!': true,
'"': false,
'#': true,
'$': true,
'%': true,
'&': false,
'\'': true,
'(': true,
')': true,
'*': true,
'+': true,
',': true,
'-': true,
'.': true,
'/': true,
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
':': true,
';': true,
'<': false,
'=': true,
'>': false,
'?': true,
'@': true,
'A': true,
'B': true,
'C': true,
'D': true,
'E': true,
'F': true,
'G': true,
'H': true,
'I': true,
'J': true,
'K': true,
'L': true,
'M': true,
'N': true,
'O': true,
'P': true,
'Q': true,
'R': true,
'S': true,
'T': true,
'U': true,
'V': true,
'W': true,
'X': true,
'Y': true,
'Z': true,
'[': true,
'\\': false,
']': true,
'^': true,
'_': true,
'`': true,
'a': true,
'b': true,
'c': true,
'd': true,
'e': true,
'f': true,
'g': true,
'h': true,
'i': true,
'j': true,
'k': true,
'l': true,
'm': true,
'n': true,
'o': true,
'p': true,
'q': true,
'r': true,
's': true,
't': true,
'u': true,
'v': true,
'w': true,
'x': true,
'y': true,
'z': true,
'{': true,
'|': true,
'}': true,
'~': true,
'\u007f': true,
}