Safe Haskell | None |
---|---|
Language | Haskell2010 |
Data.Word.Base62
Contents
Description
This module provides functions for encoding fixed-width words
using the base-62 encoding scheme. The encoding functions in this
module produce byte sequences that are ASCII-compatible text
encodings (e.g. ISO-8859-1 and UTF-8). Similarly, the decoding
functions only decode byte sequences that are an ASCII-compatible
text encoding of characters in the class [A-Za-Z0-9]
. Other
encodings (notably UTF-16) are not supported but would be
accepted in a pull request.
64-bit Word
encode64 :: Word64 -> ByteArray Source #
Base62 encode a 64-bit word. Leading zero bits are suppressed.
>>>
putStrLn (Bytes.toLatinString (Bytes.fromByteArray (encode64 213635)))
tZj
Note that this will encode the number 0 as the character 0 rather than as the empty byte array.
decode64 :: Bytes -> Maybe Word64 Source #
Decode a base62-encoded 64-bit word. This rejects the empty
string rather than decoding it as zero. This also rejects encoded
numbers greater than or equal to 2^64
.
>>>
decode64 (Bytes.fromAsciiString "LygHa16AHYB")
Just 18446744073709551611>>>
decode64 (Bytes.fromAsciiString "1IdHllabYuAOlNK4")
Nothing
128-bit Word
encode128 :: Word128 -> ByteArray Source #
Base62 encode a 128-bit word. Leading zero bits are suppressed.
>>>
let octillion = 1_000_000_000_000_000_000_000_000_000
>>>
putStrLn (Bytes.toLatinString (Bytes.fromByteArray (encode128 octillion)))
1IdHllabYuAOlNK4
builder128 :: Word128 -> Builder 22 Source #
Base62 encode a 128-bit word as a builder.
decode128 :: Bytes -> Maybe Word128 Source #
Decode a base62-encoded 128-bit word. This rejects the empty
string rather than decoding it as zero. This also rejects encoded
numbers greater than or equal to 2^128
.
>>>
decode128 (Bytes.fromAsciiString "LygHa16AHYB")
Just 18446744073709551611>>>
decode128 (Bytes.fromAsciiString "7n42DGM5Tflk9n8mt7Fhc6")
Just 340282366920938463463374607431768211454>>>
decode128 (Bytes.fromAsciiString "7n42DGM5Tflk9n8mt7Fhc9")
Nothing