@@ -31,9 +31,12 @@ module Data.Text.Encoding
31
31
32
32
-- ** Catchable failure
33
33
, decodeUtf8'
34
+ , decodeUtf8Either
35
+ , decodeUtf8Maybe
34
36
35
37
-- ** Controllable error handling
36
38
, decodeUtf8With
39
+ , decodeUtf8Lenient
37
40
, decodeUtf16LEWith
38
41
, decodeUtf16BEWith
39
42
, decodeUtf32LEWith
@@ -64,7 +67,7 @@ import Control.Monad.ST (runST)
64
67
import Data.Bits ((.&.) )
65
68
import Data.ByteString as B
66
69
import qualified Data.ByteString.Internal as B
67
- import Data.Text.Encoding.Error (OnDecodeError , UnicodeException , strictDecode )
70
+ import Data.Text.Encoding.Error (OnDecodeError , UnicodeException , strictDecode , lenientDecode )
68
71
import Data.Text.Internal (Text (.. ), safe , text )
69
72
import Data.Text.Internal.Functions
70
73
import Data.Text.Internal.Private (runText )
@@ -353,6 +356,27 @@ decodeUtf8' :: ByteString -> Either UnicodeException Text
353
356
decodeUtf8' = unsafeDupablePerformIO . try . evaluate . decodeUtf8With strictDecode
354
357
{-# INLINE decodeUtf8' #-}
355
358
359
+ -- | Decode a 'ByteString' containing UTF-8 encoded text.
360
+ --
361
+ -- If the input contains any invalid UTF-8 data, the relevant
362
+ -- exception will be returned, otherwise the decoded text.
363
+ decodeUtf8Either :: ByteString -> Either UnicodeException Text
364
+ decodeUtf8Either = decodeUtf8'
365
+
366
+ -- | Decode a 'ByteString' containing UTF-8 encoded text.
367
+ --
368
+ -- If the input contains any invalid UTF-8 data, 'Nothing' will be
369
+ -- returned, otherwise the decoded text.
370
+ decodeUtf8Maybe :: ByteString -> Maybe Text
371
+ decodeUtf8Maybe = either (const Nothing ) Just . decodeUtf8'
372
+
373
+ -- | Decode a 'ByteString' containing UTF-8 encoded text.
374
+ --
375
+ -- Any invalid input bytes will be replaced with the Unicode replacement
376
+ -- character U+FFFD.
377
+ decodeUtf8Lenient :: ByteString -> Text
378
+ decodeUtf8Lenient = decodeUtf8With lenientDecode
379
+
356
380
-- | Encode text to a ByteString 'B.Builder' using UTF-8 encoding.
357
381
--
358
382
-- @since 1.1.0.0
0 commit comments