@@ -18,7 +18,7 @@ module Data.Text.Lazy.Builder.Int
1818import Data.Int (Int8, Int16, Int32, Int64)
1919import Data.Monoid (mempty)
2020import Data.Text.Lazy.Builder.Functions ((<>), i2d)
21−import Data.Text.Lazy.Builder
21+import Data.Text.Lazy.Builder.Internal
2222import Data.Word (Word, Word8, Word16, Word32, Word64)
2323import GHC.Base (quotInt, remInt)
2424import GHC.Num (quotRemInteger)
@@ -67,11 +67,16 @@ boundedDecimal i = decimal' (== minBound) i
6767decimal' :: (Integral a) => (a -> Bool) -> a -> Builder
6868{-# INLINE decimal' #-}
6969decimal' p i
70− | i < 0 = singleton '-' <>
71− if p i
72− then positive (-(i `quot` 10)) <> digit (-(i `rem` 10))
73− else positive (-i)
74− | otherwise = positive i
70+ | i < 0 = if p i
71+ then let j = -(i `quot` 10)
72+ n = countDigits j + 2
73+ in go n $ singleton '-' <>
74+ positive j <> digit (-(i `rem` 10))
75+ else let j = -i
76+ n = countDigits j + 1
77+ in go n $ singleton '-' <> positive j
78+ | otherwise = go (countDigits i) $ positive i
79+ where go k b = ensureFree k `append'` b
7580
7681positive :: (Integral a) => a -> Builder
7782{-# SPECIALIZE positive :: Int -> Builder #-}