-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvl64.go
74 lines (60 loc) · 1.92 KB
/
vl64.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
// Copyright 2018 Ewout van Mansom. All rights reserved.
// Use of this source code is governed by a AGPLv3
// license that can be found in the LICENSE file.
// Package vl64 provides an implementation of Sulake's FUSE mixed radix numeric system.
package vl64
import (
"math"
)
const (
// Negative constant is a char code for negative representation.
Negative int = 72
// Positive constant is a char code for positive representation.
Positive int = 73
// MaxInteger as 32-bit integer causes VL64 to have max length of 6.
MaxInteger int = 6
)
// Encode transforms integer i into a mixed radix representation.
func Encode(i int) []byte {
vl64 := make([]byte, 6) // 32-bit integer causes VL64 to have max length of 6.
num := int(math.Abs(float64(i))) // Operate on normalized, positive integer
len := 1 // Length indicator, updated during encode
j := 1
var indicator int
if i < 0 {
indicator = 'D'
} else {
indicator = '@'
}
vl64[0] = byte(num%4 + indicator) // Base4 char, positive(+64)/negative(+68) indicator
num /= 4 // Base4 processed, prepare for remaining b64 symbols
for j < 6 {
vl64[j] = byte(num%64 + '@') // b64
num /= 64
if vl64[j] != '@' {
len = j + 1 // @ = padding / zero symbol
}
j++
}
vl64[0] = byte(int(vl64[0]) + len*8)
return vl64[:len]
}
// Length calculates the length of a VL64 sequence.
func Length(firstChar byte) int {
return (int(firstChar) - int('@')) / 8
}
// Decode transforms the mixed radix representation into an integer.
func Decode(s []byte) (int, int) {
len := Length(s[0]) // (firstChar - 64) / 8
total := int(s[0]) % 4 // Base4 value
inc := 0
for inc < len { // Increment all b64 symbols to the total
total += (int(s[inc]) - int(byte('@'))) * int((math.Pow(float64(64), float64(inc)))/16)
inc++
}
positive := s[0]%8 < 4 // Base4 positive/negative
if positive {
return total, len
}
return -total, len
}