-
Notifications
You must be signed in to change notification settings - Fork 489
/
strings.go
74 lines (62 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
71
72
73
74
package utils
import (
"bytes"
"fmt"
"html"
"regexp"
"strconv"
)
type StringFilter interface {
Do(string) string
}
type StringFilterFunc func(string) string
func (f StringFilterFunc) Do(s string) string {
return f(s)
}
type StringFilterChain struct {
filters []StringFilter
}
func NewStringFilterChain(filter ...StringFilter) *StringFilterChain {
return &StringFilterChain{
filters: filter,
}
}
func (c *StringFilterChain) Do(str string) string {
for _, f := range c.filters {
str = f.Do(str)
}
return str
}
func ParseString(str string, filter ...StringFilter) string {
return NewStringFilterChain(filter...).Do(str)
}
var ParseUnicode = StringFilterFunc(func(str string) string {
buf := new(bytes.Buffer)
chars := []byte(str)
for i := 0; i < len(str); {
if chars[i] == 92 && chars[i+1] == 117 {
t, _ := strconv.ParseInt(string(chars[i+2:i+6]), 16, 32)
buf.WriteString(fmt.Sprintf("%c", t))
i += 6
} else {
buf.WriteByte(chars[i])
i++
}
}
return buf.String()
})
var ReplaceIllegalChar = StringFilterFunc(func(str string) string {
reg := regexp.MustCompile(`[\/\\\:\*\?\"\<\>\|]|[\.\s]+$`)
for reg.MatchString(str) {
str = reg.ReplaceAllString(str, "_")
}
return str
})
var UnescapeHTMLEntity = StringFilterFunc(html.UnescapeString)
var RemoveSymbolOtherChar = StringFilterFunc(func(str string) string {
reg := regexp.MustCompile(`\p{So}`)
for reg.MatchString(str) {
str = reg.ReplaceAllString(str, "_")
}
return str
})