{-# LANGUAGE CPP, DeriveDataTypeable #-}
#if __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE Safe #-}
#elif __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif
module Data.Text.Encoding.Error
(
UnicodeException(..)
, OnError
, OnDecodeError
, OnEncodeError
, lenientDecode
, strictDecode
, strictEncode
, ignore
, replace
) where
import Control.DeepSeq (NFData (..))
import Control.Exception (Exception, throw)
import Data.Typeable (Typeable)
import Data.Word (Word8)
import Numeric (showHex)
type OnError a b = String -> Maybe a -> Maybe b
type OnDecodeError = OnError Word8 Char
{-# DEPRECATED OnEncodeError "This exception is never used in practice, and will be removed." #-}
type OnEncodeError = OnError Char Word8
data UnicodeException =
DecodeError String (Maybe Word8)
| EncodeError String (Maybe Char)
deriving (UnicodeException -> UnicodeException -> Bool
(UnicodeException -> UnicodeException -> Bool)
-> (UnicodeException -> UnicodeException -> Bool)
-> Eq UnicodeException
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UnicodeException -> UnicodeException -> Bool
$c/= :: UnicodeException -> UnicodeException -> Bool
== :: UnicodeException -> UnicodeException -> Bool
$c== :: UnicodeException -> UnicodeException -> Bool
External instance of the constraint type Eq Word8
External instance of the constraint type Eq Char
External instance of the constraint type Eq Word8
External instance of the constraint type forall a. Eq a => Eq (Maybe a)
External instance of the constraint type Eq Char
External instance of the constraint type forall a. Eq a => Eq [a]
External instance of the constraint type forall a. Eq a => Eq [a]
External instance of the constraint type Eq Char
Eq, Typeable)
{-# DEPRECATED EncodeError "This constructor is never used, and will be removed." #-}
showUnicodeException :: UnicodeException -> String
showUnicodeException :: UnicodeException -> String
showUnicodeException (DecodeError String
desc (Just Word8
w))
= String
"Cannot decode byte '\\x" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Word8 -> String -> String
forall a. (Integral a, Show a) => a -> String -> String
External instance of the constraint type Show Word8
External instance of the constraint type Integral Word8
showHex Word8
w (String
"': " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
desc)
showUnicodeException (DecodeError String
desc Maybe Word8
Nothing)
= String
"Cannot decode input: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
desc
showUnicodeException (EncodeError String
desc (Just Char
c))
= String
"Cannot encode character '\\x" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String -> String
forall a. (Integral a, Show a) => a -> String -> String
External instance of the constraint type Show Int
External instance of the constraint type Integral Int
showHex (Char -> Int
forall a. Enum a => a -> Int
External instance of the constraint type Enum Char
fromEnum Char
c) (String
"': " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
desc)
showUnicodeException (EncodeError String
desc Maybe Char
Nothing)
= String
"Cannot encode input: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
desc
instance Show UnicodeException where
show :: UnicodeException -> String
show = UnicodeException -> String
showUnicodeException
instance Exception UnicodeException
instance NFData UnicodeException where
rnf :: UnicodeException -> ()
rnf (DecodeError String
desc Maybe Word8
w) = String -> ()
forall a. NFData a => a -> ()
External instance of the constraint type forall a. NFData a => NFData [a]
External instance of the constraint type NFData Char
rnf String
desc () -> () -> ()
`seq` Maybe Word8 -> ()
forall a. NFData a => a -> ()
External instance of the constraint type forall a. NFData a => NFData (Maybe a)
External instance of the constraint type NFData Word8
rnf Maybe Word8
w () -> () -> ()
`seq` ()
rnf (EncodeError String
desc Maybe Char
c) = String -> ()
forall a. NFData a => a -> ()
External instance of the constraint type forall a. NFData a => NFData [a]
External instance of the constraint type NFData Char
rnf String
desc () -> () -> ()
`seq` Maybe Char -> ()
forall a. NFData a => a -> ()
External instance of the constraint type forall a. NFData a => NFData (Maybe a)
External instance of the constraint type NFData Char
rnf Maybe Char
c () -> () -> ()
`seq` ()
strictDecode :: OnDecodeError
strictDecode :: OnDecodeError
strictDecode String
desc Maybe Word8
c = UnicodeException -> Maybe Char
forall a e. Exception e => e -> a
Instance of class: Exception of the constraint type Exception UnicodeException
throw (String -> Maybe Word8 -> UnicodeException
DecodeError String
desc Maybe Word8
c)
lenientDecode :: OnDecodeError
lenientDecode :: OnDecodeError
lenientDecode String
_ Maybe Word8
_ = Char -> Maybe Char
forall a. a -> Maybe a
Just Char
'\xfffd'
{-# DEPRECATED strictEncode "This function always throws an exception, and will be removed." #-}
strictEncode :: OnEncodeError
strictEncode :: OnEncodeError
strictEncode String
desc Maybe Char
c = UnicodeException -> Maybe Word8
forall a e. Exception e => e -> a
Instance of class: Exception of the constraint type Exception UnicodeException
throw (String -> Maybe Char -> UnicodeException
EncodeError String
desc Maybe Char
c)
ignore :: OnError a b
ignore :: OnError a b
ignore String
_ Maybe a
_ = Maybe b
forall a. Maybe a
Nothing
replace :: b -> OnError a b
replace :: b -> OnError a b
replace b
c String
_ Maybe a
_ = b -> Maybe b
forall a. a -> Maybe a
Just b
c