1 Star 0 Fork 0

igo/pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
string.go 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
layteXiao 提交于 2023-12-19 21:58 . add func
package xutils
import (
"bytes"
"encoding/hex"
"gitee.com/igolang/pkg/xjson"
"math/rand"
"strings"
"time"
)
func StrArrayEmpty(arr []string) bool {
for _, v := range arr {
if v != "" {
return false
}
}
return true
}
func ParseMac(mac string) []byte {
if mac == "" {
return nil
}
macArr := make([]string, 0)
if strings.Contains(mac, ":") {
macArr = strings.Split(mac, ":")
} else if strings.Contains(mac, "-") {
macArr = strings.Split(mac, "-")
} else {
if len(mac)%2 != 0 {
return bytes.NewBufferString(mac).Bytes()
}
for i := 0; i < len(mac); {
s := mac[i : i+2]
macArr = append(macArr, s)
i += 2
}
}
if len(macArr) == 0 {
return nil
}
result := make([]byte, 0)
for _, v := range macArr {
b, err := hex.DecodeString(v)
if err != nil {
return nil
}
result = append(result, b[0])
}
return result
}
func Bytes2Mac(b []byte) string {
s := ""
for _, v := range b {
s = s + hex.EncodeToString([]byte{v}) + ":"
}
s = strings.TrimRight(s, ":")
return s
}
func FormatStrJson(b []byte) []byte {
if len(b) == 0 {
return b
}
b = bytes.Trim(b, `"`)
if IsJson(b) {
return b
}
b = bytes.ReplaceAll(b, []byte(`\`), []byte{})
return b
}
func IsJson(b []byte) bool {
return xjson.Valid(b)
}
func RandStr(length int) string {
str := "yzABCDEFGHIJKLlmnopq78STUrstuvwxPQR3456VWXYZ0129MNOabcdefghijk"
b := []byte(str)
var result []byte
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < length; i++ {
result = append(result, b[r.Intn(len(b))])
}
return string(result)
}
func ParseTag(tag string) map[string]any {
result := make(map[string]any)
tagArr := strings.Split(tag, ";")
for _, v := range tagArr {
vv := strings.Split(v, ":")
if len(vv) < 2 {
result[vv[0]] = true
continue
}
result[vv[0]] = vv[1]
}
return result
}
// FirstUpper 字符串首字母大写
func FirstUpper(s string) string {
if s == "" {
return ""
}
return strings.ToUpper(s[:1]) + s[1:]
}
// FirstLower 字符串首字母小写
func FirstLower(s string) string {
if s == "" {
return ""
}
return strings.ToLower(s[:1]) + s[1:]
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/igolang/pkg.git
git@gitee.com:igolang/pkg.git
igolang
pkg
pkg
v1.27.0

搜索帮助