on master help sign in
Propagate decoding failure in the Get monad
commit5bef24bb1c8c3179a8a354d69f40196981b61336 authorDan Burton <danburton.email@gmail.com> dateThu 9 Jul 2015, 23:12 (11 years ago) parentb114f2a
2 files changed+12−4
Data/Text.hs+6−2 Data/Text/Lazy.hs+6−2
Data/Text.hs+6−2
@@ -217,7 +217,7 @@ import Data.Monoid (Monoid(..))
217217import Data.String (IsString(..))
218218import qualified Data.Text.Internal.Fusion as S
219219import qualified Data.Text.Internal.Fusion.Common as S
220−import Data.Text.Encoding (decodeUtf8, encodeUtf8)
220+import Data.Text.Encoding (decodeUtf8', encodeUtf8)
221221import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
222222import Data.Text.Internal.Private (span_)
223223import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text)
@@ -346,7 +346,11 @@ instance NFData Text where rnf !_ = ()
346346
347347instance Binary Text where
348348 put t = put (encodeUtf8 t)
349− get = P.fmap decodeUtf8 get
349+ get = do
350+ bs <- get
351+ case decodeUtf8' bs of
352+ P.Left exn -> P.fail (P.show exn)
353+ P.Right a -> P.return a
350354
351355-- | This instance preserves data abstraction at the cost of inefficiency.
352356-- 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)
225225import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks,
226226 foldrChunks, smallChunkSize)
227227import Data.Text.Internal (firstf, safe, text)
228−import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
228+import Data.Text.Lazy.Encoding (decodeUtf8', encodeUtf8)
229229import qualified Data.Text.Internal.Functions as F
230230import Data.Text.Internal.Lazy.Search (indices)
231231#if __GLASGOW_HASKELL__ >= 702
@@ -357,7 +357,11 @@ instance NFData Text where
357357
358358instance Binary Text where
359359 put t = put (encodeUtf8 t)
360− get = P.fmap decodeUtf8 get
360+ get = do
361+ bs <- get
362+ case decodeUtf8' bs of
363+ P.Left exn -> P.fail (P.show exn)
364+ P.Right a -> P.return a
361365
362366-- | This instance preserves data abstraction at the cost of inefficiency.
363367-- We omit reflection services for the sake of data abstraction.