bytestring-show 0.1.1 → 0.2
raw patch · 2 files changed
+71/−15 lines, 2 filesdep +arraydep +containers
Dependencies added: array, containers
Files
- Text/Show/ByteString.hs +69/−13
- bytestring-show.cabal +2/−2
Text/Show/ByteString.hs view
@@ -29,6 +29,7 @@ -- * Combining builders , unlinesP , unwordsP+ , showpParen -- * Printing values , print ) where@@ -44,6 +45,11 @@ import Data.Ratio import Data.Complex +import Data.Array++import qualified Data.Map as M+import qualified Data.Set as S+ import Text.Show.ByteString.Util ( putAscii , putUTF8 , putAsciiStr, putUTF8Str , unsafePutDigit@@ -55,9 +61,18 @@ import Text.Show.ByteString.Float -- | Conversion of values to readable byte strings.--- Minimal complete definition: 'showp'+-- Minimal complete definition: 'showp' or 'showpPrec' class Show a where -- | Encodes a value to an efficient byte string builder.+ -- The precedence is used to determine where to put+ -- parentheses in a shown expression involving operators.+ --+ -- Values of type Put can be efficiently combined, so the+ -- showp functions are available for showing multiple values+ -- before producing an output byte string.+ showpPrec :: Int -> a -> Put++ -- | Encodes a value to an efficient byte string builder. -- Values of type Put can be efficiently combined, so this -- is available for building strings from multiple values. showp :: a -> Put@@ -66,6 +81,10 @@ -- This is used, for example, when showing arrays of Chars. showpList :: [a] -> Put + showpPrec _ = showp++ showp = showpPrec 0+ showpList [] = putWord8 91 >> putWord8 93 -- "[]" showpList (x:xs) = putWord8 91 >> showp x >> go xs -- "[.. where@@ -76,6 +95,12 @@ show :: Show a => a -> ByteString show = runPut . showp +-- | A utility function for surrounding output by parentheses+-- conditionally.+showpParen :: Bool -> Put -> Put+showpParen b p | b = putAscii '(' >> p >> putAscii ')'+ | otherwise = p+ -- | Print a value to the standard output print :: Show a => a -> IO () print = putStrLn . show@@ -119,15 +144,23 @@ instance Show Int where showp = showpInt + showpPrec k i = showpParen (i < 0 && k > 0) $ showpInt i+ instance Show Int8 where showp = showpInt8 + showpPrec k i = showpParen (i < 0 && k > 0) $ showpInt8 i+ instance Show Int16 where showp = showpInt16 + showpPrec k i = showpParen (i < 0 && k > 0) $ showpInt16 i+ instance Show Int32 where showp = showpInt32 + showpPrec k i = showpParen (i < 0 && k > 0) $ showpInt32 i+ instance Show Word where showp = showpWord @@ -143,30 +176,40 @@ instance Show Integer where showp = showpInteger + showpPrec k i = showpParen (i < 0 && k > 0) $ showpInteger i+ instance Show Float where showp = showpGFloat Nothing + showpPrec k f = showpParen (f < 0 && k > 0) $ showpGFloat Nothing f+ instance Show Double where showp = showpGFloat Nothing + showpPrec k f = showpParen (f < 0 && k > 0) $ showpGFloat Nothing f+ 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+ showpPrec k q = showpParen (k > 7) $ showpPrec 8 (numerator q) >>+ putAscii '%' >> showp (denominator q) instance (Show a, RealFloat a) => Show (Complex a) where- showp (a :+ b) = showp a >>- putAscii ' ' >> putAscii ':' >> putAscii '+' >> putAscii ' ' >>- showp b+ showpPrec k (a :+ b) = showpParen (k > 6) $ showpPrec 7 a >>+ putAscii ' ' >> putAscii ':' >> putAscii '+' >> putAscii ' ' >>+ showpPrec 7 b instance Show a => Show (Maybe a) where- showp Nothing = putAsciiStr "Nothing"- showp (Just a) = putAsciiStr "Just " >> showp a+ showpPrec _ Nothing = putAsciiStr "Nothing"+ showpPrec k (Just a) = showpParen (k > 10) $ putAsciiStr "Just " >> showpPrec 11 a +instance (Show a, Show b) => Show (Either a b) where+ showpPrec k (Left a) = showpParen (k > 10) $ putAsciiStr "Left " >> showpPrec 11 a+ showpPrec k (Right b) = showpParen (k > 10) $ putAsciiStr "Right " >> showpPrec 11 b++instance Show Ordering where+ showp LT = putAscii 'L' >> putAscii 'T'+ showp EQ = putAscii 'E' >> putAscii 'Q'+ showp GT = putAscii 'G' >> putAscii 'T'+ instance (Show a, Show b) => Show (a,b) where showp (a,b) = putAscii '(' >> showp a >> putAscii ',' >> showp b >> putAscii ')' @@ -213,3 +256,16 @@ putAscii ',' >> showp f >> putAscii ',' >> showp g >> putAscii ')'++instance (Show i, Show e, Ix i) => Show (Array i e) where+ showpPrec k a = showpParen (k > 10) $ putAsciiStr "array " >>+ showp (bounds a) >> putAscii ' ' >> showp (assocs a)++instance (Show k, Show v) => Show (M.Map k v) where+ showpPrec k m = showpParen (k > 10) $ putAsciiStr "fromList " >>+ showp (M.toList m)++instance (Show e) => Show (S.Set e) where+ showpPrec k s = showpParen (k > 10) $ putAsciiStr "fromList " >>+ showp (S.toList s)+
bytestring-show.cabal view
@@ -1,5 +1,5 @@ Name: bytestring-show-Version: 0.1.1+Version: 0.2 License: BSD3 License-File: LICENSE Author: Dan Doel@@ -12,7 +12,7 @@ Cabal-Version: >= 1.2 Library- Build-Depends: base, binary, bytestring >= 0.9+ Build-Depends: base, binary, bytestring >= 0.9, array, containers Exposed-Modules: Text.Show.ByteString