Repository navigation
Expand file tree
/
Copy pathgstr.go
More file actions
687 lines (626 loc) · 17.2 KB
/
Copy pathgstr.go
File metadata and controls
687 lines (626 loc) · 17.2 KB
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gstr provides functions for string handling.
package gstr
import (
"bytes"
"fmt"
"math"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"github.com/gogf/gf/internal/utils"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/grand"
)
// Replace returns a copy of the string <origin>
// in which string <search> replaced by <replace> case-sensitively.
func Replace(origin, search, replace string, count ...int) string {
n := -1
if len(count) > 0 {
n = count[0]
}
return strings.Replace(origin, search, replace, n)
}
// Replace returns a copy of the string <origin>
// in which string <search> replaced by <replace> case-insensitively.
func ReplaceI(origin, search, replace string, count ...int) string {
n := -1
if len(count) > 0 {
n = count[0]
}
if n == 0 {
return origin
}
length := len(search)
searchLower := strings.ToLower(search)
for {
originLower := strings.ToLower(origin)
if pos := strings.Index(originLower, searchLower); pos != -1 {
origin = origin[:pos] + replace + origin[pos+length:]
if n--; n == 0 {
break
}
} else {
break
}
}
return origin
}
// Count counts the number of <substr> appears in <s>.
// It returns 0 if no <substr> found in <s>.
func Count(s, substr string) int {
return strings.Count(s, substr)
}
// CountI counts the number of <substr> appears in <s>, case-insensitively.
// It returns 0 if no <substr> found in <s>.
func CountI(s, substr string) int {
return strings.Count(ToLower(s), ToLower(substr))
}
// ReplaceByArray returns a copy of <origin>,
// which is replaced by a slice in order, case-sensitively.
func ReplaceByArray(origin string, array []string) string {
for i := 0; i < len(array); i += 2 {
if i+1 >= len(array) {
break
}
origin = Replace(origin, array[i], array[i+1])
}
return origin
}
// ReplaceIByArray returns a copy of <origin>,
// which is replaced by a slice in order, case-insensitively.
func ReplaceIByArray(origin string, array []string) string {
for i := 0; i < len(array); i += 2 {
if i+1 >= len(array) {
break
}
origin = ReplaceI(origin, array[i], array[i+1])
}
return origin
}
// ReplaceByMap returns a copy of <origin>,
// which is replaced by a map in unordered way, case-sensitively.
func ReplaceByMap(origin string, replaces map[string]string) string {
return utils.ReplaceByMap(origin, replaces)
}
// ReplaceIByMap returns a copy of <origin>,
// which is replaced by a map in unordered way, case-insensitively.
func ReplaceIByMap(origin string, replaces map[string]string) string {
for k, v := range replaces {
origin = ReplaceI(origin, k, v)
}
return origin
}
// ToLower returns a copy of the string s with all Unicode letters mapped to their lower case.
func ToLower(s string) string {
return strings.ToLower(s)
}
// ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case.
func ToUpper(s string) string {
return strings.ToUpper(s)
}
// UcFirst returns a copy of the string s with the first letter mapped to its upper case.
func UcFirst(s string) string {
return utils.UcFirst(s)
}
// LcFirst returns a copy of the string s with the first letter mapped to its lower case.
func LcFirst(s string) string {
if len(s) == 0 {
return s
}
if IsLetterUpper(s[0]) {
return string(s[0]+32) + s[1:]
}
return s
}
// UcWords uppercase the first character of each word in a string.
func UcWords(str string) string {
return strings.Title(str)
}
// IsLetterLower tests whether the given byte b is in lower case.
func IsLetterLower(b byte) bool {
return utils.IsLetterLower(b)
}
// IsLetterUpper tests whether the given byte b is in upper case.
func IsLetterUpper(b byte) bool {
return utils.IsLetterUpper(b)
}
// IsNumeric tests whether the given string s is numeric.
func IsNumeric(s string) bool {
return utils.IsNumeric(s)
}
// SubStr returns a portion of string <str> specified by the <start> and <length> parameters.
func SubStr(str string, start int, length ...int) (substr string) {
lth := len(str)
// Simple border checks.
if start < 0 {
start = 0
}
if start >= lth {
start = lth
}
end := lth
if len(length) > 0 {
end = start + length[0]
if end < start {
end = lth
}
}
if end > lth {
end = lth
}
return str[start:end]
}
// SubStrRune returns a portion of string <str> specified by the <start> and <length> parameters.
// SubStrRune considers parameter <str> as unicode string.
func SubStrRune(str string, start int, length ...int) (substr string) {
// Converting to []rune to support unicode.
rs := []rune(str)
lth := len(rs)
// Simple border checks.
if start < 0 {
start = 0
}
if start >= lth {
start = lth
}
end := lth
if len(length) > 0 {
end = start + length[0]
if end < start {
end = lth
}
}
if end > lth {
end = lth
}
return string(rs[start:end])
}
// StrLimit returns a portion of string <str> specified by <length> parameters, if the length
// of <str> is greater than <length>, then the <suffix> will be appended to the result string.
func StrLimit(str string, length int, suffix ...string) string {
if len(str) < length {
return str
}
addStr := "..."
if len(suffix) > 0 {
addStr = suffix[0]
}
return str[0:length] + addStr
}
// StrLimitRune returns a portion of string <str> specified by <length> parameters, if the length
// of <str> is greater than <length>, then the <suffix> will be appended to the result string.
// StrLimitRune considers parameter <str> as unicode string.
func StrLimitRune(str string, length int, suffix ...string) string {
rs := []rune(str)
if len(rs) < length {
return str
}
addStr := "..."
if len(suffix) > 0 {
addStr = suffix[0]
}
return string(rs[0:length]) + addStr
}
// Reverse returns a string which is the reverse of <str>.
func Reverse(str string) string {
runes := []rune(str)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
// NumberFormat formats a number with grouped thousands.
// <decimals>: Sets the number of decimal points.
// <decPoint>: Sets the separator for the decimal point.
// <thousandsSep>: Sets the thousands separator.
// See http://php.net/manual/en/function.number-format.php.
func NumberFormat(number float64, decimals int, decPoint, thousandsSep string) string {
neg := false
if number < 0 {
number = -number
neg = true
}
// Will round off
str := fmt.Sprintf("%."+strconv.Itoa(decimals)+"F", number)
prefix, suffix := "", ""
if decimals > 0 {
prefix = str[:len(str)-(decimals+1)]
suffix = str[len(str)-decimals:]
} else {
prefix = str
}
sep := []byte(thousandsSep)
n, l1, l2 := 0, len(prefix), len(sep)
// thousands sep num
c := (l1 - 1) / 3
tmp := make([]byte, l2*c+l1)
pos := len(tmp) - 1
for i := l1 - 1; i >= 0; i, n, pos = i-1, n+1, pos-1 {
if l2 > 0 && n > 0 && n%3 == 0 {
for j := range sep {
tmp[pos] = sep[l2-j-1]
pos--
}
}
tmp[pos] = prefix[i]
}
s := string(tmp)
if decimals > 0 {
s += decPoint + suffix
}
if neg {
s = "-" + s
}
return s
}
// ChunkSplit splits a string into smaller chunks.
// Can be used to split a string into smaller chunks which is useful for
// e.g. converting BASE64 string output to match RFC 2045 semantics.
// It inserts end every chunkLen characters.
// It considers parameter <body> and <end> as unicode string.
func ChunkSplit(body string, chunkLen int, end string) string {
if end == "" {
end = "\r\n"
}
runes, endRunes := []rune(body), []rune(end)
l := len(runes)
if l <= 1 || l < chunkLen {
return body + e
8000
nd
}
ns := make([]rune, 0, len(runes)+len(endRunes))
for i := 0; i < l; i += chunkLen {
if i+chunkLen > l {
ns = append(ns, runes[i:]...)
} else {
ns = append(ns, runes[i:i+chunkLen]...)
}
ns = append(ns, endRunes...)
}
return string(ns)
}
// Compare returns an integer comparing two strings lexicographically.
// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
func Compare(a, b string) int {
return strings.Compare(a, b)
}
// Equal reports whether <a> and <b>, interpreted as UTF-8 strings,
// are equal under Unicode case-folding, case-insensitively.
func Equal(a, b string) bool {
return strings.EqualFold(a, b)
}
// Fields returns the words used in a string as slice.
func Fields(str string) []string {
return strings.Fields(str)
}
// HasPrefix tests whether the string s begins with prefix.
func HasPrefix(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}
// HasSuffix tests whether the string s ends with suffix.
func HasSuffix(s, suffix string) bool {
return strings.HasSuffix(s, suffix)
}
// CountWords returns information about words' count used in a string.
// It considers parameter <str> as unicode string.
func CountWords(str string) map[string]int {
m := make(map[string]int)
buffer := bytes.NewBuffer(nil)
for _, r := range []rune(str) {
if unicode.IsSpace(r) {
if buffer.Len() > 0 {
m[buffer.String()]++
buffer.Reset()
}
} else {
buffer.WriteRune(r)
}
}
if buffer.Len() > 0 {
m[buffer.String()]++
}
return m
}
// CountChars returns information about chars' count used in a string.
// It considers parameter <str> as unicode string.
func CountChars(str string, noSpace ...bool) map[string]int {
m := make(map[string]int)
countSpace := true
if len(noSpace) > 0 && noSpace[0] {
countSpace = false
}
for _, r := range []rune(str) {
if !countSpace && unicode.IsSpace(r) {
continue
}
m[string(r)]++
}
return m
}
// WordWrap wraps a string to a given number of characters.
// TODO: Enable cut parameter, see http://php.net/manual/en/function.wordwrap.php.
func WordWrap(str string, width int, br string) string {
if br == "" {
br = "\n"
}
var (
current int
wordBuf, spaceBuf bytes.Buffer
init = make([]byte, 0, len(str))
buf = bytes.NewBuffer(init)
)
for _, char := range []rune(str) {
if char == '\n' {
if wordBuf.Len() == 0 {
if current+spaceBuf.Len() > width {
current = 0
} else {
current += spaceBuf.Len()
spaceBuf.WriteTo(buf)
}
spaceBuf.Reset()
} else {
current += spaceBuf.Len() + wordBuf.Len()
spaceBuf.WriteTo(buf)
spaceBuf.Reset()
wordBuf.WriteTo(buf)
wordBuf.Reset()
}
buf.WriteRune(char)
current = 0
} else if unicode.IsSpace(char) {
if spaceBuf.Len() == 0 || wordBuf.Len() > 0 {
current += spaceBuf.Len() + wordBuf.Len()
spaceBuf.WriteTo(buf)
spaceBuf.Reset()
wordBuf.WriteTo(buf)
wordBuf.Reset()
}
spaceBuf.WriteRune(char)
} else {
wordBuf.WriteRune(char)
if current+spaceBuf.Len()+wordBuf.Len() > width && wordBuf.Len() < width {
buf.WriteString(br)
current = 0
spaceBuf.Reset()
}
}
}
if wordBuf.Len() == 0 {
if current+spaceBuf.Len() <= width {
spaceBuf.WriteTo(buf)
}
} else {
spaceBuf.WriteTo(buf)
wordBuf.WriteTo(buf)
}
return buf.String()
}
// RuneLen returns string length of unicode.
// Deprecated, use LenRune instead.
func RuneLen(str string) int {
return LenRune(str)
}
// LenRune returns string length of unicode.
func LenRune(str string) int {
return utf8.RuneCountInString(str)
}
// Repeat returns a new string consisting of multiplier copies of the string input.
func Repeat(input string, multiplier int) string {
return strings.Repeat(input, multiplier)
}
// Str returns part of <haystack> string starting from and including
// the first occurrence of <needle> to the end of <haystack>.
// See http://php.net/manual/en/function.strstr.php.
func Str(haystack string, needle string) string {
if needle == "" {
return ""
}
idx := strings.Index(haystack, needle)
if idx == -1 {
return ""
}
return haystack[idx+len([]byte(needle))-1:]
}
// Shuffle randomly shuffles a string.
// It considers parameter <str> as unicode string.
func Shuffle(str string) string {
runes := []rune(str)
s := make([]rune, len(runes))
for i, v := range grand.Perm(len(runes)) {
s[i] = runes[v]
}
return string(s)
}
// Split splits string <str> by a string <delimiter>, to an array.
func Split(str, delimiter string) []string {
return strings.Split(str, delimiter)
}
// SplitAndTrim splits string <str> by a string <delimiter> to an array,
// and calls Trim to every element of this array. It ignores the elements
// which are empty after Trim.
func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
array := make([]string, 0)
for _, v := range strings.Split(str, delimiter) {
v = Trim(v, characterMask...)
if v != "" {
array = append(array, v)
}
}
return array
}
// SplitAndTrimSpace splits string <str> by a string <delimiter> to an array,
// and calls TrimSpace to every element of this array.
// Deprecated.
func SplitAndTrimSpace(str, delimiter string) []string {
array := make([]string, 0)
for _, v := range strings.Split(str, delimiter) {
v = strings.TrimSpace(v)
if v != "" {
array = append(array, v)
}
}
return array
}
// Join concatenates the elements of <array> to create a single string. The separator string
// <sep> is placed between elements in the resulting string.
func Join(array []string, sep string) string {
return strings.Join(array, sep)
}
// JoinAny concatenates the elements of <array> to create a single string. The separator string
// <sep> is placed between elements in the resulting string.
//
// The parameter <array> can be any type of slice, which be converted to string array.
func JoinAny(array interface{}, sep string) string {
return strings.Join(gconv.Strings(array), sep)
}
// Explode splits string <str> by a string <delimiter>, to an array.
// See http://php.net/manual/en/function.explode.php.
func Explode(delimiter, str string) []string {
return Split(str, delimiter)
}
// Implode joins array elements <pieces> with a string <glue>.
// http://php.net/manual/en/function.implode.php
func Implode(glue string, pieces []string) string {
return strings.Join(pieces, glue)
}
// Chr return the ascii string of a number(0-255).
func Chr(ascii int) string {
return string([]byte{byte(ascii % 256)})
}
// Ord converts the first byte of a string to a value between 0 and 255.
func Ord(char string) int {
return int(char[0])
}
// HideStr replaces part of the the string <str> to <hide> by <percentage> from the <middle>.
// It considers parameter <str> as unicode string.
func HideStr(str string, percent int, hide string) string {
array := strings.Split(str, "@")
if len(array) > 1 {
str = array[0]
}
var (
rs = []rune(str)
length = len(rs)
mid = math.Floor(float64(length / 2))
hideLen = int(math.Floor(float64(length) * (float64(percent) / 100)))
start = int(mid - math.Floor(float64(hideLen)/2))
hideStr = []rune("")
hideRune = []rune(hide)
)
for i := 0; i < hideLen; i++ {
hideStr = append(hideStr, hideRune...)
}
buffer := bytes.NewBuffer(nil)
buffer.WriteString(string(rs[0:start]))
buffer.WriteString(string(hideStr))
buffer.WriteString(string(rs[start+hideLen:]))
if len(array) > 1 {
buffer.WriteString("@" + array[1])
}
return buffer.String()
}
// Nl2Br inserts HTML line breaks(<br>|<br />) before all newlines in a string:
// \n\r, \r\n, \r, \n.
// It considers parameter <str> as unicode string.
func Nl2Br(str string, isXhtml ...bool) string {
r, n, runes := '\r', '\n', []rune(str)
var br []byte
if len(isXhtml) > 0 && isXhtml[0] {
br = []byte("<br />")
} else {
br = []byte("<br>")
}
skip := false
length := len(runes)
var buf bytes.Buffer
for i, v := range runes {
if skip {
skip = false
continue
}
switch v {
case n, r:
if (i+1 < length) && (v == r && runes[i+1] == n) || (v == n && runes[i+1] == r) {
buf.Write(br)
skip = true
continue
}
buf.Write(br)
default:
buf.WriteRune(v)
}
}
return buf.String()
}
// AddSlashes quotes chars('"\) with slashes.
func AddSlashes(str string) string {
var buf bytes.Buffer
for _, char := range str {
switch char {
case '\'', '"', '\\':
buf.WriteRune('\\')
}
buf.WriteRune(char)
}
return buf.String()
}
// StripSlashes un-quotes a quoted string by AddSlashes.
func StripSlashes(str string) string {
var buf bytes.Buffer
l, skip := len(str), false
for i, char := range str {
if skip {
skip = false
} else if char == '\\' {
if i+1 < l && str[i+1] == '\\' {
skip = true
}
continue
}
buf.WriteRune(char)
}
return buf.String()
}
// QuoteMeta returns a version of str with a backslash character (\)
// before every character that is among: .\+*?[^]($)
func QuoteMeta(str string, chars ...string) string {
var buf bytes.Buffer
for _, char := range str {
if len(chars) > 0 {
for _, c := range chars[0] {
if c == char {
buf.WriteRune('\\')
break
}
}
} else {
switch char {
case '.', '+', '\\', '(', '$', ')', '[', '^', ']', '*', '?':
buf.WriteRune('\\')
}
}
buf.WriteRune(char)
}
return buf.String()
}
// SearchArray searches string <s> in string slice <a> case-sensitively,
// returns its index in <a>.
// If <s> is not found in <a>, it returns -1.
func SearchArray(a []string, s string) int {
for i, v := range a {
if s == v {
return i
}
}
return -1
}
// InArray checks whether string <s> in slice <a>.
func InArray(a []string, s string) bool {
return SearchArray(a, s) != -1
}