-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumeral_system.go
More file actions
147 lines (111 loc) · 2.65 KB
/
Copy pathnumeral_system.go
File metadata and controls
147 lines (111 loc) · 2.65 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package lexorank
import (
"fmt"
)
const (
map36 = "0123456789abcdefghijklmnopqrstuvwxyz"
map64 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_abcdefghijklmnopqrstuvwxyz"
)
type LexoNumeralSystem interface {
GetBase() byte
GetPositiveChar() byte
GetNegativeChar() byte
GetRadixPointChar() byte
Digit(ch byte) (byte, error)
Char(digit byte) byte
}
var (
_ LexoNumeralSystem = (*LexoNumeralSystem10)(nil)
_ LexoNumeralSystem = (*LexoNumeralSystem36)(nil)
_ LexoNumeralSystem = (*LexoNumeralSystem64)(nil)
)
type LexoNumeralSystem10 struct {
}
func NewLexoNumeralSystem10() *LexoNumeralSystem10 {
return &LexoNumeralSystem10{}
}
func (n *LexoNumeralSystem10) GetBase() byte {
return 10
}
func (n *LexoNumeralSystem10) GetPositiveChar() byte {
return '+'
}
func (n *LexoNumeralSystem10) GetNegativeChar() byte {
return '-'
}
func (n *LexoNumeralSystem10) GetRadixPointChar() byte {
return '.'
}
func (n *LexoNumeralSystem10) Digit(ch byte) (byte, error) {
if ch >= '0' && ch <= '9' {
return ch - '0', nil
}
return 0, fmt.Errorf("not valid digit: %q", ch)
}
func (n *LexoNumeralSystem10) Char(digit byte) byte {
return digit + '0'
}
type LexoNumeralSystem36 struct {
}
func NewLexoNumeralSystem36() LexoNumeralSystem {
return &LexoNumeralSystem36{}
}
func (n *LexoNumeralSystem36) GetBase() byte {
return 36
}
func (n *LexoNumeralSystem36) GetPositiveChar() byte {
return '+'
}
func (n *LexoNumeralSystem36) GetNegativeChar() byte {
return '-'
}
func (n *LexoNumeralSystem36) GetRadixPointChar() byte {
return ':'
}
func (n *LexoNumeralSystem36) Digit(ch byte) (byte, error) {
switch {
case ch >= '0' && ch <= '9':
return ch - '0', nil
case ch >= 'a' && ch <= 'z':
return ch - 'a' + 10, nil
}
return 0, fmt.Errorf("not valid digit: %q", ch)
}
func (n *LexoNumeralSystem36) Char(digit byte) byte {
return map36[digit]
}
type LexoNumeralSystem64 struct {
}
func NewLexoNumeralSystem64() *LexoNumeralSystem64 {
return &LexoNumeralSystem64{}
}
func (n *LexoNumeralSystem64) GetBase() byte {
return 64
}
func (n *LexoNumeralSystem64) GetPositiveChar() byte {
return '+'
}
func (n *LexoNumeralSystem64) GetNegativeChar() byte {
return '-'
}
func (n *LexoNumeralSystem64) GetRadixPointChar() byte {
return ':'
}
func (n *LexoNumeralSystem64) Digit(ch byte) (byte, error) {
switch {
case ch >= '0' && ch <= '9':
return ch - '0', nil
case ch >= 'A' && ch <= 'Z':
return ch - 'A' + 10, nil
case ch == '^':
return 36, nil
case ch == '_':
return 37, nil
case ch >= 'a' && ch <= 'z':
return ch - 'a' + 38, nil
}
return 0, fmt.Errorf("not valid digit: %q", ch)
}
func (n *LexoNumeralSystem64) Char(digit byte) byte {
return map64[digit]
}