{-# LANGUAGE Safe #-}
{-# LANGUAGE TypeOperators #-}
{-# 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 = String -> [UPrintf] -> r
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 = 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 String
fmts [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 String
fmts [UPrintf]
args =
String -> IO ()
putStr (String -> IO ()) -> String -> IO ()
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 Handle
hdl String
fmts [UPrintf]
args =
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 String
fmts [UPrintf]
args = \ 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 Handle
hdl String
fmts [UPrintf]
args = \ 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 a
_ (Char
c : String
cs) = String -> Char -> ModifierParser
FormatParse String
"" Char
c String
cs
parseFormat a
_ String
"" = FormatParse
forall a. a
errorShortFormat
instance PrintfArg Char where
formatArg :: Char -> FieldFormatter
formatArg = Char -> FieldFormatter
formatChar
parseFormat :: Char -> ModifierParser
parseFormat Char
_ 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 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", Int8 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int8
forall a. Bounded a => a
minBound :: Int8)),
(String
"h", Int16 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int16
forall a. Bounded a => a
minBound :: Int16)),
(String
"l", Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int32
forall a. Bounded a => a
minBound :: Int32)),
(String
"ll", Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
minBound :: Int64)),
(String
"L", Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
minBound :: Int64)) ]
parseIntFormat :: a -> String -> FormatParse
parseIntFormat :: forall a. a -> ModifierParser
parseIntFormat a
_ String
s =
case ((String, Integer) -> Maybe FormatParse -> Maybe FormatParse)
-> Maybe FormatParse -> [(String, Integer)] -> Maybe FormatParse
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (String, Integer) -> Maybe FormatParse -> Maybe FormatParse
forall {b}. (String, b) -> Maybe FormatParse -> Maybe FormatParse
matchPrefix Maybe FormatParse
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
"" -> FormatParse
forall a. a
errorShortFormat
where
matchPrefix :: (String, b) -> Maybe FormatParse -> Maybe FormatParse
matchPrefix (String
p, b
_) m :: Maybe FormatParse
m@(Just (FormatParse String
p0 Char
_ String
_))
| String -> Int
forall a. [a] -> 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 a. [a] -> 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
Maybe FormatParse
Nothing -> Maybe FormatParse
m
Just FormatParse
fp -> FormatParse -> Maybe FormatParse
forall a. a -> Maybe a
Just FormatParse
fp
matchPrefix (String
p, b
_) Maybe FormatParse
Nothing =
String -> Maybe FormatParse
getFormat String
p
getFormat :: String -> Maybe FormatParse
getFormat 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 a b. Maybe a -> (a -> Maybe b) -> Maybe b
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) = 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 String
"" = Maybe FormatParse
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 = c}
vFmt Char
_ FieldFormat
ufmt = FieldFormat
ufmt
formatChar :: Char -> FieldFormatter
formatChar :: Char -> FieldFormatter
formatChar Char
x FieldFormat
ufmt =
Maybe Integer -> Integer -> FieldFormatter
formatIntegral (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
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 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 (FieldFormat -> Char) -> FieldFormat -> Char
forall a b. (a -> b) -> a -> b
$ Char -> FieldFormat -> FieldFormat
vFmt Char
's' FieldFormat
ufmt of
Char
'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
"", 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 Maybe Int
Nothing = [a]
x
trunc (Just Int
n) = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n [a]
x
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 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 String -> [(String, Integer)] -> Maybe Integer
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
mods [(String, Integer)]
intModifierMap of
Just Integer
m0 -> Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
m0
Maybe Integer
Nothing -> String -> Maybe Integer
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 = 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
Integer
0 -> Char -> FieldFormat -> FieldFormat
vFmt Char
'u' FieldFormat
ufmt
Integer
_ -> 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 Integer
x 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 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) String -> String -> String
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) String -> String -> String
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
forall {a} {a}. (Eq a, Num a) => a -> a -> Maybe a
alt String
"0x" Integer
x) Maybe Int
prec Maybe Integer
m Integer
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
Char
'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 Integer
16 (String -> Integer -> Maybe String
forall {a} {a}. (Eq a, Num a) => a -> a -> Maybe a
alt String
"0X" Integer
x) Maybe Int
prec Maybe Integer
m Integer
x) String -> String -> String
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
forall {a} {a}. (Eq a, Num a) => a -> a -> Maybe a
alt String
"0b" Integer
x) Maybe Int
prec Maybe Integer
m Integer
x) String -> String -> String
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
forall {a} {a}. (Eq a, Num a) => a -> a -> Maybe a
alt String
"0" Integer
x) Maybe Int
prec Maybe Integer
m Integer
x) String -> String -> String
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 Maybe String
forall a. Maybe a
Nothing Maybe Int
prec Maybe Integer
m Integer
x) String -> String -> String
forall a. [a] -> [a] -> [a]
++)
Char
'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
"" ->
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 = 's' })
Char
'c' -> String -> String -> String
forall a. String -> a
perror String
"illegal char conversion"
Char
c -> Char -> String -> String
forall a. Char -> a
errorBadFormat Char
c
where
ufmt :: FieldFormat
ufmt = Char -> FieldFormat -> FieldFormat
vFmt Char
'd' (FieldFormat -> FieldFormat) -> FieldFormat -> FieldFormat
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 = Nothing }
FieldFormat
_ -> FieldFormat
ufmt0
alt :: a -> a -> Maybe a
alt a
_ a
0 = Maybe a
forall a. Maybe a
Nothing
alt a
p a
_ = case FieldFormat -> Bool
fmtAlternate FieldFormat
ufmt of
Bool
True -> a -> Maybe a
forall a. a -> Maybe a
Just a
p
Bool
False -> Maybe a
forall a. Maybe a
Nothing
upcase :: (a, String) -> (a, String)
upcase (a
s1, 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 :: forall a. RealFloat a => a -> FieldFormatter
formatRealFloat a
x FieldFormat
ufmt =
let c :: Char
c = FieldFormat -> Char
fmtChar (FieldFormat -> Char) -> FieldFormat -> Char
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 (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
'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]
++)
Char
'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]
++)
Char
'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]
++)
Char
'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
'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
_ -> Char -> String -> String
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] -> String -> String
uprintfs String
s [UPrintf]
us String
""
uprintfs :: String -> [UPrintf] -> ShowS
uprintfs :: String -> [UPrintf] -> String -> String
uprintfs String
"" [] = String -> String
forall a. a -> a
id
uprintfs String
"" (UPrintf
_:[UPrintf]
_) = String -> String
forall a. a
errorShortFormat
uprintfs (Char
'%':Char
'%':String
cs) [UPrintf]
us = (Char
'%' 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 (Char
'%':String
_) [] = String -> String
forall a. a
errorMissingArgument
uprintfs (Char
'%':String
cs) us :: [UPrintf]
us@(UPrintf
_:[UPrintf]
_) = String -> [UPrintf] -> String -> String
fmt String
cs [UPrintf]
us
uprintfs (Char
c:String
cs) [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 String
cs0 [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
(FieldFormat
_, String
_, []) -> String -> String
forall a. a
errorMissingArgument
(FieldFormat
ufmt, String
cs, (ModifierParser
_, FieldFormatter
u) : [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 FieldFormat
ufmt (String
pre, String
str) =
let naturalWidth :: Int
naturalWidth = String -> Int
forall a. [a] -> 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 a. [a] -> Int
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
width ->
let fillchar :: Char
fillchar = if Bool
zero then Char
'0' else Char
' ' 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
Maybe Int
_ -> String
""
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 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 Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = (String
"-", Maybe Int -> String -> String
integral_prec Maybe Int
prec (Integer -> String
forall a. Show a => a -> String
show (-Integer
i)))
| Bool
otherwise = (String
"", 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 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 Maybe String
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 -> (String, String)
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 Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 =
Maybe Int -> Maybe Integer -> Integer -> Maybe String
fmtu' Maybe Int
prec Maybe Integer
forall a. Maybe a
Nothing (-Integer
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 Int
prec) Maybe Integer
_ Integer
i | Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 =
(String -> String) -> Maybe String -> Maybe String
forall a b. (a -> b) -> Maybe a -> Maybe b
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' Maybe Int
Nothing Maybe Integer
_ Integer
i | Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
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 => a -> (Int -> Char) -> a -> String -> String
showIntAtBase Integer
b Int -> Char
intToDigit Integer
i String
""
fmtu' Maybe Int
_ Maybe Integer
_ Integer
_ = Maybe String
forall a. Maybe a
Nothing
integral_prec :: Maybe Int -> String -> String
integral_prec :: Maybe Int -> String -> String
integral_prec Maybe Int
Nothing String
integral = String
integral
integral_prec (Just Int
0) String
"0" = String
""
integral_prec (Just Int
prec) 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 a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
integral) Char
'0' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
integral
stoi :: String -> (Int, String)
stoi :: String -> (Int, String)
stoi String
cs =
let (String
as, 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
String
"" -> (Int
0, String
cs')
String
_ -> (String -> Int
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 -> Maybe a -> Bool -> Bool -> Maybe FormatAdjustment
forall {p}. p -> Bool -> Bool -> Maybe FormatAdjustment
adjl Maybe a
p Bool
True Bool
z
Maybe Int
_ -> 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 p
_ Bool
True Bool
_ = FormatAdjustment -> Maybe FormatAdjustment
forall a. a -> Maybe a
Just FormatAdjustment
LeftAdjust
adjl p
_ Bool
False Bool
True = FormatAdjustment -> Maybe FormatAdjustment
forall a. a -> Maybe a
Just FormatAdjustment
ZeroPad
adjl p
_ Bool
_ Bool
_ = 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 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 (FormatSign -> Maybe FormatSign
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 -> FormatSign -> Maybe FormatSign
forall a. a -> Maybe a
Just FormatSign
SignPlus
Maybe FormatSign
_ -> FormatSign -> 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 ((Int -> Maybe Int
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 ((Int -> Maybe Int
forall a. a -> Maybe a
Just Int
p', String
r'), [UPrintf]
us')
String
_ ->
((Maybe Int
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''
[] -> FormatParse
forall a. a
errorMissingArgument
in
(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 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'
[] -> FormatParse
forall a. a
errorMissingArgument
in
(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 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 ((Int -> Maybe Int
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 ((Int -> Maybe Int
forall a. a -> Maybe a
Just Int
p', String
r'), [UPrintf]
us)
String
_ ->
((Maybe Int
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''
[] -> FormatParse
forall a. a
errorMissingArgument
in
(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 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
[] -> FormatParse
forall a. a
errorMissingArgument
in
(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 Bool
_ Bool
_ Maybe FormatSign
_ Bool
_ String
"" [UPrintf]
_ =
(FieldFormat, 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 = 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 = String
"",
fmtChar :: Char
fmtChar = Char
'd' } in
case [UPrintf]
us of
[] -> ([UPrintf], Int)
forall a. a
errorMissingArgument
(ModifierParser
_, FieldFormatter
nu) : [UPrintf]
us' -> ([UPrintf]
us', String -> Int
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 :: 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
Char
'e' -> Maybe Int -> a -> String -> String
forall a. RealFloat a => Maybe Int -> a -> String -> String
showEFloat
Char
'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
Char
'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
Char
_ -> String -> Maybe Int -> a -> String -> String
forall a. String -> a
perror String
"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 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 = String -> a
forall a. String -> a
errorWithoutStackTrace (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"printf: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
errorBadFormat :: Char -> a
errorBadFormat :: forall a. Char -> a
errorBadFormat Char
c = String -> a
forall a. String -> a
perror (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"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 :: forall a. a
errorShortFormat = String -> a
forall a. String -> a
perror String
"formatting string ended prematurely"
errorMissingArgument :: forall a. a
errorMissingArgument = String -> a
forall a. String -> a
perror String
"argument list ended prematurely"
errorBadArgument :: forall a. a
errorBadArgument = String -> a
forall a. String -> a
perror String
"bad argument"