add word32Dec builder

This commit is contained in:
Alice McKean 2019-09-01 16:17:29 -07:00
parent 5d3d6d1afd
commit 22dfde2936
2 changed files with 17 additions and 1 deletions

View file

@ -27,6 +27,7 @@ module Data.ByteArray.Builder.Small
-- * Encode Integral Types -- * Encode Integral Types
-- ** Human-Readable -- ** Human-Readable
, word64Dec , word64Dec
, word32Dec
, word16Dec , word16Dec
, int64Dec , int64Dec
, word64PaddedUpperHex , word64PaddedUpperHex
@ -80,6 +81,9 @@ instance Semigroup Builder where
instance Monoid Builder where instance Monoid Builder where
mempty = Builder $ \_ off0 _ s0 -> (# s0, off0 #) mempty = Builder $ \_ off0 _ s0 -> (# s0, off0 #)
instance IsString Builder where
fromString = shortTextUtf8 . TS.fromString
-- | Run a builder. An accurate size hint is important for good performance. -- | Run a builder. An accurate size hint is important for good performance.
-- The size hint should be slightly larger than the actual size. -- The size hint should be slightly larger than the actual size.
run :: run ::
@ -260,6 +264,12 @@ shortTextJsonString a =
word64Dec :: Word64 -> Builder word64Dec :: Word64 -> Builder
word64Dec w = fromUnsafe (Unsafe.word64Dec w) word64Dec w = fromUnsafe (Unsafe.word64Dec w)
-- | Encodes an unsigned 16-bit integer as decimal.
-- This encoding never starts with a zero unless the
-- argument was zero.
word32Dec :: Word32 -> Builder
word32Dec w = fromUnsafe (Unsafe.word32Dec w)
-- | Encodes an unsigned 16-bit integer as decimal. -- | Encodes an unsigned 16-bit integer as decimal.
-- This encoding never starts with a zero unless the -- This encoding never starts with a zero unless the
-- argument was zero. -- argument was zero.

View file

@ -26,6 +26,7 @@ module Data.ByteArray.Builder.Small.Unsafe
-- * Encode Integral Types -- * Encode Integral Types
-- ** Human-Readable -- ** Human-Readable
, word64Dec , word64Dec
, word32Dec
, word16Dec , word16Dec
, int64Dec , int64Dec
, word64PaddedUpperHex , word64PaddedUpperHex
@ -142,7 +143,12 @@ doubleDec (D# d) = Builder (\arr off0 s0 -> doubleDec# d arr off0 s0)
word64Dec :: Word64 -> Builder 19 word64Dec :: Word64 -> Builder 19
word64Dec (W64# w) = wordCommonDec# w word64Dec (W64# w) = wordCommonDec# w
-- | Requires up to 19 bytes. Encodes an unsigned 64-bit integer as decimal. -- | Requires up to 10 bytes. Encodes an unsigned 32-bit integer as decimal.
-- This encoding never starts with a zero unless the argument was zero.
word32Dec :: Word32 -> Builder 10
word32Dec (W32# w) = wordCommonDec# w
-- | Requires up to 5 bytes. Encodes an unsigned 16-bit integer as decimal.
-- This encoding never starts with a zero unless the argument was zero. -- This encoding never starts with a zero unless the argument was zero.
word16Dec :: Word16 -> Builder 5 word16Dec :: Word16 -> Builder 5
word16Dec (W16# w) = wordCommonDec# w word16Dec (W16# w) = wordCommonDec# w