-
Notifications
You must be signed in to change notification settings - Fork 183
/
strutil.go
224 lines (186 loc) · 4.63 KB
/
strutil.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
package strutil
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"text/template"
)
// Position for padding string
const (
PosLeft uint8 = iota
PosRight
)
/*************************************************************
* String filtering
*************************************************************/
// Trim string
func Trim(s string, cutSet ...string) string {
if len(cutSet) > 0 && cutSet[0] != "" {
return strings.Trim(s, cutSet[0])
}
return strings.TrimSpace(s)
}
// TrimLeft char in the string.
func TrimLeft(s string, cutSet ...string) string {
if len(cutSet) > 0 {
return strings.TrimLeft(s, cutSet[0])
}
return strings.TrimLeft(s, " ")
}
// TrimRight char in the string.
func TrimRight(s string, cutSet ...string) string {
if len(cutSet) > 0 {
return strings.TrimRight(s, cutSet[0])
}
return strings.TrimRight(s, " ")
}
// FilterEmail filter email, clear invalid chars.
func FilterEmail(s string) string {
s = strings.TrimSpace(s)
i := strings.LastIndex(s, "@")
if i == -1 {
return s
}
// According to rfc5321, "The local-part of a mailbox MUST BE treated as case sensitive"
return s[0:i] + "@" + strings.ToLower(s[i+1:])
}
/*************************************************************
* String operation
*************************************************************/
// Split string to slice. will clear empty string node.
func Split(s, sep string) (ss []string) {
if s = strings.TrimSpace(s); s == "" {
return
}
for _, val := range strings.Split(s, sep) {
if val = strings.TrimSpace(val); val != "" {
ss = append(ss, val)
}
}
return
}
// Substr for a string.
func Substr(s string, pos, length int) string {
runes := []rune(s)
strLen := len(runes)
// pos is to large
if pos >= strLen {
return ""
}
l := pos + length
if l > strLen {
l = strLen
}
return string(runes[pos:l])
}
// Padding a string.
func Padding(s, pad string, length int, pos uint8) string {
diff := len(s) - length
if diff >= 0 { // do not need padding.
return s
}
if pad == "" || pad == " " {
mark := ""
if pos == PosRight { // to right
mark = "-"
}
// padding left: "%7s", padding right: "%-7s"
tpl := fmt.Sprintf("%s%d", mark, length)
return fmt.Sprintf(`%`+tpl+`s`, s)
}
if pos == PosRight { // to right
return s + Repeat(pad, -diff)
}
return Repeat(pad, -diff) + s
}
// PadLeft a string.
func PadLeft(s, pad string, length int) string {
return Padding(s, pad, length, PosLeft)
}
// PadRight a string.
func PadRight(s, pad string, length int) string {
return Padding(s, pad, length, PosRight)
}
// Repeat repeat a string
func Repeat(s string, times int) string {
if times < 2 {
return s
}
var ss []string
for i := 0; i < times; i++ {
ss = append(ss, s)
}
return strings.Join(ss, "")
}
// RepeatRune repeat a rune char.
func RepeatRune(char rune, times int) (chars []rune) {
for i := 0; i < times; i++ {
chars = append(chars, char)
}
return
}
// Replaces replace multi strings
//
// pairs: {old1: new1, old2: new2, ...}
//
// Can also use:
// strings.NewReplacer("old1", "new1", "old2", "new2").Replace(str)
func Replaces(str string, pairs map[string]string) string {
ss := make([]string, len(pairs)*2)
for old, newVal := range pairs {
ss = append(ss, old, newVal)
}
return strings.NewReplacer(ss...).Replace(str)
}
// PrettyJSON get pretty Json string
// Deprecated
// please use fmtutil.PrettyJSON() or jsonutil.Pretty() instead it
func PrettyJSON(v interface{}) (string, error) {
out, err := json.MarshalIndent(v, "", " ")
return string(out), err
}
// RenderTemplate render text template
func RenderTemplate(input string, data interface{}, fns template.FuncMap, isFile ...bool) string {
return RenderText(input, data, fns, isFile...)
}
// RenderText render text template
func RenderText(input string, data interface{}, fns template.FuncMap, isFile ...bool) string {
t := template.New("simple-text")
t.Funcs(template.FuncMap{
// don't escape content
"raw": func(s string) string {
return s
},
"trim": func(s string) string {
return strings.TrimSpace(string(s))
},
// join strings
"join": func(ss []string, sep string) string {
return strings.Join(ss, sep)
},
// lower first char
"lcFirst": func(s string) string {
return LowerFirst(s)
},
// upper first char
"upFirst": func(s string) string {
return UpperFirst(s)
},
})
// custom add template functions
if len(fns) > 0 {
t.Funcs(fns)
}
if len(isFile) > 0 && isFile[0] {
template.Must(t.ParseFiles(input))
} else {
template.Must(t.Parse(input))
}
// use buffer receive rendered content
buf := new(bytes.Buffer)
if err := t.Execute(buf, data); err != nil {
panic(err)
}
return buf.String()
}