-
Notifications
You must be signed in to change notification settings - Fork 0
/
util-func.go
143 lines (118 loc) · 2.41 KB
/
util-func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (c) 2024.
* Created by Andy Pangaribuan <https://github.com/apangaribuan>.
*
* This product is protected by copyright and distributed under
* licenses restricting copying, distribution and decompilation.
* All Rights Reserved.
*/
package util
import (
"errors"
"fmt"
"log"
"strings"
"github.com/andypangaribuan/gmod/fm"
)
func l3uidGenerate() ([]string, map[string]string, map[string]string) {
chars := func(withNumber bool) []string {
length := 26 * 2
if withNumber {
length += 10
}
var (
chars = make([]string, length)
rn = rune('0')
rl = rune('a')
ru = rune('A')
index = -1
)
if withNumber {
for i := 0; i < 10; i++ {
index++
chars[index] = string(rn + int32(i))
}
}
for i := 0; i < 26; i++ {
index++
chars[index] = string(ru + int32(i))
}
for i := 0; i < 26; i++ {
index++
chars[index] = string(rl + int32(i))
}
return chars
}
uidL3 := func(withNumber bool) []string {
var (
chars = chars(withNumber)
length = len(chars)
uids = make([]string, length*length*length)
index = 0
)
for _, c1 := range chars {
for _, c2 := range chars {
for _, c3 := range chars {
uids[index] = c1 + c2 + c3
index++
}
}
}
return uids
}
km := func(uids []string) (map[string]string, map[string]string) {
length := len(uids)
lc := len(fmt.Sprint(length))
mn := make(map[string]string, length)
mk := make(map[string]string, length)
for i, k := range uids {
n := l3uidAddZero(lc, fmt.Sprint(i))
mn[n] = k
mk[k] = n
}
return mn, mk
}
l3x := uidL3(false) // 140.608 item
l3xn, l3xk := km(l3x)
return l3x, l3xn, l3xk
}
func l3uidAddZero(length int, v string) string {
for {
if len(v) >= length {
break
}
v = "0" + v
}
return v
}
func l3uidCheckUnique(ids []string) {
mids := make(map[string]any, 0)
for _, id := range ids {
if id == "" {
log.Fatalf("l3-uid: found empty id")
}
_, ok := mids[id]
if !ok {
mids[id] = nil
} else {
log.Fatalf(`l3-uid: have duplicate id "%v"`, id)
}
}
}
func l3uidSplitter(uid string) ([]string, error) {
uid = strings.TrimSpace(uid)
if len(uid)%3 != 0 {
return nil, errors.New("l3-uid: invalid uid")
}
uids := make([]string, 0)
for {
if uid == "" {
break
}
length := len(uid)
size := fm.Ternary(length >= 3, 3, length)
uids = append(uids, uid[:size])
uid = uid[size:]
}
return uids, nil
}