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