Add word8PaddedLowerHex

This commit is contained in:
Andrew Martin 2020-01-03 06:42:32 -05:00
parent ecde041d9d
commit d496f23fd8
3 changed files with 15 additions and 0 deletions

View file

@ -9,6 +9,7 @@
* Add `runOnto` * Add `runOnto`
* Add `Data.Bytes.Chunks.length` * Add `Data.Bytes.Chunks.length`
* Add `wordPaddedDec2` and `wordPaddedDec9`. * Add `wordPaddedDec2` and `wordPaddedDec9`.
* Add `word8PaddedLowerHex`.
## 0.3.1.0 -- 2019-11-20 ## 0.3.1.0 -- 2019-11-20

View file

@ -47,6 +47,7 @@ module Data.ByteArray.Builder.Bounded
, word16LowerHex , word16LowerHex
, word16UpperHex , word16UpperHex
-- ** 8-bit -- ** 8-bit
, word8PaddedLowerHex
, word8PaddedUpperHex , word8PaddedUpperHex
, word8LowerHex , word8LowerHex
, ascii , ascii
@ -383,6 +384,12 @@ word8LowerHex (W8# w) = word8LowerHex# w
word8PaddedUpperHex :: Word8 -> Builder 2 word8PaddedUpperHex :: Word8 -> Builder 2
word8PaddedUpperHex (W8# w) = word8PaddedUpperHex# w word8PaddedUpperHex (W8# w) = word8PaddedUpperHex# w
-- | Requires exactly 2 bytes. Encodes a 8-bit unsigned integer as
-- hexadecimal, zero-padding the encoding to 2 digits. This uses
-- lowercase for the alphabetical digits.
word8PaddedLowerHex :: Word8 -> Builder 2
word8PaddedLowerHex (W8# w) = word8PaddedLowerHex# w
-- TODO: Is it actually worth unrolling this loop. I suspect that it -- TODO: Is it actually worth unrolling this loop. I suspect that it
-- might not be. Benchmark this. -- might not be. Benchmark this.
word64PaddedUpperHex# :: Word# -> Builder 16 word64PaddedUpperHex# :: Word# -> Builder 16

View file

@ -64,6 +64,10 @@ tests = testGroup "Tests"
runConcat 1 (word64PaddedUpperHex w) runConcat 1 (word64PaddedUpperHex w)
=== ===
pack (showWord64PaddedUpperHex w) pack (showWord64PaddedUpperHex w)
, TQC.testProperty "word16PaddedLowerHex" $ \w ->
runConcat 1 (word16PaddedLowerHex w)
===
pack (showWord16PaddedLowerHex w)
, TQC.testProperty "wordPaddedDec2" $ TQC.forAll (TQC.choose (0,99)) $ \w -> , TQC.testProperty "wordPaddedDec2" $ TQC.forAll (TQC.choose (0,99)) $ \w ->
Bounded.run Nat.two (Bounded.wordPaddedDec2 w) Bounded.run Nat.two (Bounded.wordPaddedDec2 w)
=== ===
@ -278,6 +282,9 @@ packUtf8 = Exts.fromList . ByteString.unpack . TE.encodeUtf8 . T.pack
showWord64PaddedUpperHex :: Word64 -> String showWord64PaddedUpperHex :: Word64 -> String
showWord64PaddedUpperHex = printf "%016X" showWord64PaddedUpperHex = printf "%016X"
showWord16PaddedLowerHex :: Word16 -> String
showWord16PaddedLowerHex = printf "%04x"
runConcat :: Int -> Builder -> ByteArray runConcat :: Int -> Builder -> ByteArray
runConcat n = Chunks.concat . run n runConcat n = Chunks.concat . run n