-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalnum.go
58 lines (47 loc) · 1.37 KB
/
alnum.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
package basexx
// Alnum is a type for bases from 2 through 36,
// where the digits for the first 10 digit values are '0' through '9'
// and the remaining digits are 'a' through 'z'.
// For decoding, upper-case 'A' through 'Z' are the same as lower-case.
type Alnum int
// N implements Base.N.
func (a Alnum) N() int64 { return int64(a) }
// Digit implements Base.Digit.
func (a Alnum) Digit(val int64) (byte, error) {
if val < 0 || val >= int64(a) {
return 0, ErrInvalid
}
if val < 10 {
return byte(val) + '0', nil
}
return byte(val) - 10 + 'a', nil
}
// Val implements Base.Val.
func (a Alnum) Val(digit byte) (int64, error) {
switch {
case '0' <= digit && digit <= '9':
return int64(digit - '0'), nil
case 'a' <= digit && digit <= 'z':
return int64(digit - 'a' + 10), nil
case 'A' <= digit && digit <= 'Z':
return int64(digit - 'A' + 10), nil
default:
return 0, ErrInvalid
}
}
const (
// Base2 uses digits "0" and "1"
Base2 = Alnum(2)
// Base8 uses digits "0" through "7"
Base8 = Alnum(8)
// Base10 uses digits "0" through "9"
Base10 = Alnum(10)
// Base12 uses digits "0" through "9" plus "a" and "b"
Base12 = Alnum(12)
// Base16 uses digits "0" through "9" plus "a" through "f"
Base16 = Alnum(16)
// Base32 uses digits "0" through "9" plus "a" through "v"
Base32 = Alnum(32)
// Base36 uses digits "0" through "9" plus "a" through "z"
Base36 = Alnum(36)
)