Skip to content

Commit

Permalink
Support strict Text
Browse files Browse the repository at this point in the history
  • Loading branch information
3noch committed Oct 29, 2016
1 parent 83c1310 commit ffdca24
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
21 changes: 18 additions & 3 deletions Text/PrettyPrint/Leijen/Text.hs
Expand Up @@ -70,7 +70,7 @@ module Text.PrettyPrint.Leijen.Text (
Doc,

-- * Basic combinators
empty, char, text, (<>), nest, line, linebreak, group, softline,
empty, char, text, textStrict, (<>), nest, line, linebreak, group, softline,
softbreak, spacebreak,

-- * Alignment
Expand Down Expand Up @@ -104,7 +104,7 @@ module Text.PrettyPrint.Leijen.Text (
squote, dquote, semi, colon, comma, space, dot, backslash, equals,

-- * Primitive type documents
string, int, integer, float, double, rational, bool,
string, stringStrict, int, integer, float, double, rational, bool,

-- * Position-based combinators
column, nesting, width,
Expand All @@ -114,7 +114,7 @@ module Text.PrettyPrint.Leijen.Text (

-- * Rendering
SimpleDoc(..), renderPretty, renderCompact, renderOneLine,
displayB, displayT, displayIO, putDoc, hPutDoc
displayB, displayT, displayTStrict, displayIO, putDoc, hPutDoc

) where

Expand All @@ -128,6 +128,7 @@ import System.IO (Handle, hPutChar, stdout)
import Data.Int (Int64)
import Data.List (intersperse)
import Data.Monoid (Monoid (..), (<>))
import qualified Data.Text as TS
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import Data.Text.Lazy.Builder (Builder)
Expand Down Expand Up @@ -513,6 +514,9 @@ equals = char '='
string :: Text -> Doc
string = mconcat . intersperse line . map text . T.lines

stringStrict :: TS.Text -> Doc
stringStrict = mconcat . intersperse line . map textStrict . TS.lines

-- | The document @(bool b)@ shows the literal boolean @b@ using
-- 'text'.
bool :: Bool -> Doc
Expand Down Expand Up @@ -568,6 +572,9 @@ instance Pretty Doc where
instance Pretty Text where
pretty = string

instance Pretty TS.Text where
pretty = stringStrict

instance Pretty () where
pretty () = text' ()

Expand Down Expand Up @@ -806,6 +813,11 @@ text s
| T.null s = Empty
| otherwise = Text (T.length s) (B.fromLazyText s)

textStrict :: TS.Text -> Doc
textStrict s
| TS.null s = Empty
| otherwise = Text (fromIntegral $ TS.length s) (B.fromText s)

-- | The @line@ document advances to the next line and indents to the
-- current nesting level. Document @line@ behaves like @(text \"
-- \")@ if the line break is undone by 'group' or if rendered with
Expand Down Expand Up @@ -1005,6 +1017,9 @@ c `consB` b = B.singleton c `mappend` b
displayT :: SimpleDoc -> Text
displayT = B.toLazyText . displayB

displayTStrict :: SimpleDoc -> TS.Text
displayTStrict = T.toStrict . displayT

-- | @(displayIO handle simpleDoc)@ writes @simpleDoc@ to the
-- file handle @handle@. This function is used for example by
-- 'hPutDoc':
Expand Down
15 changes: 11 additions & 4 deletions Text/PrettyPrint/Leijen/Text/Monadic.hs
Expand Up @@ -21,7 +21,7 @@ module Text.PrettyPrint.Leijen.Text.Monadic (
Doc, -- putDoc, hPutDoc,

-- * Basic combinators
empty, char, text, (<>), nest, line, linebreak, group, softline,
empty, char, text, textStrict, (<>), nest, line, linebreak, group, softline,
softbreak, spacebreak,

-- * Alignment
Expand Down Expand Up @@ -55,7 +55,7 @@ module Text.PrettyPrint.Leijen.Text.Monadic (
squote, dquote, semi, colon, comma, space, dot, backslash, equals,

-- * Primitive type documents
string, int, integer, float, double, rational, bool,
string, stringStrict, int, integer, float, double, rational, bool,

-- * Position-based combinators
column, nesting, width,
Expand All @@ -65,7 +65,7 @@ module Text.PrettyPrint.Leijen.Text.Monadic (

-- * Rendering
SimpleDoc(..), renderPretty, renderCompact, renderOneLine,
displayB, displayT, displayIO, putDoc, hPutDoc
displayB, displayT, displayTStrict, displayIO, putDoc, hPutDoc

) where

Expand All @@ -74,13 +74,14 @@ import Prelude hiding ((<$>))
#endif

import Text.PrettyPrint.Leijen.Text (Doc, Pretty (..), SimpleDoc (..),
displayB, displayIO, displayT,
displayB, displayIO, displayT, displayTStrict,
hPutDoc, putDoc, renderCompact,
renderOneLine, renderPretty)
import qualified Text.PrettyPrint.Leijen.Text as PP

import Control.Monad (liftM, liftM2, liftM3, liftM4)
import Data.String (IsString (..))
import qualified Data.Text as TS
import Data.Text.Lazy (Text)

infixr 5 </>,<//>,<$>,<$$>
Expand Down Expand Up @@ -423,6 +424,9 @@ equals = return PP.equals
string :: (Monad m) => Text -> m Doc
string = return . PP.string

stringStrict :: Monad m => TS.Text -> m Doc
stringStrict = return . PP.stringStrict

-- | The document @(bool b)@ shows the literal boolean @b@ using
-- 'text'.
bool :: (Monad m) => Bool -> m Doc
Expand Down Expand Up @@ -587,6 +591,9 @@ char = return . PP.char
text :: (Monad m) => Text -> m Doc
text = return . PP.text

textStrict :: Monad m => TS.Text -> m Doc
textStrict = return . PP.textStrict

-- | The @line@ document advances to the next line and indents to the
-- current nesting level. Document @line@ behaves like @(text \"
-- \")@ if the line break is undone by 'group' or if rendered with
Expand Down

0 comments on commit ffdca24

Please sign in to comment.