on master help sign in
A more straightforward (and slower) countDigits
5ee1548Bryan O'Sullivan
Data/Text/Lazy/Builder/Int.hs+15−15
@@ -120,22 +120,22 @@ i2w v = zero + fromIntegral v
120
120
121countDigits :: (Integral a) => a -> Int
121countDigits :: (Integral a) => a -> Int
122{-# INLINE countDigits #-}
122{-# INLINE countDigits #-}
123−countDigits v0 = go 1 (fromIntegral v0 :: Word64)
123+countDigits v0 = go (fromIntegral v0 :: Word64)
124− where go !k v
124+ where go v
125− | v < 10 = k
125+ | v < 10 = 1
126− | v < 100 = k + 1
126+ | v < 100 = 2
127− | v < 1000 = k + 2
127+ | v < 1000 = 3
128 | v < 1000000000000 =
128 | v < 1000000000000 =
129− k + if v < 100000000
129+ if v < 100000000
130− then if v < 1000000
130+ then if v < 1000000
131− then if v < 10000
131+ then if v < 10000
132− then 3
132+ then 4
133− else 4 + fin v 100000
133+ else 5 + fin v 100000
134− else 6 + fin v 10000000
134+ else 7 + fin v 10000000
135− else if v < 10000000000
135+ else if v < 10000000000
136− then 8 + fin v 1000000000
136+ then 9 + fin v 1000000000
137− else 10 + fin v 100000000000
137+ else 11 + fin v 100000000000
138− | otherwise = go (k + 12) (v `quot` 1000000000000)
138+ | otherwise = 12 + go (v `quot` 1000000000000)
139 fin v n = if v >= n then 1 else 0
139 fin v n = if v >= n then 1 else 0
140
140
141hexadecimal :: Integral a => a -> Builder
141hexadecimal :: Integral a => a -> Builder