2023-07-21 17:07:04 +08:00
|
|
|
|
package stringx
|
2020-09-01 10:34:11 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"text/template"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2024-02-29 22:12:50 +08:00
|
|
|
|
// 逻辑空字符串(由于gorm更新结构体只更新非零值,所以使用该值最为逻辑空字符串,方便更新结构体)
|
|
|
|
|
|
const LogicEmptyStr = "-"
|
|
|
|
|
|
|
|
|
|
|
|
// 是否为逻辑上空字符串
|
|
|
|
|
|
func IsLogicEmpty(str string) bool {
|
|
|
|
|
|
return str == "" || str == LogicEmptyStr
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
// 可判断中文
|
2023-07-21 17:07:04 +08:00
|
|
|
|
func Len(str string) int {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
return len([]rune(str))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 去除字符串左右空字符
|
2023-07-21 17:07:04 +08:00
|
|
|
|
func Trim(str string) string {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
return strings.Trim(str, " ")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-17 16:04:21 +08:00
|
|
|
|
// 去除字符串左右空字符与\n\r换行回车符
|
2023-07-21 17:07:04 +08:00
|
|
|
|
func TrimSpaceAndBr(str string) string {
|
2022-11-02 19:27:40 +08:00
|
|
|
|
return strings.TrimFunc(str, func(r rune) bool {
|
|
|
|
|
|
s := string(r)
|
2023-06-17 16:04:21 +08:00
|
|
|
|
return s == " " || s == "\n" || s == "\r"
|
2022-11-02 19:27:40 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
func SubString(str string, begin, end int) (substr string) {
|
|
|
|
|
|
// 将字符串的转换成[]rune
|
|
|
|
|
|
rs := []rune(str)
|
|
|
|
|
|
lth := len(rs)
|
|
|
|
|
|
|
|
|
|
|
|
// 简单的越界判断
|
|
|
|
|
|
if begin < 0 {
|
|
|
|
|
|
begin = 0
|
|
|
|
|
|
}
|
|
|
|
|
|
if begin >= lth {
|
|
|
|
|
|
begin = lth
|
|
|
|
|
|
}
|
|
|
|
|
|
if end > lth {
|
|
|
|
|
|
end = lth
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 返回子串
|
|
|
|
|
|
return string(rs[begin:end])
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-08 15:37:32 +08:00
|
|
|
|
func Camel2Underline(name string) string {
|
|
|
|
|
|
if name == "" {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
temp := strings.Split(name, "_")
|
|
|
|
|
|
var s string
|
|
|
|
|
|
for _, v := range temp {
|
|
|
|
|
|
vv := []rune(v)
|
|
|
|
|
|
if len(vv) > 0 {
|
|
|
|
|
|
if bool(vv[0] >= 'a' && vv[0] <= 'z') { //首字母大写
|
|
|
|
|
|
vv[0] -= 32
|
|
|
|
|
|
}
|
|
|
|
|
|
s += string(vv)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
func UnicodeIndex(str, substr string) int {
|
|
|
|
|
|
// 子串在字符串的字节位置
|
|
|
|
|
|
result := strings.Index(str, substr)
|
|
|
|
|
|
if result >= 0 {
|
|
|
|
|
|
// 获得子串之前的字符串并转换成[]byte
|
|
|
|
|
|
prefix := []byte(str)[0:result]
|
|
|
|
|
|
// 将子串之前的字符串转换成[]rune
|
|
|
|
|
|
rs := []rune(string(prefix))
|
|
|
|
|
|
// 获得子串之前的字符串的长度,便是子串在字符串的字符位置
|
|
|
|
|
|
result = len(rs)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 字符串模板解析
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func TemplateResolve(temp string, data any) string {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
t, _ := template.New("string-temp").Parse(temp)
|
|
|
|
|
|
var tmplBytes bytes.Buffer
|
|
|
|
|
|
|
|
|
|
|
|
err := t.Execute(&tmplBytes, data)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return tmplBytes.String()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func ReverStrTemplate(temp, str string, res map[string]any) {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
index := UnicodeIndex(temp, "{")
|
|
|
|
|
|
ei := UnicodeIndex(temp, "}") + 1
|
2023-07-21 17:07:04 +08:00
|
|
|
|
next := Trim(temp[ei:])
|
2020-09-01 10:34:11 +08:00
|
|
|
|
nextContain := UnicodeIndex(next, "{")
|
|
|
|
|
|
nextIndexValue := next
|
|
|
|
|
|
if nextContain != -1 {
|
|
|
|
|
|
nextIndexValue = SubString(next, 0, nextContain)
|
|
|
|
|
|
}
|
|
|
|
|
|
key := temp[index+1 : ei-1]
|
|
|
|
|
|
// 如果后面没有内容了,则取字符串的长度即可
|
|
|
|
|
|
var valueLastIndex int
|
|
|
|
|
|
if nextIndexValue == "" {
|
2023-07-21 17:07:04 +08:00
|
|
|
|
valueLastIndex = Len(str)
|
2020-09-01 10:34:11 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
valueLastIndex = UnicodeIndex(str, nextIndexValue)
|
|
|
|
|
|
}
|
2023-07-21 17:07:04 +08:00
|
|
|
|
value := Trim(SubString(str, index, valueLastIndex))
|
2020-09-01 10:34:11 +08:00
|
|
|
|
res[key] = value
|
|
|
|
|
|
// 如果后面的还有需要解析的,则递归调用解析
|
|
|
|
|
|
if nextContain != -1 {
|
2023-07-21 17:07:04 +08:00
|
|
|
|
ReverStrTemplate(next, Trim(SubString(str, UnicodeIndex(str, value)+Len(value), Len(str))), res)
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-01-05 08:55:34 +08:00
|
|
|
|
|
|
|
|
|
|
func TruncateStr(s string, length int) string {
|
|
|
|
|
|
if length >= len(s) {
|
|
|
|
|
|
return s
|
|
|
|
|
|
}
|
|
|
|
|
|
var last int
|
|
|
|
|
|
for i := range s {
|
|
|
|
|
|
if i > length {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
last = i
|
|
|
|
|
|
}
|
|
|
|
|
|
return s[:last]
|
|
|
|
|
|
}
|