2 -- A C printf like formatter.
6 -- * as num, but taken from argument list
7 -- . separates width from precision
8 -- Formatting characters:
9 -- c Char, Int, Integer
10 -- d Char, Int, Integer
11 -- o Char, Int, Integer
12 -- x Char, Int, Integer
13 -- u Char, Int, Integer
19 module Printf(UPrintf(..), printf) where
25 #if defined(__YALE_HASKELL__)
29 #if defined(__GLASGOW_HASKELL__)
31 import TyArray ( _ByteArray(..) )
34 data UPrintf = UChar Char | UString String | UInt Int | UInteger Integer | UFloat Float | UDouble Double
36 printf :: String -> [UPrintf] -> String
38 printf "" (_:_) = fmterr
39 printf ('%':'%':cs) us = '%':printf cs us
40 printf ('%':_) [] = argerr
41 printf ('%':cs) us@(_:_) = fmt cs us
42 printf (c:cs) us = c:printf cs us
44 fmt :: String -> [UPrintf] -> String
46 let (width, prec, ladj, zero, cs', us') = getSpecs False False cs us
50 fill = if lstr+lpre < width then take (width-(lstr+lpre)) (repeat (if zero then '0' else ' ')) else ""
51 in if ladj then pre ++ str ++ fill else if zero then pre ++ fill ++ str else fill ++ pre ++ str
60 'c' -> adjust ("", [chr (toint u)])
61 'd' -> adjust (fmti u)
62 'x' -> adjust ("", fmtu 16 u)
63 'o' -> adjust ("", fmtu 8 u)
64 'u' -> adjust ("", fmtu 10 u)
65 #if defined __YALE_HASKELL__
66 'e' -> adjust (fmte prec (todbl u))
67 'f' -> adjust (fmtf prec (todbl u))
68 'g' -> adjust (fmtg prec (todbl u))
70 'e' -> adjust (dfmt c prec (todbl u))
71 'f' -> adjust (dfmt c prec (todbl u))
72 'g' -> adjust (dfmt c prec (todbl u))
74 's' -> adjust ("", tostr u)
75 c -> perror ("bad formatting char " ++ [c])
78 fmti (UInt i) = if i < 0 then
79 if i == -i then fmti (UInteger (toInteger i)) else ("-", itos (-i))
82 fmti (UInteger i) = if i < 0 then ("-", itos (-i)) else ("", itos i)
83 fmti (UChar c) = fmti (UInt (ord c))
86 fmtu b (UInt i) = if i < 0 then
87 if i == -i then itosb b (maxi - toInteger (i+1) - 1) else itosb b (maxi - toInteger (-i))
90 fmtu b (UInteger i) = itosb b i
91 fmtu b (UChar c) = itosb b (toInteger (ord c))
95 maxi = (toInteger maxInt + 1) * 2
98 toint (UInteger i) = toInt i
99 toint (UChar c) = ord c
102 tostr (UString s) = s
105 todbl (UDouble d) = d
106 #if defined(__GLASGOW_HASKELL__)
107 todbl (UFloat (F# f)) = D# (float2Double# f) -- What a great system(TM) !
109 todbl (UFloat f) = fromRational (toRational f)
115 [chr (ord '0' + toInt n)]
117 let (q, r) = quotRem n 10 in
118 itos q ++ [chr (ord '0' + toInt r)]
120 chars :: Array Int Char
122 chars = array (0,15) (zipWith (:=) [0..] "0123456789abcdef")
124 chars = array (0,15) (zipWith (\x y -> (x,y)) [0..] "0123456789abcdef")
127 itosb :: Integer -> Integer -> String
130 [chars ! fromInteger n]
132 let (q, r) = quotRem n b in
133 itosb b q ++ [chars ! fromInteger r]
135 stoi :: Int -> String -> (Int, String)
136 stoi a (c:cs) | isDigit c = stoi (a*10 + ord c - ord '0') cs
139 getSpecs :: Bool -> Bool -> String -> [UPrintf] -> (Int, Int, Bool, Bool, String, [UPrintf])
140 getSpecs l z ('-':cs) us = getSpecs True z cs us
141 getSpecs l z ('0':cs) us = getSpecs l True cs us
142 getSpecs l z ('*':cs) us =
149 '.':'*':r -> case us' of { [] -> argerr; pu:us'' -> (toint pu, r, us'') }
150 '.':r -> let (n, cs') = stoi 0 r in (n, cs', us')
152 in (n, p, l, z, cs'', us'')
153 getSpecs l z cs@(c:_) us | isDigit c =
154 let (n, cs') = stoi 0 cs
155 (p, cs'') = case cs' of
158 in (n, p, l, z, cs'', us)
159 getSpecs l z cs us = (0, -1, l, z, cs, us)
161 #if !defined(__YALE_HASKELL__)
162 dfmt :: Char -> Int -> Double -> (String, String)
165 #if defined(__GLASGOW_HASKELL__)
166 dfmt c{-e,f, or g-} prec d
167 = unsafePerformPrimIO (
168 newCharArray (0 :: Int, 511){-pathetic malloc-} `thenStrictlyST` \ sprintf_here ->
170 sprintf_fmt = "%1" ++ (if prec < 0 then "" else '.':itos prec) ++ [c]
172 _ccall_ sprintf sprintf_here sprintf_fmt d `seqPrimIO`
173 freezeCharArray sprintf_here `thenST` \ (_ByteArray _ arr#) ->
175 unpack :: Int# -> [Char]
176 unpack nh = case (ord# (indexCharArray# arr# nh)) of
178 ch -> case (nh +# 1#) of
179 mh -> C# (chr# ch) : unpack mh
182 case (indexCharArray# arr# 0#) of
183 '-'# -> ("-", unpack 1#)
184 _ -> ("" , unpack 0#)
191 case fmtf ("1" ++ (if p < 0 then "" else '.':itos p) ++ [c]) d of
196 #if defined(__YALE_HASKELL__)
198 case (primFmte p d) of
202 case (primFmtf p d) of
206 case (primFmtg p d) of
211 perror s = error ("Printf.printf: "++s)
212 fmterr = perror "formatting string ended prematurely"
213 argerr = perror "argument list ended prematurely"
214 baderr = perror "bad argument"
216 #if defined(__YALE_HASKELL__)
217 -- This is needed because standard Haskell does not have toInt
219 toInt :: Integral a => a -> Int
220 toInt x = fromIntegral x