-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
243 lines (208 loc) · 4.37 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
package util
import (
"encoding/json"
"net/url"
"strconv"
"strings"
"github.com/axgle/mahonia"
jsoniter "github.com/json-iterator/go"
)
func Split(str *string, sep string) []string {
return strings.Split(*str, sep)
}
func StrLR(str *string, subStr string) string {
pot := strings.Index(*str, subStr)
if pot == -1 {
return ""
}
subStrPot := pot + len(subStr)
return (*str)[subStrPot:]
}
func StrLL(str *string, subStr string) string {
pot := strings.Index(*str, subStr)
if pot == -1 {
return ""
}
return (*str)[0:pot]
}
func StrRL(str *string, subStr string) string {
pot := strings.LastIndex(*str, subStr)
if pot == -1 {
return ""
}
return (*str)[0:pot]
}
func StrRR(str *string, subStr string) string {
pot := strings.LastIndex(*str, subStr)
if pot == -1 {
return ""
}
pot += len(subStr)
return (*str)[pot:]
}
func JsonIter() jsoniter.API {
return jsoniter.ConfigCompatibleWithStandardLibrary
}
func JsonDecode(s *string, data interface{}) {
if *s == "" {
return
}
err := JsonIter().Unmarshal([]byte(*s), &data)
if err != nil {
//util.ThrowSys("JsonDecode失败:"+err.Error(), *s)
// // todo
}
}
func JsonEncode(data interface{}) string {
bs, err := JsonIter().Marshal(data)
if err != nil {
//util.ThrowSys("jsonEncode失败", data)// todo
}
return string(bs)
}
func Trim(s string) string {
return strings.TrimSpace(s)
}
func StrIndex(str *string, subStr string) int {
res := strings.Index(*str, subStr)
if res >= 0 {
prefix := []byte(*str)[0:res]
rs := []rune(string(prefix))
res = len(rs)
}
return res
}
func ConvertToByte(src *string, srcCode string, targetCode string) []byte {
srcCoder := mahonia.NewDecoder(srcCode)
srcResult := srcCoder.ConvertString(*src)
tagCoder := mahonia.NewDecoder(targetCode)
_, cdata, _ := tagCoder.Translate([]byte(srcResult), true)
return cdata
}
func Substr(str *string, start int, length ...int) string {
rs := []rune(*str)
var strLen = len(rs)
if start < 0 {
start = 0
}
var end = strLen
if len(length) != 0 {
end = start + length[0]
if end > strLen {
end = strLen
}
}
return string(rs[start:end])
}
func Replace(str *string, old string, new string) string {
return strings.Replace(*str, old, new, -1)
}
func SnakeString(s string) string {
data := make([]byte, 0, len(s)*2)
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
if i > 0 && d >= 'A' && d <= 'Z' {
data = append(data, '_')
}
data = append(data, d)
}
return strings.ToLower(string(data[:]))
}
func CamelCaseString(s string) string {
res := ""
arr := strings.Split(s, "_")
for _, v := range arr {
vv := []rune(v)
if bool(vv[0] >= 'a' && vv[0] <= 'z') {
vv[0] -= 32
}
res += string(vv)
}
return res
}
func StrCut(str string, length int) string {
rs := []rune(str)
if len(rs) <= length {
return str
}
return string(rs[0:length]) + "..."
}
func StrCutDirect(str string, length int) string {
rs := []rune(str)
if len(rs) <= length {
return str
}
return string(rs[0:length])
}
func UrlEncode(value string) string {
u := url.Values{}
u.Set("tmp", value)
res := u.Encode()
return StrLR(&res, "tmp=")
}
func IsMobile(str string) bool {
if len(str) != 11 {
return false
}
for _, v := range str {
if v < '0' || v > '9' {
return false
}
}
return true
}
// Strval
//convert interface to string
func Strval(value interface{}) string {
var key string
if value == nil {
return key
}
switch value.(type) {
case float64:
ft := value.(float64)
key = strconv.FormatFloat(ft, 'f', -1, 64)
case float32:
ft := value.(float32)
key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
case int:
it := value.(int)
key = strconv.Itoa(it)
case uint:
it := value.(uint)
key = strconv.Itoa(int(it))
case int8:
it := value.(int8)
key = strconv.Itoa(int(it))
case uint8:
it := value.(uint8)
key = strconv.Itoa(int(it))
case int16:
it := value.(int16)
key = strconv.Itoa(int(it))
case uint16:
it := value.(uint16)
key = strconv.Itoa(int(it))
case int32:
it := value.(int32)
key = strconv.Itoa(int(it))
case uint32:
it := value.(uint32)
key = strconv.Itoa(int(it))
case int64:
it := value.(int64)
key = strconv.FormatInt(it, 10)
case uint64:
it := value.(uint64)
key = strconv.FormatUint(it, 10)
case string:
key = value.(string)
case []byte:
key = string(value.([]byte))
default:
newValue, _ := json.Marshal(value)
key = string(newValue)
}
return key
}