-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.go
165 lines (159 loc) · 4.1 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 gutils
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"unicode/utf8"
"github.com/axgle/mahonia"
)
// MsgTplCompile 字符串模板渲染
// "您的验证码{0},过期时间{1}分钟", ["123", "5"] => 您的验证码123,过期时间5分钟
func MsgTplCompile(tpl string, args []string) string {
re, _ := regexp.Compile(`\{\d\}`)
maxI := len(args)
for _, indexStr := range re.FindAll([]byte(tpl), -1) {
i, err := strconv.Atoi(string(indexStr[1 : len(indexStr)-1]))
if err != nil || i < 0 || i >= maxI {
continue
}
tpl = strings.ReplaceAll(tpl, string(indexStr), args[i])
}
return tpl
}
// MustUtf8 强制字符串必须为utf8
func MustUtf8(s string) string {
if !utf8.ValidString(s) {
v := make([]rune, 0, len(s))
for i, r := range s {
if r == utf8.RuneError {
_, size := utf8.DecodeRuneInString(s[i:])
if size == 1 {
continue
}
}
v = append(v, r)
}
s = string(v)
}
return s
}
// ConvertToString 转换字符串编码
// src 字符串 srccode 源编码 tagCode 目标编码
func ConvertToString(src, srcCode, tagCode string) string {
srcCoder := mahonia.NewDecoder(srcCode)
srcResult := srcCoder.ConvertString(src)
tagCoder := mahonia.NewDecoder(tagCode)
_, cdata, _ := tagCoder.Translate([]byte(srcResult), true)
result := string(cdata)
return result
}
// ParseArrString 解析字符串数组
// str 待解析字符串 sep 分割符号 dto 期望解析切片(指针)
func ParseArrString(str, sep string, dto interface{}) error {
arr := strings.Split(str, sep)
typ := reflect.TypeOf(dto)
if typ.Kind() != reflect.Ptr {
return fmt.Errorf("dto is not ptr")
}
etyp := typ.Elem()
if etyp.Kind() != reflect.Slice {
return fmt.Errorf("dto type is not slice")
}
eetyp := etyp.Elem()
rval := make([]reflect.Value, 0)
switch eetyp.Kind() {
case reflect.Bool:
for _, s := range arr {
if v, err := strconv.ParseBool(s); err == nil {
rval = append(rval, reflect.ValueOf(v))
}
}
case reflect.Int:
for _, s := range arr {
if v, err := strconv.Atoi(s); err == nil {
rval = append(rval, reflect.ValueOf(v))
}
}
case reflect.Int8:
for _, s := range arr {
if v, err := strconv.ParseInt(s, 0, 8); err == nil {
rval = append(rval, reflect.ValueOf(int8(v)))
}
}
case reflect.Int16:
for _, s := range arr {
if v, err := strconv.ParseInt(s, 0, 16); err == nil {
rval = append(rval, reflect.ValueOf(int16(v)))
}
}
case reflect.Int32:
for _, s := range arr {
if v, err := strconv.ParseInt(s, 0, 32); err == nil {
rval = append(rval, reflect.ValueOf(int32(v)))
}
}
case reflect.Int64:
for _, s := range arr {
if v, err := strconv.ParseInt(s, 0, 64); err == nil {
rval = append(rval, reflect.ValueOf(v))
}
}
case reflect.Uint:
for _, s := range arr {
if v, err := strconv.ParseUint(s, 0, 0); err == nil {
rval = append(rval, reflect.ValueOf(uint(v)))
}
}
case reflect.Uint8:
for _, s := range arr {
if v, err := strconv.ParseUint(s, 0, 8); err == nil {
rval = append(rval, reflect.ValueOf(uint8(v)))
}
}
case reflect.Uint16:
for _, s := range arr {
if v, err := strconv.ParseUint(s, 0, 16); err == nil {
rval = append(rval, reflect.ValueOf(uint16(v)))
}
}
case reflect.Uint32:
for _, s := range arr {
if v, err := strconv.ParseUint(s, 0, 32); err == nil {
rval = append(rval, reflect.ValueOf(uint32(v)))
}
}
case reflect.Uint64:
for _, s := range arr {
if v, err := strconv.ParseUint(s, 0, 64); err == nil {
rval = append(rval, reflect.ValueOf(v))
}
}
case reflect.Float64:
for _, s := range arr {
if v, err := strconv.ParseFloat(s, 64); err == nil {
rval = append(rval, reflect.ValueOf(v))
}
}
case reflect.Float32:
for _, s := range arr {
if v, err := strconv.ParseFloat(s, 32); err == nil {
rval = append(rval, reflect.ValueOf(float32(v)))
}
}
case reflect.String:
for _, s := range arr {
if s != "" {
rval = append(rval, reflect.ValueOf(s))
}
}
default:
return fmt.Errorf("dto type Unknown")
}
val := reflect.ValueOf(dto).Elem()
if val.CanSet() {
val.Set(reflect.Append(val, rval...))
}
return nil
}