/
olc64.go
111 lines (106 loc) · 1.89 KB
/
olc64.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2017 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package notelib
const (
codeMaxLength = 14
codeModulus = 22
codeTable = "23456789CFGHJMPQRVWX+"
)
// OLCFromINT64 converts a 64-bit OLC code into a string
func OLCFromINT64(code int64) (olc string) {
if code > 0 {
for i := 0; i < codeMaxLength; i++ {
c := code % codeModulus
if c != codeModulus-1 {
olc = string([]byte(codeTable)[c]) + olc
}
code = code / codeModulus
}
}
return
}
// OLCToINT64 converts an OLC string into a 64-bit code
func OLCToINT64(olc string) (result int64) {
blen := len(olc)
for i := 0; i < codeMaxLength; i++ {
var c int64
if i >= blen {
c = codeModulus - 1
} else {
switch []byte(olc)[i] {
case 50: // 2
c = 0
case 51: // 3
c = 1
case 52: // 4
c = 2
case 53: // 5
c = 3
case 54: // 6
c = 4
case 55: // 7
c = 5
case 56: // 8
c = 6
case 57: // 9
c = 7
case 67: // C
fallthrough
case 99: // c
c = 8
case 70: // F
fallthrough
case 102: // f
c = 9
case 71: // G
fallthrough
case 103: // g
c = 10
case 72: // H
fallthrough
case 104: // h
c = 11
case 74: // J
fallthrough
case 106: // j
c = 12
case 77: // M
fallthrough
case 109: // m
c = 13
case 80: // P
fallthrough
case 112: // p
c = 14
case 81: // Q
fallthrough
case 113: // q
c = 15
case 82: // R
fallthrough
case 114: // r
c = 16
case 86: // V
fallthrough
case 118: // v
c = 17
case 87: // W
fallthrough
case 119: // w
c = 18
case 88: // X
fallthrough
case 120: // x
c = 19
case 43: // +
c = 20
default:
result = -(int64(i) + 1)
return
}
}
result = result*codeModulus + c
}
return
}