{-# LANGUAGE Safe #-}
{-# LANGUAGE GADTs #-}
module Text.Printf(
printf, hPrintf,
PrintfArg(..),
FieldFormatter,
FieldFormat(..),
FormatAdjustment(..), FormatSign(..),
vFmt,
ModifierParser, FormatParse(..),
formatString, formatChar, formatInt,
formatInteger, formatRealFloat,
errorBadFormat, errorShortFormat, errorMissingArgument,
errorBadArgument,
perror,
PrintfType, HPrintfType,
IsChar(..)
) where
import Data.Char
import Data.Int
import Data.List
import Data.Word
import Numeric
import Numeric.Natural
import System.IO
printf :: (PrintfType r) => String -> r
printf :: String -> r
printf fmts :: String
fmts = String -> [UPrintf] -> r
forall t. PrintfType t => String -> [UPrintf] -> t
spr String
fmts []
hPrintf :: (HPrintfType r) => Handle -> String -> r
hPrintf :: Handle -> String -> r
hPrintf hdl :: Handle
hdl fmts :: String
fmts = Handle -> String -> [UPrintf] -> r
forall t. HPrintfType t => Handle -> String -> [UPrintf] -> t
hspr Handle
hdl String
fmts []
class PrintfType t where
spr :: String -> [UPrintf] -> t
class HPrintfType t where
hspr :: Handle -> String -> [UPrintf] -> t
instance (IsChar c) => PrintfType [c] where
spr :: String -> [UPrintf] -> [c]
spr fmts :: String
fmts args :: [UPrintf]
args = (Char -> c) -> String -> [c]
forall a b. (a -> b) -> [a] -> [b]
map Char -> c
forall c. IsChar c => Char -> c
fromChar (String -> [UPrintf] -> String
uprintf String
fmts ([UPrintf] -> [UPrintf]
forall a. [a] -> [a]
reverse [UPrintf]
args))
instance (a ~ ()) => PrintfType (IO a) where
spr :: String -> [UPrintf] -> IO a
spr fmts :: String
fmts args :: [UPrintf]
args =
String -> IO a
String -> IO ()
putStr (String -> IO a) -> String -> IO a
forall a b. (a -> b) -> a -> b
$ (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
forall c. IsChar c => Char -> c
fromChar (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String -> [UPrintf] -> String
uprintf String
fmts ([UPrintf] -> String) -> [UPrintf] -> String
forall a b. (a -> b) -> a -> b
$ [UPrintf] -> [UPrintf]
forall a. [a] -> [a]
reverse [UPrintf]
args
instance (a ~ ()) => HPrintfType (IO a) where
hspr :: Handle -> String -> [UPrintf] -> IO a
hspr hdl :: Handle
hdl fmts :: String
fmts args :: [UPrintf]
args = do
Handle -> String -> IO ()
hPutStr Handle
hdl (String -> [UPrintf] -> String
uprintf String
fmts ([UPrintf] -> [UPrintf]
forall a. [a] -> [a]
reverse [UPrintf]
args))
instance (PrintfArg a, PrintfType r) => PrintfType (a -> r) where
spr :: String -> [UPrintf] -> a -> r
spr fmts :: String
fmts args :: [UPrintf]
args = \ a :: a
a -> String -> [UPrintf] -> r
forall t. PrintfType t => String -> [UPrintf] -> t
spr String
fmts
((a -> ModifierParser
forall a. PrintfArg a => a -> ModifierParser
parseFormat a
a, a -> FieldFormatter
forall a. PrintfArg a => a -> FieldFormatter
formatArg a
a) UPrintf -> [UPrintf] -> [UPrintf]
forall a. a -> [a] -> [a]
: [UPrintf]
args)
instance (PrintfArg a, HPrintfType r) => HPrintfType (a -> r) where
hspr :: Handle -> String -> [UPrintf] -> a -> r
hspr hdl :: Handle
hdl fmts :: String
fmts args :: [UPrintf]
args = \ a :: a
a -> Handle -> String -> [UPrintf] -> r
forall t. HPrintfType t => Handle -> String -> [UPrintf] -> t
hspr Handle
hdl String
fmts
((a -> ModifierParser
forall a. PrintfArg a => a -> ModifierParser
parseFormat a
a, a -> FieldFormatter
forall a. PrintfArg a => a -> FieldFormatter
formatArg a
a) UPrintf -> [UPrintf] -> [UPrintf]
forall a. a -> [a] -> [a]
: [UPrintf]
args)
class PrintfArg a where
formatArg :: a -> FieldFormatter
parseFormat :: a -> ModifierParser
parseFormat _ (c :: Char
c : cs :: String
cs) = String -> Char -> ModifierParser
FormatParse "" Char
c String
cs
parseFormat _ "" = FormatParse
forall a. a
errorShortFormat
instance PrintfArg Char where
formatArg :: Char -> FieldFormatter
formatArg = Char -> FieldFormatter
formatChar
parseFormat :: Char -> ModifierParser
parseFormat _ cf :: String
cf = Int -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat (Int
forall a. HasCallStack => a
undefined :: Int) String
cf
instance (IsChar c) => PrintfArg [c] where
formatArg :: [c] -> FieldFormatter
formatArg = [c] -> FieldFormatter
forall c. IsChar c => [c] -> FieldFormatter
formatString
instance PrintfArg Int where
formatArg :: Int -> FieldFormatter
formatArg = Int -> FieldFormatter
forall a. (Integral a, Bounded a) => a -> FieldFormatter
formatInt
parseFormat :: Int -> ModifierParser
parseFormat = Int -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Int8 where
formatArg :: Int8 -> FieldFormatter
formatArg = Int8 -> FieldFormatter
forall a. (Integral a, Bounded a) => a -> FieldFormatter
formatInt
parseFormat :: Int8 -> ModifierParser
parseFormat = Int8 -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Int16 where
formatArg :: Int16 -> FieldFormatter
formatArg = Int16 -> FieldFormatter
forall a. (Integral a, Bounded a) => a -> FieldFormatter
formatInt
parseFormat :: Int16 -> ModifierParser
parseFormat = Int16 -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Int32 where
formatArg :: Int32 -> FieldFormatter
formatArg = Int32 -> FieldFormatter
forall a. (Integral a, Bounded a) => a -> FieldFormatter
formatInt
parseFormat :: Int32 -> ModifierParser
parseFormat = Int32 -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Int64 where
formatArg :: Int64 -> FieldFormatter
formatArg = Int64 -> FieldFormatter
forall a. (Integral a, Bounded a) => a -> FieldFormatter
formatInt
parseFormat :: Int64 -> ModifierParser
parseFormat = Int64 -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Word where
formatArg :: Word -> FieldFormatter
formatArg = Word -> FieldFormatter
forall a. (Integral a, Bounded a) => a -> FieldFormatter
formatInt
parseFormat :: Word -> ModifierParser
parseFormat = Word -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Word8 where
formatArg :: Word8 -> FieldFormatter
formatArg = Word8 -> FieldFormatter
forall a. (Integral a, Bounded a) => a -> FieldFormatter
formatInt
parseFormat :: Word8 -> ModifierParser
parseFormat = Word8 -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Word16 where
formatArg :: Word16 -> FieldFormatter
formatArg = Word16 -> FieldFormatter
forall a. (Integral a, Bounded a) => a -> FieldFormatter
formatInt
parseFormat :: Word16 -> ModifierParser
parseFormat = Word16 -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Word32 where
formatArg :: Word32 -> FieldFormatter
formatArg = Word32 -> FieldFormatter
forall a. (Integral a, Bounded a) => a -> FieldFormatter
formatInt
parseFormat :: Word32 -> ModifierParser
parseFormat = Word32 -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Word64 where
formatArg :: Word64 -> FieldFormatter
formatArg = Word64 -> FieldFormatter
forall a. (Integral a, Bounded a) => a -> FieldFormatter
formatInt
parseFormat :: Word64 -> ModifierParser
parseFormat = Word64 -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Integer where
formatArg :: Integer -> FieldFormatter
formatArg = Integer -> FieldFormatter
formatInteger
parseFormat :: Integer -> ModifierParser
parseFormat = Integer -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Natural where
formatArg :: Natural -> FieldFormatter
formatArg = Integer -> FieldFormatter
formatInteger (Integer -> FieldFormatter)
-> (Natural -> Integer) -> Natural -> FieldFormatter
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Natural -> Integer
forall a. Integral a => a -> Integer
toInteger
parseFormat :: Natural -> ModifierParser
parseFormat = Natural -> ModifierParser
forall a. a -> ModifierParser
parseIntFormat
instance PrintfArg Float where
formatArg :: Float -> FieldFormatter
formatArg = Float -> FieldFormatter
forall a. RealFloat a => a -> FieldFormatter
formatRealFloat
instance PrintfArg Double where
formatArg :: Double -> FieldFormatter
formatArg = Double -> FieldFormatter
forall a. RealFloat a => a -> FieldFormatter
formatRealFloat
class IsChar c where
toChar :: c -> Char
fromChar :: Char -> c
instance IsChar Char where
toChar :: Char -> Char
toChar c :: Char
c = Char
c
fromChar :: Char -> Char
fromChar c :: Char
c = Char
c
data FormatAdjustment = LeftAdjust | ZeroPad
data FormatSign = SignPlus | SignSpace
data FieldFormat = FieldFormat {
FieldFormat -> Maybe Int
fmtWidth :: Maybe Int,
FieldFormat -> Maybe Int
fmtPrecision :: Maybe Int,
FieldFormat -> Maybe FormatAdjustment
fmtAdjust :: Maybe FormatAdjustment,
FieldFormat -> Maybe FormatSign
fmtSign :: Maybe FormatSign,
FieldFormat -> Bool
fmtAlternate :: Bool,
FieldFormat -> String
fmtModifiers :: String,
FieldFormat -> Char
fmtChar :: Char
}
data FormatParse = FormatParse {
FormatParse -> String
fpModifiers :: String,
FormatParse -> Char
fpChar :: Char,
FormatParse -> String
fpRest :: String
}
intModifierMap :: [(String, Integer)]
intModifierMap :: [(String, Integer)]
intModifierMap = [
("hh", Int8 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int8
forall a. Bounded a => a
minBound :: Int8)),
("h", Int16 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int16
forall a. Bounded a => a
minBound :: Int16)),
("l", Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int32
forall a. Bounded a => a
minBound :: Int32)),
("ll", Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
minBound :: Int64)),
("L", Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
minBound :: Int64)) ]
parseIntFormat :: a -> String -> FormatParse
parseIntFormat :: a -> ModifierParser
parseIntFormat _ s :: String
s =
case ((String, Integer) -> Maybe FormatParse -> Maybe FormatParse)
-> Maybe FormatParse -> [(String, Integer)] -> Maybe FormatParse
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (String, Integer) -> Maybe FormatParse -> Maybe FormatParse
matchPrefix Maybe FormatParse
forall a. Maybe a
Nothing [(String, Integer)]
intModifierMap of
Just m :: FormatParse
m -> FormatParse
m
Nothing ->
case String
s of
c :: Char
c : cs :: String
cs -> String -> Char -> ModifierParser
FormatParse "" Char
c String
cs
"" -> FormatParse
forall a. a
errorShortFormat
where
matchPrefix :: (String, Integer) -> Maybe FormatParse -> Maybe FormatParse
matchPrefix (p :: String
p, _) m :: Maybe FormatParse
m@(Just (FormatParse p0 :: String
p0 _ _))
| String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
p0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
p = Maybe FormatParse
m
| Bool
otherwise = case String -> Maybe FormatParse
getFormat String
p of
Nothing -> Maybe FormatParse
m
Just fp :: FormatParse
fp -> FormatParse -> Maybe FormatParse
forall a. a -> Maybe a
Just FormatParse
fp
matchPrefix (p :: String
p, _) Nothing =
String -> Maybe FormatParse
getFormat String
p
getFormat :: String -> Maybe FormatParse
getFormat p :: String
p =
String -> String -> Maybe String
forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix String
p String
s Maybe String -> (String -> Maybe FormatParse) -> Maybe FormatParse
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Maybe FormatParse
fp
where
fp :: String -> Maybe FormatParse
fp (c :: Char
c : cs :: String
cs) = FormatParse -> Maybe FormatParse
forall a. a -> Maybe a
Just (FormatParse -> Maybe FormatParse)
-> FormatParse -> Maybe FormatParse
forall a b. (a -> b) -> a -> b
$ String -> Char -> ModifierParser
FormatParse String
p Char
c String
cs
fp "" = Maybe FormatParse
forall a. a
errorShortFormat
type FieldFormatter = FieldFormat -> ShowS
type ModifierParser = String -> FormatParse
vFmt :: Char -> FieldFormat -> FieldFormat
vFmt :: Char -> FieldFormat -> FieldFormat
vFmt c :: Char
c ufmt :: FieldFormat
ufmt@(FieldFormat {fmtChar :: FieldFormat -> Char
fmtChar = Char
'v'}) = FieldFormat
ufmt {fmtChar :: Char
fmtChar = Char
c}
vFmt _ ufmt :: FieldFormat
ufmt = FieldFormat
ufmt
formatChar :: Char -> FieldFormatter
formatChar :: Char -> FieldFormatter
formatChar x :: Char
x ufmt :: FieldFormat
ufmt =
Maybe Integer -> Integer -> FieldFormatter
formatIntegral (Integer -> Maybe Integer
forall a. a -> Maybe a
Just 0) (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ Char -> Int
ord Char
x) FieldFormatter -> FieldFormatter
forall a b. (a -> b) -> a -> b
$ Char -> FieldFormat -> FieldFormat
vFmt 'c' FieldFormat
ufmt
formatString :: IsChar a => [a] -> FieldFormatter
formatString :: [a] -> FieldFormatter
formatString x :: [a]
x ufmt :: FieldFormat
ufmt =
case FieldFormat -> Char
fmtChar (FieldFormat -> Char) -> FieldFormat -> Char
forall a b. (a -> b) -> a -> b
$ Char -> FieldFormat -> FieldFormat
vFmt 's' FieldFormat
ufmt of
's' -> (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
forall c. IsChar c => c -> Char
toChar (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FieldFormat -> (String, String) -> String
adjust FieldFormat
ufmt ("", String
ts) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
where
ts :: String
ts = (a -> Char) -> [a] -> String
forall a b. (a -> b) -> [a] -> [b]
map a -> Char
forall c. IsChar c => c -> Char
toChar ([a] -> String) -> [a] -> String
forall a b. (a -> b) -> a -> b
$ Maybe Int -> [a]
trunc (Maybe Int -> [a]) -> Maybe Int -> [a]
forall a b. (a -> b) -> a -> b
$ FieldFormat -> Maybe Int
fmtPrecision FieldFormat
ufmt
where
trunc :: Maybe Int -> [a]
trunc Nothing = [a]
x
trunc (Just n :: Int
n) = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n [a]
x
c :: Char
c -> Char -> String -> String
forall a. Char -> a
errorBadFormat Char
c
fixupMods :: FieldFormat -> Maybe Integer -> Maybe Integer
fixupMods :: FieldFormat -> Maybe Integer -> Maybe Integer
fixupMods ufmt :: FieldFormat
ufmt m :: Maybe Integer
m =
let mods :: String
mods = FieldFormat -> String
fmtModifiers FieldFormat
ufmt in
case String
mods of
"" -> Maybe Integer
m
_ -> case String -> [(String, Integer)] -> Maybe Integer
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
mods [(String, Integer)]
intModifierMap of
Just m0 :: Integer
m0 -> Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
m0
Nothing -> String -> Maybe Integer
forall a. String -> a
perror "unknown format modifier"
formatInt :: (Integral a, Bounded a) => a -> FieldFormatter
formatInt :: a -> FieldFormatter
formatInt x :: a
x ufmt :: FieldFormat
ufmt =
let lb :: Integer
lb = a -> Integer
forall a. Integral a => a -> Integer
toInteger (a -> Integer) -> a -> Integer
forall a b. (a -> b) -> a -> b
$ a
forall a. Bounded a => a
minBound a -> a -> a
forall a. a -> a -> a
`asTypeOf` a
x
m :: Maybe Integer
m = FieldFormat -> Maybe Integer -> Maybe Integer
fixupMods FieldFormat
ufmt (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
lb)
ufmt' :: FieldFormat
ufmt' = case Integer
lb of
0 -> Char -> FieldFormat -> FieldFormat
vFmt 'u' FieldFormat
ufmt
_ -> FieldFormat
ufmt
in
Maybe Integer -> Integer -> FieldFormatter
formatIntegral Maybe Integer
m (a -> Integer
forall a. Integral a => a -> Integer
toInteger a
x) FieldFormat
ufmt'
formatInteger :: Integer -> FieldFormatter
formatInteger :: Integer -> FieldFormatter
formatInteger x :: Integer
x ufmt :: FieldFormat
ufmt =
let m :: Maybe Integer
m = FieldFormat -> Maybe Integer -> Maybe Integer
fixupMods FieldFormat
ufmt Maybe Integer
forall a. Maybe a
Nothing in
Maybe Integer -> Integer -> FieldFormatter
formatIntegral Maybe Integer
m Integer
x FieldFormat
ufmt
formatIntegral :: Maybe Integer -> Integer -> FieldFormatter
formatIntegral :: Maybe Integer -> Integer -> FieldFormatter
formatIntegral m :: Maybe Integer
m x :: Integer
x ufmt0 :: FieldFormat
ufmt0 =
let prec :: Maybe Int
prec = FieldFormat -> Maybe Int
fmtPrecision FieldFormat
ufmt0 in
case FieldFormat -> Char
fmtChar FieldFormat
ufmt of
'd' -> (FieldFormat -> (String, String) -> String
adjustSigned FieldFormat
ufmt (Maybe Int -> Integer -> (String, String)
fmti Maybe Int
prec Integer
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'i' -> (FieldFormat -> (String, String) -> String
adjustSigned FieldFormat
ufmt (Maybe Int -> Integer -> (String, String)
fmti Maybe Int
prec Integer
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'x' -> (FieldFormat -> (String, String) -> String
adjust FieldFormat
ufmt (Integer
-> Maybe String
-> Maybe Int
-> Maybe Integer
-> Integer
-> (String, String)
fmtu 16 (String -> Integer -> Maybe String
alt "0x" Integer
x) Maybe Int
prec Maybe Integer
m Integer
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'X' -> (FieldFormat -> (String, String) -> String
adjust FieldFormat
ufmt ((String, String) -> (String, String)
forall a. (a, String) -> (a, String)
upcase ((String, String) -> (String, String))
-> (String, String) -> (String, String)
forall a b. (a -> b) -> a -> b
$ Integer
-> Maybe String
-> Maybe Int
-> Maybe Integer
-> Integer
-> (String, String)
fmtu 16 (String -> Integer -> Maybe String
alt "0X" Integer
x) Maybe Int
prec Maybe Integer
m Integer
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'b' -> (FieldFormat -> (String, String) -> String
adjust FieldFormat
ufmt (Integer
-> Maybe String
-> Maybe Int
-> Maybe Integer
-> Integer
-> (String, String)
fmtu 2 (String -> Integer -> Maybe String
alt "0b" Integer
x) Maybe Int
prec Maybe Integer
m Integer
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'o' -> (FieldFormat -> (String, String) -> String
adjust FieldFormat
ufmt (Integer
-> Maybe String
-> Maybe Int
-> Maybe Integer
-> Integer
-> (String, String)
fmtu 8 (String -> Integer -> Maybe String
alt "0" Integer
x) Maybe Int
prec Maybe Integer
m Integer
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'u' -> (FieldFormat -> (String, String) -> String
adjust FieldFormat
ufmt (Integer
-> Maybe String
-> Maybe Int
-> Maybe Integer
-> Integer
-> (String, String)
fmtu 10 Maybe String
forall a. Maybe a
Nothing Maybe Int
prec Maybe Integer
m Integer
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'c' | Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Int
ord (Char
forall a. Bounded a => a
minBound :: Char)) Bool -> Bool -> Bool
&&
Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Int
ord (Char
forall a. Bounded a => a
maxBound :: Char)) Bool -> Bool -> Bool
&&
FieldFormat -> Maybe Int
fmtPrecision FieldFormat
ufmt Maybe Int -> Maybe Int -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe Int
forall a. Maybe a
Nothing Bool -> Bool -> Bool
&&
FieldFormat -> String
fmtModifiers FieldFormat
ufmt String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "" ->
String -> FieldFormatter
forall c. IsChar c => [c] -> FieldFormatter
formatString [Int -> Char
chr (Int -> Char) -> Int -> Char
forall a b. (a -> b) -> a -> b
$ Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x] (FieldFormat
ufmt { fmtChar :: Char
fmtChar = 's' })
'c' -> String -> String -> String
forall a. String -> a
perror "illegal char conversion"
c :: Char
c -> Char -> String -> String
forall a. Char -> a
errorBadFormat Char
c
where
ufmt :: FieldFormat
ufmt = Char -> FieldFormat -> FieldFormat
vFmt 'd' (FieldFormat -> FieldFormat) -> FieldFormat -> FieldFormat
forall a b. (a -> b) -> a -> b
$ case FieldFormat
ufmt0 of
FieldFormat { fmtPrecision :: FieldFormat -> Maybe Int
fmtPrecision = Just _, fmtAdjust :: FieldFormat -> Maybe FormatAdjustment
fmtAdjust = Just ZeroPad } ->
FieldFormat
ufmt0 { fmtAdjust :: Maybe FormatAdjustment
fmtAdjust = Maybe FormatAdjustment
forall a. Maybe a
Nothing }
_ -> FieldFormat
ufmt0
alt :: String -> Integer -> Maybe String
alt _ 0 = Maybe String
forall a. Maybe a
Nothing
alt p :: String
p _ = case FieldFormat -> Bool
fmtAlternate FieldFormat
ufmt of
True -> String -> Maybe String
forall a. a -> Maybe a
Just String
p
False -> Maybe String
forall a. Maybe a
Nothing
upcase :: (a, String) -> (a, String)
upcase (s1 :: a
s1, s2 :: String
s2) = (a
s1, (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toUpper String
s2)
formatRealFloat :: RealFloat a => a -> FieldFormatter
formatRealFloat :: a -> FieldFormatter
formatRealFloat x :: a
x ufmt :: FieldFormat
ufmt =
let c :: Char
c = FieldFormat -> Char
fmtChar (FieldFormat -> Char) -> FieldFormat -> Char
forall a b. (a -> b) -> a -> b
$ Char -> FieldFormat -> FieldFormat
vFmt 'g' FieldFormat
ufmt
prec :: Maybe Int
prec = FieldFormat -> Maybe Int
fmtPrecision FieldFormat
ufmt
alt :: Bool
alt = FieldFormat -> Bool
fmtAlternate FieldFormat
ufmt
in
case Char
c of
'e' -> (FieldFormat -> (String, String) -> String
adjustSigned FieldFormat
ufmt (Char -> Maybe Int -> Bool -> a -> (String, String)
forall a.
RealFloat a =>
Char -> Maybe Int -> Bool -> a -> (String, String)
dfmt Char
c Maybe Int
prec Bool
alt a
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'E' -> (FieldFormat -> (String, String) -> String
adjustSigned FieldFormat
ufmt (Char -> Maybe Int -> Bool -> a -> (String, String)
forall a.
RealFloat a =>
Char -> Maybe Int -> Bool -> a -> (String, String)
dfmt Char
c Maybe Int
prec Bool
alt a
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'f' -> (FieldFormat -> (String, String) -> String
adjustSigned FieldFormat
ufmt (Char -> Maybe Int -> Bool -> a -> (String, String)
forall a.
RealFloat a =>
Char -> Maybe Int -> Bool -> a -> (String, String)
dfmt Char
c Maybe Int
prec Bool
alt a
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'F' -> (FieldFormat -> (String, String) -> String
adjustSigned FieldFormat
ufmt (Char -> Maybe Int -> Bool -> a -> (String, String)
forall a.
RealFloat a =>
Char -> Maybe Int -> Bool -> a -> (String, String)
dfmt Char
c Maybe Int
prec Bool
alt a
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'g' -> (FieldFormat -> (String, String) -> String
adjustSigned FieldFormat
ufmt (Char -> Maybe Int -> Bool -> a -> (String, String)
forall a.
RealFloat a =>
Char -> Maybe Int -> Bool -> a -> (String, String)
dfmt Char
c Maybe Int
prec Bool
alt a
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
'G' -> (FieldFormat -> (String, String) -> String
adjustSigned FieldFormat
ufmt (Char -> Maybe Int -> Bool -> a -> (String, String)
forall a.
RealFloat a =>
Char -> Maybe Int -> Bool -> a -> (String, String)
dfmt Char
c Maybe Int
prec Bool
alt a
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
_ -> Char -> String -> String
forall a. Char -> a
errorBadFormat Char
c
type UPrintf = (ModifierParser, FieldFormatter)
uprintf :: String -> [UPrintf] -> String
uprintf :: String -> [UPrintf] -> String
uprintf s :: String
s us :: [UPrintf]
us = String -> [UPrintf] -> String -> String
uprintfs String
s [UPrintf]
us ""
uprintfs :: String -> [UPrintf] -> ShowS
uprintfs :: String -> [UPrintf] -> String -> String
uprintfs "" [] = String -> String
forall a. a -> a
id
uprintfs "" (_:_) = String -> String
forall a. a
errorShortFormat
uprintfs ('%':'%':cs :: String
cs) us :: [UPrintf]
us = ('%' Char -> String -> String
forall a. a -> [a] -> [a]
:) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [UPrintf] -> String -> String
uprintfs String
cs [UPrintf]
us
uprintfs ('%':_) [] = String -> String
forall a. a
errorMissingArgument
uprintfs ('%':cs :: String
cs) us :: [UPrintf]
us@(_:_) = String -> [UPrintf] -> String -> String
fmt String
cs [UPrintf]
us
uprintfs (c :: Char
c:cs :: String
cs) us :: [UPrintf]
us = (Char
c Char -> String -> String
forall a. a -> [a] -> [a]
:) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [UPrintf] -> String -> String
uprintfs String
cs [UPrintf]
us
fmt :: String -> [UPrintf] -> ShowS
fmt :: String -> [UPrintf] -> String -> String
fmt cs0 :: String
cs0 us0 :: [UPrintf]
us0 =
case Bool
-> Bool
-> Maybe FormatSign
-> Bool
-> String
-> [UPrintf]
-> (FieldFormat, String, [UPrintf])
getSpecs Bool
False Bool
False Maybe FormatSign
forall a. Maybe a
Nothing Bool
False String
cs0 [UPrintf]
us0 of
(_, _, []) -> String -> String
forall a. a
errorMissingArgument
(ufmt :: FieldFormat
ufmt, cs :: String
cs, (_, u :: FieldFormatter
u) : us :: [UPrintf]
us) -> FieldFormatter
u FieldFormat
ufmt (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [UPrintf] -> String -> String
uprintfs String
cs [UPrintf]
us
adjust :: FieldFormat -> (String, String) -> String
adjust :: FieldFormat -> (String, String) -> String
adjust ufmt :: FieldFormat
ufmt (pre :: String
pre, str :: String
str) =
let naturalWidth :: Int
naturalWidth = String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
pre Int -> Int -> Int
forall a. Num a => a -> a -> a
+ String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
str
zero :: Bool
zero = case FieldFormat -> Maybe FormatAdjustment
fmtAdjust FieldFormat
ufmt of
Just ZeroPad -> Bool
True
_ -> Bool
False
left :: Bool
left = case FieldFormat -> Maybe FormatAdjustment
fmtAdjust FieldFormat
ufmt of
Just LeftAdjust -> Bool
True
_ -> Bool
False
fill :: String
fill = case FieldFormat -> Maybe Int
fmtWidth FieldFormat
ufmt of
Just width :: Int
width | Int
naturalWidth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
width ->
let fillchar :: Char
fillchar = if Bool
zero then '0' else ' ' in
Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
naturalWidth) Char
fillchar
_ -> ""
in
if Bool
left
then String
pre String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
str String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fill
else if Bool
zero
then String
pre String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fill String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
str
else String
fill String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
pre String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
str
adjustSigned :: FieldFormat -> (String, String) -> String
adjustSigned :: FieldFormat -> (String, String) -> String
adjustSigned ufmt :: FieldFormat
ufmt@(FieldFormat {fmtSign :: FieldFormat -> Maybe FormatSign
fmtSign = Just SignPlus}) ("", str :: String
str) =
FieldFormat -> (String, String) -> String
adjust FieldFormat
ufmt ("+", String
str)
adjustSigned ufmt :: FieldFormat
ufmt@(FieldFormat {fmtSign :: FieldFormat -> Maybe FormatSign
fmtSign = Just SignSpace}) ("", str :: String
str) =
FieldFormat -> (String, String) -> String
adjust FieldFormat
ufmt (" ", String
str)
adjustSigned ufmt :: FieldFormat
ufmt ps :: (String, String)
ps =
FieldFormat -> (String, String) -> String
adjust FieldFormat
ufmt (String, String)
ps
fmti :: Maybe Int -> Integer -> (String, String)
fmti :: Maybe Int -> Integer -> (String, String)
fmti prec :: Maybe Int
prec i :: Integer
i
| Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 0 = ("-", Maybe Int -> String -> String
integral_prec Maybe Int
prec (Integer -> String
forall a. Show a => a -> String
show (-Integer
i)))
| Bool
otherwise = ("", Maybe Int -> String -> String
integral_prec Maybe Int
prec (Integer -> String
forall a. Show a => a -> String
show Integer
i))
fmtu :: Integer -> Maybe String -> Maybe Int -> Maybe Integer -> Integer
-> (String, String)
fmtu :: Integer
-> Maybe String
-> Maybe Int
-> Maybe Integer
-> Integer
-> (String, String)
fmtu b :: Integer
b (Just pre :: String
pre) prec :: Maybe Int
prec m :: Maybe Integer
m i :: Integer
i =
let ("", s :: String
s) = Integer
-> Maybe String
-> Maybe Int
-> Maybe Integer
-> Integer
-> (String, String)
fmtu Integer
b Maybe String
forall a. Maybe a
Nothing Maybe Int
prec Maybe Integer
m Integer
i in
case String
pre of
"0" -> case String
s of
'0' : _ -> ("", String
s)
_ -> (String
pre, String
s)
_ -> (String
pre, String
s)
fmtu b :: Integer
b Nothing prec0 :: Maybe Int
prec0 m0 :: Maybe Integer
m0 i0 :: Integer
i0 =
case Maybe Int -> Maybe Integer -> Integer -> Maybe String
fmtu' Maybe Int
prec0 Maybe Integer
m0 Integer
i0 of
Just s :: String
s -> ("", String
s)
Nothing -> (String, String)
forall a. a
errorBadArgument
where
fmtu' :: Maybe Int -> Maybe Integer -> Integer -> Maybe String
fmtu' :: Maybe Int -> Maybe Integer -> Integer -> Maybe String
fmtu' prec :: Maybe Int
prec (Just m :: Integer
m) i :: Integer
i | Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< 0 =
Maybe Int -> Maybe Integer -> Integer -> Maybe String
fmtu' Maybe Int
prec Maybe Integer
forall a. Maybe a
Nothing (-2 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
m Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
i)
fmtu' (Just prec :: Int
prec) _ i :: Integer
i | Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= 0 =
(String -> String) -> Maybe String -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Int -> String -> String
integral_prec (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
prec)) (Maybe String -> Maybe String) -> Maybe String -> Maybe String
forall a b. (a -> b) -> a -> b
$ Maybe Int -> Maybe Integer -> Integer -> Maybe String
fmtu' Maybe Int
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing Integer
i
fmtu' Nothing _ i :: Integer
i | Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= 0 =
String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> String -> Maybe String
forall a b. (a -> b) -> a -> b
$ Integer -> (Int -> Char) -> Integer -> String -> String
forall a.
(Integral a, Show a) =>
a -> (Int -> Char) -> a -> String -> String
showIntAtBase Integer
b Int -> Char
intToDigit Integer
i ""
fmtu' _ _ _ = Maybe String
forall a. Maybe a
Nothing
integral_prec :: Maybe Int -> String -> String
integral_prec :: Maybe Int -> String -> String
integral_prec Nothing integral :: String
integral = String
integral
integral_prec (Just 0) "0" = ""
integral_prec (Just prec :: Int
prec) integral :: String
integral =
Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
prec Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
integral) '0' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
integral
stoi :: String -> (Int, String)
stoi :: String -> (Int, String)
stoi cs :: String
cs =
let (as :: String
as, cs' :: String
cs') = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
span Char -> Bool
isDigit String
cs in
case String
as of
"" -> (0, String
cs')
_ -> (String -> Int
forall a. Read a => String -> a
read String
as, String
cs')
adjustment :: Maybe Int -> Maybe a -> Bool -> Bool
-> Maybe FormatAdjustment
adjustment :: Maybe Int -> Maybe a -> Bool -> Bool -> Maybe FormatAdjustment
adjustment w :: Maybe Int
w p :: Maybe a
p l :: Bool
l z :: Bool
z =
case Maybe Int
w of
Just n :: Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0 -> Maybe a -> Bool -> Bool -> Maybe FormatAdjustment
forall p. p -> Bool -> Bool -> Maybe FormatAdjustment
adjl Maybe a
p Bool
True Bool
z
_ -> Maybe a -> Bool -> Bool -> Maybe FormatAdjustment
forall p. p -> Bool -> Bool -> Maybe FormatAdjustment
adjl Maybe a
p Bool
l Bool
z
where
adjl :: p -> Bool -> Bool -> Maybe FormatAdjustment
adjl _ True _ = FormatAdjustment -> Maybe FormatAdjustment
forall a. a -> Maybe a
Just FormatAdjustment
LeftAdjust
adjl _ False True = FormatAdjustment -> Maybe FormatAdjustment
forall a. a -> Maybe a
Just FormatAdjustment
ZeroPad
adjl _ _ _ = Maybe FormatAdjustment
forall a. Maybe a
Nothing
getSpecs :: Bool -> Bool -> Maybe FormatSign -> Bool -> String -> [UPrintf]
-> (FieldFormat, String, [UPrintf])
getSpecs :: Bool
-> Bool
-> Maybe FormatSign
-> Bool
-> String
-> [UPrintf]
-> (FieldFormat, String, [UPrintf])
getSpecs _ z :: Bool
z s :: Maybe FormatSign
s a :: Bool
a ('-' : cs0 :: String
cs0) us :: [UPrintf]
us = Bool
-> Bool
-> Maybe FormatSign
-> Bool
-> String
-> [UPrintf]
-> (FieldFormat, String, [UPrintf])
getSpecs Bool
True Bool
z Maybe FormatSign
s Bool
a String
cs0 [UPrintf]
us
getSpecs l :: Bool
l z :: Bool
z _ a :: Bool
a ('+' : cs0 :: String
cs0) us :: [UPrintf]
us = Bool
-> Bool
-> Maybe FormatSign
-> Bool
-> String
-> [UPrintf]
-> (FieldFormat, String, [UPrintf])
getSpecs Bool
l Bool
z (FormatSign -> Maybe FormatSign
forall a. a -> Maybe a
Just FormatSign
SignPlus) Bool
a String
cs0 [UPrintf]
us
getSpecs l :: Bool
l z :: Bool
z s :: Maybe FormatSign
s a :: Bool
a (' ' : cs0 :: String
cs0) us :: [UPrintf]
us =
Bool
-> Bool
-> Maybe FormatSign
-> Bool
-> String
-> [UPrintf]
-> (FieldFormat, String, [UPrintf])
getSpecs Bool
l Bool
z Maybe FormatSign
ss Bool
a String
cs0 [UPrintf]
us
where
ss :: Maybe FormatSign
ss = case Maybe FormatSign
s of
Just SignPlus -> FormatSign -> Maybe FormatSign
forall a. a -> Maybe a
Just FormatSign
SignPlus
_ -> FormatSign -> Maybe FormatSign
forall a. a -> Maybe a
Just FormatSign
SignSpace
getSpecs l :: Bool
l _ s :: Maybe FormatSign
s a :: Bool
a ('0' : cs0 :: String
cs0) us :: [UPrintf]
us = Bool
-> Bool
-> Maybe FormatSign
-> Bool
-> String
-> [UPrintf]
-> (FieldFormat, String, [UPrintf])
getSpecs Bool
l Bool
True Maybe FormatSign
s Bool
a String
cs0 [UPrintf]
us
getSpecs l :: Bool
l z :: Bool
z s :: Maybe FormatSign
s _ ('#' : cs0 :: String
cs0) us :: [UPrintf]
us = Bool
-> Bool
-> Maybe FormatSign
-> Bool
-> String
-> [UPrintf]
-> (FieldFormat, String, [UPrintf])
getSpecs Bool
l Bool
z Maybe FormatSign
s Bool
True String
cs0 [UPrintf]
us
getSpecs l :: Bool
l z :: Bool
z s :: Maybe FormatSign
s a :: Bool
a ('*' : cs0 :: String
cs0) us :: [UPrintf]
us =
let (us' :: [UPrintf]
us', n :: Int
n) = [UPrintf] -> ([UPrintf], Int)
getStar [UPrintf]
us
((p :: Maybe Int
p, cs'' :: String
cs''), us'' :: [UPrintf]
us'') = case String
cs0 of
'.':'*':r :: String
r ->
let (us''' :: [UPrintf]
us''', p' :: Int
p') = [UPrintf] -> ([UPrintf], Int)
getStar [UPrintf]
us' in ((Int -> Maybe Int
forall a. a -> Maybe a
Just Int
p', String
r), [UPrintf]
us''')
'.':r :: String
r ->
let (p' :: Int
p', r' :: String
r') = String -> (Int, String)
stoi String
r in ((Int -> Maybe Int
forall a. a -> Maybe a
Just Int
p', String
r'), [UPrintf]
us')
_ ->
((Maybe Int
forall a. Maybe a
Nothing, String
cs0), [UPrintf]
us')
FormatParse ms :: String
ms c :: Char
c cs :: String
cs =
case [UPrintf]
us'' of
(ufmt :: ModifierParser
ufmt, _) : _ -> ModifierParser
ufmt String
cs''
[] -> FormatParse
forall a. a
errorMissingArgument
in
(FieldFormat :: Maybe Int
-> Maybe Int
-> Maybe FormatAdjustment
-> Maybe FormatSign
-> Bool
-> String
-> Char
-> FieldFormat
FieldFormat {
fmtWidth :: Maybe Int
fmtWidth = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Int
forall a. Num a => a -> a
abs Int
n),
fmtPrecision :: Maybe Int
fmtPrecision = Maybe Int
p,
fmtAdjust :: Maybe FormatAdjustment
fmtAdjust = Maybe Int -> Maybe Int -> Bool -> Bool -> Maybe FormatAdjustment
forall a.
Maybe Int -> Maybe a -> Bool -> Bool -> Maybe FormatAdjustment
adjustment (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n) Maybe Int
p Bool
l Bool
z,
fmtSign :: Maybe FormatSign
fmtSign = Maybe FormatSign
s,
fmtAlternate :: Bool
fmtAlternate = Bool
a,
fmtModifiers :: String
fmtModifiers = String
ms,
fmtChar :: Char
fmtChar = Char
c}, String
cs, [UPrintf]
us'')
getSpecs l :: Bool
l z :: Bool
z s :: Maybe FormatSign
s a :: Bool
a ('.' : cs0 :: String
cs0) us :: [UPrintf]
us =
let ((p :: Int
p, cs' :: String
cs'), us' :: [UPrintf]
us') = case String
cs0 of
'*':cs'' :: String
cs'' -> let (us'' :: [UPrintf]
us'', p' :: Int
p') = [UPrintf] -> ([UPrintf], Int)
getStar [UPrintf]
us in ((Int
p', String
cs''), [UPrintf]
us'')
_ -> (String -> (Int, String)
stoi String
cs0, [UPrintf]
us)
FormatParse ms :: String
ms c :: Char
c cs :: String
cs =
case [UPrintf]
us' of
(ufmt :: ModifierParser
ufmt, _) : _ -> ModifierParser
ufmt String
cs'
[] -> FormatParse
forall a. a
errorMissingArgument
in
(FieldFormat :: Maybe Int
-> Maybe Int
-> Maybe FormatAdjustment
-> Maybe FormatSign
-> Bool
-> String
-> Char
-> FieldFormat
FieldFormat {
fmtWidth :: Maybe Int
fmtWidth = Maybe Int
forall a. Maybe a
Nothing,
fmtPrecision :: Maybe Int
fmtPrecision = Int -> Maybe Int
forall a. a -> Maybe a
Just Int
p,
fmtAdjust :: Maybe FormatAdjustment
fmtAdjust = Maybe Int -> Maybe Int -> Bool -> Bool -> Maybe FormatAdjustment
forall a.
Maybe Int -> Maybe a -> Bool -> Bool -> Maybe FormatAdjustment
adjustment Maybe Int
forall a. Maybe a
Nothing (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
p) Bool
l Bool
z,
fmtSign :: Maybe FormatSign
fmtSign = Maybe FormatSign
s,
fmtAlternate :: Bool
fmtAlternate = Bool
a,
fmtModifiers :: String
fmtModifiers = String
ms,
fmtChar :: Char
fmtChar = Char
c}, String
cs, [UPrintf]
us')
getSpecs l :: Bool
l z :: Bool
z s :: Maybe FormatSign
s a :: Bool
a cs0 :: String
cs0@(c0 :: Char
c0 : _) us :: [UPrintf]
us | Char -> Bool
isDigit Char
c0 =
let (n :: Int
n, cs' :: String
cs') = String -> (Int, String)
stoi String
cs0
((p :: Maybe Int
p, cs'' :: String
cs''), us' :: [UPrintf]
us') = case String
cs' of
'.' : '*' : r :: String
r ->
let (us'' :: [UPrintf]
us'', p' :: Int
p') = [UPrintf] -> ([UPrintf], Int)
getStar [UPrintf]
us in ((Int -> Maybe Int
forall a. a -> Maybe a
Just Int
p', String
r), [UPrintf]
us'')
'.' : r :: String
r ->
let (p' :: Int
p', r' :: String
r') = String -> (Int, String)
stoi String
r in ((Int -> Maybe Int
forall a. a -> Maybe a
Just Int
p', String
r'), [UPrintf]
us)
_ ->
((Maybe Int
forall a. Maybe a
Nothing, String
cs'), [UPrintf]
us)
FormatParse ms :: String
ms c :: Char
c cs :: String
cs =
case [UPrintf]
us' of
(ufmt :: ModifierParser
ufmt, _) : _ -> ModifierParser
ufmt String
cs''
[] -> FormatParse
forall a. a
errorMissingArgument
in
(FieldFormat :: Maybe Int
-> Maybe Int
-> Maybe FormatAdjustment
-> Maybe FormatSign
-> Bool
-> String
-> Char
-> FieldFormat
FieldFormat {
fmtWidth :: Maybe Int
fmtWidth = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Int
forall a. Num a => a -> a
abs Int
n),
fmtPrecision :: Maybe Int
fmtPrecision = Maybe Int
p,
fmtAdjust :: Maybe FormatAdjustment
fmtAdjust = Maybe Int -> Maybe Int -> Bool -> Bool -> Maybe FormatAdjustment
forall a.
Maybe Int -> Maybe a -> Bool -> Bool -> Maybe FormatAdjustment
adjustment (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n) Maybe Int
p Bool
l Bool
z,
fmtSign :: Maybe FormatSign
fmtSign = Maybe FormatSign
s,
fmtAlternate :: Bool
fmtAlternate = Bool
a,
fmtModifiers :: String
fmtModifiers = String
ms,
fmtChar :: Char
fmtChar = Char
c}, String
cs, [UPrintf]
us')
getSpecs l :: Bool
l z :: Bool
z s :: Maybe FormatSign
s a :: Bool
a cs0 :: String
cs0@(_ : _) us :: [UPrintf]
us =
let FormatParse ms :: String
ms c :: Char
c cs :: String
cs =
case [UPrintf]
us of
(ufmt :: ModifierParser
ufmt, _) : _ -> ModifierParser
ufmt String
cs0
[] -> FormatParse
forall a. a
errorMissingArgument
in
(FieldFormat :: Maybe Int
-> Maybe Int
-> Maybe FormatAdjustment
-> Maybe FormatSign
-> Bool
-> String
-> Char
-> FieldFormat
FieldFormat {
fmtWidth :: Maybe Int
fmtWidth = Maybe Int
forall a. Maybe a
Nothing,
fmtPrecision :: Maybe Int
fmtPrecision = Maybe Int
forall a. Maybe a
Nothing,
fmtAdjust :: Maybe FormatAdjustment
fmtAdjust = Maybe Int -> Maybe Any -> Bool -> Bool -> Maybe FormatAdjustment
forall a.
Maybe Int -> Maybe a -> Bool -> Bool -> Maybe FormatAdjustment
adjustment Maybe Int
forall a. Maybe a
Nothing Maybe Any
forall a. Maybe a
Nothing Bool
l Bool
z,
fmtSign :: Maybe FormatSign
fmtSign = Maybe FormatSign
s,
fmtAlternate :: Bool
fmtAlternate = Bool
a,
fmtModifiers :: String
fmtModifiers = String
ms,
fmtChar :: Char
fmtChar = Char
c}, String
cs, [UPrintf]
us)
getSpecs _ _ _ _ "" _ =
(FieldFormat, String, [UPrintf])
forall a. a
errorShortFormat
getStar :: [UPrintf] -> ([UPrintf], Int)
getStar :: [UPrintf] -> ([UPrintf], Int)
getStar us :: [UPrintf]
us =
let ufmt :: FieldFormat
ufmt = FieldFormat :: Maybe Int
-> Maybe Int
-> Maybe FormatAdjustment
-> Maybe FormatSign
-> Bool
-> String
-> Char
-> FieldFormat
FieldFormat {
fmtWidth :: Maybe Int
fmtWidth = Maybe Int
forall a. Maybe a
Nothing,
fmtPrecision :: Maybe Int
fmtPrecision = Maybe Int
forall a. Maybe a
Nothing,
fmtAdjust :: Maybe FormatAdjustment
fmtAdjust = Maybe FormatAdjustment
forall a. Maybe a
Nothing,
fmtSign :: Maybe FormatSign
fmtSign = Maybe FormatSign
forall a. Maybe a
Nothing,
fmtAlternate :: Bool
fmtAlternate = Bool
False,
fmtModifiers :: String
fmtModifiers = "",
fmtChar :: Char
fmtChar = 'd' } in
case [UPrintf]
us of
[] -> ([UPrintf], Int)
forall a. a
errorMissingArgument
(_, nu :: FieldFormatter
nu) : us' :: [UPrintf]
us' -> ([UPrintf]
us', String -> Int
forall a. Read a => String -> a
read (FieldFormatter
nu FieldFormat
ufmt ""))
dfmt :: (RealFloat a) => Char -> Maybe Int -> Bool -> a -> (String, String)
dfmt :: Char -> Maybe Int -> Bool -> a -> (String, String)
dfmt c :: Char
c p :: Maybe Int
p a :: Bool
a d :: a
d =
let caseConvert :: String -> String
caseConvert = if Char -> Bool
isUpper Char
c then (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toUpper else String -> String
forall a. a -> a
id
showFunction :: Maybe Int -> a -> String -> String
showFunction = case Char -> Char
toLower Char
c of
'e' -> Maybe Int -> a -> String -> String
forall a. RealFloat a => Maybe Int -> a -> String -> String
showEFloat
'f' -> if Bool
a then Maybe Int -> a -> String -> String
forall a. RealFloat a => Maybe Int -> a -> String -> String
showFFloatAlt else Maybe Int -> a -> String -> String
forall a. RealFloat a => Maybe Int -> a -> String -> String
showFFloat
'g' -> if Bool
a then Maybe Int -> a -> String -> String
forall a. RealFloat a => Maybe Int -> a -> String -> String
showGFloatAlt else Maybe Int -> a -> String -> String
forall a. RealFloat a => Maybe Int -> a -> String -> String
showGFloat
_ -> String -> Maybe Int -> a -> String -> String
forall a. String -> a
perror "internal error: impossible dfmt"
result :: String
result = String -> String
caseConvert (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Maybe Int -> a -> String -> String
showFunction Maybe Int
p a
d ""
in
case String
result of
'-' : cs :: String
cs -> ("-", String
cs)
cs :: String
cs -> ("" , String
cs)
perror :: String -> a
perror :: String -> a
perror s :: String
s = String -> a
forall a. String -> a
errorWithoutStackTrace (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ "printf: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
errorBadFormat :: Char -> a
errorBadFormat :: Char -> a
errorBadFormat c :: Char
c = String -> a
forall a. String -> a
perror (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ "bad formatting char " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Char -> String
forall a. Show a => a -> String
show Char
c
errorShortFormat, errorMissingArgument, errorBadArgument :: a
errorShortFormat :: a
errorShortFormat = String -> a
forall a. String -> a
perror "formatting string ended prematurely"
errorMissingArgument :: a
errorMissingArgument = String -> a
forall a. String -> a
perror "argument list ended prematurely"
errorBadArgument :: a
errorBadArgument = String -> a
forall a. String -> a
perror "bad argument"