module Text.Show.ByteString (
Show (..)
, show
, putAscii
, putUTF8
, putAsciiStr
, putUTF8Str
, unsafePutDigit
, putDigit
, showpGFloat
, showpFFloat
, showpEFloat
, unlinesP
, unwordsP
, print
) where
import Prelude hiding (Show(..), print, putStrLn)
import qualified Prelude
import Data.Binary.Put
import Data.ByteString.Lazy
import Data.Int
import Data.Word
import Data.Ratio
import Data.Complex
import Text.Show.ByteString.Util ( putAscii , putUTF8
, putAsciiStr, putUTF8Str
, unsafePutDigit
)
import Text.Show.ByteString.Char
import Text.Show.ByteString.Int
import Text.Show.ByteString.Integer
import Text.Show.ByteString.Float
class Show a where
showp :: a -> Put
showpList :: [a] -> Put
showpList [] = putWord8 91 >> putWord8 93
showpList (x:xs) = putWord8 91 >> showp x >> go xs
where
go (y:ys) = putWord8 44 >> showp y >> go ys
go [ ] = putWord8 93
show :: Show a => a -> ByteString
show = runPut . showp
print :: Show a => a -> IO ()
print = putStrLn . show
unlinesP :: [Put] -> Put
unlinesP [ ] = return ()
unlinesP (p:ps) = p >> putAscii '\n' >> unlinesP ps
unwordsP :: [Put] -> Put
unwordsP [ ] = return ()
unwordsP [p] = p
unwordsP (p:ps) = p >> putAscii ' ' >> unwordsP ps
putDigit :: Int -> Put
putDigit i
| i < 0 = error $ "putDigit: Negative integer: " ++ Prelude.show i
| i > 9 = error $ "putDigit: Non-decimal digit: " ++ Prelude.show i
| otherwise = unsafePutDigit i
instance Show Put where
showp p = p
instance Show () where
showp () = putAscii '(' >> putAscii ')'
instance Show Char where
showp = showpChar
showpList = showpString
instance Show Bool where
showp True = putAsciiStr "True"
showp False = putAsciiStr "False"
instance (Show a) => Show [a] where
showp = showpList
instance Show Int where
showp = showpInt
instance Show Int8 where
showp = showpInt8
instance Show Int16 where
showp = showpInt16
instance Show Int32 where
showp = showpInt32
instance Show Word where
showp = showpWord
instance Show Word8 where
showp = showpWord8
instance Show Word16 where
showp = showpWord16
instance Show Word32 where
showp = showpWord32
instance Show Integer where
showp = showpInteger
instance Show Float where
showp = showpGFloat Nothing
instance Show Double where
showp = showpGFloat Nothing
instance (Show a, Integral a) => Show (Ratio a) where
showp q = wrap (numerator q) >>
putAscii '%' >>
showp (denominator q)
where
wrap n
| n < 0 = putAscii '(' >> showp n >> putAscii ')'
| otherwise = showp n
instance (Show a, RealFloat a) => Show (Complex a) where
showp (a :+ b) = showp a >>
putAscii ' ' >> putAscii ':' >> putAscii '+' >> putAscii ' ' >>
showp b
instance Show a => Show (Maybe a) where
showp Nothing = putAsciiStr "Nothing"
showp (Just a) = putAsciiStr "Just " >> showp a
instance (Show a, Show b) => Show (a,b) where
showp (a,b) = putAscii '(' >> showp a >> putAscii ',' >> showp b >> putAscii ')'
instance (Show a, Show b, Show c) => Show (a,b,c) where
showp (a,b,c) = putAscii '(' >> showp a >>
putAscii ',' >> showp b >>
putAscii ',' >> showp c >>
putAscii ')'
instance (Show a, Show b, Show c, Show d) => Show (a,b,c,d) where
showp (a,b,c,d) =
putAscii '(' >> showp a >>
putAscii ',' >> showp b >>
putAscii ',' >> showp c >>
putAscii ',' >> showp d >>
putAscii ')'
instance (Show a, Show b, Show c, Show d, Show e) => Show (a,b,c,d,e) where
showp (a,b,c,d,e) =
putAscii '(' >> showp a >>
putAscii ',' >> showp b >>
putAscii ',' >> showp c >>
putAscii ',' >> showp d >>
putAscii ',' >> showp e >>
putAscii ')'
instance (Show a, Show b, Show c, Show d, Show e, Show f) => Show (a,b,c,d,e,f) where
showp (a,b,c,d,e,f) =
putAscii '(' >> showp a >>
putAscii ',' >> showp b >>
putAscii ',' >> showp c >>
putAscii ',' >> showp d >>
putAscii ',' >> showp e >>
putAscii ',' >> showp f >>
putAscii ')'
instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g) => Show (a,b,c,d,e,f,g) where
showp (a,b,c,d,e,f,g) =
putAscii '(' >> showp a >>
putAscii ',' >> showp b >>
putAscii ',' >> showp c >>
putAscii ',' >> showp d >>
putAscii ',' >> showp e >>
putAscii ',' >> showp f >>
putAscii ',' >> showp g >>
putAscii ')'