1 -----------------------------------------------------------------------------
2 -- $Id: Printf.lhs,v 1.2 2001/02/21 16:24:34 simonmar Exp $
4 -- (c) Simon Marlow 1997-2001
5 -----------------------------------------------------------------------------
7 > module Printf (showFloat, showFloat') where
15 > import PrelPack (unpackCString)
18 > :: Bool -- Always print decimal point
19 > -> Bool -- Left adjustment
20 > -> Bool -- Always print sign
21 > -> Bool -- Leave blank before positive number
22 > -> Bool -- Use zero padding
23 > -> Maybe Int -- Field Width
24 > -> Maybe Int -- Precision
28 > bUFSIZE = 512 :: Int
30 > showFloat alt left sign blank zero width prec num =
31 > unsafePerformIO ( do
32 > buf <- malloc bUFSIZE
33 > snprintf buf (fromIntegral bUFSIZE) (packString format) num
34 > let s = unpackCString buf
35 > length s `seq` -- urk! need to force the string before we
36 > -- free the buffer. A better solution would
37 > -- be to use foreign objects and finalisers,
38 > -- but that's just too heavyweight.
48 > if_bool blank " " ++
50 > if_maybe width show ++
51 > if_maybe prec (\s -> "." ++ show s) ++
54 > showFloat' :: Maybe Int -> Maybe Int -> Float -> String
55 > showFloat' = showFloat False False False False False
57 > if_bool False s = []
60 > if_maybe Nothing f = []
61 > if_maybe (Just s) f = f s
63 > type PackedString = ByteArray Int
64 > foreign import unsafe snprintf :: Addr -> CSize -> PackedString -> Float -> IO ()