2022-05-14 13:45:51 +08:00
|
|
|
// Package util...
|
|
|
|
//
|
|
|
|
// Description : 字符串相关的工具
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2021-03-09 6:00 下午
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-07-09 14:42:31 +08:00
|
|
|
|
|
|
|
"github.com/axgle/mahonia"
|
2022-05-14 13:45:51 +08:00
|
|
|
)
|
|
|
|
|
2022-05-14 15:11:45 +08:00
|
|
|
// stringOperate ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:09 2022/5/14
|
|
|
|
type stringOperate struct {
|
|
|
|
}
|
|
|
|
|
2022-05-14 16:26:33 +08:00
|
|
|
// GenRandom 获取随机长度的字符串
|
2022-05-14 13:45:51 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 6:01 下午 2021/3/9
|
2022-05-14 16:26:33 +08:00
|
|
|
func (s *stringOperate) GenRandom(source string, length uint) string {
|
2022-05-14 13:45:51 +08:00
|
|
|
if length == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if len(source) == 0 {
|
|
|
|
//字符串为空,默认字符源为如下(去除易混淆的i/l):
|
|
|
|
source = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ"
|
|
|
|
}
|
|
|
|
strByte := []byte(source)
|
|
|
|
var genStrByte = make([]byte, 0)
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
for i := 0; i < int(length); i++ {
|
|
|
|
genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))])
|
|
|
|
}
|
|
|
|
return string(genStrByte)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Md5 对字符串进行md5加密
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 6:01 下午 2021/3/9
|
2022-05-14 15:11:45 +08:00
|
|
|
func (s *stringOperate) Md5(str string) string {
|
2022-05-14 13:45:51 +08:00
|
|
|
h := md5.New()
|
|
|
|
_, _ = h.Write([]byte(str))
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2022-07-12 20:12:04 +08:00
|
|
|
// GenRandomMd5 生成随机md5
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:11 2022/7/12
|
|
|
|
func (s *stringOperate) GenRandomMd5() string {
|
|
|
|
return s.Md5(s.GenRandom("", 16))
|
|
|
|
}
|
|
|
|
|
2022-05-14 13:45:51 +08:00
|
|
|
// SnakeCaseToCamel 蛇形字符串转换为驼峰
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 4:58 下午 2021/10/25
|
2022-05-14 15:11:45 +08:00
|
|
|
func (s *stringOperate) SnakeCaseToCamel(str string) string {
|
2022-05-14 13:45:51 +08:00
|
|
|
if len(str) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
builder := strings.Builder{}
|
|
|
|
index := 0
|
|
|
|
if str[0] >= 'a' && str[0] <= 'z' {
|
|
|
|
builder.WriteByte(str[0] - ('a' - 'A'))
|
|
|
|
index = 1
|
|
|
|
}
|
|
|
|
for i := index; i < len(str); i++ {
|
|
|
|
if str[i] == '_' && i+1 < len(str) {
|
|
|
|
if str[i+1] >= 'a' && str[i+1] <= 'z' {
|
|
|
|
builder.WriteByte(str[i+1] - ('a' - 'A'))
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 将ID转为大写
|
|
|
|
if str[i] == 'd' && i-1 >= 0 && (str[i-1] == 'i' || str[i-1] == 'I') && (i+1 == len(str) || i+1 < len(str) && str[i+1] == '_') {
|
|
|
|
builder.WriteByte('d' - ('a' - 'A'))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
builder.WriteByte(str[i])
|
|
|
|
}
|
|
|
|
return builder.String()
|
|
|
|
}
|
2022-07-09 14:42:31 +08:00
|
|
|
|
|
|
|
// Convert 字符串编码转换
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:38 2022/7/9
|
|
|
|
func (s *stringOperate) Convert(str string, sourceCode string, targetCode string) string {
|
|
|
|
sourceCoder := mahonia.NewDecoder(sourceCode)
|
|
|
|
sourceResult := sourceCoder.ConvertString(str)
|
|
|
|
targetCoder := mahonia.NewDecoder(targetCode)
|
|
|
|
_, cdata, _ := targetCoder.Translate([]byte(sourceResult), true)
|
|
|
|
return string(cdata)
|
|
|
|
}
|
2022-07-23 21:15:33 +08:00
|
|
|
|
|
|
|
// RemoveDuplicates 对列表数据去重
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:12 2022/7/23
|
|
|
|
func (s *stringOperate) RemoveDuplicates(sourceList []string) []string {
|
|
|
|
result := make([]string, 0)
|
|
|
|
hasDeal := make(map[string]bool)
|
|
|
|
if len(sourceList) == 0 {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
for _, val := range sourceList {
|
|
|
|
if _, exist := hasDeal[val]; exist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, val)
|
|
|
|
hasDeal[val] = true
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2022-10-10 20:52:06 +08:00
|
|
|
|
|
|
|
// Map2Query map参数转换为url query
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:51 2022/10/10
|
|
|
|
func (s *stringOperate) Map2Query(data map[string]string) string {
|
|
|
|
list := make([]string, 0)
|
|
|
|
for k, v := range data {
|
|
|
|
list = append(list, k+"="+v)
|
|
|
|
}
|
|
|
|
return strings.Join(list, "&")
|
|
|
|
}
|