Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Pretty bytestrings via printing each byte as two hex digits.
This is primarily for aeson and when we want better show
ing of non-textual
bytestrings. It's not really binrep-related, but it needs _somewhere_ to go
and my projects that need it usually also touch binrep, so here it is.
Sadly, we can't use it to make aeson print integers as hex literals. It only deals in Scientifics, and if we tried printing them as strings, it would quote them. I need a YAML-like with better literals...
Synopsis
- newtype Hex a = Hex {
- unHex :: a
- type HexByteString = Hex ByteString
- parseHexByteString :: (MonadParsec e s m, Token s ~ Char) => ([Word8] -> a) -> m a
- parseHexByte :: (MonadParsec e s m, Token s ~ Char, Num a) => m a
- prettyHexByteString :: (a -> [Word8]) -> a -> Text
- prettyHexByte :: (Char -> Char) -> Word8 -> (Char, Char)
- prettyHexByteStringCompact :: (a -> [Word8]) -> a -> Text
Documentation
Instances
type HexByteString = Hex ByteString Source #
parseHexByteString :: (MonadParsec e s m, Token s ~ Char) => ([Word8] -> a) -> m a Source #
A hex bytestring looks like this: 00 01 89 8a FEff
. You can mix and
match capitalization and spacing, but I prefer to space each byte, full caps.
parseHexByte :: (MonadParsec e s m, Token s ~ Char, Num a) => m a Source #
Parse a byte formatted as two hex digits e.g. EF. You _must_ provide both
nibbles e.g. 0F
, not F
. They cannot be spaced e.g. E F
is invalid.
Returns a value 0-255, so can fit in any Num type that can store that.
prettyHexByteString :: (a -> [Word8]) -> a -> Text Source #
Pretty print to default format 00 12 AB FF
: space between each byte, all
caps.
This format I consider most human readable. I prefer caps to draw attention to this being data instead of text (you don't see that many capital letters packed together in prose).
prettyHexByteStringCompact :: (a -> [Word8]) -> a -> Text Source #
Pretty print to "compact" format 0012abff
(often output by hashers).