1 -----------------------------------------------------------------------------
3 -- Module : Text.Printf
4 -- Copyright : (c) Lennart Augustsson, 2004
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : lennart@augustsson.net
8 -- Stability : provisional
9 -- Portability : portable
11 -- A C printf like formatter.
13 -----------------------------------------------------------------------------
17 PrintfType, HPrintfType, PrintfArg, IsChar
23 import Numeric(showEFloat, showFFloat, showGFloat)
28 -- | Format a variable number of arguments with the C-style formatting string.
29 -- The return value is either 'String' or @('IO' a)@.
31 -- The format string consists of ordinary characters and /conversion
32 -- specifications/, which specify how to format one of the arguments
33 -- to printf in the output string. A conversion specification begins with the
34 -- character @%@, followed by one or more of the following flags:
36 -- > - left adjust (default is right adjust)
37 -- > 0 pad with zeroes rather than spaces
39 -- followed optionally by a field width:
42 -- > * as num, but taken from argument list
44 -- followed optionally by a precision:
46 -- > .num precision (number of decimal places)
48 -- and finally, a format character:
50 -- > c character Char, Int, Integer
51 -- > d decimal Char, Int, Integer
52 -- > o octal Char, Int, Integer
53 -- > x hexadecimal Char, Int, Integer
54 -- > u unsigned decimal Char, Int, Integer
55 -- > f floating point Float, Double
56 -- > g general format float Float, Double
57 -- > e exponent format float Float, Double
60 -- Mismatch between the argument types and the format string will cause
61 -- an exception to be thrown at runtime.
65 -- > > printf "%d\n" (23::Int)
67 -- > > printf "%s %s\n" "Hello" "World"
69 -- > > printf "%.2f\n" pi
72 printf :: (PrintfType r) => String -> r
73 printf fmt = spr fmt []
75 -- | Similar to 'printf', except that output is via the specified
76 -- 'Handle'. The return type is restricted to @('IO' a)@.
77 hPrintf :: (HPrintfType r) => Handle -> String -> r
78 hPrintf hdl fmt = hspr hdl fmt []
80 -- |The 'PrintfType' class provides the variable argument magic for
81 -- 'printf'. Its implementation is intentionally not visible from
82 -- this module. If you attempt to pass an argument of a type which
83 -- is not an instance of this class to 'printf' or 'hPrintf', then
84 -- the compiler will report it as a missing instance of 'PrintfArg'.
85 class PrintfType t where
86 spr :: String -> [UPrintf] -> t
88 -- | The 'HPrintfType' class provides the variable argument magic for
89 -- 'hPrintf'. Its implementation is intentionally not visible from
91 class HPrintfType t where
92 hspr :: Handle -> String -> [UPrintf] -> t
94 {- not allowed in Haskell 98
95 instance PrintfType String where
96 spr fmt args = uprintf fmt (reverse args)
98 instance (IsChar c) => PrintfType [c] where
99 spr fmt args = map fromChar (uprintf fmt (reverse args))
101 instance PrintfType (IO a) where
103 putStr (uprintf fmt (reverse args))
106 instance HPrintfType (IO a) where
107 hspr hdl fmt args = do
108 hPutStr hdl (uprintf fmt (reverse args))
111 instance (PrintfArg a, PrintfType r) => PrintfType (a -> r) where
112 spr fmt args = \ a -> spr fmt (toUPrintf a : args)
114 instance (PrintfArg a, HPrintfType r) => HPrintfType (a -> r) where
115 hspr hdl fmt args = \ a -> hspr hdl fmt (toUPrintf a : args)
117 class PrintfArg a where
118 toUPrintf :: a -> UPrintf
120 instance PrintfArg Char where
121 toUPrintf c = UChar c
123 {- not allowed in Haskell 98
124 instance PrintfArg String where
125 toUPrintf s = UString s
127 instance (IsChar c) => PrintfArg [c] where
128 toUPrintf s = UString (map toChar s)
130 instance PrintfArg Int where
133 instance PrintfArg Integer where
134 toUPrintf i = UInteger i
136 instance PrintfArg Float where
137 toUPrintf f = UFloat f
139 instance PrintfArg Double where
140 toUPrintf d = UDouble d
144 fromChar :: Char -> c
146 instance IsChar Char where
152 data UPrintf = UChar Char | UString String | UInt Int | UInteger Integer | UFloat Float | UDouble Double
154 uprintf :: String -> [UPrintf] -> String
156 uprintf "" (_:_) = fmterr
157 uprintf ('%':'%':cs) us = '%':uprintf cs us
158 uprintf ('%':_) [] = argerr
159 uprintf ('%':cs) us@(_:_) = fmt cs us
160 uprintf (c:cs) us = c:uprintf cs us
162 fmt :: String -> [UPrintf] -> String
164 let (width, prec, ladj, zero, cs', us') = getSpecs False False cs us
166 let lstr = length str
168 fill = if lstr+lpre < width then take (width-(lstr+lpre)) (repeat (if zero then '0' else ' ')) else ""
169 in if ladj then pre ++ str ++ fill else if zero then pre ++ fill ++ str else fill ++ pre ++ str
178 'c' -> adjust ("", [toEnum (toint u)])
179 'd' -> adjust (fmti u)
180 'x' -> adjust ("", fmtu 16 u)
181 'o' -> adjust ("", fmtu 8 u)
182 'u' -> adjust ("", fmtu 10 u)
183 'e' -> adjust (dfmt' c prec u)
184 'f' -> adjust (dfmt' c prec u)
185 'g' -> adjust (dfmt' c prec u)
186 's' -> adjust ("", tostr u)
187 c -> perror ("bad formatting char " ++ [c])
188 ) ++ uprintf cs'' us''
190 fmti (UInt i) = if i < 0 then
191 if i == -i then fmti (UInteger (toInteger i)) else ("-", itos (-i))
194 fmti (UInteger i) = if i < 0 then ("-", itos (-i)) else ("", itos i)
195 fmti (UChar c) = fmti (UInt (fromEnum c))
198 fmtu b (UInt i) = if i < 0 then
199 if i == -i then itosb b (maxi - toInteger (i+1) - 1) else itosb b (maxi - toInteger (-i))
201 itosb b (toInteger i)
202 fmtu b (UInteger i) = itosb b i
203 fmtu b (UChar c) = itosb b (toInteger (fromEnum c))
207 maxi = (toInteger (maxBound::Int) + 1) * 2
210 toint (UInteger i) = toInt i
211 toint (UChar c) = fromEnum c
214 tostr (UString s) = s
219 [toEnum (fromEnum '0' + toInt n)]
221 let (q, r) = quotRem n 10 in
222 itos q ++ [toEnum (fromEnum '0' + toInt r)]
224 chars = array (0,15) (zipWith (,) [0..] "0123456789abcdef")
225 itosb :: Integer -> Integer -> String
230 let (q, r) = quotRem n b in
231 itosb b q ++ [chars!r]
233 stoi :: Int -> String -> (Int, String)
234 stoi a (c:cs) | isDigit c = stoi (a*10 + fromEnum c - fromEnum '0') cs
237 getSpecs :: Bool -> Bool -> String -> [UPrintf] -> (Int, Int, Bool, Bool, String, [UPrintf])
238 getSpecs l z ('-':cs) us = getSpecs True z cs us
239 getSpecs l z ('0':cs) us = getSpecs l True cs us
240 getSpecs l z ('*':cs) us =
247 '.':'*':r -> case us' of { [] -> argerr; pu:us'' -> (toint pu, r, us'') }
248 '.':r -> let (n, cs') = stoi 0 r in (n, cs', us')
250 in (n, p, l, z, cs'', us'')
251 getSpecs l z ('.':cs) us =
252 let (p, cs') = stoi 0 cs
253 in (0, p, l, z, cs', us)
254 getSpecs l z cs@(c:_) us | isDigit c =
255 let (n, cs') = stoi 0 cs
256 (p, cs'') = case cs' of
259 in (n, p, l, z, cs'', us)
260 getSpecs l z cs us = (0, -1, l, z, cs, us)
262 dfmt' c p (UDouble d) = dfmt c p d
263 dfmt' c p (UFloat f) = dfmt c p f
267 case (case c of 'e' -> showEFloat; 'f' -> showFFloat; 'g' -> showGFloat)
268 (if p < 0 then Nothing else Just p) d "" of
272 perror s = error ("Printf.printf: "++s)
273 fmterr = perror "formatting string ended prematurely"
274 argerr = perror "argument list ended prematurely"
275 baderr = perror "bad argument"
277 toInt :: (Integral a) => a -> Int
278 toInt x = fromInteger (toInteger x)