on master help sign in
Add a Binary instance for lazy Text
708d3d3Bryan O'Sullivan
Data/Text/Lazy.hs+7−1
@@ -212,6 +212,7 @@ import qualified Data.List as L
212import Data.Char (isSpace)
212import Data.Char (isSpace)
213import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf), constrIndex,
213import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf), constrIndex,
214 Constr, mkConstr, DataType, mkDataType, Fixity(Prefix))
214 Constr, mkConstr, DataType, mkDataType, Fixity(Prefix))
215+import Data.Binary (Binary(get, put))
215import Data.Monoid (Monoid(..))
216import Data.Monoid (Monoid(..))
216import Data.String (IsString(..))
217import Data.String (IsString(..))
217import qualified Data.Text as T
218import qualified Data.Text as T
@@ -224,6 +225,7 @@ import Data.Text.Internal.Lazy.Fusion (stream, unstream)
224import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks,
225import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks,
225 foldrChunks, smallChunkSize)
226 foldrChunks, smallChunkSize)
226import Data.Text.Internal (firstf, safe, text)
227import Data.Text.Internal (firstf, safe, text)
228+import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
227import qualified Data.Text.Internal.Functions as F
229import qualified Data.Text.Internal.Functions as F
228import Data.Text.Internal.Lazy.Search (indices)
230import Data.Text.Internal.Lazy.Search (indices)
229#if __GLASGOW_HASKELL__ >= 702
231#if __GLASGOW_HASKELL__ >= 702
@@ -353,6 +355,10 @@ instance NFData Text where
353 rnf (Chunk _ ts) = rnf ts
355 rnf (Chunk _ ts) = rnf ts
354#endif
356#endif
355
357
358+instance Binary Text where
359+ put t = put (encodeUtf8 t)
360+ get = P.fmap decodeUtf8 get
361+
356-- | This instance preserves data abstraction at the cost of inefficiency.
362-- | This instance preserves data abstraction at the cost of inefficiency.
357-- We omit reflection services for the sake of data abstraction.
363-- We omit reflection services for the sake of data abstraction.
358--
364--
@@ -961,7 +967,7 @@ cycle t = let t' = foldrChunks Chunk t' t
961
967
962-- | @'iterate' f x@ returns an infinite 'Text' of repeated applications
968-- | @'iterate' f x@ returns an infinite 'Text' of repeated applications
963-- of @f@ to @x@:
969-- of @f@ to @x@:
964−--
970+--
965-- > iterate f x == [x, f x, f (f x), ...]
971-- > iterate f x == [x, f x, f (f x), ...]
966iterate :: (Char -> Char) -> Char -> Text
972iterate :: (Char -> Char) -> Char -> Text
967iterate f c = let t c' = Chunk (T.singleton c') (t (f c'))
973iterate f c = let t c' = Chunk (T.singleton c') (t (f c'))
Data/Text/Lazy/Encoding.hs+1−2
@@ -69,7 +69,6 @@ import qualified Data.ByteString.Builder.Prim as BP
69import qualified Data.Text as T
69import qualified Data.Text as T
70#endif
70#endif
71import qualified Data.Text.Encoding as TE
71import qualified Data.Text.Encoding as TE
72−import qualified Data.Text.Lazy as L
73import qualified Data.Text.Internal.Lazy.Encoding.Fusion as E
72import qualified Data.Text.Internal.Lazy.Encoding.Fusion as E
74import qualified Data.Text.Internal.Lazy.Fusion as F
73import qualified Data.Text.Internal.Lazy.Fusion as F
75import Data.Text.Unsafe (unsafeDupablePerformIO)
74import Data.Text.Unsafe (unsafeDupablePerformIO)
@@ -110,7 +109,7 @@ decodeUtf8With onErr (B.Chunk b0 bs0) =
110 | S.null l = empty
109 | S.null l = empty
111 | otherwise = case onErr desc (Just (B.unsafeHead l)) of
110 | otherwise = case onErr desc (Just (B.unsafeHead l)) of
112 Nothing -> empty
111 Nothing -> empty
113− Just c -> L.singleton c
112+ Just c -> Chunk (T.singleton c) Empty
114 desc = "Data.Text.Lazy.Encoding.decodeUtf8With: Invalid UTF-8 stream"
113 desc = "Data.Text.Lazy.Encoding.decodeUtf8With: Invalid UTF-8 stream"
115decodeUtf8With _ _ = empty
114decodeUtf8With _ _ = empty
116
115