-
Notifications
You must be signed in to change notification settings - Fork 1
/
readable.go
89 lines (73 loc) · 1.97 KB
/
readable.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
package base256
import (
utls "github.com/cyrildever/go-utls/common/utils"
)
// Based on the 512-characters UTF-8 table - @see https://www.utf8-chartable.de/unicode-utf8-table.pl?number=512
var CHARSET = []rune("!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^`abcdefghijklmnopqrstuvwxyz{|}€¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷ùúûüýÿăąĊčđĕĘğħĩĭıĵķĿŀŁłňŋŏœŖřŝşŦŧũūůŲŵſƀƁƂƄƆƇƔƕƗƙƛƜƟƢƥƦƧƩƪƭƮưƱƲƵƸƺƾǀǁǂƿǬǮǵǶǹǻǿ")
//--- TYPES
// Readable represents a character in a readable base-256 charset
type Readable string
//--- METHODS
// Bytes ...
func (b256 Readable) Bytes() []byte {
var barray []byte
for _, char := range b256 {
barray = append(barray, byte(IndexOf(char)))
}
return barray
}
// IsEmpty ...
func (b256 Readable) IsEmpty() bool {
return b256.String() == ""
}
// Len ...
func (b256 Readable) Len() int {
length := 0
for range b256 {
length++
}
return length
}
// NonEmpty ...
func (b256 Readable) NonEmpty() bool {
return !b256.IsEmpty()
}
// String ...
func (b256 Readable) String() string {
return string(b256)
}
// ToHex ...
func (b256 Readable) ToHex() string {
return utls.ToHex(b256.Bytes())
}
//--- FUNCTION
// CharAt returns the character string at the passed index in the Base-256 character set
func CharAt(index int) string {
return string(CHARSET[index : index+1])
}
// IndexOf ...
func IndexOf(char rune) int {
for i, r := range CHARSET {
if r == char {
return i
}
}
return -1
}
// ToBase256Readable ...
func ToBase256Readable(barray []byte) Readable {
str := ""
for _, b := range barray {
str += CharAt(int(b))
}
return Readable(str)
}
// HexToReadable ...
func HexToReadable(hex string) (b256 Readable, err error) {
bytes, err := utls.FromHex(hex)
if err != nil {
return
}
b256 = ToBase256Readable(bytes)
return
}