-
Notifications
You must be signed in to change notification settings - Fork 0
/
shorten.go
52 lines (41 loc) · 1.04 KB
/
shorten.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
package shorten
import (
"math/rand"
"strings"
"time"
)
var chars string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
func Shorten(url string) string {
var builder strings.Builder
builder.Grow(8)
rand.Seed(time.Now().UnixNano())
var alphabet []rune = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890")
builder.WriteString(encode(time.Now().Unix(), 3))
builder.WriteString(randomString(5, alphabet))
return builder.String()
}
func encode(num int64, bit int) string {
bytes := []byte{}
for i := 0; i < bit; i++ {
bytes = append(bytes, chars[num%62])
num = num / 62
}
// return bytes
reverse(bytes)
return string(bytes)
}
func reverse(a []byte) {
for left, right := 0, len(a)-1; left < right; left, right = left+1, right-1 {
a[left], a[right] = a[right], a[left]
}
}
func randomString(n int, alphabet []rune) string {
alphabetSize := len(alphabet)
var sb strings.Builder
for i := 0; i < n; i++ {
ch := alphabet[rand.Intn(alphabetSize)]
sb.WriteRune(ch)
}
s := sb.String()
return s
}