-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshorturl.go
94 lines (82 loc) · 1.85 KB
/
shorturl.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
package shorturl
import (
"fmt"
"strings"
)
const (
//VERSION is SDK version
VERSION = "0.1"
default_alphabet = "asdfghjklURLEncoderConfigqwertyui"
default_block_size = uint(24)
min_length = 5
one = uint64(1)
)
type urlEncoder struct {
alphabet string
block_size uint
}
type URLEncoderConfig struct {
alphabet string
block_size uint
}
func NewURLEncoder(config *URLEncoderConfig) *urlEncoder {
alphabet := default_alphabet
block_size := default_block_size
if config.alphabet != "" {
alphabet = config.alphabet
}
if config.block_size != 0 {
block_size = config.block_size
}
url_encoder := &urlEncoder{
alphabet: alphabet,
block_size: block_size,
}
return url_encoder
}
func getBit(n uint64, pos uint) int {
if (n & (one << pos)) != 0 {
return 1
}
return 0
}
func (encoder *urlEncoder) encode(n uint64) uint64 {
for i, j := uint(0), uint(encoder.block_size-1); i < j; i, j = i+1, j-1 {
if getBit(n, i) != getBit(n, j) {
n ^= ((one << i) | (one << j))
}
}
return n
}
func (encoder *urlEncoder) enbase(x uint64) string {
n := uint64(len(encoder.alphabet))
result := []byte{}
for {
ch := encoder.alphabet[x%n]
result = append(result, ch)
x = x / n
if x == 0 && len(result) >= min_length {
break
}
}
revResult := []byte{}
for i := len(result) - 1; i >= 0; i-- {
revResult = append(revResult, result[i])
}
return string(revResult)
}
func (encoder *urlEncoder) debase(x string) uint64 {
n := uint64(len(encoder.alphabet))
result := uint64(0)
bits := []byte(x)
for _, bitValue := range bits {
result = result*n + uint64(strings.IndexByte(encoder.alphabet, bitValue))
}
return result
}
func (encoder *urlEncoder) EncodeURL(n uint64) string {
return encoder.enbase(encoder.encode(n))
}
func (encoder *urlEncoder) DecodeURL(n string) uint64 {
return encoder.encode(encoder.debase(n))
}