Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 128 lines (108 sloc) 4.191 kb
a70e3a0 @bos Split encoding support out into new modules
authored
1 -- |
2 -- Module : Data.Text.Encoding
3 -- Copyright : (c) Tom Harper 2008-2009,
4 -- (c) Bryan O'Sullivan 2009,
5 -- (c) Duncan Coutts 2009
6 --
7 -- License : BSD-style
01bb6aa @bos Change Tom's email address
authored
8 -- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,
a70e3a0 @bos Split encoding support out into new modules
authored
9 -- duncan@haskell.org
10 -- Stability : experimental
11 -- Portability : portable
12 --
13 -- Functions for converting 'Text' values to and from 'ByteString',
960d36a @bos Fix Haddocks
authored
14 -- using several standard encodings.
15 --
16 -- To make use of a much larger variety of encodings, use the @text-icu@
17 -- package.
a70e3a0 @bos Split encoding support out into new modules
authored
18
19 module Data.Text.Encoding
20 (
21 -- * Decoding ByteStrings to Text
22 decodeASCII
23 , decodeUtf8
24 , decodeUtf16LE
25 , decodeUtf16BE
26 , decodeUtf32LE
27 , decodeUtf32BE
8766bac @bos Add controllable error handling and recovery code.
authored
28 -- ** Controllable error handling
29 , decodeUtf8With
30 , decodeUtf16LEWith
31 , decodeUtf16BEWith
32 , decodeUtf32LEWith
33 , decodeUtf32BEWith
a70e3a0 @bos Split encoding support out into new modules
authored
34
35 -- * Encoding Text to ByteStrings
36 , encodeUtf8
37 , encodeUtf16LE
38 , encodeUtf16BE
39 , encodeUtf32LE
40 , encodeUtf32BE
41 ) where
42
43 import Data.ByteString (ByteString)
44 import qualified Data.Text.Fusion as F
8766bac @bos Add controllable error handling and recovery code.
authored
45 import Data.Text.Encoding.Error (OnDecodeError, strictDecode)
a70e3a0 @bos Split encoding support out into new modules
authored
46 import qualified Data.Text.Encoding.Fusion as E
47 import Data.Text.Internal (Text)
48
49 -- | Decode a 'ByteString' containing 7-bit ASCII encoded text.
50 decodeASCII :: ByteString -> Text
51 decodeASCII bs = F.unstream (E.streamASCII bs)
52 {-# INLINE decodeASCII #-}
53
54 -- | Decode a 'ByteString' containing UTF-8 encoded text.
8766bac @bos Add controllable error handling and recovery code.
authored
55 decodeUtf8With :: OnDecodeError -> ByteString -> Text
56 decodeUtf8With onErr bs = F.unstream (E.streamUtf8 onErr bs)
57 {-# INLINE decodeUtf8With #-}
58
59 -- | Decode a 'ByteString' containing UTF-8 encoded text.
a70e3a0 @bos Split encoding support out into new modules
authored
60 decodeUtf8 :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
61 decodeUtf8 = decodeUtf8With strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
62 {-# INLINE decodeUtf8 #-}
63
64 -- | Encode text using UTF-8 encoding.
65 encodeUtf8 :: Text -> ByteString
66 encodeUtf8 txt = E.unstream (E.restreamUtf8 (F.stream txt))
67 {-# INLINE encodeUtf8 #-}
68
69 -- | Decode text from little endian UTF-16 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
70 decodeUtf16LEWith :: OnDecodeError -> ByteString -> Text
71 decodeUtf16LEWith onErr bs = F.unstream (E.streamUtf16LE onErr bs)
72 {-# INLINE decodeUtf16LEWith #-}
73
74 -- | Decode text from little endian UTF-16 encoding.
a70e3a0 @bos Split encoding support out into new modules
authored
75 decodeUtf16LE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
76 decodeUtf16LE = decodeUtf16LEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
77 {-# INLINE decodeUtf16LE #-}
78
79 -- | Decode text from big endian UTF-16 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
80 decodeUtf16BEWith :: OnDecodeError -> ByteString -> Text
81 decodeUtf16BEWith onErr bs = F.unstream (E.streamUtf16BE onErr bs)
82 {-# INLINE decodeUtf16BEWith #-}
83
84 -- | Decode text from big endian UTF-16 encoding.
a70e3a0 @bos Split encoding support out into new modules
authored
85 decodeUtf16BE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
86 decodeUtf16BE = decodeUtf16BEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
87 {-# INLINE decodeUtf16BE #-}
88
89 -- | Encode text using little endian UTF-16 encoding.
90 encodeUtf16LE :: Text -> ByteString
91 encodeUtf16LE txt = E.unstream (E.restreamUtf16LE (F.stream txt))
92 {-# INLINE encodeUtf16LE #-}
93
94 -- | Encode text using big endian UTF-16 encoding.
95 encodeUtf16BE :: Text -> ByteString
96 encodeUtf16BE txt = E.unstream (E.restreamUtf16BE (F.stream txt))
97 {-# INLINE encodeUtf16BE #-}
98
99 -- | Decode text from little endian UTF-32 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
100 decodeUtf32LEWith :: OnDecodeError -> ByteString -> Text
101 decodeUtf32LEWith onErr bs = F.unstream (E.streamUtf32LE onErr bs)
102 {-# INLINE decodeUtf32LEWith #-}
103
104 -- | Decode text from little endian UTF-32 encoding.
a70e3a0 @bos Split encoding support out into new modules
authored
105 decodeUtf32LE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
106 decodeUtf32LE = decodeUtf32LEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
107 {-# INLINE decodeUtf32LE #-}
108
109 -- | Decode text from big endian UTF-32 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
110 decodeUtf32BEWith :: OnDecodeError -> ByteString -> Text
111 decodeUtf32BEWith onErr bs = F.unstream (E.streamUtf32BE onErr bs)
112 {-# INLINE decodeUtf32BEWith #-}
113
114 -- | Decode text from big endian UTF-32 encoding.
a70e3a0 @bos Split encoding support out into new modules
authored
115 decodeUtf32BE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
116 decodeUtf32BE = decodeUtf32BEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
117 {-# INLINE decodeUtf32BE #-}
118
119 -- | Encode text using little endian UTF-32 encoding.
120 encodeUtf32LE :: Text -> ByteString
121 encodeUtf32LE txt = E.unstream (E.restreamUtf32LE (F.stream txt))
122 {-# INLINE encodeUtf32LE #-}
123
124 -- | Encode text using big endian UTF-32 encoding.
125 encodeUtf32BE :: Text -> ByteString
126 encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
127 {-# INLINE encodeUtf32BE #-}
Something went wrong with that request. Please try again.