From 22dfde29364144e5a47518179fa0847c4d107f31 Mon Sep 17 00:00:00 2001 From: Alice McKean Date: Sun, 1 Sep 2019 16:17:29 -0700 Subject: [PATCH] add word32Dec builder --- src/Data/ByteArray/Builder/Small.hs | 10 ++++++++++ src/Data/ByteArray/Builder/Small/Unsafe.hs | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Data/ByteArray/Builder/Small.hs b/src/Data/ByteArray/Builder/Small.hs index 70e0bbb..f2f0e54 100644 --- a/src/Data/ByteArray/Builder/Small.hs +++ b/src/Data/ByteArray/Builder/Small.hs @@ -27,6 +27,7 @@ module Data.ByteArray.Builder.Small -- * Encode Integral Types -- ** Human-Readable , word64Dec + , word32Dec , word16Dec , int64Dec , word64PaddedUpperHex @@ -80,6 +81,9 @@ instance Semigroup Builder where instance Monoid Builder where 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. -- The size hint should be slightly larger than the actual size. run :: @@ -260,6 +264,12 @@ shortTextJsonString a = word64Dec :: Word64 -> Builder 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. -- This encoding never starts with a zero unless the -- argument was zero. diff --git a/src/Data/ByteArray/Builder/Small/Unsafe.hs b/src/Data/ByteArray/Builder/Small/Unsafe.hs index 84df3a9..1ae6695 100644 --- a/src/Data/ByteArray/Builder/Small/Unsafe.hs +++ b/src/Data/ByteArray/Builder/Small/Unsafe.hs @@ -26,6 +26,7 @@ module Data.ByteArray.Builder.Small.Unsafe -- * Encode Integral Types -- ** Human-Readable , word64Dec + , word32Dec , word16Dec , int64Dec , word64PaddedUpperHex @@ -142,7 +143,12 @@ doubleDec (D# d) = Builder (\arr off0 s0 -> doubleDec# d arr off0 s0) word64Dec :: Word64 -> Builder 19 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. word16Dec :: Word16 -> Builder 5 word16Dec (W16# w) = wordCommonDec# w