Add decode map

main
Wisellama 2024-02-19 09:05:16 -08:00
parent 094bac9c10
commit 9edb7e20c7
1 changed files with 64 additions and 5 deletions

69
main.go
View File

@ -130,6 +130,8 @@ func GetMSBytes(t time.Time) ([]byte, error) {
}
var (
// crockfordEncodeMap takes binary values and converts them to
// characters in Crockford base32.
crockfordEncodeMap = map[uint8]rune{
0: '0',
1: '1',
@ -165,22 +167,79 @@ var (
31: 'Z',
}
crockfordDecodeMap = map[rune]uint32{
// crockfordDecodeMap takes characters and converts them to binary
// values based on Crockford base32.
crockfordDecodeMap = map[rune]uint8{
'0': 0,
'O': 0,
'o': 0,
'1': 1,
'I': 1,
'i': 1,
'L': 1,
'l': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'A': 10,
'a': 10,
'B': 11,
'b': 11,
'C': 12,
'c': 12,
'D': 13,
'd': 13,
'E': 14,
'e': 14,
'F': 15,
'f': 15,
'G': 16,
'g': 16,
'H': 17,
'h': 17,
'J': 18,
'j': 18,
'K': 19,
'k': 19,
'M': 20,
'm': 20,
'N': 21,
'n': 21,
'P': 22,
'p': 22,
'Q': 23,
'q': 23,
'R': 24,
'r': 24,
'S': 25,
's': 25,
'T': 26,
't': 26,
'V': 27,
'v': 27,
'W': 28,
'w': 28,
'X': 29,
'x': 29,
'Y': 30,
'y': 30,
'Z': 31,
'z': 31,
}
)
// CrockfordEncode takes a byte array and encodes every 5-bits as a
// character string according to Crockford's base 32 encoding. This
// specific implementation uses Big Endian byte order to fit the ULID
// spec ("network byte ordering")
// CrockfordEncode takes a byte array and encodes it into a character
// string according to Crockford's base 32 encoding. Every 5-bits
// corresponds to a character. This specific implementation uses Big
// Endian byte order to fit the ULID spec ("network byte ordering")
//
// https://www.crockford.com/base32.html
// https://git.wisellama.rocks/Mirrors/ulid-spec
func CrockfordEncode(bytes []byte) string {
// Crockford is a base 32 encoding.
// 2^5 = 32, so every 5 bits will give us a character.