mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 19:17:38 +08:00
84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"math/big"
|
|
"reflect"
|
|
"unsafe"
|
|
)
|
|
|
|
func RandomString(len int) string {
|
|
return RandomStringRange(len, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
|
|
}
|
|
|
|
func RandomStringRange(len int, str string) string {
|
|
var res string
|
|
b := bytes.NewBufferString(str)
|
|
length := b.Len()
|
|
bigInt := big.NewInt(int64(length))
|
|
for i := 0; i < len; i++ {
|
|
randomInt, _ := rand.Int(rand.Reader, bigInt)
|
|
res += string(str[randomInt.Int64()])
|
|
}
|
|
return res
|
|
}
|
|
|
|
func ChunkString(s string, chunkSize int) []string {
|
|
var chunks []string
|
|
runes := []rune(s)
|
|
|
|
if len(runes) == 0 || len(runes) <= chunkSize {
|
|
return []string{s}
|
|
}
|
|
|
|
for i := 0; i < len(runes); i += chunkSize {
|
|
nn := i + chunkSize
|
|
if nn > len(runes) {
|
|
nn = len(runes)
|
|
}
|
|
chunks = append(chunks, string(runes[i:nn]))
|
|
}
|
|
return chunks
|
|
}
|
|
|
|
func ChineseLength(str string, limit int) int {
|
|
sum := 0
|
|
for _, r := range []rune(str) {
|
|
switch {
|
|
case r >= '\u0000' && r <= '\u007F':
|
|
sum += 1
|
|
case r >= '\u0080' && r <= '\u07FF':
|
|
sum += 2
|
|
case r >= '\u0800' && r <= '\uFFFF':
|
|
sum += 3
|
|
default:
|
|
sum += 4
|
|
}
|
|
if sum >= limit {
|
|
break
|
|
}
|
|
}
|
|
return sum
|
|
}
|
|
|
|
// from github.com/savsgio/gotils/strconv
|
|
// B2S converts byte slice to a string without memory allocation.
|
|
// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .
|
|
func B2S(b []byte) string {
|
|
return *(*string)(unsafe.Pointer(&b))
|
|
}
|
|
|
|
// S2B converts string to a byte slice without memory allocation.
|
|
//
|
|
// Note it may break if string and/or slice header will change
|
|
// in the future go versions.
|
|
func S2B(s string) (b []byte) {
|
|
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
|
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
bh.Data = sh.Data
|
|
bh.Cap = sh.Len
|
|
bh.Len = sh.Len
|
|
return
|
|
}
|