From 95f30b5e581073854b7761c7b953fcc71e22b621 Mon Sep 17 00:00:00 2001 From: Matthew Craven Date: Sat, 3 Feb 2024 07:45:28 -0500 Subject: [PATCH] Use `NonEmpty` to clean up errors in `integerDec` --- Data/ByteString/Builder/ASCII.hs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/Data/ByteString/Builder/ASCII.hs b/Data/ByteString/Builder/ASCII.hs index 0d780c073..3017a0395 100644 --- a/Data/ByteString/Builder/ASCII.hs +++ b/Data/ByteString/Builder/ASCII.hs @@ -85,6 +85,7 @@ import Data.ByteString.Builder.RealFloat (floatDec, doubleDec) import Foreign import Foreign.C.Types +import Data.List.NonEmpty (NonEmpty(..)) ------------------------------------------------------------------------------ -- Decimal Encoding @@ -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