/
str_util.go
185 lines (164 loc) · 3.2 KB
/
str_util.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
package mcore
//String Utils
import (
"bytes"
"fmt"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io/ioutil"
"strings"
"unicode"
)
const (
UP_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
// IsUpLetter
func IsUpLetter(l string) bool {
if len(l) != 1 {
return false
}
return strings.Contains(UP_LETTERS, l)
}
// SubRight
func SubRight(s string, start int) string {
return Sub(s, start, len(s)-start)
}
// SubLeft
func SubLeft(s string, end int) string {
return Sub(s, 0, end)
}
// Sub
func Sub(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])
}
// SubByByte
func SubByByte(str string, length int) string {
bs := []byte(str)[:length]
bl := 0
for i := len(bs) - 1; i >= 0; i-- {
switch {
case bs[i] >= 0 && bs[i] <= 127:
return string(bs[:i+1])
case bs[i] >= 128 && bs[i] <= 191:
bl++
case bs[i] >= 192 && bs[i] <= 253:
cl := 0
switch {
case bs[i]&252 == 252:
cl = 6
case bs[i]&248 == 248:
cl = 5
case bs[i]&240 == 240:
cl = 4
case bs[i]&224 == 224:
cl = 3
default:
cl = 2
}
if bl+1 == cl {
return string(bs[:i+cl])
}
return string(bs[:i])
}
}
return ""
}
// GetFixedWidthNum used for line number
// add space left side, align right
func GetFixedWidthNum(num int, width int) string {
s := fmt.Sprint(num)
return GetFixedWidthString(s, width, " ", false)
}
// GetFixedWidthStringAlignLeft add blank to left
// used for pretty output.
func GetFixedWidthStringAlignLeft(v string, width int) string {
return GetFixedWidthString(v, width, " ", true)
}
// GetFixedWidthStringAlignRight add blank right
func GetFixedWidthStringAlignRight(v string, width int) string {
return GetFixedWidthString(v, width, " ", false)
}
// GetFixedWidthString
func GetFixedWidthString(
v string,
width int,
fil string,
alignLeft bool) string {
n := StringWidth(v)
// not cut long string
if n >= width {
return v
}
// less than width
fillNums := width - n
if fil == "" {
fil = " "
}
fillStr := strings.Repeat(fil, fillNums)
// add blank right side
if alignLeft {
return v + fillStr
}
// add blank left side
return fillStr + v
}
// EncodeGBK
func EncodeGBK(in string) string {
reader := transform.NewReader(bytes.NewReader([]byte(in)), simplifiedchinese.GBK.NewEncoder())
d, _ := ioutil.ReadAll(reader)
return string(d)
}
// UpperCaseFirst
func UpperCaseFirst(in string) string {
in = strings.TrimSpace(in)
begin := SubLeft(in, 1)
begin = strings.ToUpper(begin)
end := SubRight(in, 1)
return begin + end
}
// StringWidth chinese char is width 2
func StringWidth(v string) int {
r := 0
for _, c := range []rune(v) {
if IsChineseChar(string(c)) {
r = r + 2
} else {
r = r + 1
}
}
return r
}
func IsChineseChar(str string) bool {
for _, r := range str {
if unicode.Is(unicode.Scripts["Han"], r) {
return true
}
}
return false
}
// StringLen how many chars
func StringLen(v string) int {
return len([]rune(v))
}