@@ -18,8 +18,10 @@ module Data.Text.Lazy.Builder.Int
19import Data.Int (Int8, Int16, Int32, Int64)
19import Data.Int (Int8, Int16, Int32, Int64)
20import Data.Monoid (mempty)
20import Data.Monoid (mempty)
21+import qualified Data.ByteString.Unsafe as B
21import Data.Text.Lazy.Builder.Functions ((<>), i2d)
22import Data.Text.Lazy.Builder.Functions ((<>), i2d)
22import Data.Text.Lazy.Builder.Internal
23import Data.Text.Lazy.Builder.Internal
24+import Data.Text.Lazy.Builder.Int.Digits (digits)
23import Data.Text.Array
25import Data.Text.Array
24import Data.Word (Word, Word8, Word16, Word32, Word64)
26import Data.Word (Word, Word8, Word16, Word32, Word64)
25import GHC.Base (quotInt, remInt)
27import GHC.Base (quotInt, remInt)
@@ -99,14 +101,21 @@ positive i
99 | otherwise = let !n = countDigits i
101 | otherwise = let !n = countDigits i
100 in writeN n $ \marr off -> posDecimal marr off n i
102 in writeN n $ \marr off -> posDecimal marr off n i
104+posDecimal :: (Integral a) =>
102−posDecimal :: Integral a => forall s. MArray s -> Int -> Int -> a -> ST s ()
105+ forall s. MArray s -> Int -> Int -> a -> ST s ()
103{-# INLINE posDecimal #-}
106{-# INLINE posDecimal #-}
104posDecimal marr off0 ds v0 = go (off0 + ds - 1) v0
107posDecimal marr off0 ds v0 = go (off0 + ds - 1) v0
105 where go off v
108 where go off v
106− | v < 10 = unsafeWrite marr off (i2w v)
109+ | v >= 100 = do
107− | otherwise = do
110+ write2 off $ let u = v `rem` 100
108− unsafeWrite marr off (i2w (v `rem` 10))
111+ in u + u
109− go (off-1) (v `quot` 10)
112+ go (off - 2) (v `quot` 100)
113+ | v < 10 = unsafeWrite marr off (i2w v)
114+ | otherwise = write2 off (v + v)
116+ unsafeWrite marr off $ get (i + 1)
117+ unsafeWrite marr (off - 1) $ get i
118+ get i = fromIntegral $ B.unsafeIndex digits (fromIntegral i) :: Word16
111minus, zero :: Word16
120minus, zero :: Word16
112{-# INLINE minus #-}
121{-# INLINE minus #-}