-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbytes.go
141 lines (126 loc) · 2.86 KB
/
bytes.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
package utils
import (
"bytes"
)
// GetBytes 先转为字符串再转为 []byte, 可选指定默认值
func GetBytes(v interface{}, defaultVal ...[]byte) []byte {
switch b := v.(type) {
default:
bs := S2B(MustString(v))
if len(bs) == 0 && len(defaultVal) > 0 {
return defaultVal[0]
}
return bs
case []byte:
return b
}
}
// GetSafeBytes Immutable, 可选指定默认值
func GetSafeBytes(b []byte, defaultVal ...[]byte) []byte {
b = CopyBytes(b)
if len(b) == 0 && len(defaultVal) > 0 {
return defaultVal[0]
}
return b
}
// GetSafeS2B Immutable, 可选指定默认值
func GetSafeS2B(s string, defaultVal ...[]byte) []byte {
b := []byte(s)
if len(b) == 0 && len(defaultVal) > 0 {
return defaultVal[0]
}
return b
}
// CopyBytes Immutable, []byte to []byte
func CopyBytes(b []byte) []byte {
tmp := make([]byte, len(b))
copy(tmp, b)
return tmp
}
// CopyS2B Immutable, string to []byte
// []byte(s)
func CopyS2B(s string) []byte {
tmp := make([]byte, len(s))
copy(tmp, s)
return tmp
}
// JoinBytes 拼接 []byte
func JoinBytes(b ...[]byte) []byte {
return bytes.Join(b, nil)
}
// ToLowerBytes converts ascii slice to lower-case
// Ref: fiber
func ToLowerBytes(b []byte) []byte {
for i := 0; i < len(b); i++ {
b[i] = toLowerTable[b[i]]
}
return b
}
// ToUpperBytes converts ascii slice to upper-case
// Ref: fiber
func ToUpperBytes(b []byte) []byte {
for i := 0; i < len(b); i++ {
b[i] = toUpperTable[b[i]]
}
return b
}
// TrimLeftBytes is the equivalent of bytes.TrimLeft
// Ref: fiber
func TrimLeftBytes(b []byte, cutset byte) []byte {
lenStr, start := len(b), 0
for start < lenStr && b[start] == cutset {
start++
}
return b[start:]
}
// TrimRightBytes is the equivalent of bytes.TrimRight
// Ref: fiber
func TrimRightBytes(b []byte, cutset byte) []byte {
lenStr := len(b)
for lenStr > 0 && b[lenStr-1] == cutset {
lenStr--
}
return b[:lenStr]
}
// TrimBytes is the equivalent of bytes.Trim
// Ref: fiber
func TrimBytes(b []byte, cutset byte) []byte {
i, j := 0, len(b)-1
for ; i <= j; i++ {
if b[i] != cutset {
break
}
}
for ; i < j; j-- {
if b[j] != cutset {
break
}
}
return b[i : j+1]
}
// EqualFoldBytes tests ascii slices for equality case-insensitively
// Ref: fiber
func EqualFoldBytes(b, s []byte) bool {
if len(b) != len(s) {
return false
}
for i := len(b) - 1; i >= 0; i-- {
if toUpperTable[b[i]] != toUpperTable[s[i]] {
return false
}
}
return true
}
// CutBytes slices s around the first instance of sep,
// returning the text before and after sep.
// The found result reports whether sep appears in s.
// If sep does not appear in s, cut returns s, nil, false.
//
// Cut returns slices of the original slice s, not copies.
// Ref: go1.18
func CutBytes(s, sep []byte) (before, after []byte, found bool) {
if i := bytes.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, nil, false
}