on master help sign in
Propagate decoding failure in the Get monad
5bef24bDan Burton
Data/Text.hs+6−2
@@ -217,7 +217,7 @@ import Data.Monoid (Monoid(..))
217import Data.String (IsString(..))
217import Data.String (IsString(..))
218import qualified Data.Text.Internal.Fusion as S
218import qualified Data.Text.Internal.Fusion as S
219import qualified Data.Text.Internal.Fusion.Common as S
219import qualified Data.Text.Internal.Fusion.Common as S
220−import Data.Text.Encoding (decodeUtf8, encodeUtf8)
220+import Data.Text.Encoding (decodeUtf8', encodeUtf8)
221import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
221import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
222import Data.Text.Internal.Private (span_)
222import Data.Text.Internal.Private (span_)
223import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text)
223import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text)
@@ -346,7 +346,11 @@ instance NFData Text where rnf !_ = ()
346
346
347instance Binary Text where
347instance Binary Text where
348 put t = put (encodeUtf8 t)
348 put t = put (encodeUtf8 t)
349+ get = do
350+ bs <- get
349− get = P.fmap decodeUtf8 get
351+ case decodeUtf8' bs of
352+ P.Left exn -> P.fail (P.show exn)
353+ P.Right a -> P.return a
350
354
351-- | This instance preserves data abstraction at the cost of inefficiency.
355-- | This instance preserves data abstraction at the cost of inefficiency.
352-- We omit reflection services for the sake of data abstraction.
356-- We omit reflection services for the sake of data abstraction.
Data/Text/Lazy.hs+6−2
@@ -225,7 +225,7 @@ import Data.Text.Internal.Lazy.Fusion (stream, unstream)
225import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks,
225import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks,
226 foldrChunks, smallChunkSize)
226 foldrChunks, smallChunkSize)
227import Data.Text.Internal (firstf, safe, text)
227import Data.Text.Internal (firstf, safe, text)
228−import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
228+import Data.Text.Lazy.Encoding (decodeUtf8', encodeUtf8)
229import qualified Data.Text.Internal.Functions as F
229import qualified Data.Text.Internal.Functions as F
230import Data.Text.Internal.Lazy.Search (indices)
230import Data.Text.Internal.Lazy.Search (indices)
231#if __GLASGOW_HASKELL__ >= 702
231#if __GLASGOW_HASKELL__ >= 702
@@ -357,7 +357,11 @@ instance NFData Text where
357
357
358instance Binary Text where
358instance Binary Text where
359 put t = put (encodeUtf8 t)
359 put t = put (encodeUtf8 t)
360+ get = do
361+ bs <- get
360− get = P.fmap decodeUtf8 get
362+ case decodeUtf8' bs of
363+ P.Left exn -> P.fail (P.show exn)
364+ P.Right a -> P.return a
361
365
362-- | This instance preserves data abstraction at the cost of inefficiency.
366-- | This instance preserves data abstraction at the cost of inefficiency.
363-- We omit reflection services for the sake of data abstraction.
367-- We omit reflection services for the sake of data abstraction.