From 7ed17f5b6d0fe9e71fb3f87244c2fabd231d153f Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Wed, 4 Sep 2019 10:30:23 -0400 Subject: [PATCH] add word8 and int --- src/Data/ByteArray/Builder.hs | 15 +++++++++++++++ src/Data/ByteArray/Builder/Bounded.hs | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Data/ByteArray/Builder.hs b/src/Data/ByteArray/Builder.hs index 9302292..3fd7f36 100644 --- a/src/Data/ByteArray/Builder.hs +++ b/src/Data/ByteArray/Builder.hs @@ -31,7 +31,9 @@ module Data.ByteArray.Builder , word64Dec , word32Dec , word16Dec + , word8Dec , int64Dec + , intDec , word64PaddedUpperHex , word32PaddedUpperHex , word16PaddedUpperHex @@ -271,6 +273,12 @@ word32Dec w = fromBounded Nat.constant (Bounded.word32Dec w) word16Dec :: Word16 -> Builder word16Dec w = fromBounded Nat.constant (Bounded.word16Dec w) +-- | Encodes an unsigned 8-bit integer as decimal. +-- This encoding never starts with a zero unless the +-- argument was zero. +word8Dec :: Word8 -> Builder +word8Dec w = fromBounded Nat.constant (Bounded.word8Dec w) + -- | Encode a double-floating-point number, using decimal notation or -- scientific notation depending on the magnitude. This has undefined -- behavior when representing @+inf@, @-inf@, and @NaN@. It will not @@ -285,6 +293,13 @@ doubleDec w = fromBounded Nat.constant (Bounded.doubleDec w) int64Dec :: Int64 -> Builder int64Dec w = fromBounded Nat.constant (Bounded.int64Dec w) +-- | Encodes a signed machine-sized integer as decimal. +-- This encoding never starts with a zero unless the argument was zero. +-- Negative numbers are preceded by a minus sign. Positive numbers +-- are not preceded by anything. +intDec :: Int -> Builder +intDec w = fromBounded Nat.constant (Bounded.intDec w) + -- | Encode a 64-bit unsigned integer as hexadecimal, zero-padding -- the encoding to 16 digits. This uses uppercase for the alphabetical -- digits. For example, this encodes the number 1022 as @00000000000003FE@. diff --git a/src/Data/ByteArray/Builder/Bounded.hs b/src/Data/ByteArray/Builder/Bounded.hs index d4bcb52..04cc186 100644 --- a/src/Data/ByteArray/Builder/Bounded.hs +++ b/src/Data/ByteArray/Builder/Bounded.hs @@ -28,7 +28,9 @@ module Data.ByteArray.Builder.Bounded , word64Dec , word32Dec , word16Dec + , word8Dec , int64Dec + , intDec , word64PaddedUpperHex , word32PaddedUpperHex , word16PaddedUpperHex @@ -151,6 +153,11 @@ word32Dec (W32# w) = wordCommonDec# w word16Dec :: Word16 -> Builder 5 word16Dec (W16# w) = wordCommonDec# w +-- | Requires up to 3 bytes. Encodes an unsigned 8-bit integer as decimal. +-- This encoding never starts with a zero unless the argument was zero. +word8Dec :: Word8 -> Builder 3 +word8Dec (W8# w) = wordCommonDec# w + -- | Requires up to 20 bytes. Encodes a signed 64-bit integer as decimal. -- This encoding never starts with a zero unless the argument was zero. -- Negative numbers are preceded by a minus sign. Positive numbers @@ -158,6 +165,13 @@ word16Dec (W16# w) = wordCommonDec# w int64Dec :: Int64 -> Builder 20 int64Dec (I64# w) = int64Dec# w +-- | Requires up to 20 bytes. Encodes a signed machine-sized integer +-- as decimal. This encoding never starts with a zero unless the +-- argument was zero. Negative numbers are preceded by a minus sign. +-- Positive numbers are not preceded by anything. +intDec :: Int -> Builder 20 +intDec (I# w) = int64Dec# w + -- Requires a number of bytes that is bounded by the size of -- the word. This is only used internally. wordCommonDec# :: Word# -> Builder n