@@ -195,7 +195,7 @@ module Data.Text
196import Prelude (Char, Bool(..), Int, Maybe(..), String,
196import Prelude (Char, Bool(..), Int, Maybe(..), String,
197 Eq(..), Ord(..), Ordering(..), (++),
197 Eq(..), Ord(..), Ordering(..), (++),
198− Read(..), Show(..),
198+ Read(..),
199 (&&), (||), (+), (-), (.), ($), ($!), (>>),
199 (&&), (||), (+), (-), (.), ($), ($!), (>>),
200 not, return, otherwise, quot)
200 not, return, otherwise, quot)
201#if defined(HAVE_DEEPSEQ)
201#if defined(HAVE_DEEPSEQ)
@@ -211,17 +211,20 @@ import Control.Monad (foldM)
211import Control.Monad.ST (ST)
211import Control.Monad.ST (ST)
212import qualified Data.Text.Array as A
212import qualified Data.Text.Array as A
213import qualified Data.List as L
213import qualified Data.List as L
214+import Data.Binary (Binary(get, put))
214import Data.Monoid (Monoid(..))
215import Data.Monoid (Monoid(..))
215import Data.String (IsString(..))
216import Data.String (IsString(..))
216import qualified Data.Text.Internal.Fusion as S
217import qualified Data.Text.Internal.Fusion as S
217import qualified Data.Text.Internal.Fusion.Common as S
218import qualified Data.Text.Internal.Fusion.Common as S
219+import Data.Text.Encoding (decodeUtf8, encodeUtf8)
218import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
220import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
219import Data.Text.Internal.Private (span_)
221import Data.Text.Internal.Private (span_)
220−import Data.Text.Internal (Text(..), empty, empty_, firstf, mul, safe, text)
222+import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text)
223+import Data.Text.Show (singleton, unpack)
221import qualified Prelude as P
224import qualified Prelude as P
222import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
225import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
223 reverseIter_, unsafeHead, unsafeTail)
226 reverseIter_, unsafeHead, unsafeTail)
224−import Data.Text.Internal.Unsafe.Char (unsafeChr, unsafeWrite)
227+import Data.Text.Internal.Unsafe.Char (unsafeChr)
225import qualified Data.Text.Internal.Functions as F
228import qualified Data.Text.Internal.Functions as F
226import qualified Data.Text.Internal.Encoding.Utf16 as U16
229import qualified Data.Text.Internal.Encoding.Utf16 as U16
227import Data.Text.Internal.Search (indices)
230import Data.Text.Internal.Search (indices)
@@ -230,15 +233,9 @@ import Data.ByteString (ByteString)
230import qualified Data.Text.Lazy as L
233import qualified Data.Text.Lazy as L
231import Data.Int (Int64)
234import Data.Int (Int64)
233−#if __GLASGOW_HASKELL__ >= 702
234−import qualified GHC.CString as GHC
236−import qualified GHC.Base as GHC
238#if __GLASGOW_HASKELL__ >= 708
236#if __GLASGOW_HASKELL__ >= 708
239import qualified GHC.Exts as Exts
237import qualified GHC.Exts as Exts
241−import GHC.Prim (Addr#)
243-- $strict
240-- $strict
@@ -324,9 +321,6 @@ instance Eq Text where
324instance Ord Text where
321instance Ord Text where
325 compare = compareText
322 compare = compareText
327−instance Show Text where
328− showsPrec p ps r = showsPrec p (unpack ps) r
330instance Read Text where
324instance Read Text where
331 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
325 readsPrec p str = [(pack x,y) | (x,y) <- readsPrec p str]
@@ -349,6 +343,10 @@ instance Exts.IsList Text where
349instance NFData Text where rnf !_ = ()
343instance NFData Text where rnf !_ = ()
346+instance Binary Text where
347+ put t = put (encodeUtf8 t)
348+ get = P.fmap decodeUtf8 get
352-- | This instance preserves data abstraction at the cost of inefficiency.
350-- | This instance preserves data abstraction at the cost of inefficiency.
353-- We omit reflection services for the sake of data abstraction.
351-- We omit reflection services for the sake of data abstraction.
@@ -401,54 +399,6 @@ pack :: String -> Text
401pack = unstream . S.map safe . S.streamList
399pack = unstream . S.map safe . S.streamList
402{-# INLINE [1] pack #-}
400{-# INLINE [1] pack #-}
404−-- | /O(n)/ Convert a 'Text' into a 'String'. Subject to fusion.
405−unpack :: Text -> String
406−unpack = S.unstreamList . stream
407−{-# INLINE [1] unpack #-}
409−-- | /O(n)/ Convert a literal string into a Text. Subject to fusion.
410−unpackCString# :: Addr# -> Text
411−unpackCString# addr# = unstream (S.streamCString# addr#)
412−{-# NOINLINE unpackCString# #-}
414−{-# RULES "TEXT literal" forall a.
415− unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
416− = unpackCString# a #-}
418−{-# RULES "TEXT literal UTF8" forall a.
419− unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
420− = unpackCString# a #-}
422−{-# RULES "TEXT empty literal"
423− unstream (S.map safe (S.streamList []))
426−{-# RULES "TEXT singleton literal" forall a.
427− unstream (S.map safe (S.streamList [a]))
430−-- | /O(1)/ Convert a character into a Text. Subject to fusion.
431−-- Performs replacement on invalid scalar values.
432−singleton :: Char -> Text
433−singleton = unstream . S.singleton . safe
434−{-# INLINE [1] singleton #-}
436−{-# RULES "TEXT singleton" forall a.
437− unstream (S.singleton (safe a))
440−-- This is intended to reduce inlining bloat.
441−singleton_ :: Char -> Text
442−singleton_ c = Text (A.run x) 0 len
443− where x :: ST s (A.MArray s)
444− x = do arr <- A.new len
445− _ <- unsafeWrite arr 0 d
447− len | d < '\x10000' = 1
450−{-# NOINLINE singleton_ #-}
452-- -----------------------------------------------------------------------------
402-- -----------------------------------------------------------------------------
453-- * Basic functions
403-- * Basic functions