@@ -14,18 +14,20 @@
1414-- Functions for converting 'Text' values to and from 'ByteString',
1515-- using several standard encodings.
1616--
17−-- To make use of a much larger variety of encodings, use the @text-icu@
18−-- package.
17+-- To gain access to a much larger family of encodings, use the
18+-- @text-icu@ package: <http://hackage.haskell.org/package/text-icu>
1919
2020module Data.Text.Encoding
2121 (
2222 -- * Decoding ByteStrings to Text
23+ -- $strict
2324 decodeASCII
2425 , decodeUtf8
2526 , decodeUtf16LE
2627 , decodeUtf16BE
2728 , decodeUtf32LE
2829 , decodeUtf32BE
30+
2931 -- ** Controllable error handling
3032 , decodeUtf8With
3133 , decodeUtf16LEWith
@@ -60,11 +62,25 @@ import qualified Data.Text.Encoding.Utf16 as U16
6062import qualified Data.Text.Encoding.Utf8 as U8
6163import qualified Data.Text.Fusion as F
6264
65+-- $strict
66+--
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
70+-- input.
71+--
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
76+-- decoding error.
77+
6378-- | Decode a 'ByteString' containing 7-bit ASCII encoded text.
6479decodeASCII :: ByteString -> Text
6580decodeASCII bs = F.unstream (E.streamASCII bs)
6681{-# INLINE decodeASCII #-}
6782
83+-- | Decode a 'ByteString' containing UTF-8 encoded text.
6884decodeUtf8With :: OnDecodeError -> ByteString -> Text
6985decodeUtf8With onErr bs = textP (fst a) 0 (snd a)
7086 where
@@ -101,6 +117,10 @@ decodeUtf8With onErr bs = textP (fst a) 0 (snd a)
101117{-# INLINE[0] decodeUtf8With #-}
102118
103119-- | Decode a 'ByteString' containing UTF-8 encoded text.
120+--
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
123+-- 'decodeUtf8With'.
104124decodeUtf8 :: ByteString -> Text
105125decodeUtf8 = decodeUtf8With strictDecode
106126{-# INLINE[0] decodeUtf8 #-}
@@ -156,6 +176,10 @@ decodeUtf16LEWith onErr bs = F.unstream (E.streamUtf16LE onErr bs)
156176{-# INLINE decodeUtf16LEWith #-}
157177
158178-- | Decode text from little endian UTF-16 encoding.
179+--
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'.
159183decodeUtf16LE :: ByteString -> Text
160184decodeUtf16LE = decodeUtf16LEWith strictDecode
161185{-# INLINE decodeUtf16LE #-}
@@ -166,6 +190,10 @@ decodeUtf16BEWith onErr bs = F.unstream (E.streamUtf16BE onErr bs)
166190{-# INLINE decodeUtf16BEWith #-}
167191
168192-- | Decode text from big endian UTF-16 encoding.
193+--
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'.
169197decodeUtf16BE :: ByteString -> Text
170198decodeUtf16BE = decodeUtf16BEWith strictDecode
171199{-# INLINE decodeUtf16BE #-}
@@ -186,6 +214,10 @@ decodeUtf32LEWith onErr bs = F.unstream (E.streamUtf32LE onErr bs)
186214{-# INLINE decodeUtf32LEWith #-}
187215
188216-- | Decode text from little endian UTF-32 encoding.
217+--
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'.
189221decodeUtf32LE :: ByteString -> Text
190222decodeUtf32LE = decodeUtf32LEWith strictDecode
191223{-# INLINE decodeUtf32LE #-}
@@ -196,6 +228,10 @@ decodeUtf32BEWith onErr bs = F.unstream (E.streamUtf32BE onErr bs)
196228{-# INLINE decodeUtf32BEWith #-}
197229
198230-- | Decode text from big endian UTF-32 encoding.
231+--
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'.
199235decodeUtf32BE :: ByteString -> Text
200236decodeUtf32BE = decodeUtf32BEWith strictDecode
201237{-# INLINE decodeUtf32BE #-}