on master help sign in
Merge pull request #126 from DanBurton/master

Propagate decoding failure in the Get monad

commit75726f05ab85275931dbeae72d5528ab87d53ef7 authorBryan O'Sullivan <bos@serpentine.com> dateTue 22 Dec 2015, 11:10 (11 years ago) parent8aef8505bef24b
2 files changed+12−4
Data/Text.hs+6−2 Data/Text/Lazy.hs+6−2
Data/Text.hs+6−2
@@ -221,7 +221,7 @@ import Data.Semigroup (Semigroup(..))
221221import Data.String (IsString(..))
222222import qualified Data.Text.Internal.Fusion as S
223223import qualified Data.Text.Internal.Fusion.Common as S
224−import Data.Text.Encoding (decodeUtf8, encodeUtf8)
224+import Data.Text.Encoding (decodeUtf8', encodeUtf8)
225225import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
226226import Data.Text.Internal.Private (span_)
227227import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text)
@@ -365,7 +365,11 @@ instance NFData Text where rnf !_ = ()
365365
366366instance Binary Text where
367367 put t = put (encodeUtf8 t)
368− get = P.fmap decodeUtf8 get
368+ get = do
369+ bs <- get
370+ case decodeUtf8' bs of
371+ P.Left exn -> P.fail (P.show exn)
372+ P.Right a -> P.return a
369373
370374-- | This instance preserves data abstraction at the cost of inefficiency.
371375-- We omit reflection services for the sake of data abstraction.
Data/Text/Lazy.hs+6−2
@@ -229,7 +229,7 @@ import Data.Text.Internal.Lazy.Fusion (stream, unstream)
229229import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks,
230230 foldrChunks, smallChunkSize)
231231import Data.Text.Internal (firstf, safe, text)
232−import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
232+import Data.Text.Lazy.Encoding (decodeUtf8', encodeUtf8)
233233import qualified Data.Text.Internal.Functions as F
234234import Data.Text.Internal.Lazy.Search (indices)
235235#if __GLASGOW_HASKELL__ >= 702
@@ -376,7 +376,11 @@ instance NFData Text where
376376
377377instance Binary Text where
378378 put t = put (encodeUtf8 t)
379− get = P.fmap decodeUtf8 get
379+ get = do
380+ bs <- get
381+ case decodeUtf8' bs of
382+ P.Left exn -> P.fail (P.show exn)
383+ P.Right a -> P.return a
380384
381385-- | This instance preserves data abstraction at the cost of inefficiency.
382386-- We omit reflection services for the sake of data abstraction.