-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.go
More file actions
90 lines (76 loc) · 1.69 KB
/
base.go
File metadata and controls
90 lines (76 loc) · 1.69 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
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
package basex
import (
"errors"
"math"
"math/bits"
)
var ErrInvalidCharacter = errors.New("invalid character in base encoding")
type Base struct {
base int
bitSize int
alpha []rune
baseMap map[rune]int
replacements map[rune]rune
padding rune
}
func NewBase(alpha []rune) *Base {
b := &Base{
base: len(alpha),
alpha: alpha,
baseMap: make(map[rune]int),
replacements: make(map[rune]rune),
}
b.bitSize = bits.Len(uint(b.base) - 1)
if b.bitSize == 0 {
b.bitSize = 1
} else if b.bitSize > 8 {
panic("invalid base size, must be <= 256")
}
for i := 0; i < b.base; i++ {
b.baseMap[b.alpha[i]] = i
}
return b
}
func (b Base) mapReplacements(input string) string {
if len(b.replacements) == 0 {
return input
}
var result []rune
for _, char := range input {
if replacement, ok := b.replacements[char]; ok {
result = append(result, replacement)
} else {
result = append(result, char)
}
}
return string(result)
}
func (b Base) EncodeInt(value uint64) string {
if value == 0 {
return string(b.alpha[0])
}
var result []rune
for value > 0 {
remainder := value % uint64(b.base)
result = append([]rune{b.alpha[remainder]}, result...)
value /= uint64(b.base)
}
return string(result)
}
func (b Base) DecodeInt(encoded string) (uint64, error) {
if len(encoded) == 0 {
return 0, nil
}
var value uint64
encoded = b.mapReplacements(encoded)
length := len(encoded) - 1
for pos, char := range encoded {
charPos := length - pos
if index, ok := b.baseMap[char]; ok {
value += uint64(index) * uint64(math.Pow(float64(b.base), float64(charPos)))
} else {
return 0, ErrInvalidCharacter
}
}
return value, nil
}