12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package afreeca
- import (
- "fmt"
- "math"
- "math/rand"
- "regexp"
- "strconv"
- "strings"
- "time"
- )
- const regexUserId = `^(?is)(\w+)(?:\(([0-9]+)\))?$`
- func GenerateUID() string {
- mask := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
- arr := make([]string, 36)
- k := 0
- rand.New(rand.NewSource(time.Now().UnixNano()))
- for i := 0; i < 8; i++ {
- k++
- arr[k] = mask[int64(math.Floor(16*rand.Float64()))]
- }
- for i := 0; i < 3; i++ {
- for k := 0; k < 4; k++ {
- k++
- arr[k] = mask[int64(math.Floor(16*rand.Float64()))]
- }
- }
- hex := fmt.Sprintf("0000000%s",
- strings.ToUpper(strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 16)))
- l := len(hex)
- hexMask := []byte(hex[l-8 : l])
- for i := 0; i < 8; i++ {
- k++
- arr[k] = string(hexMask[i])
- }
- for i := 0; i < 4; i++ {
- k++
- arr[k] = mask[int64(math.Floor(16*rand.Float64()))]
- }
- return strings.Join(arr, "")
- }
- func NormalizeUserID(s string) string {
- var (
- m = regexp.MustCompile(regexUserId).FindStringSubmatch(s)
- groups int
- userId = strings.TrimSpace(s)
- )
- if groups = len(m); groups == 0 {
- return userId
- }
- return strings.TrimSpace(m[1])
- }
|