feat: 相关操作升级为泛型实现
This commit is contained in:
148
op_string/tool.go
Normal file
148
op_string/tool.go
Normal file
@ -0,0 +1,148 @@
|
||||
// Package op_string ...
|
||||
//
|
||||
// Description : op_string ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-10-13 15:28
|
||||
package op_string
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper/define"
|
||||
"github.com/spaolacci/murmur3"
|
||||
)
|
||||
|
||||
// GetLetterList 获取字符列表
|
||||
func GetLetterList() []string {
|
||||
return []string{
|
||||
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
|
||||
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
|
||||
}
|
||||
}
|
||||
|
||||
// SnakeCaseToCamel 蛇形转驼峰
|
||||
func SnakeCaseToCamel(str string) string {
|
||||
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()
|
||||
}
|
||||
|
||||
// Md5 计算Md5值
|
||||
func Md5(str string) define.StringResult {
|
||||
h := md5.New()
|
||||
_, err := io.WriteString(h, str)
|
||||
if nil != err {
|
||||
return define.StringResult{
|
||||
Value: "",
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
return define.StringResult{
|
||||
Value: hex.EncodeToString(h.Sum(nil)),
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// ClearChar 清理指定字符
|
||||
func ClearChar(str string, charList ...string) string {
|
||||
if len(charList) == 0 {
|
||||
return str
|
||||
}
|
||||
for _, item := range charList {
|
||||
str = strings.ReplaceAll(str, item, "")
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// ReplaceChineseChar 替换常见的中文符号
|
||||
func ReplaceChineseChar(str string) string {
|
||||
charTable := map[string]string{
|
||||
"(": "(",
|
||||
")": ")",
|
||||
":": ":",
|
||||
",": ",",
|
||||
"。": ".",
|
||||
"【": "]",
|
||||
"】": "]",
|
||||
}
|
||||
return ReplaceChar(str, charTable)
|
||||
}
|
||||
|
||||
// ReplaceChar 替换指定字符
|
||||
func ReplaceChar(str string, charTable map[string]string) string {
|
||||
if len(charTable) == 0 {
|
||||
return str
|
||||
}
|
||||
for k, v := range charTable {
|
||||
str = strings.ReplaceAll(str, k, v)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// HasSubStr 是否包含指定的子串
|
||||
func HasSubStr(str string, subStrList []string) bool {
|
||||
if len(subStrList) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, item := range subStrList {
|
||||
if strings.Contains(str, item) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// HashNumber 生成字符串哈希值
|
||||
func HashNumber(str string) define.BaseValueResult[uint64] {
|
||||
return define.BaseValueResult[uint64]{
|
||||
Value: murmur3.Sum64([]byte(str)),
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// Random 生成随机字符串
|
||||
func Random(length int, sourceCharList string) string {
|
||||
if length == 0 {
|
||||
return ""
|
||||
}
|
||||
if len(sourceCharList) == 0 {
|
||||
//字符串为空,默认字符源为如下(去除易混淆的i/l):
|
||||
sourceCharList = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ"
|
||||
}
|
||||
strByte := []byte(sourceCharList)
|
||||
var genStrByte = make([]byte, 0)
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := 0; i < length; i++ {
|
||||
genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))])
|
||||
}
|
||||
return string(genStrByte)
|
||||
}
|
Reference in New Issue
Block a user