-
Notifications
You must be signed in to change notification settings - Fork 4
/
string.go
188 lines (172 loc) · 3.86 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
package Figo
import (
"encoding/json"
"fmt"
"github.com/oliveagle/jsonpath"
"github.com/quexer/utee"
"regexp"
"strconv"
"strings"
)
func SplitUTF8BOM(str string) string {
b := []byte(str)
if len(b) < 3 {
return str
}
prefix := fmt.Sprintf("%X", b[0:3])
if prefix == "EFBBBF" {
return string(b[3:len(b)])
}
return str
}
type Parser struct {
PrepareReg []string
ProcessReg []string
}
func (p *Parser) Exe(content string) []string {
prep := func(reg string, contents ...string) []string {
var result []string
for _, content := range contents {
rs := regexp.MustCompile(reg).FindAllString(content, -1)
result = append(result, rs...)
}
return result
}
proc := func(reg string, contents ...string) []string {
var result []string
for _, content := range contents {
rs := regexp.MustCompile(reg).ReplaceAllString(content, "")
result = append(result, rs)
}
return result
}
result := []string{content}
for _, reg := range p.PrepareReg {
result = prep(reg, result...)
}
for _, reg := range p.ProcessReg {
result = proc(reg, result...)
}
return TrimAndClear(result...)
}
func TrimAndClear(strs ...string) []string {
result := []string{}
for _, v := range strs {
v = strings.TrimSpace(v)
if v != "" {
result = append(result, v)
}
}
return result
}
func ParserWithPipe(content string, ps ...Parser) []string {
exc := func(parser Parser, contents ...string) []string {
var result []string
for _, v := range contents {
result = append(result, parser.Exe(v)...)
}
return result
}
result := []string{content}
for _, parser := range ps {
result = exc(parser, result...)
}
return result
}
func Md5ShardPiece(key string, piece int) int {
shardVal, err := strconv.ParseUint(utee.PlainMd5(key)[16:32], 16, 0)
utee.Chk(err)
shardVal = shardVal % uint64(piece)
return int(shardVal)
}
func Md5Shard(key string, piece int) string {
return fmt.Sprint(key, "_", Md5ShardPiece(key, piece))
}
// snake string, XxYy to xx_yy , XxYY to xx_yy
func SnakeString(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '_')
}
if d != '_' {
j = true
}
data = append(data, d)
}
return strings.ToLower(string(data[:]))
}
func SnakeStrings(strs ...string) (result []string) {
for _, str := range strs {
s := SnakeString(str)
result = append(result, s)
}
return result
}
// camel string, xx_yy to XxYy
func CamelString(s string) string {
data := make([]byte, 0, len(s))
j := false
k := false
num := len(s) - 1
for i := 0; i <= num; i++ {
d := s[i]
if k == false && d >= 'A' && d <= 'Z' {
k = true
}
if d >= 'a' && d <= 'z' && (j || k == false) {
d = d - 32
j = false
k = true
}
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
j = true
continue
}
data = append(data, d)
}
return string(data[:])
}
func JsonPathParse(data, jsonPath string) interface{} {
var json_data interface{}
err := json.Unmarshal([]byte(data), &json_data)
utee.Chk(err)
v, err := jsonpath.JsonPathLookup(json_data, jsonPath)
utee.Chk(err)
return v
}
func FillPrefix(v, fillChar string, resultLen int) string {
if len(v) > resultLen || len(fillChar) <= 0 {
return v
}
blank, result := fillChar[0:1], v
for i := 0; i < resultLen-len(v); i++ {
result = fmt.Sprint(blank, result)
}
return result
}
func FillSuffix(v, fillChar string, resultLen int) string {
if len(v) > resultLen || len(fillChar) <= 0 {
return v
}
blank, result := fillChar[0:1], v
for i := 0; i < resultLen-len(v); i++ {
result = fmt.Sprint(result, blank)
}
return result
}
type SwitchStrParam struct {
Condition bool
Value string
}
func SwitchString(defaultVal string, params ... SwitchStrParam) string {
for _,param:=range params{
if param.Condition {
return param.Value
}
}
return defaultVal
}