123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package util
- import (
- "bytes"
- "fmt"
- "io"
- "math/rand"
- "strings"
- "github.com/google/uuid"
- "golang.org/x/text/encoding/simplifiedchinese"
- "golang.org/x/text/transform"
- )
- const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*?"
- const projectIdCharset = "0123456789ABCDEF"
- func GenerateUuid() string {
- uuid := uuid.New().String()
- return uuid
- }
- func GenRandomString(strLen int) string {
- sb := strings.Builder{}
- sb.Grow(strLen)
- for i := 0; i < strLen; i++ {
- sb.WriteByte(charset[rand.Intn(len(charset))])
- }
- return sb.String()
- }
- func GenRandomProjectID(strLen int) string {
- sb := strings.Builder{}
- sb.Grow(strLen)
- for i := 0; i < strLen; i++ {
- sb.WriteByte(projectIdCharset[rand.Intn(len(projectIdCharset))])
- }
- return sb.String()
- }
- func RemoveNullChars(s string) string {
-
- var result []byte
-
- for i := 0; i < len(s); i++ {
-
- if s[i] != 0 {
-
- result = append(result, s[i])
- }
- }
-
- return string(result)
- }
- func CharTransGbToUtf8(gb2312Bytes []byte) ([]byte, error) {
-
- reader := transform.NewReader(bytes.NewReader(gb2312Bytes), simplifiedchinese.GB18030.NewDecoder())
-
- utf8Bytes, err := io.ReadAll(reader)
- if err != nil {
- fmt.Println("Error converting encoding:", err)
- return nil, err
- }
-
- return utf8Bytes, nil
- }
- func CharTransUtf8ToGb(utf8Bytes []byte) ([]byte, error) {
- var buf bytes.Buffer
-
- encoder := simplifiedchinese.GB18030.NewEncoder()
-
- _, err := transform.NewWriter(&buf, encoder).Write(utf8Bytes)
- if err != nil {
- return nil, err
- }
-
- return buf.Bytes(), nil
- }
|