-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathShortURL.go
More file actions
46 lines (40 loc) · 1.11 KB
/
ShortURL.go
File metadata and controls
46 lines (40 loc) · 1.11 KB
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
/*
* ShortURL (https://github.com/delight-im/ShortURL)
* Copyright (c) delight.im (https://www.delight.im/)
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
*/
/**
* shorturl: Bijective conversion between natural numbers (IDs) and short strings
*
* shorturl.Encode() takes an ID and turns it into a short string
* shorturl.Decode() takes a short string and turns it into an ID
*
* Features:
* + large alphabet (51 chars) and thus very short resulting strings
* + proof against offensive words (removed 'a', 'e', 'i', 'o' and 'u')
* + unambiguous (removed 'I', 'l', '1', 'O' and '0')
*
* Example output:
* 123456789 <=> pgK8p
*/
package shorturl
import (
"strings"
)
const Alphabet string = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_"
const Base int = len(Alphabet)
func Encode(num int) string {
bytes := []byte{}
for num > 0 {
bytes = append([]byte{Alphabet[num%Base]}, bytes...)
num = num / Base
}
return string(bytes)
}
func Decode(str string) int {
num := 0
for i := 0; i < len(str); i++ {
num = num*Base + strings.Index(Alphabet, string(str[i]))
}
return num
}