-
-
Notifications
You must be signed in to change notification settings - Fork 949
Expand file tree
/
Copy pathstring.go
More file actions
539 lines (484 loc) · 16.1 KB
/
Copy pathstring.go
File metadata and controls
539 lines (484 loc) · 16.1 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
package lo
import (
"math"
"strings"
"sync"
"unicode"
"unicode/utf8"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/samber/lo/internal/xrand"
)
var (
//nolint:revive
LowerCaseLettersCharset = []rune("abcdefghijklmnopqrstuvwxyz")
UpperCaseLettersCharset = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
LettersCharset = append(LowerCaseLettersCharset, UpperCaseLettersCharset...)
NumbersCharset = []rune("0123456789")
AlphanumericCharset = append(LettersCharset, NumbersCharset...)
SpecialCharset = []rune("!@#$%^&*()_+-=[]{}|;':\",./<>?")
AllCharset = append(AlphanumericCharset, SpecialCharset...)
maximumCapacity = math.MaxInt>>1 + 1
)
// Constructing a Caser is far more expensive than using one, and a Caser is not safe for
// concurrent use, so they cannot be plain package-level singletons.
//
// English gets dedicated pools so the default (non-WithLanguage) functions pay zero sync.Map
// overhead. Other languages share pools lazily created in titleCaserPools / lowerCaserPools.
var (
englishTitleCaserPool = sync.Pool{New: func() any { c := cases.Title(language.English); return &c }}
englishLowerCaserPool = sync.Pool{New: func() any { c := cases.Lower(language.English); return &c }}
titleCaserPools sync.Map // map[string]*sync.Pool (BCP 47 tag → pool of *cases.Caser)
lowerCaserPools sync.Map // map[string]*sync.Pool (BCP 47 tag → pool of *cases.Caser)
)
// acquireTitleCaser returns a pool and a ready-to-use title Caser for the given language tag.
// Caller must return the Caser: defer pool.Put(c).
// Load-before-LoadOrStore avoids allocating a throwaway *sync.Pool on every call once the
// entry is warm (LoadOrStore evaluates its value argument unconditionally).
func acquireTitleCaser(tag language.Tag) (*sync.Pool, *cases.Caser) {
key := tag.String()
if v, ok := titleCaserPools.Load(key); ok {
pool, _ := v.(*sync.Pool) // always *sync.Pool: we only ever store that type
c, _ := pool.Get().(*cases.Caser) // Pool.New always returns *cases.Caser
return pool, c
}
p := &sync.Pool{New: func() any { c := cases.Title(tag); return &c }}
actual, _ := titleCaserPools.LoadOrStore(key, p)
pool, _ := actual.(*sync.Pool) // always *sync.Pool: we only ever store that type
c, _ := pool.Get().(*cases.Caser) // Pool.New always returns *cases.Caser
return pool, c
}
// acquireLowerCaser returns a pool and a ready-to-use lower Caser for the given language tag.
// Caller must return the Caser: defer pool.Put(c).
// Same Load-before-LoadOrStore pattern as acquireTitleCaser.
func acquireLowerCaser(tag language.Tag) (*sync.Pool, *cases.Caser) {
key := tag.String()
if v, ok := lowerCaserPools.Load(key); ok {
pool, _ := v.(*sync.Pool) // always *sync.Pool: we only ever store that type
c, _ := pool.Get().(*cases.Caser) // Pool.New always returns *cases.Caser
return pool, c
}
p := &sync.Pool{New: func() any { c := cases.Lower(tag); return &c }}
actual, _ := lowerCaserPools.LoadOrStore(key, p)
pool, _ := actual.(*sync.Pool) // always *sync.Pool: we only ever store that type
c, _ := pool.Get().(*cases.Caser) // Pool.New always returns *cases.Caser
return pool, c
}
// RandomString return a random string.
// Play: https://go.dev/play/p/rRseOQVVum4
func RandomString(size int, charset []rune) string {
if size <= 0 {
panic("lo.RandomString: size must be greater than 0")
}
if len(charset) == 0 {
panic("lo.RandomString: charset must not be empty")
}
// see https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
var sb strings.Builder
sb.Grow(size)
if len(charset) == 1 {
// Edge case, because if the charset is a single character,
// it will panic below (divide by zero).
// -> https://github.com/samber/lo/issues/679
for i := 0; i < size; i++ {
sb.WriteRune(charset[0])
}
return sb.String()
}
// Calculate the number of bits required to represent the charset,
// e.g., for 62 characters, it would need 6 bits (since 62 -> 64 = 2^6)
letterIDBits := int(math.Log2(float64(nearestPowerOfTwo(len(charset)))))
// Determine the corresponding bitmask,
// e.g., for 62 characters, the bitmask would be 111111.
var letterIDMask int64 = 1<<letterIDBits - 1
// Available count, since xrand.Int64() returns a non-negative number, the first bit is fixed, so there are 63 random bits
// e.g., for 62 characters, this value is 10 (63 / 6).
letterIDMax := 63 / letterIDBits
// Generate the random string in a loop.
for i, cache, remain := size-1, xrand.Int64(), letterIDMax; i >= 0; {
// Regenerate the random number if all available bits have been used
if remain == 0 {
cache, remain = xrand.Int64(), letterIDMax
}
// Select a character from the charset
if idx := int(cache & letterIDMask); idx < len(charset) {
sb.WriteRune(charset[idx])
i--
}
// Shift the bits to the right to prepare for the next character selection,
// e.g., for 62 characters, shift by 6 bits.
cache >>= letterIDBits
// Decrease the remaining number of uses for the current random number.
remain--
}
return sb.String()
}
// nearestPowerOfTwo returns the nearest power of two.
func nearestPowerOfTwo(capacity int) int {
n := capacity - 1
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
if n < 0 {
return 1
}
if n >= maximumCapacity {
return maximumCapacity
}
return n + 1
}
// Substring extracts a substring from a string with Unicode character (rune) awareness.
// offset - starting position of the substring (can be positive, negative, or zero)
// length - number of characters to extract
// With positive offset, counting starts from the beginning of the string
// With negative offset, counting starts from the end of the string
// Play: https://go.dev/play/p/emzCC9zBjHu
func Substring[T ~string](str T, offset int, length uint) T {
str = substring(str, offset, length)
// Validate UTF-8 and fix invalid sequences
if !utf8.ValidString(string(str)) {
// Convert to []rune to replicate behavior with duplicated �
str = T([]rune(str))
}
// Remove null bytes from result
return T(strings.ReplaceAll(string(str), "\x00", ""))
}
func substring[T ~string](str T, offset int, length uint) T {
switch {
// Empty length or offset beyond string bounds - return empty string
case length == 0, offset >= len(str):
return ""
// Positive offset - count from the beginning
case offset > 0:
// Skip offset runes from the start
for i, r := range str {
if offset--; offset == 0 {
str = str[i+utf8.RuneLen(r):]
break
}
}
// If couldn't skip enough runes - string is shorter than offset
if offset != 0 {
return ""
}
// If remaining string is shorter than or equal to length - return it entirely
if uint(len(str)) <= length {
return str
}
// Otherwise proceed to trimming by length
fallthrough
// Zero offset or offset less than minus string length - start from beginning
case offset < -len(str), offset == 0:
// Count length runes from the start
for i := range str {
if length == 0 {
return str[:i]
}
length--
}
return str
// Negative offset - count from the end of string
default: // -len(str) < offset < 0
// Helper function to move backward through runes
backwardPos := func(end int, count uint) (start int) {
for {
_, i := utf8.DecodeLastRuneInString(string(str[:end]))
end -= i
if count--; count == 0 || end == 0 {
return end
}
}
}
offset := uint(-offset)
// If offset is less than or equal to length - take from position to end
if offset <= length {
start := backwardPos(len(str), offset)
return str[start:]
}
// Otherwise calculate start and end positions
end := backwardPos(len(str), offset-length)
start := backwardPos(end, length)
return str[start:end]
}
}
// ChunkString returns a slice of strings split into groups of length size. If the string can't be split evenly,
// the final chunk will be the remaining characters.
// Play: https://go.dev/play/p/__FLTuJVz54
//
// Note: lo.ChunkString and lo.Chunk functions behave inconsistently for empty input: lo.ChunkString("", n) returns [""] instead of [].
// See https://github.com/samber/lo/issues/788
func ChunkString[T ~string](str T, size int) []T {
if size <= 0 {
panic("lo.ChunkString: size must be greater than 0")
}
if size >= len(str) {
return []T{str}
}
chunks := make([]T, 0, ((len(str)-1)/size)+1)
currentLen := 0
currentStart := 0
for i := range str {
if currentLen == size {
chunks = append(chunks, str[currentStart:i])
currentLen = 0
currentStart = i
}
currentLen++
}
chunks = append(chunks, str[currentStart:])
return chunks
}
// RuneLength is an alias to utf8.RuneCountInString which returns the number of runes in string.
// Play: https://go.dev/play/p/BXT52mBk0zO
func RuneLength(str string) int {
return utf8.RuneCountInString(str)
}
// PascalCase converts string to pascal case.
// Play: https://go.dev/play/p/uxER7XpRHLB
func PascalCase(str string) string {
items := Words(str)
if len(items) == 0 {
return ""
}
c, _ := englishTitleCaserPool.Get().(*cases.Caser)
defer englishTitleCaserPool.Put(c)
for i := range items {
items[i] = c.String(items[i])
}
return strings.Join(items, "")
}
// PascalCaseWithLanguage converts string to pascal case using language-aware title casing.
// This matters for languages such as Turkish where the uppercase of "i" is "İ", not "I".
func PascalCaseWithLanguage(str string, tag language.Tag) string {
items := Words(str)
if len(items) == 0 {
return ""
}
pool, c := acquireTitleCaser(tag)
defer pool.Put(c)
for i := range items {
items[i] = c.String(items[i])
}
return strings.Join(items, "")
}
// CamelCase converts string to camel case.
// Play: https://go.dev/play/p/4JNDzaMwXkm
func CamelCase(str string) string {
items := Words(str)
if len(items) == 0 {
return ""
}
lc, _ := englishLowerCaserPool.Get().(*cases.Caser)
tc, _ := englishTitleCaserPool.Get().(*cases.Caser)
defer englishLowerCaserPool.Put(lc)
defer englishTitleCaserPool.Put(tc)
items[0] = lc.String(items[0])
for i := 1; i < len(items); i++ {
items[i] = tc.String(items[i])
}
return strings.Join(items, "")
}
// CamelCaseWithLanguage converts string to camel case using language-aware casing.
// This matters for languages such as Turkish where the uppercase of "i" is "İ", not "I".
func CamelCaseWithLanguage(str string, tag language.Tag) string {
items := Words(str)
if len(items) == 0 {
return ""
}
lPool, lc := acquireLowerCaser(tag)
tPool, tc := acquireTitleCaser(tag)
defer lPool.Put(lc)
defer tPool.Put(tc)
items[0] = lc.String(items[0])
for i := 1; i < len(items); i++ {
items[i] = tc.String(items[i])
}
return strings.Join(items, "")
}
// KebabCase converts string to kebab case.
// Play: https://go.dev/play/p/ZBeMB4-pq45
func KebabCase(str string) string {
items := Words(str)
if len(items) == 0 {
return ""
}
c, _ := englishLowerCaserPool.Get().(*cases.Caser)
defer englishLowerCaserPool.Put(c)
for i := range items {
items[i] = c.String(items[i])
}
return strings.Join(items, "-")
}
// KebabCaseWithLanguage converts string to kebab case using language-aware lowercasing.
// This matters for languages such as Turkish where "I" lowercases to "ı" (dotless i), not "i".
func KebabCaseWithLanguage(str string, tag language.Tag) string {
items := Words(str)
if len(items) == 0 {
return ""
}
pool, c := acquireLowerCaser(tag)
defer pool.Put(c)
for i := range items {
items[i] = c.String(items[i])
}
return strings.Join(items, "-")
}
// SnakeCase converts string to snake case.
// Play: https://go.dev/play/p/ziB0V89IeVH
func SnakeCase(str string) string {
items := Words(str)
if len(items) == 0 {
return ""
}
c, _ := englishLowerCaserPool.Get().(*cases.Caser)
defer englishLowerCaserPool.Put(c)
for i := range items {
items[i] = c.String(items[i])
}
return strings.Join(items, "_")
}
// SnakeCaseWithLanguage converts string to snake case using language-aware lowercasing.
// This matters for languages such as Turkish where "I" lowercases to "ı" (dotless i), not "i".
func SnakeCaseWithLanguage(str string, tag language.Tag) string {
items := Words(str)
if len(items) == 0 {
return ""
}
pool, c := acquireLowerCaser(tag)
defer pool.Put(c)
for i := range items {
items[i] = c.String(items[i])
}
return strings.Join(items, "_")
}
// Words splits string into a slice of its words.
// Play: https://go.dev/play/p/-f3VIQqiaVw
func Words(str string) []string {
buf := splitWordBoundaries(str)
// example: Int8Value => Int 8Value => Int 8 Value
buf = splitNumberLetter(buf)
return fieldsAlnum(string(buf))
}
func isASCIILower(c byte) bool { return 'a' <= c && c <= 'z' }
func isASCIIUpper(c byte) bool { return 'A' <= c && c <= 'Z' }
func isASCIIDigit(c byte) bool { return '0' <= c && c <= '9' }
func isASCIILetter(c byte) bool { return isASCIILower(c) || isASCIIUpper(c) }
// splitWordBoundaries inserts a space at case and letter/digit boundaries. It is the
// manual-scan equivalent of replacing
// `([a-z])([A-Z0-9])|([a-zA-Z])([0-9])|([0-9])([a-zA-Z])|([A-Z])([A-Z])([a-z])`
// with `$1$3$5$7 $2$4$6$8$9`, preserving the regexp's non-overlapping
// leftmost-match consumption.
func splitWordBoundaries(s string) []byte {
out := make([]byte, 0, len(s)+8)
i := 0
for i < len(s) {
c := s[i]
if i+1 < len(s) {
d := s[i+1]
if (isASCIILower(c) && (isASCIIUpper(d) || isASCIIDigit(d))) ||
(isASCIILetter(c) && isASCIIDigit(d)) ||
(isASCIIDigit(c) && isASCIILetter(d)) {
out = append(out, c, ' ', d)
i += 2
continue
}
if i+2 < len(s) {
if e := s[i+2]; isASCIIUpper(c) && isASCIIUpper(d) && isASCIILower(e) {
out = append(out, c, ' ', d, e)
i += 3
continue
}
}
}
out = append(out, c)
i++
}
return out
}
// splitNumberLetter inserts a space between a digit and a following letter. It is the
// manual-scan equivalent of replacing `([0-9])([a-zA-Z])` with `$1 $2`,
// preserving the regexp's non-overlapping leftmost-match consumption.
func splitNumberLetter(s []byte) []byte {
out := make([]byte, 0, len(s)+8)
i := 0
for i < len(s) {
c := s[i]
if i+1 < len(s) {
if d := s[i+1]; isASCIIDigit(c) && isASCIILetter(d) {
out = append(out, c, ' ', d)
i += 2
continue
}
}
out = append(out, c)
i++
}
return out
}
// fieldsAlnum returns the maximal runs of unicode letters and digits in s,
// like strings.Fields after mapping every other rune to a space.
func fieldsAlnum(s string) []string {
count := 0
inField := false
for _, r := range s {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
if !inField {
count++
inField = true
}
} else {
inField = false
}
}
fields := make([]string, 0, count)
start := -1
for i, r := range s {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
if start < 0 {
start = i
}
} else if start >= 0 {
fields = append(fields, s[start:i])
start = -1
}
}
if start >= 0 {
fields = append(fields, s[start:])
}
return fields
}
// Capitalize converts the first character of string to upper case and the remaining to lower case.
// Play: https://go.dev/play/p/uLTZZQXqnsa
func Capitalize(str string) string {
c, _ := englishTitleCaserPool.Get().(*cases.Caser)
defer englishTitleCaserPool.Put(c)
return c.String(str)
}
// CapitalizeWithLanguage converts the first character of string to upper case and the remaining to
// lower case, using language-aware title casing.
// This matters for languages such as Turkish where the uppercase of "i" is "İ", not "I".
func CapitalizeWithLanguage(str string, tag language.Tag) string {
pool, c := acquireTitleCaser(tag)
defer pool.Put(c)
return c.String(str)
}
// Ellipsis trims and truncates a string to a specified length in runes and appends an ellipsis
// if truncated. The length parameter counts Unicode code points (runes), not bytes, so multi-byte
// characters such as emoji or CJK ideographs are never split in the middle.
// Play: https://go.dev/play/p/qE93rgqe1TW
func Ellipsis(str string, length int) string {
str = strings.TrimSpace(str)
const ellipsis = "..."
cutPosition := 0
for i := range str {
if length == len(ellipsis) {
cutPosition = i
}
if length--; length < 0 {
return strings.TrimSpace(str[:cutPosition]) + ellipsis
}
}
return str
}