Commit 708d3d32 authored by bos's avatar bos
Browse files

Add a Binary instance for lazy Text

Thus should be completed the fix for gh-115.
parent d70ce271
......@@ -212,6 +212,7 @@ import qualified Data.List as L
import Data.Char (isSpace)
import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf), constrIndex,
Constr, mkConstr, DataType, mkDataType, Fixity(Prefix))
import Data.Binary (Binary(get, put))
import Data.Monoid (Monoid(..))
import Data.String (IsString(..))
import qualified Data.Text as T
......@@ -224,6 +225,7 @@ import Data.Text.Internal.Lazy.Fusion (stream, unstream)
import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks,
foldrChunks, smallChunkSize)
import Data.Text.Internal (firstf, safe, text)
import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
import qualified Data.Text.Internal.Functions as F
import Data.Text.Internal.Lazy.Search (indices)
#if __GLASGOW_HASKELL__ >= 702
......@@ -353,6 +355,10 @@ instance NFData Text where
rnf (Chunk _ ts) = rnf ts
instance Binary Text where
put t = put (encodeUtf8 t)
get = P.fmap decodeUtf8 get
-- | This instance preserves data abstraction at the cost of inefficiency.
-- We omit reflection services for the sake of data abstraction.
......@@ -961,7 +967,7 @@ cycle t = let t' = foldrChunks Chunk t' t
-- | @'iterate' f x@ returns an infinite 'Text' of repeated applications
-- of @f@ to @x@:
-- > iterate f x == [x, f x, f (f x), ...]
iterate :: (Char -> Char) -> Char -> Text
iterate f c = let t c' = Chunk (T.singleton c') (t (f c'))
......@@ -69,7 +69,6 @@ import qualified Data.ByteString.Builder.Prim as BP
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified Data.Text.Lazy as L
import qualified Data.Text.Internal.Lazy.Encoding.Fusion as E
import qualified Data.Text.Internal.Lazy.Fusion as F
import Data.Text.Unsafe (unsafeDupablePerformIO)
......@@ -110,7 +109,7 @@ decodeUtf8With onErr (B.Chunk b0 bs0) =
| S.null l = empty
| otherwise = case onErr desc (Just (B.unsafeHead l)) of
Nothing -> empty
Just c -> L.singleton c
Just c -> Chunk (T.singleton c) Empty
desc = "Data.Text.Lazy.Encoding.decodeUtf8With: Invalid UTF-8 stream"
decodeUtf8With _ _ = empty
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment