-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
444 lines (361 loc) · 9.36 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
package gosc
import (
"bytes"
cryrand "crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var camelingRegex = regexp.MustCompile("[0-9A-Za-z]+")
// ToBytes converts a string to bytes slice
func ToBytes(s string) []byte {
return []byte(s)
}
// ByteToString converts a bytes slice to a string
func ByteToString(b []byte) string {
return string(b[:])
}
// Rstring returns the string reversed
func Rstring(s string) string {
runes := []rune(s)
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)
}
// ReverseString is an alias of Rstring
func ReverseString(s string) string {
return Rstring(s)
}
// LcFirst returns a string with the first character lowercased
func LcFirst(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToLower(r)) + s[n:]
}
// LowerFirst is an alias of LcFirst
func LowerFirst(s string) string {
return LcFirst(s)
}
// UcFirst returns a string with the first character uppercased
func UcFirst(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToUpper(r)) + s[n:]
}
// UpperFirst is an alias of UcFirst
func UpperFirst(s string) string {
return UcFirst(s)
}
// ------------------
// "To" section
// ------------------
// ToSnake converts a string to snake_case
func ToSnake(s string) string {
s = strings.Replace(strings.Replace(s, "-", "_", -1), " ", "_", -1)
runes := []rune(s)
length := len(runes)
var out []rune
for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
out = append(out, '_')
}
out = append(out, unicode.ToLower(runes[i]))
}
return string(out)
}
// ToSnakeCase is an alias of ToSnake
func ToSnakeCase(s string) string {
return ToSnake(s)
}
// ToCamel converts a string to camelCase
func ToCamel(s string) string {
byteSrc := []byte(s)
chunks := camelingRegex.FindAll(byteSrc, -1)
for idx, val := range chunks {
val = bytes.ToLower(val)
if idx > 0 {
chunks[idx] = bytes.Title(val)
}
}
return string(bytes.Join(chunks, nil))
}
// ToCamelCase is an alias of ToCamel
func ToCamelCase(s string) string {
return ToCamel(s)
}
// ToPascal converts a string to PascalCase
func ToPascal(s string) string {
byteSrc := []byte(s)
chunks := camelingRegex.FindAll(byteSrc, -1)
for idx, val := range chunks {
chunks[idx] = bytes.Title(val)
}
return string(bytes.Join(chunks, nil))
}
// ToPascalCase is an alias of ToPascal
func ToPascalCase(s string) string {
return ToPascal(s)
}
// ToKebab converts a string to kebab-case
func ToKebab(s string) string {
s = strings.Replace(strings.Replace(s, "_", "-", -1), " ", "-", -1)
runes := []rune(s)
length := len(runes)
var out []rune
for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
out = append(out, '-')
}
out = append(out, unicode.ToLower(runes[i]))
}
return string(out)
}
// ToKebabCase is an alias of ToKebab
func ToKebabCase(s string) string {
return ToKebab(s)
}
// ToInt returns an int from a string
func ToInt(s string) int {
if strings.Contains(s, ".") {
s = strings.Split(s, ".")[0]
}
v, err := strconv.Atoi(s)
if err != nil {
return 0
}
return v
}
// ToInt64 returns an int64 from a string
func ToInt64(s string) int64 {
if strings.Contains(s, ".") {
s = strings.Split(s, ".")[0]
}
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0
}
return v
}
// ToUint returns a uint from a string
func ToUint(s string) uint {
if strings.Contains(s, ".") {
s = strings.Split(s, ".")[0]
}
v, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return uint(0)
}
return uint(v)
}
// ToBase64 encodes a string in base64
func ToBase64(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}
// FromBase64 decodes a string from base64
func FromBase64(s string) string {
str, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return ""
}
return string(str)
}
// ------------------
// "Is" section
// ------------------
// IsBool checks if a string is a boolean
func IsBool(s string) bool {
_, err := strconv.ParseBool(s)
return err == nil
}
// IsEmail returns true if the provided string is a valid email address
func IsEmail(s string) bool {
emailRegexp := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
return emailRegexp.MatchString(s)
}
// IsURL returns true if the provided string is a valid url
func IsURL(s string) bool {
_, err := url.ParseRequestURI(s)
return err == nil
}
// IsJSON returns true if the provided string is a valid JSON document
func IsJSON(s string) bool {
var js json.RawMessage
return json.Unmarshal([]byte(s), &js) == nil
}
// IsIP returns true if the provided string is a valid IPv4
func IsIP(s string) bool {
ip := net.ParseIP(s)
return ip != nil && strings.Count(s, ".") == 3
}
// IsHexColor returns true if the provided string is a valid HEX color
func IsHexColor(s string) bool {
if s == "" {
return false
}
if s[0] == '#' {
s = s[1:]
}
if len(s) != 3 && len(s) != 6 {
return false
}
for _, c := range s {
if ('F' < c || c < 'A') && ('f' < c || c < 'a') && ('9' < c || c < '0') {
return false
}
}
return true
}
// IsRGBColor returns true if the provided string is a valid RGB color
func IsRGBColor(s string) bool {
if s == "" || len(s) < 10 {
return false
}
if s[0:4] != "rgb(" || s[len(s)-1] != ')' {
return false
}
s = s[4 : len(s)-1]
s = strings.TrimSpace(s)
if strings.Count(s, ",") != 2 {
return false
}
for _, p := range strings.Split(s, ",") {
if len(p) > 1 && p[0] == '0' {
return false
}
p = strings.TrimSpace(p)
if i, e := strconv.Atoi(p); (255 < i || i < 0) || e != nil {
return false
}
}
return true
}
// IsRGB is an alias of IsRGBColor
func IsRGB(s string) bool {
return IsRGBColor(s)
}
// IsCreditCard returns true if the provided string is a valid credit card
func IsCreditCard(s string) bool {
rxCreditCard := regexp.MustCompile("^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})$")
r, _ := regexp.Compile("[^0-9]+")
sanitized := r.ReplaceAll([]byte(s), []byte(""))
if !rxCreditCard.MatchString(string(sanitized)) {
return false
}
var sum int
var digit string
var tmpNum int
var shouldDouble bool
for i := len(sanitized) - 1; i >= 0; i-- {
digit = string(sanitized[i:(i + 1)])
tmpNum = ToInt(digit)
if shouldDouble {
tmpNum *= 2
if tmpNum >= 10 {
sum += ((tmpNum % 10) + 1)
} else {
sum += tmpNum
}
} else {
sum += tmpNum
}
shouldDouble = !shouldDouble
}
if sum%10 == 0 {
return true
}
return false
}
// IsOnlyDigits returns true if the provided string is composed only by numbers
func IsOnlyDigits(s string) bool {
r := regexp.MustCompile("^[0-9]$")
return r.MatchString(s)
}
// IsOnlyNumbers is an alias of IsOnlyDigits
func IsOnlyNumbers(s string) bool {
return IsOnlyDigits(s)
}
// IsOnlyLetters returns true if the provided string is composed only by letters
func IsOnlyLetters(s string) bool {
r := regexp.MustCompile("^[A-Za-z]+$")
return r.MatchString(s)
}
// IsOnlyAlpha is an alias of IsOnlyLetters
func IsOnlyAlpha(s string) bool {
return IsOnlyLetters(s)
}
// IsOnlyAlphaNumeric returns true if the provided string is composed only by letters and numbers
func IsOnlyAlphaNumeric(s string) bool {
r := regexp.MustCompile("^[A-Za-z0-9]+$")
return r.MatchString(s)
}
// IsOnlyAlphaDigits is an alias of IsOnlyAlphaNumeric
func IsOnlyAlphaDigits(s string) bool {
return IsOnlyAlphaNumeric(s)
}
// IsOnlyLettersNumbers is an alias of IsOnlyAlphaNumeric
func IsOnlyLettersNumbers(s string) bool {
return IsOnlyAlphaNumeric(s)
}
// IsOnlyAlphaNum is an alias of IsOnlyAlphaNumeric
func IsOnlyAlphaNum(s string) bool {
return IsOnlyAlphaNumeric(s)
}
// ------------------
// "Generator" section
// ------------------
// Uniq returns a unique token based on current time and crypted in sha256
func Uniq() string {
t := strconv.Itoa(int(time.Now().UnixNano()))
return fmt.Sprintf("%x", sha256.Sum256([]byte("gosc_"+t)))
}
// Unique is an alias of Uniq
func Unique() string {
return Uniq()
}
// StrRand returns a random string
func StrRand(n int) string {
rand.Seed(time.Now().Unix())
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
// RandStr is an alias of StrRand
func RandStr(n int) string {
return StrRand(n)
}
// RandomString is an alias of StrRand
func RandomString(n int) string {
return StrRand(n)
}
// UUID generates a random UUID according to RFC 4122
func UUID() string {
uuid := make([]byte, 16)
n, err := io.ReadFull(cryrand.Reader, uuid)
if n != len(uuid) || err != nil {
return ""
}
// variant bits; see section 4.1.1
uuid[8] = uuid[8]&^0xc0 | 0x80
// version 4 (pseudo-random); see section 4.1.3
uuid[6] = uuid[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
}