Skip to content

Commit

Permalink
Use NonEmpty to clean up errors in integerDec (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
clyring authored Feb 3, 2024
1 parent e456ea1 commit cff4421
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions Data/ByteString/Builder/ASCII.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import Data.ByteString.Builder.RealFloat (floatDec, doubleDec)

import Foreign
import Foreign.C.Types
import Data.List.NonEmpty (NonEmpty(..))

------------------------------------------------------------------------------
-- Decimal Encoding
Expand Down Expand Up @@ -276,37 +277,31 @@ integerDec i
| i < 0 = P.primFixed P.char8 '-' `mappend` go (-i)
| otherwise = go i
where
errImpossible fun =
error $ "integerDec: " ++ fun ++ ": the impossible happened."

go :: Integer -> Builder
go n | n < maxPow10 = intDec (fromInteger n)
| otherwise =
case putH (splitf (maxPow10 * maxPow10) n) of
(x:xs) -> intDec x `mappend` P.primMapListBounded intDecPadded xs
[] -> errImpossible "integerDec: go"
x:|xs -> intDec x `mappend` P.primMapListBounded intDecPadded xs

splitf :: Integer -> Integer -> [Integer]
splitf :: Integer -> Integer -> NonEmpty Integer
splitf pow10 n0
| pow10 > n0 = [n0]
| pow10 > n0 = n0 :| []
| otherwise = splith (splitf (pow10 * pow10) n0)
where
splith [] = errImpossible "splith"
splith (n:ns) =
splith (n:|ns) =
case n `quotRem` pow10 of
(q,r) | q > 0 -> q : r : splitb ns
| otherwise -> r : splitb ns
(q,r) | q > 0 -> q :| r : splitb ns
| otherwise -> r :| splitb ns

splitb [] = []
splitb (n:ns) = case n `quotRem` pow10 of
(q,r) -> q : r : splitb ns

putH :: [Integer] -> [Int]
putH [] = errImpossible "putH"
putH (n:ns) = case n `quotRem` maxPow10 of
putH :: NonEmpty Integer -> NonEmpty Int
putH (n:|ns) = case n `quotRem` maxPow10 of
(x,y)
| q > 0 -> q : r : putB ns
| otherwise -> r : putB ns
| q > 0 -> q :| r : putB ns
| otherwise -> r :| putB ns
where q = fromInteger x
r = fromInteger y

Expand Down

0 comments on commit cff4421

Please sign in to comment.