-
Notifications
You must be signed in to change notification settings - Fork 42
/
string-op.go
286 lines (252 loc) · 5.81 KB
/
string-op.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
package utils
import (
"bufio"
"bytes"
"fmt"
"github.com/yaklang/yaklang/common/go-funk"
"github.com/yaklang/yaklang/common/yak/yaklib/codec"
"regexp"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
func PrettifyListFromStringSplited(Raw string, sep string) (targets []string) {
targetsRaw := strings.Split(Raw, sep)
for _, tRaw := range targetsRaw {
r := strings.TrimSpace(tRaw)
if len(r) > 0 {
targets = append(targets, r)
}
}
return
}
func PrettifyListFromStringSplitEx(Raw string, sep ...string) (targets []string) {
if len(sep) <= 0 {
sep = []string{",", "|"}
}
var targetsRaw []string
for _, s := range sep {
targetsRaw = append(targetsRaw, strings.Split(Raw, s)...)
}
for _, tRaw := range targetsRaw {
r := strings.TrimSpace(tRaw)
if len(r) > 0 {
targets = append(targets, r)
}
}
return
}
func ToLowerAndStrip(s string) string {
return strings.TrimSpace(strings.ToLower(s))
}
func StringSliceContain(s interface{}, raw string) (result bool) {
defer func() {
if err := recover(); err != nil {
return
}
}()
haveResult := false
funk.ForEach(s, func(i interface{}) {
if haveResult {
return
}
if InterfaceToString(i) == raw {
haveResult = true
}
})
return haveResult
}
func StringContainsAnyOfSubString(s string, subs []string) bool {
for _, sub := range subs {
if strings.Contains(s, sub) {
return true
}
}
return false
}
func StringContainsAllOfSubString(s string, subs []string) bool {
if len(subs) <= 0 {
return false
}
for _, sub := range subs {
if !strings.Contains(s, sub) {
return false
}
}
return true
}
func IStringContainsAnyOfSubString(s string, subs []string) bool {
for _, sub := range subs {
if IContains(s, sub) {
return true
}
}
return false
}
func ConvertToStringSlice(raw ...interface{}) (r []string) {
for _, e := range raw {
r = append(r, fmt.Sprintf("%v", e))
}
return
}
func ChanStringToSlice(c chan string) (result []string) {
for l := range c {
result = append(result, l)
}
return
}
var (
cStyleCharPRegexp, _ = regexp.Compile(`\\((x[0-9abcdef]{2})|([0-9]{1,3}))`)
)
func ParseCStyleBinaryRawToBytes(raw []byte) []byte {
// like "\\x12" => "\x12"
return cStyleCharPRegexp.ReplaceAllFunc(raw, func(i []byte) []byte {
if bytes.HasPrefix(i, []byte("\\x")) {
if len(i) == 4 {
rawChar := string(i[2:])
charInt, err := strconv.ParseInt("0x"+string(rawChar), 0, 16)
if err != nil {
return i
}
return []byte{byte(charInt)}
} else {
return i
}
} else if bytes.HasPrefix(raw, []byte("\\")) {
if len(i) > 1 && len(i) <= 4 {
rawChar := string(i[1:])
charInt, err := strconv.ParseInt(string(rawChar), 10, 8)
if err != nil {
return i
}
return []byte{byte(charInt)}
} else {
return i
}
}
return i
})
}
var GbkToUtf8 = codec.GbkToUtf8
var Utf8ToGbk = codec.Utf8ToGbk
func ParseStringToVisible(raw interface{}) string {
var s = InterfaceToString(raw)
s = EscapeInvalidUTF8Byte([]byte(s))
//s = strings.ReplaceAll(s, "\x20", "\\x20")
s = strings.ReplaceAll(s, "\x0b", "\\v")
r, err := regexp.Compile(`\s`)
if err != nil {
return s
}
return r.ReplaceAllStringFunc(s, func(s string) string {
var result = strconv.Quote(s)
for strings.HasPrefix(result, "\"") {
result = result[1:]
}
for strings.HasSuffix(result, "\"") {
result = result[:len(result)-1]
}
return result
})
}
func EscapeInvalidUTF8Byte(s []byte) string {
// 这个操作返回的结果和原始字符串是非等价的
ret := make([]rune, 0, len(s)+20)
start := 0
for {
r, size := utf8.DecodeRune(s[start:])
if r == utf8.RuneError {
// 说明是空的
if size == 0 {
break
} else {
// 不是 rune
ret = append(ret, []rune(fmt.Sprintf("\\x%02x", s[start]))...)
}
} else {
// 不是换行之类的控制字符
if unicode.IsControl(r) && !unicode.IsSpace(r) {
ret = append(ret, []rune(fmt.Sprintf("\\x%02x", r))...)
} else {
// 正常字符
ret = append(ret, r)
}
}
start += size
}
return string(ret)
}
var GBKSafeString = codec.GBKSafeString
func LastLine(s []byte) []byte {
s = bytes.TrimSpace(s)
scanner := bufio.NewScanner(bytes.NewReader(s))
scanner.Split(bufio.ScanLines)
var lastLine = s
for scanner.Scan() {
lastLine = scanner.Bytes()
}
return lastLine
}
func RemoveUnprintableChars(raw string) string {
scanner := bufio.NewScanner(bytes.NewBufferString(raw))
scanner.Split(bufio.ScanBytes)
var buf = bytes.NewBufferString("")
for scanner.Scan() {
c := scanner.Bytes()[0]
if c <= 0x7e && c >= 0x20 {
buf.WriteByte(c)
} else {
buf.WriteString(`\x` + fmt.Sprintf("%02x", c))
}
}
return buf.String()
}
func RemoveUnprintableCharsWithReplace(raw string, handle func(i byte) string) string {
scanner := bufio.NewScanner(bytes.NewBufferString(raw))
scanner.Split(bufio.ScanBytes)
var r []byte
for scanner.Scan() {
c := scanner.Bytes()[0]
if c <= 0x7e && c >= 0x20 {
r = append(r, c)
} else {
r = append(r, []byte(handle(c))...)
}
}
return string(r)
}
func RemoveUnprintableCharsWithReplaceItem(raw string) string {
return RemoveUnprintableCharsWithReplace(raw, func(i byte) string {
return fmt.Sprintf("__HEX_%v__", codec.EncodeToHex([]byte{i}))
})
}
func RemoveRepeatedWithStringSlice(slice []string) []string {
r := map[string]interface{}{}
for _, s := range slice {
r[s] = 1
}
var r2 []string
for k, _ := range r {
r2 = append(r2, k)
}
return r2
}
var (
titleRegexp = regexp.MustCompile(`(?is)\<title\>(.*?)\</?title\>`)
)
func ExtractTitleFromHTMLTitle(s string, defaultValue string) string {
var title string
l := titleRegexp.FindString(s)
if len(l) > 15 {
title = EscapeInvalidUTF8Byte([]byte(l))[7 : len(l)-8]
}
titleRunes := []rune(title)
if len(titleRunes) > 128 {
title = string(titleRunes[0:128]) + "..."
}
if title == "" {
return defaultValue
}
return title
}