Skip to content

Add catchable failure decoders for more encodings: #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Data/Text/Encoding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ module Data.Text.Encoding

-- ** Catchable failure
, decodeUtf8'
, decodeUtf16LE'
, decodeUtf16BE'
, decodeUtf32LE'
, decodeUtf32BE'

-- ** Controllable error handling
, decodeUtf8With
Expand Down Expand Up @@ -401,6 +405,14 @@ decodeUtf16LE :: ByteString -> Text
decodeUtf16LE = decodeUtf16LEWith strictDecode
{-# INLINE decodeUtf16LE #-}

-- | Decode a 'ByteString' containing little endian UTF-16 encoded text.
--
-- If the input contains any invalid little endian UTF-16 data, the relevant
-- exception will be returned, otherwise the decoded text.
decodeUtf16LE' :: ByteString -> Either UnicodeException Text
decodeUtf16LE' = unsafeDupablePerformIO . try . evaluate . decodeUtf16LEWith strictDecode
{-# INLINE decodeUtf16LE' #-}

-- | Decode text from big endian UTF-16 encoding.
decodeUtf16BEWith :: OnDecodeError -> ByteString -> Text
decodeUtf16BEWith onErr bs = F.unstream (E.streamUtf16BE onErr bs)
Expand All @@ -415,6 +427,14 @@ decodeUtf16BE :: ByteString -> Text
decodeUtf16BE = decodeUtf16BEWith strictDecode
{-# INLINE decodeUtf16BE #-}

-- | Decode a 'ByteString' containing big endian UTF-16 encoded text.
--
-- If the input contains any invalid big endian UTF-16 data, the relevant
-- exception will be returned, otherwise the decoded text.
decodeUtf16BE' :: ByteString -> Either UnicodeException Text
decodeUtf16BE' = unsafeDupablePerformIO . try . evaluate . decodeUtf16BEWith strictDecode
{-# INLINE decodeUtf16BE' #-}

-- | Encode text using little endian UTF-16 encoding.
encodeUtf16LE :: Text -> ByteString
encodeUtf16LE txt = E.unstream (E.restreamUtf16LE (F.stream txt))
Expand All @@ -439,6 +459,14 @@ decodeUtf32LE :: ByteString -> Text
decodeUtf32LE = decodeUtf32LEWith strictDecode
{-# INLINE decodeUtf32LE #-}

-- | Decode a 'ByteString' containing little endian UTF-32 encoded text.
--
-- If the input contains any invalid little endian UTF-32 data, the relevant
-- exception will be returned, otherwise the decoded text.
decodeUtf32LE' :: ByteString -> Either UnicodeException Text
decodeUtf32LE' = unsafeDupablePerformIO . try . evaluate . decodeUtf32LEWith strictDecode
{-# INLINE decodeUtf32LE' #-}

-- | Decode text from big endian UTF-32 encoding.
decodeUtf32BEWith :: OnDecodeError -> ByteString -> Text
decodeUtf32BEWith onErr bs = F.unstream (E.streamUtf32BE onErr bs)
Expand All @@ -453,6 +481,14 @@ decodeUtf32BE :: ByteString -> Text
decodeUtf32BE = decodeUtf32BEWith strictDecode
{-# INLINE decodeUtf32BE #-}

-- | Decode a 'ByteString' containing big endian UTF-32 encoded text.
--
-- If the input contains any invalid big endian UTF-32 data, the relevant
-- exception will be returned, otherwise the decoded text.
decodeUtf32BE' :: ByteString -> Either UnicodeException Text
decodeUtf32BE' = unsafeDupablePerformIO . try . evaluate . decodeUtf32BEWith strictDecode
{-# INLINE decodeUtf32BE' #-}

-- | Encode text using little endian UTF-32 encoding.
encodeUtf32LE :: Text -> ByteString
encodeUtf32LE txt = E.unstream (E.restreamUtf32LE (F.stream txt))
Expand Down