on master help sign in
Add a countDigits function

--HG-- extra : histedit_source : aee87aeba221dc0d9cb40f9fe7bf31f589e8b6d7

commit2d089a680d298f4349d1f8583fdb138cd5383e20 authorBryan O'Sullivan <bos@serpentine.com> dateMon 18 Mar 2013, 22:32 (14 years ago) parent8532c9c
1 file changed+10
Data/Text/Lazy/Builder/Int.hs+10
Data/Text/Lazy/Builder/Int.hs+10
@@ -89,6 +89,16 @@ positive = go
8989 where go n | n < 10 = digit n
9090 | otherwise = go (n `quot` 10) <> digit (n `rem` 10)
9191
92+countDigits :: (Integral a) => a -> Int
93+{-# INLINE countDigits #-}
94+countDigits = go 1
95+ where go !k v
96+ | v < 10 = k
97+ | v < 100 = k + 1
98+ | v < 1000 = k + 2
99+ | v < 10000 = k + 3
100+ | otherwise = go (k+4) (v `quot` 10000)
101+
92102hexadecimal :: Integral a => a -> Builder
93103{-# SPECIALIZE hexadecimal :: Int -> Builder #-}
94104{-# SPECIALIZE hexadecimal :: Int8 -> Builder #-}