@@ -14,18 +14,20 @@
14-- Functions for converting 'Text' values to and from 'ByteString',
14-- Functions for converting 'Text' values to and from 'ByteString',
15-- using several standard encodings.
15-- using several standard encodings.
17−-- To make use of a much larger variety of encodings, use the @text-icu@
17+-- To gain access to a much larger family of encodings, use the
18−-- package.
18+-- @text-icu@ package: <http://hackage.haskell.org/package/text-icu>
20module Data.Text.Encoding
20module Data.Text.Encoding
22 -- * Decoding ByteStrings to Text
22 -- * Decoding ByteStrings to Text
23 decodeASCII
24 decodeASCII
24 , decodeUtf8
25 , decodeUtf8
25 , decodeUtf16LE
26 , decodeUtf16LE
26 , decodeUtf16BE
27 , decodeUtf16BE
27 , decodeUtf32LE
28 , decodeUtf32LE
28 , decodeUtf32BE
29 , decodeUtf32BE
29 -- ** Controllable error handling
31 -- ** Controllable error handling
30 , decodeUtf8With
32 , decodeUtf8With
31 , decodeUtf16LEWith
33 , decodeUtf16LEWith
@@ -60,11 +62,25 @@ import qualified Data.Text.Encoding.Utf16 as U16
60import qualified Data.Text.Encoding.Utf8 as U8
62import qualified Data.Text.Encoding.Utf8 as U8
61import qualified Data.Text.Fusion as F
63import qualified Data.Text.Fusion as F
67+-- All of the single-parameter functions for decoding bytestrings
68+-- encoded in one of the Unicode Transformation Formats (UTF) operate
69+-- in a /strict/ mode: each will throw an exception if given invalid
72+-- Each function has a variant, whose name is suffixed with -'With',
73+-- that gives greater control over the handling of decoding errors.
74+-- For instance, 'decodeUtf8' will throw an exception, but
75+-- 'decodeUtf8With' allows the programmer to determine what to do on a
63-- | Decode a 'ByteString' containing 7-bit ASCII encoded text.
78-- | Decode a 'ByteString' containing 7-bit ASCII encoded text.
64decodeASCII :: ByteString -> Text
79decodeASCII :: ByteString -> Text
65decodeASCII bs = F.unstream (E.streamASCII bs)
80decodeASCII bs = F.unstream (E.streamASCII bs)
66{-# INLINE decodeASCII #-}
81{-# INLINE decodeASCII #-}
83+-- | Decode a 'ByteString' containing UTF-8 encoded text.
68decodeUtf8With :: OnDecodeError -> ByteString -> Text
84decodeUtf8With :: OnDecodeError -> ByteString -> Text
69decodeUtf8With onErr bs = textP (fst a) 0 (snd a)
85decodeUtf8With onErr bs = textP (fst a) 0 (snd a)
@@ -101,6 +117,10 @@ decodeUtf8With onErr bs = textP (fst a) 0 (snd a)
101{-# INLINE[0] decodeUtf8With #-}
117{-# INLINE[0] decodeUtf8With #-}
103-- | Decode a 'ByteString' containing UTF-8 encoded text.
119-- | Decode a 'ByteString' containing UTF-8 encoded text.
121+-- If the input contains any invalid UTF-8 data, an exception will be
122+-- thrown. For more control over the handling of invalid data, use
104decodeUtf8 :: ByteString -> Text
124decodeUtf8 :: ByteString -> Text
105decodeUtf8 = decodeUtf8With strictDecode
125decodeUtf8 = decodeUtf8With strictDecode
106{-# INLINE[0] decodeUtf8 #-}
126{-# INLINE[0] decodeUtf8 #-}
@@ -156,6 +176,10 @@ decodeUtf16LEWith onErr bs = F.unstream (E.streamUtf16LE onErr bs)
156{-# INLINE decodeUtf16LEWith #-}
176{-# INLINE decodeUtf16LEWith #-}
158-- | Decode text from little endian UTF-16 encoding.
178-- | Decode text from little endian UTF-16 encoding.
180+-- If the input contains any invalid little endian UTF-16 data, an
181+-- exception will be thrown. For more control over the handling of
182+-- invalid data, use 'decodeUtf16LEWith'.
159decodeUtf16LE :: ByteString -> Text
183decodeUtf16LE :: ByteString -> Text
160decodeUtf16LE = decodeUtf16LEWith strictDecode
184decodeUtf16LE = decodeUtf16LEWith strictDecode
161{-# INLINE decodeUtf16LE #-}
185{-# INLINE decodeUtf16LE #-}
@@ -166,6 +190,10 @@ decodeUtf16BEWith onErr bs = F.unstream (E.streamUtf16BE onErr bs)
166{-# INLINE decodeUtf16BEWith #-}
190{-# INLINE decodeUtf16BEWith #-}
168-- | Decode text from big endian UTF-16 encoding.
192-- | Decode text from big endian UTF-16 encoding.
194+-- If the input contains any invalid big endian UTF-16 data, an
195+-- exception will be thrown. For more control over the handling of
196+-- invalid data, use 'decodeUtf16BEWith'.
169decodeUtf16BE :: ByteString -> Text
197decodeUtf16BE :: ByteString -> Text
170decodeUtf16BE = decodeUtf16BEWith strictDecode
198decodeUtf16BE = decodeUtf16BEWith strictDecode
171{-# INLINE decodeUtf16BE #-}
199{-# INLINE decodeUtf16BE #-}
@@ -186,6 +214,10 @@ decodeUtf32LEWith onErr bs = F.unstream (E.streamUtf32LE onErr bs)
186{-# INLINE decodeUtf32LEWith #-}
214{-# INLINE decodeUtf32LEWith #-}
188-- | Decode text from little endian UTF-32 encoding.
216-- | Decode text from little endian UTF-32 encoding.
218+-- If the input contains any invalid little endian UTF-32 data, an
219+-- exception will be thrown. For more control over the handling of
220+-- invalid data, use 'decodeUtf32LEWith'.
189decodeUtf32LE :: ByteString -> Text
221decodeUtf32LE :: ByteString -> Text
190decodeUtf32LE = decodeUtf32LEWith strictDecode
222decodeUtf32LE = decodeUtf32LEWith strictDecode
191{-# INLINE decodeUtf32LE #-}
223{-# INLINE decodeUtf32LE #-}
@@ -196,6 +228,10 @@ decodeUtf32BEWith onErr bs = F.unstream (E.streamUtf32BE onErr bs)
196{-# INLINE decodeUtf32BEWith #-}
228{-# INLINE decodeUtf32BEWith #-}
198-- | Decode text from big endian UTF-32 encoding.
230-- | Decode text from big endian UTF-32 encoding.
232+-- If the input contains any invalid big endian UTF-32 data, an
233+-- exception will be thrown. For more control over the handling of
234+-- invalid data, use 'decodeUtf32BEWith'.
199decodeUtf32BE :: ByteString -> Text
235decodeUtf32BE :: ByteString -> Text
200decodeUtf32BE = decodeUtf32BEWith strictDecode
236decodeUtf32BE = decodeUtf32BEWith strictDecode
201{-# INLINE decodeUtf32BE #-}
237{-# INLINE decodeUtf32BE #-}