mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 02:34:50 +08:00
63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
// Package charset
|
|
// @Link https://github.com/bufanyun/hotgo
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
|
// @Author Ms <133814250@qq.com>
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
package charset
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
r "math/rand"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// RandomCreateBytes 生成随机字串符
|
|
func RandomCreateBytes(n int, alphabets ...byte) []byte {
|
|
if len(alphabets) == 0 {
|
|
alphabets = []byte(`0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`)
|
|
}
|
|
var bytes = make([]byte, n)
|
|
var randBy bool
|
|
if num, err := rand.Read(bytes); num != n || err != nil {
|
|
randBy = true
|
|
}
|
|
for i, b := range bytes {
|
|
if randBy {
|
|
bytes[i] = alphabets[r.New(r.NewSource(time.Now().UnixNano())).Intn(len(alphabets))]
|
|
} else {
|
|
bytes[i] = alphabets[b%byte(len(alphabets))]
|
|
}
|
|
}
|
|
return bytes
|
|
}
|
|
|
|
// ParseErrStack 解析错误的堆栈信息
|
|
func ParseErrStack(err error) []string {
|
|
return ParseStack(gerror.Stack(err))
|
|
}
|
|
|
|
// ParseStack 解析堆栈信息
|
|
func ParseStack(st string) []string {
|
|
stack := gstr.Split(st, "\n")
|
|
for i := 0; i < len(stack); i++ {
|
|
stack[i] = gstr.Replace(stack[i], "\t", "--> ")
|
|
}
|
|
return stack
|
|
}
|
|
|
|
// SubstrAfter 截取指定字符后的内容
|
|
func SubstrAfter(str string, symbol string) string {
|
|
comma := strings.Index(str, symbol)
|
|
if comma < 0 { // -1 不存在
|
|
return ""
|
|
}
|
|
pos := strings.Index(str[comma:], symbol)
|
|
if comma+pos+1 > len(str) {
|
|
return ""
|
|
}
|
|
return str[comma+pos+1:]
|
|
}
|