Files
pet-house/core/helper/str/string.go
T
2024-03-27 23:25:08 +08:00

64 lines
1.2 KiB
Go

package str
import (
"encoding/json"
"regexp"
"strings"
"unicode"
)
type StringArray string
func (r StringArray) MarshalJSON() ([]byte, error) {
items := []string{}
if string(r) != "" {
items = strings.Split(string(r), ",")
}
for _, item := range items {
item = strings.TrimSpace(item)
}
return json.Marshal(items)
}
func Ellipsis(text string, length int) string {
r := []rune(text)
if len(r) > length {
return string(r[0:length]) + "..."
}
return text
}
func HasChinese(str string) bool {
for _, r := range str {
if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
return true
}
}
return false
}
func IsGBK(data []byte) bool {
length := len(data)
var i int = 0
for i < length {
if data[i] <= 0x7f {
//编码0~127,只有一个字节的编码,兼容ASCII码
i++
continue
} else {
//大于127的使用双字节编码,落在gbk编码范围内的字符
if data[i] >= 0x81 &&
data[i] <= 0xfe &&
data[i+1] >= 0x40 &&
data[i+1] <= 0xfe &&
data[i+1] != 0xf7 {
i += 2
continue
} else {
return false
}
}
}
return true
}