packages feed

text-printer 0.4 → 0.5.0.2

raw patch · 5 files changed

Files

+ README.md view
@@ -0,0 +1,14 @@+Text-Printer+============++[![Travis](https://img.shields.io/travis/mvv/text-printer/master.svg)](https://travis-ci.org/mvv/text-printer) [![Hackage](https://img.shields.io/hackage/v/text-printer.svg)](http://hackage.haskell.org/package/text-printer)++This package provides an interface for injecting text into monoids such as+builders and printers.++Installation+------------+The usual:++	$ cabal install+
src/Text/Printer.hs view
@@ -2,6 +2,9 @@ {-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE DeriveDataTypeable #-}+#if __GLASGOW_HASKELL__ >= 706+{-# LANGUAGE DeriveGeneric #-}+#endif {-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE FlexibleInstances #-} @@ -21,6 +24,8 @@   , Utf8Builder(..)   , buildUtf8   , buildLazyUtf8+  , PrettyPrinter(..)+  , renderPretty   -- * Combinators   , (<>)   , hcat@@ -49,10 +54,17 @@   ) where  import Prelude hiding (foldr, foldr1, print, lines)+#if __GLASGOW_HASKELL__ >= 706+import GHC.Generics (Generic)+#endif import Data.Typeable (Typeable) import Data.String (IsString(..))+import Data.Semigroup (Semigroup) import qualified Data.Semigroup as S-import Data.Monoid (Monoid(..), (<>))+import Data.Monoid (Monoid(..))+#if MIN_VERSION_base(4,5,0)+import Data.Monoid ((<>))+#endif import Data.Foldable (Foldable(..), toList) import Data.Traversable (Traversable, mapAccumL, mapAccumR) import qualified Data.Text as TS@@ -64,7 +76,11 @@ import qualified Data.ByteString.Char8 as BS8 import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy.Char8 as BL8+#if MIN_VERSION_bytestring(0,10,2)+import qualified Data.ByteString.Builder as BB+#else import qualified Data.ByteString.Lazy.Builder as BB+#endif import qualified Text.PrettyPrint as PP  -- | Text monoid. 'string' must be equivalent to 'fromString' and be a monoid@@ -73,7 +89,7 @@ --   Other operations must be monoid homomorphisms that are eqiuvalent (but --   possibly faster) to the composition of 'string' and the corresponding --   embedding, e.g. @'text' = 'string' . 'TS.unpack'@.-class (IsString p, Monoid p) ⇒ Printer p where+class (IsString p, Semigroup p, Monoid p) ⇒ Printer p where   -- | Print a character. @'char' /c/@ must be equivalent to   --   @'string' [/c/]@, but hopefully is faster.   char ∷ Char → p@@ -120,16 +136,17 @@  -- | A simple string builder as used by 'Show'. newtype StringBuilder = StringBuilder { stringBuilder ∷ String → String }-                        deriving (Typeable, Monoid)+                        deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                                 , Generic+#endif+                                 , Semigroup+                                 , Monoid)  instance IsString StringBuilder where   fromString s = StringBuilder (s ++)   {-# INLINE fromString #-} -instance S.Semigroup StringBuilder where-  (<>) = mappend-  {-# INLINE (<>) #-}- instance Printer StringBuilder where   char c = StringBuilder (c :)   {-# INLINE char #-}@@ -157,15 +174,21 @@ -- | Use this builder when you are sure that only ASCII characters --   will get printed to it. newtype AsciiBuilder = AsciiBuilder { asciiBuilder ∷ BB.Builder }-                       deriving (Typeable, Monoid)+                       deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                                , Generic+#endif+                                , Monoid)  instance IsString AsciiBuilder where   fromString = AsciiBuilder . BB.string7   {-# INLINE fromString #-} -instance S.Semigroup AsciiBuilder where-  (<>) = mappend+instance Semigroup AsciiBuilder where+  b₁ <> b₂ = AsciiBuilder $ asciiBuilder b₁ <> asciiBuilder b₂   {-# INLINE (<>) #-}+  stimes = S.stimesMonoid+  {-# INLINE stimes #-}  instance Printer AsciiBuilder where   char = AsciiBuilder . BB.char7@@ -189,15 +212,21 @@  -- | UTF-8 lazy 'BL.ByteString' builder. newtype Utf8Builder = Utf8Builder { utf8Builder ∷ BB.Builder }-                      deriving (Typeable, Monoid)+                      deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                               , Generic+#endif+                               , Monoid)  instance IsString Utf8Builder where   fromString = Utf8Builder . BB.stringUtf8   {-# INLINE fromString #-} -instance S.Semigroup Utf8Builder where-  (<>) = mappend+instance Semigroup Utf8Builder where+  b₁ <> b₂ = Utf8Builder $ utf8Builder b₁ <> utf8Builder b₂   {-# INLINE (<>) #-}+  stimes = S.stimesMonoid+  {-# INLINE stimes #-}  instance Printer Utf8Builder where   char = Utf8Builder . BB.charUtf8@@ -227,10 +256,51 @@ buildLazyUtf8 = BB.toLazyByteString . utf8Builder {-# INLINE buildLazyUtf8 #-} -instance Printer PP.Doc where-  char = PP.char+newtype PrettyPrinter = PrettyPrinter { prettyPrinter ∷ PP.Doc }+                        deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                                 , Generic+#endif+#if MIN_VERSION_pretty(1,1,0)+                                 , IsString+# if MIN_VERSION_base(4,9,0)+                                 , Semigroup+# endif+                                 , Monoid+#endif+                                 )++#if !MIN_VERSION_pretty(1,1,0)+instance IsString PrettyPrinter where+  fromString = PrettyPrinter . PP.text+  {-# INLINE fromString #-}+#endif++#if !MIN_VERSION_base(4,9,0) || !MIN_VERSION_pretty(1,1,0)+instance Semigroup PrettyPrinter where+  p₁ <> p₂ = PrettyPrinter+           $ (PP.<>) (prettyPrinter p₁) (prettyPrinter p₂)+  {-# INLINE (<>) #-}+  stimes = S.stimesMonoid+  {-# INLINE stimes #-}+#endif++#if !MIN_VERSION_pretty(1,1,0)+instance Monoid PrettyPrinter where+  mempty = PP.empty+  {-# INLINE mempty #-}+  mappend = (S.<>)+  {-# INLINE mappend #-}+#endif++instance Printer PrettyPrinter where+  char = PrettyPrinter . PP.char   {-# INLINE char #-} +-- | An alias for @'PP.render' . 'prettyPrinter'@+renderPretty ∷ PrettyPrinter → String+renderPretty = PP.render . prettyPrinter+ #if !MIN_VERSION_base(4,5,0) -- | An infix synonym for 'mappend'. (<>) ∷ Monoid m ⇒ m → m → m@@ -341,8 +411,9 @@   --   /x/ '<->' (/y/ '<->' /z/) = (/x/ '<->' /y/) '<->' /z/.   (<->) ∷ p → p → p -instance MultilinePrinter PP.Doc where-  (<->) = (PP.$+$)+instance MultilinePrinter PrettyPrinter where+  p₁ <-> p₂ = PrettyPrinter +            $ (PP.$+$) (prettyPrinter p₁) (prettyPrinter p₂)   {-# INLINE (<->) #-}  -- | Combine the items of a 'Foldable' data structure with '<->'.@@ -362,17 +433,26 @@  -- | A multiline printer that combines lines with the provided function. newtype LinePrinter p = LinePrinter { linePrinter ∷ (p → p → p) → p }-                        deriving Typeable+                        deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                                 , Generic+#endif+                                 )  instance IsString p ⇒ IsString (LinePrinter p) where   fromString = LinePrinter . const . fromString   {-# INLINE fromString #-} +instance Semigroup p ⇒ Semigroup (LinePrinter p) where+  x <> y = LinePrinter $ \l → linePrinter x l S.<> linePrinter y l+  {-# INLINE (<>) #-}+  stimes n x = LinePrinter $ S.stimes n . linePrinter x+  {-# INLINE stimes #-}+ instance Monoid p ⇒ Monoid (LinePrinter p) where   mempty = LinePrinter $ const mempty   {-# INLINE mempty #-}-  mappend x y = LinePrinter $ \l →-                  mappend (linePrinter x l) (linePrinter y l)+  mappend x y = LinePrinter $ \l → mappend (linePrinter x l) (linePrinter y l)   {-# INLINE mappend #-}   mconcat xs = LinePrinter $ \l → mconcat (map (\x → linePrinter x l) xs)   {-# INLINE mconcat #-}@@ -412,4 +492,3 @@ crlfPrinter ∷ Printer p ⇒ LinePrinter p → p crlfPrinter p = linePrinter p (separate crlf) {-# INLINE crlfPrinter #-}-
src/Text/Printer/Fractional.hs view
@@ -1,5 +1,9 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE DeriveDataTypeable #-}+#if __GLASGOW_HASKELL__ >= 706+{-# LANGUAGE DeriveGeneric #-}+#endif  -- | Print fractions. module Text.Printer.Fractional@@ -22,6 +26,9 @@   , fraction   ) where +#if __GLASGOW_HASKELL__ >= 706+import GHC.Generics (Generic)+#endif import Data.Typeable (Typeable) import Data.Ix (Ix) import Data.Monoid (mempty)@@ -32,7 +39,11 @@ -- | Optionality characteristic. data Optional = Optional               | Required-              deriving (Typeable, Show, Read, Eq, Ord, Enum, Bounded, Ix)+              deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                       , Generic+#endif+                       , Show, Read, Eq, Ord, Enum, Bounded, Ix)  -- | True if the supplied value is 'Optional' and false otherwise. isOptional ∷ Optional → Bool
src/Text/Printer/Integral.hs view
@@ -1,5 +1,9 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE DeriveDataTypeable #-}+#if __GLASGOW_HASKELL__ >= 706+{-# LANGUAGE DeriveGeneric #-}+#endif {-# LANGUAGE BangPatterns #-}  -- | Print integral numbers in common positional numeral systems. @@ -61,6 +65,9 @@   , upHexBits   ) where +#if __GLASGOW_HASKELL__ >= 706+import GHC.Generics (Generic)+#endif import Data.Typeable (Typeable) import Data.Char (chr, ord) import Data.Int@@ -110,7 +117,11 @@   lastDigitIn ∷ Bits α ⇒ s → α → Int  -- | The binary numeral system.-data Binary = Binary deriving (Typeable, Eq, Ord, Show, Read)+data Binary = Binary deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                              , Generic+#endif+                              , Eq, Ord, Show, Read )  instance PositionalSystem Binary where   systemName _ = "binary"@@ -141,7 +152,11 @@   {-# INLINE lastDigitIn #-}  -- | The octal numeral system.-data Octal = Octal deriving (Typeable, Eq, Ord, Show, Read)+data Octal = Octal deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                            , Generic+#endif+                            , Eq, Ord, Show, Read )  instance PositionalSystem Octal where   systemName _ = "octal"@@ -174,7 +189,11 @@   {-# INLINE lastDigitIn #-}  -- | The decimal numeral system.-data Decimal = Decimal deriving (Typeable, Eq, Ord, Show, Read)+data Decimal = Decimal deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                                , Generic+#endif+                                , Eq, Ord, Show, Read )  instance PositionalSystem Decimal where   systemName _ = "decimal"@@ -197,7 +216,11 @@   {-# INLINE printZeroIn #-}  -- | The hexadecimal numeral system.-data Hexadecimal = Hexadecimal+data Hexadecimal = Hexadecimal deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                                        , Generic+#endif+                                        , Eq, Ord, Show, Read )  instance PositionalSystem Hexadecimal where   systemName _ = "hexadecimal"@@ -232,7 +255,11 @@   {-# INLINABLE lastDigitIn #-}  -- | The hexadecimal numeral system, using lower case digits.-data LowHex = LowHex+data LowHex = LowHex deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                              , Generic+#endif+                              , Eq, Ord, Show, Read )  instance PositionalSystem LowHex where   systemName _ = "lower case hexadecimal"@@ -267,7 +294,11 @@   {-# INLINABLE lastDigitIn #-}  -- | The hexadecimal numeral system, using upper case digits.-data UpHex = UpHex+data UpHex = UpHex deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+                            , Generic+#endif+                            , Eq, Ord, Show, Read )  instance PositionalSystem UpHex where   systemName _ = "upper case hexadecimal"@@ -593,7 +624,7 @@ {-# INLINE npUpHexBits #-}  -- | Print a number in the specified positional numeral system.-number' ∷ (PositionalSystem s, Ord α, Integral α, Printer p)+number' ∷ (PositionalSystem s, Integral α, Printer p)         ⇒ s         → p -- ^ Prefix for negative values         → p -- ^ Zero printer@@ -622,21 +653,21 @@ {-# SPECIALIZE number' ∷ Printer p ⇒ Decimal → p → p → p → Word16 → p #-} {-# SPECIALIZE number' ∷ Printer p ⇒ Decimal → p → p → p → Word32 → p #-} {-# SPECIALIZE number' ∷ Printer p ⇒ Decimal → p → p → p → Word64 → p #-}-{-# SPECIALIZE number' ∷ (Ord α, Integral α, Printer p) ⇒ Binary → p → p → p → α → p #-}-{-# SPECIALIZE number' ∷ (Ord α, Integral α, Printer p) ⇒ Decimal → p → p → p → α → p #-}-{-# SPECIALIZE number' ∷ (Ord α, Integral α, Printer p) ⇒ Octal → p → p → p → α → p #-}-{-# SPECIALIZE number' ∷ (Ord α, Integral α, Printer p) ⇒ Hexadecimal → p → p → p → α → p #-}-{-# SPECIALIZE number' ∷ (Ord α, Integral α, Printer p) ⇒ LowHex → p → p → p → α → p #-}-{-# SPECIALIZE number' ∷ (Ord α, Integral α, Printer p) ⇒ UpHex → p → p → p → α → p #-}+{-# SPECIALIZE number' ∷ (Integral α, Printer p) ⇒ Binary → p → p → p → α → p #-}+{-# SPECIALIZE number' ∷ (Integral α, Printer p) ⇒ Decimal → p → p → p → α → p #-}+{-# SPECIALIZE number' ∷ (Integral α, Printer p) ⇒ Octal → p → p → p → α → p #-}+{-# SPECIALIZE number' ∷ (Integral α, Printer p) ⇒ Hexadecimal → p → p → p → α → p #-}+{-# SPECIALIZE number' ∷ (Integral α, Printer p) ⇒ LowHex → p → p → p → α → p #-}+{-# SPECIALIZE number' ∷ (Integral α, Printer p) ⇒ UpHex → p → p → p → α → p #-}  -- | Print a number in the specified positional numeral system. Negative --   values are prefixed with a minus sign.-number ∷ (PositionalSystem s, Ord α, Integral α, Printer p) ⇒ s → α → p+number ∷ (PositionalSystem s, Integral α, Printer p) ⇒ s → α → p number s = number' s (char7 '-') (printZeroIn s) mempty {-# INLINE number #-}  -- | Print a number in the binary numeral system.-binary' ∷ (Ord α, Integral α, Printer p)+binary' ∷ (Integral α, Printer p)         ⇒ p -- ^ Prefix for negative values         → p -- ^ Zero printer         → p -- ^ Prefix for positive values@@ -646,12 +677,12 @@  -- | Print a number in the binary numeral system. Negative values --   are prefixed with a minus sign.-binary ∷ (Ord α, Integral α, Printer p) ⇒ α → p+binary ∷ (Integral α, Printer p) ⇒ α → p binary = number Binary {-# INLINE binary #-}  -- | Print a number in the octal numeral system.-octal' ∷ (Ord α, Integral α, Printer p)+octal' ∷ (Integral α, Printer p)        ⇒ p -- ^ Prefix for negative values        → p -- ^ Zero printer        → p -- ^ Prefix for positive values@@ -661,12 +692,12 @@  -- | Print a number in the octal numeral system. Negative values --   are prefixed with a minus sign.-octal ∷ (Ord α, Integral α, Printer p) ⇒ α → p+octal ∷ (Integral α, Printer p) ⇒ α → p octal = number Octal {-# INLINE octal #-}  -- | Print a number in the decimal numeral system.-decimal' ∷ (Ord α, Integral α, Printer p)+decimal' ∷ (Integral α, Printer p)          ⇒ p -- ^ Prefix for negative values          → p -- ^ Zero printer          → p -- ^ Prefix for positive values@@ -676,13 +707,13 @@  -- | Print a number in the decimal numeral system. Negative values --   are prefixed with a minus sign.-decimal ∷ (Ord α, Integral α, Printer p) ⇒ α → p+decimal ∷ (Integral α, Printer p) ⇒ α → p decimal = number Decimal {-# INLINE decimal #-}  -- | Print a number in the hexadecimal numeral system using lower case --   digits.-lowHex' ∷ (Ord α, Integral α, Printer p)+lowHex' ∷ (Integral α, Printer p)         ⇒ p -- ^ Prefix for negative values         → p -- ^ Zero printer         → p -- ^ Prefix for positive values@@ -692,13 +723,13 @@  -- | Print a number in the hexadecimal numeral system using lower case --   digits. Negative values are prefixed with a minus sign.-lowHex ∷ (Ord α, Integral α, Printer p) ⇒ α → p+lowHex ∷ (Integral α, Printer p) ⇒ α → p lowHex = number LowHex {-# INLINE lowHex #-}  -- | Print a number in the hexadecimal numeral system using upper case --   digits.-upHex' ∷ (Ord α, Integral α, Printer p)+upHex' ∷ (Integral α, Printer p)        ⇒ p -- ^ Prefix for negative values        → p -- ^ Zero printer        → p -- ^ Prefix for positive values@@ -708,7 +739,7 @@  -- | Print a number in the hexadecimal numeral system using upper case --   digits. Negative values are prefixed with a minus sign.-upHex ∷ (Ord α, Integral α, Printer p) ⇒ α → p+upHex ∷ (Integral α, Printer p) ⇒ α → p upHex = number UpHex {-# INLINE upHex #-} 
text-printer.cabal view
@@ -1,5 +1,5 @@ Name: text-printer-Version: 0.4+Version: 0.5.0.2 Category: Text Stability: experimental Synopsis: Abstract interface for text builders/printers.@@ -16,6 +16,12 @@ License: BSD3 License-File: LICENSE +Extra-Source-Files:+  README.md++Tested-With: GHC==7.6.3, GHC==7.8.4, GHC==7.10.3, GHC==8.0.2, GHC==8.2.2,+             GHC==8.4.4, GHC==8.6.5, GHC==8.8.4, GHC==8.10.7, GHC==9.0.1+ Cabal-Version: >= 1.10.0 Build-Type: Simple @@ -26,12 +32,13 @@ Library   Default-Language: Haskell2010   Build-Depends:-    base >= 4 && < 5,+    base >= 4.4 && < 5,     bytestring >= 0.10,     text,     pretty,-    text-latin1 >= 0.3,-    semigroups >= 0.8+    text-latin1 >= 0.3.1+  if impl(ghc < 8.0)+    Build-Depends: semigroups >= 0.18.2   Hs-Source-Dirs: src   GHC-Options: -Wall   Exposed-Modules:@@ -43,7 +50,7 @@   Default-Language: Haskell2010   Type: exitcode-stdio-1.0   Build-Depends:-    base                       >= 4 && < 5,+    base                       >= 4.4 && < 5,     test-framework             >= 0.5,     test-framework-quickcheck2 >= 0.2,     QuickCheck                 >= 2.4,