forked from codeskyblue/gobuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
165 lines (145 loc) · 3.12 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
package com
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
"io"
"strconv"
"strings"
"unicode"
)
// explode string with proper chars
func Explode(str string, sep string) []string {
return strings.Split(str, sep)
}
// join string array to string with connection chars
func Join(str []string, sep string) string {
return strings.Join(str, sep)
}
// substring from start position and belong length
func SubStr(str string, start, length int) string {
rs := []rune(str)
rl := len(rs)
end := 0
if start < 0 {
start = rl - 1 + start
}
end = start + length
if start > end {
start, end = end, start
}
if start < 0 {
start = 0
}
if start > rl {
start = rl
}
if end < 0 {
end = 0
}
if end > rl {
end = rl
}
return string(rs[start:end])
}
// md5 hash string
func Md5(str string) string {
m := md5.New()
io.WriteString(m, str)
return fmt.Sprintf("%x", m.Sum(nil))
}
func sha(m hash.Hash, str string) string {
io.WriteString(m, str)
return fmt.Sprintf("%x", m.Sum(nil))
}
// sha1 hash string
func Sha1(str string) string {
return sha(sha1.New(), str)
}
// sha256 hash string
func Sha256(str string) string {
return sha(sha256.New(), str)
}
// trim space on left
func Ltrim(str string) string {
return strings.TrimLeftFunc(str, unicode.IsSpace)
}
// trim space on right
func Rtrim(str string) string {
return strings.TrimRightFunc(str, unicode.IsSpace)
}
// trim space in all string length
func Trim(str string) string {
return strings.TrimSpace(str)
}
// repeat string times
func StrRepeat(str string, times int) string {
return strings.Repeat(str, times)
}
// replace find all occurs to string
func StrReplace(str string, find string, to string) string {
return strings.Replace(str, find, to, -1)
}
// find the first occur position, if not found return -1
func StrPos(str string, find string) int {
return strings.Index(str, find)
}
// convert to upper
func StrToUpper(str string) string {
return strings.ToUpper(str)
}
// convert to lower
func StrToLower(str string) string {
return strings.ToLower(str)
}
// convert first letter to upper
func UcFirst(str string) string {
return strings.Title(str)
}
// IsLetter returns true if the 'l' is an English letter.
func IsLetter(l uint8) bool {
n := (l | 0x20) - 'a'
if n >= 0 && n < 26 {
return true
}
return false
}
// Expand replaces {k} in template with match[k] or subs[atoi(k)] if k is not in match.
func Expand(template string, match map[string]string, subs ...string) string {
var p []byte
var i int
for {
i = strings.Index(template, "{")
if i < 0 {
break
}
p = append(p, template[:i]...)
template = template[i+1:]
i = strings.Index(template, "}")
if s, ok := match[template[:i]]; ok {
p = append(p, s...)
} else {
j, _ := strconv.Atoi(template[:i])
if j >= len(subs) {
p = append(p, []byte("Missing")...)
} else {
p = append(p, subs[j]...)
}
}
template = template[i+1:]
}
p = append(p, template...)
return string(p)
}
// Reverse s string, support unicode
func Reverse(s string) string {
n := len(s)
runes := make([]rune, n)
for _, rune := range s {
n--
runes[n] = rune
}
return string(runes[n:])
}