-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstrings.go
70 lines (60 loc) · 1.45 KB
/
strings.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
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
// ToLower is the equivalent of strings.ToLower
func ToLower(b string) string {
var res = make([]byte, len(b))
copy(res, b)
for i := 0; i < len(res); i++ {
res[i] = toLowerTable[res[i]]
}
return GetString(res)
}
// ToUpper is the equivalent of strings.ToUpper
func ToUpper(b string) string {
var res = make([]byte, len(b))
copy(res, b)
for i := 0; i < len(res); i++ {
res[i] = toUpperTable[res[i]]
}
return GetString(res)
}
// TrimLeft is the equivalent of strings.TrimLeft
func TrimLeft(s string, cutset byte) string {
lenStr, start := len(s), 0
for start < lenStr && s[start] == cutset {
start++
}
return s[start:]
}
// Trim is the equivalent of strings.Trim
func Trim(s string, cutset byte) string {
i, j := 0, len(s)-1
for ; i < j; i++ {
if s[i] != cutset {
break
}
}
for ; i < j; j-- {
if s[j] != cutset {
break
}
}
return s[i : j+1]
}
// TrimRight is the equivalent of strings.TrimRight
func TrimRight(s string, cutset byte) string {
lenStr := len(s)
for lenStr > 0 && s[lenStr-1] == cutset {
lenStr--
}
return s[:lenStr]
}
// DefaultString returns the provided fallback value if string is empty
func DefaultString(value string, defaultValue string) string {
if len(value) <= 0 {
return defaultValue
}
return value
}