text-all 0.2.0.0 → 0.3.0.0
raw patch · 3 files changed
+109/−44 lines, 3 files
Files
- CHANGELOG.md +4/−0
- lib/Data/Text/All.hs +104/−43
- text-all.cabal +1/−1
CHANGELOG.md view
@@ -1,3 +1,7 @@+# 0.3.0.0++* Replaced functions like `strictToBuilder` with conversion typeclasses (i.e. now it's just `toStrict`, `toLazy`, `toBuilder`, and `toString`).+ # 0.2.0.0 * Renamed lots of functions, moved some into other modules.
lib/Data/Text/All.hs view
@@ -1,3 +1,17 @@+{-# LANGUAGE+GADTs,+TypeSynonymInstances+ #-}+++{- |+Here are the nice things from text that you get (thanks to a restrictive lower bound) but that aren't documented elsewhere in this module:++* The 'T.takeWhileEnd' function.+* An instance for @Semigroup@.+* An instance for @printf@ (i.e. you can use a 'Text' as one of @printf@'s arguments).+* An instance for @Binary@.+-} module Data.Text.All ( -- * Standard modules from text@@ -5,29 +19,33 @@ module Data.Text.IO, module Data.Text.Encoding, - -- * Types+ -- * Lazy 'Text' LText,- Builder, + -- * Conversion+ -- $conversion+ toStrict, toLazy, toBuilder, toString,+ -- * Showing- show, lshow, bshow,- -- ** Via 'Show'- show', lshow', bshow',+ -- $showing - -- * Conversion- strictToLazy, lazyToStrict,+ -- ** Strict 'Text'+ show, show',+ -- ** Lazy 'Text'+ lshow, lshow',+ -- ** 'Builder'+ bshow, bshow', -- * Formatting+ -- $formatting module Data.Text.Format, module Data.Text.Buildable, format, lformat, bformat, - -- * Builder+ -- * 'Builder'+ Builder, bsingleton, flush,- -- ** Conversion- builderToStrict, builderToLazy,- strictToBuilder, lazyToBuilder, ) where @@ -40,7 +58,7 @@ import qualified Data.Text.Lazy as TL -import TextShow hiding (Builder)+import TextShow hiding (Builder, toString) import Data.Text.Format hiding (format, print, hprint, build) import Data.Text.Format.Params@@ -50,10 +68,15 @@ import Prelude hiding (show) --- | Lazy 'TL.Text'. type LText = TL.Text --- | A fast variant of 'show' for 'Text' that only works for some types. If you want more instances, import <https://hackage.haskell.org/package/text-show-instances text-show-instances> or use 'show'' if the type is your own and you only have a 'Show' instance defined.+{- $showing++'show' is a fast variant of @show@ for 'Text' \/ 'Builder' that only works for some types – it's very fast for 'Int', etc, but doesn't work for types that you have defined yourself. (If you want more instances, import <https://hackage.haskell.org/package/text-show-instances text-show-instances>.)++'show'' is a shortcut for @pack.show@ that works for everything with a 'Show' instance but is slower.+-}+ show :: TextShow a => a -> Text show = showt {-# INLINE show #-}@@ -66,7 +89,6 @@ bshow = showb {-# INLINE bshow #-} --- | Like 'show', but works for anything that has a 'Show' instance. Slower than 'show'. show' :: Show a => a -> Text show' = pack . P.show {-# INLINE show' #-}@@ -79,50 +101,89 @@ bshow' = B.fromString . P.show {-# INLINE bshow' #-} --- | A variant of 'Data.Text.Format.format' that produces strict 'Text'.+{- $formatting++'format' is a function similar to @printf@ in spirit. Don't forget to enable @OverloadedStrings@ if you want to use it!++>>> format "{}+{}={}" (2, 2, 4)+"2+2=4"++If you have only one argument, use a list:++>>> format "2+2={}" [4]+"2+2=4"++There are some formatting options available:++>>> format "123 = 0x{}, pi = {}" (hex 123, fixed 5 pi)+"123 = 0x7b, pi = 3.14159"++For more formatters, see "Data.Text.Format".+-}+ format :: Params ps => Format -> ps -> Text format f = TL.toStrict . Format.format f {-# INLINE format #-} --- | A variant of 'Data.Text.Format.format' that produces lazy 'Text'. lformat :: Params ps => Format -> ps -> LText lformat = Format.format {-# INLINE lformat #-} --- | A variant of 'Data.Text.Format.format' that produces a 'Builder'. bformat :: Params ps => Format -> ps -> Builder bformat = Format.build {-# INLINE bformat #-} --- | Convert a 'Text' into a lazy 'TL.Text'.-strictToLazy :: Text -> LText-strictToLazy = TL.fromStrict-{-# INLINE strictToLazy #-}---- | Convert a lazy 'TL.Text' into a 'Text'.-lazyToStrict :: LText -> Text-lazyToStrict = TL.toStrict-{-# INLINE lazyToStrict #-}+{- $conversion+These functions can convert from strict\/lazy 'Text', 'Builder', and 'String'.+-} --- | Convert a 'Builder' into a 'Text'.-builderToStrict :: Builder -> Text-builderToStrict = TL.toStrict . B.toLazyText-{-# INLINE builderToStrict #-}+class ToStrict t where+ toStrict :: t -> Text+instance (a ~ Char) => ToStrict [a] where+ toStrict = pack+ {-# INLINE toStrict #-}+instance ToStrict LText where+ toStrict = TL.toStrict+ {-# INLINE toStrict #-}+instance ToStrict Builder where+ toStrict = TL.toStrict . B.toLazyText+ {-# INLINE toStrict #-} --- | Convert a 'Builder' into a lazy 'TL.Text'.-builderToLazy :: Builder -> LText-builderToLazy = B.toLazyText-{-# INLINE builderToLazy #-}+class ToLazy t where+ toLazy :: t -> LText+instance (a ~ Char) => ToLazy [a] where+ toLazy = TL.pack+ {-# INLINE toLazy #-}+instance ToLazy Text where+ toLazy = TL.fromStrict+ {-# INLINE toLazy #-}+instance ToLazy Builder where+ toLazy = B.toLazyText+ {-# INLINE toLazy #-} --- | Convert a 'Text' into a 'Builder'.-strictToBuilder :: Text -> Builder-strictToBuilder = B.fromText-{-# INLINE strictToBuilder #-}+class ToBuilder t where+ toBuilder :: t -> Builder+instance (a ~ Char) => ToBuilder [a] where+ toBuilder = B.fromString+ {-# INLINE toBuilder #-}+instance ToBuilder Text where+ toBuilder = B.fromText+ {-# INLINE toBuilder #-}+instance ToBuilder LText where+ toBuilder = B.fromLazyText+ {-# INLINE toBuilder #-} --- | Convert a lazy 'TL.Text' into a 'Builder'.-lazyToBuilder :: LText -> Builder-lazyToBuilder = B.fromLazyText-{-# INLINE lazyToBuilder #-}+class ToString t where+ toString :: t -> String+instance ToString Text where+ toString = unpack+ {-# INLINE toString #-}+instance ToString LText where+ toString = TL.unpack+ {-# INLINE toString #-}+instance ToString Builder where+ toString = TL.unpack . B.toLazyText+ {-# INLINE toString #-} -- | A 'Builder' producing a single character. bsingleton :: Char -> Builder
text-all.cabal view
@@ -1,5 +1,5 @@ name: text-all-version: 0.2.0.0+version: 0.3.0.0 synopsis: Everything Data.Text related in one package description: Everything @Data.Text@-related in one package.