on master help sign in
Reduce duplication
7d9d95cBryan O'Sullivan
Data/Text/Lazy/Builder/Int.hs+10−11
@@ -54,12 +54,7 @@ decimal :: Integral a => a -> Builder
54{-# RULES "decimal/Word32" decimal = positive :: Word32 -> Builder #-}
54{-# RULES "decimal/Word32" decimal = positive :: Word32 -> Builder #-}
55{-# RULES "decimal/Word64" decimal = positive :: Word64 -> Builder #-}
55{-# RULES "decimal/Word64" decimal = positive :: Word64 -> Builder #-}
56{-# RULES "decimal/Integer" decimal = integer 10 :: Integer -> Builder #-}
56{-# RULES "decimal/Integer" decimal = integer 10 :: Integer -> Builder #-}
57−decimal i
57+decimal i = decimal' (<= -128) i
58− | i < 0 = singleton '-' <>
59− if i <= -128
60− then positive (-(i `quot` 10)) <> digit (-(i `rem` 10))
61− else positive (-i)
62− | otherwise = positive i
63
58
64boundedDecimal :: (Integral a, Bounded a) => a -> Builder
59boundedDecimal :: (Integral a, Bounded a) => a -> Builder
65{-# SPECIALIZE boundedDecimal :: Int -> Builder #-}
60{-# SPECIALIZE boundedDecimal :: Int -> Builder #-}
@@ -67,11 +62,15 @@ boundedDecimal :: (Integral a, Bounded a) => a -> Builder
67{-# SPECIALIZE boundedDecimal :: Int16 -> Builder #-}
62{-# SPECIALIZE boundedDecimal :: Int16 -> Builder #-}
68{-# SPECIALIZE boundedDecimal :: Int32 -> Builder #-}
63{-# SPECIALIZE boundedDecimal :: Int32 -> Builder #-}
69{-# SPECIALIZE boundedDecimal :: Int64 -> Builder #-}
64{-# SPECIALIZE boundedDecimal :: Int64 -> Builder #-}
70−boundedDecimal i
65+boundedDecimal i = decimal' (== minBound) i
66+
67+decimal' :: (Integral a) => (a -> Bool) -> a -> Builder
68+{-# INLINE decimal' #-}
69+decimal' p i
71− | i < 0 = singleton '-' <>
70+ | i < 0 = singleton '-' <>
72− if i == minBound
71+ if p i
73− then positive (-(i `quot` 10)) <> digit (-(i `rem` 10))
72+ then positive (-(i `quot` 10)) <> digit (-(i `rem` 10))
74− else positive (-i)
73+ else positive (-i)
75 | otherwise = positive i
74 | otherwise = positive i
76
75
77positive :: (Integral a) => a -> Builder
76positive :: (Integral a) => a -> Builder
Data/Text/Lazy/Builder/Internal.hs+7−3
@@ -28,19 +28,23 @@
28-----------------------------------------------------------------------------
28-----------------------------------------------------------------------------
29
29
30module Data.Text.Lazy.Builder.Internal
30module Data.Text.Lazy.Builder.Internal
31+ ( -- * Public API
31− ( -- * The Builder type
32+ -- ** The Builder type
32 Builder
33 Builder
33 , toLazyText
34 , toLazyText
34 , toLazyTextWith
35 , toLazyTextWith
35
36
36− -- * Constructing Builders
37+ -- ** Constructing Builders
37 , singleton
38 , singleton
38 , fromText
39 , fromText
39 , fromLazyText
40 , fromLazyText
40 , fromString
41 , fromString
41
42
42− -- * Flushing the buffer state
43+ -- ** Flushing the buffer state
43 , flush
44 , flush
45+
46+ -- * Internal functions
47+ , writeN
44 ) where
48 ) where
45
49
46import Control.Monad.ST (ST, runST)
50import Control.Monad.ST (ST, runST)