-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstring.go
330 lines (260 loc) · 7.09 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
package gogu
import (
"math"
"regexp"
"strings"
"unicode"
)
func Null[T any]() T {
var t T
return t
}
// Substr returns the portion of string specified by the offset and length.
//
// If offset is non-negative, the returned string will start
// at the offset'th position in string, counting from zero.
//
// If offset is negative, the returned string will start at
// the offset'th character from the end of string.
//
// If string is less than offset characters long, an empty string will be returned.
//
// If length is negative, then that many characters will be omitted
// from the end of string starting from the offset position.
func Substr[T ~string](str T, offset, length int) T {
var end int
if offset < 0 {
offset = len(str) + offset
if Abs(offset) > len(str) {
return Null[T]()
}
}
if length < 0 {
newLength := len(str) + length
if Abs(newLength) > len(str) || newLength < offset {
return Null[T]()
}
end = newLength
} else {
end = offset + length
}
if end > len(str) {
end = len(str)
}
if !InRange(offset, 0, len(str)) || !InRange(end, 0, len(str)) {
return Null[T]()
}
return str[offset:end]
}
// ToLower converts a string to Lowercase.
func ToLower[T ~string](str T) T {
result := make([]rune, 0, len(str))
for _, val := range str {
result = append(result, unicode.ToLower(rune(val)))
}
return T(result)
}
// ToUpper converts a string to Uppercase.
func ToUpper[T ~string](str T) T {
result := make([]rune, 0, len(str))
for _, val := range str {
result = append(result, unicode.ToLower(rune(val)))
}
return T(result)
}
// Capitalize converts the first letter of the string
// to uppercase and the remaining letters to lowercase.
func Capitalize[T ~string](str T) T {
result := make([]rune, 0, len(str))
for i, val := range str {
if i == 0 {
result = append(result, unicode.ToUpper(rune(val)))
} else {
result = append(result, unicode.ToLower(rune(val)))
}
}
return T(result)
}
// CamelCase converts a string to camelCase (https://en.wikipedia.org/wiki/CamelCase).
func CamelCase[T ~string](str T) T {
newstr := strings.TrimSpace(string(str))
r, _ := regexp.Compile("[-_&]+")
newstr = r.ReplaceAllString(newstr, " ")
var sb strings.Builder
sb.Grow(len(newstr))
var idx int
for i, s := range strings.Split(newstr, " ") {
r := []rune(s)
if len(r) == 0 {
idx++
continue
}
if i == 0 || i == idx {
frag := ToLower(s)
sb.WriteString(frag)
continue
}
sb.WriteString(Capitalize(s))
}
result := sb.String()
return T(result)
}
// SnakeCase converts a string to snake_case (https://en.wikipedia.org/wiki/Snake_case).
func SnakeCase[T ~string](str T) T {
return splitStringWithDelimiter(str, "_")
}
// KebabCase converts a string to kebab-case (https://en.wikipedia.org/wiki/Letter_case#Kebab_case).
func KebabCase[T ~string](str T) T {
return splitStringWithDelimiter(str, "-")
}
// splitStringWithDelimiter splits a string to lower case with the provided delimiter.
func splitStringWithDelimiter[T ~string](str T, delimiter string) T {
var sb strings.Builder
newstr := strings.TrimSpace(string(str))
rx, _ := regexp.Compile("[-_&]+")
newstr = rx.ReplaceAllString(newstr, " ")
var idx int
chars := strings.Split(newstr, " ")
for i, str := range chars {
r := []rune(str)
if len(r) == 0 {
idx++
continue
}
rx, _ = regexp.Compile("[a-zö][A-ZÖ]+")
strIdx := rx.FindAllStringIndex(str, -1)
if len(strIdx) > 0 {
s := Substr(str, 0, strIdx[0][0]+1)
sb.WriteString(ToLower(s))
sb.WriteString(delimiter)
for i := 0; i < len(strIdx); i++ {
var s string
if i < len(strIdx)-1 {
subStrLen := strIdx[i+1][0] - strIdx[i][0]
s = Substr(str, strIdx[i][0]+1, subStrLen)
sb.WriteString(ToLower(s))
sb.WriteString(delimiter)
} else {
s = Substr(str, strIdx[i][0]+1, len(str)-strIdx[i][0]+1)
sb.WriteString(ToLower(s))
}
}
if len(chars) > 1 && i != len(chars)-1 {
sb.WriteString(delimiter)
}
} else {
frag := ToLower(str)
sb.WriteString(frag)
if len(chars) > 1 && i != len(chars)-1 {
sb.WriteString(delimiter)
}
}
}
result := sb.String()
return T(result)
}
// PadLeft pads string on the left side if it's shorter than length.
// Padding characters are truncated if they exceed length.
func PadLeft[T ~string](str T, size int, token string) T {
var tokenStr = token
strLen := len(str)
tokenLen := len(token)
if size <= strLen {
return T(str)
}
if tokenLen <= size-strLen {
tokenStr = strings.Repeat(token, size-strLen)
}
tokenStr = tokenStr[:size-strLen]
return T(tokenStr) + T(str)
}
// PadRight pads string on the right side if it's shorter than length.
// Padding characters are truncated if they exceed length.
func PadRight[T ~string](str T, size int, token string) T {
var tokenStr = token
strLen := len(str)
tokenLen := len(token)
if size <= strLen {
return T(str)
}
if tokenLen <= size-strLen {
tokenStr = strings.Repeat(token, size-strLen)
}
tokenStr = tokenStr[:size-strLen]
return T(str) + T(tokenStr)
}
// Pad pads string on the left and right sides if it's shorter than length.
// Padding characters are truncated if they can't be evenly divided by length.
func Pad[T ~string](str T, size int, token string) T {
var (
leftTokenStr = token
rightTokenStr = token
)
strLen := len(str)
tokenLen := len(token)
if size <= strLen {
return T(str)
}
split := float64(size-strLen) / 2
left := int(math.Floor(split))
right := int(math.Ceil(split))
if tokenLen <= int(split) {
leftTokenStr = strings.Repeat(token, left)
rightTokenStr = strings.Repeat(token, right)
}
leftTokenStr = leftTokenStr[:left]
rightTokenStr = rightTokenStr[:right]
return T(leftTokenStr) + str + T(rightTokenStr)
}
// SplitAtIndex split the string at the specified index and
// returns a slice with the resulted two substrings.
func SplitAtIndex[T ~string](str T, index int) []T {
result := make([]T, 0, 2)
if index < 0 {
return []T{"", str}
}
if index > len(str)-1 {
return []T{str, ""}
}
for idx := range str {
if idx == index {
result = append(result, append(result, str[:idx+1], str[idx+1:])...)
}
}
return result
}
// Wrap a string with the specified token.
func Wrap[T ~string](str T, token string) T {
var s strings.Builder
s.WriteString(token)
s.WriteString(string(str))
s.WriteString(token)
return T(s.String())
}
// Unwrap a string with the specified token.
func Unwrap[T ~string](str T, token string) T {
startToken := strings.Index(string(str), token)
endToken := strings.LastIndex(string(str), token)
if startToken == 0 && endToken <= len(str)-1 {
str = str[len(token):endToken]
}
return str
}
// WrapAllRune is like Wrap, only that it's applied over runes instead of strings.
func WrapAllRune[T ~string](str T, token string) T {
var s strings.Builder
for _, st := range str {
s.WriteString(token)
s.WriteRune(st)
s.WriteString(token)
}
return T(s.String())
}
// ReverseStr returns a new string with the characters in reverse order.
func ReverseStr[T ~string](str T) T {
res := []rune(str)
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
res[i], res[j] = res[j], res[i]
}
return T(res)
}