Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 282 lines (257 sloc) 10.812 kB
829bea1 @bos Silence a compiler warning.
authored
1 {-# LANGUAGE BangPatterns, CPP, ForeignFunctionInterface, MagicHash,
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
2 UnliftedFFITypes #-}
a70e3a0 @bos Split encoding support out into new modules
authored
3 -- |
4 -- Module : Data.Text.Encoding
2ceb577 @bos Many improvements, all small.
authored
5 -- Copyright : (c) 2009, 2010, 2011 Bryan O'Sullivan,
6 -- (c) 2009 Duncan Coutts,
7 -- (c) 2008, 2009 Tom Harper
a70e3a0 @bos Split encoding support out into new modules
authored
8 --
9 -- License : BSD-style
01bb6aa @bos Change Tom's email address
authored
10 -- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,
a70e3a0 @bos Split encoding support out into new modules
authored
11 -- duncan@haskell.org
12 -- Stability : experimental
13 -- Portability : portable
14 --
15 -- Functions for converting 'Text' values to and from 'ByteString',
960d36a @bos Fix Haddocks
authored
16 -- using several standard encodings.
17 --
f97e59a @bos Many small documentation improvements.
authored
18 -- To gain access to a much larger family of encodings, use the
19 -- @text-icu@ package: <http://hackage.haskell.org/package/text-icu>
a70e3a0 @bos Split encoding support out into new modules
authored
20
21 module Data.Text.Encoding
22 (
23 -- * Decoding ByteStrings to Text
f97e59a @bos Many small documentation improvements.
authored
24 -- $strict
a70e3a0 @bos Split encoding support out into new modules
authored
25 decodeASCII
26 , decodeUtf8
27 , decodeUtf16LE
28 , decodeUtf16BE
29 , decodeUtf32LE
30 , decodeUtf32BE
f97e59a @bos Many small documentation improvements.
authored
31
f8d5f17 @bos Add decodeUtf8'.
authored
32 -- ** Catchable failure
33 , decodeUtf8'
34
8766bac @bos Add controllable error handling and recovery code.
authored
35 -- ** Controllable error handling
36 , decodeUtf8With
37 , decodeUtf16LEWith
38 , decodeUtf16BEWith
39 , decodeUtf32LEWith
40 , decodeUtf32BEWith
a70e3a0 @bos Split encoding support out into new modules
authored
41
42 -- * Encoding Text to ByteStrings
43 , encodeUtf8
44 , encodeUtf16LE
45 , encodeUtf16BE
46 , encodeUtf32LE
47 , encodeUtf32BE
48 ) where
be3f9d8 @bos Add a rewrite rule for fusion
authored
49
f8d5f17 @bos Add decodeUtf8'.
authored
50 import Control.Exception (evaluate, try)
829bea1 @bos Silence a compiler warning.
authored
51 #if __GLASGOW_HASKELL__ >= 702
52 import Control.Monad.ST.Unsafe (unsafeIOToST, unsafeSTToIO)
53 #else
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
54 import Control.Monad.ST (unsafeIOToST, unsafeSTToIO)
829bea1 @bos Silence a compiler warning.
authored
55 #endif
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
56 import Data.Bits ((.&.))
57 import Data.ByteString as B
58 import Data.ByteString.Internal as B
f8d5f17 @bos Add decodeUtf8'.
authored
59 import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
b131ab8 @bos Switch decodeUtf8 to runText
authored
60 import Data.Text.Internal (Text(..))
61 import Data.Text.Private (runText)
2189300 @bos Write a faster UTF-8 decoder
authored
62 import Data.Text.UnsafeChar (ord, unsafeWrite)
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
63 import Data.Text.UnsafeShift (shiftL, shiftR)
64 import Data.Word (Word8)
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
65 import Foreign.C.Types (CSize)
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
66 import Foreign.ForeignPtr (withForeignPtr)
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
67 import Foreign.Marshal.Utils (with)
bd23332 @bos Reduce pointer arithmetic for better speed.
authored
68 import Foreign.Ptr (Ptr, minusPtr, plusPtr)
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
69 import Foreign.Storable (peek, poke)
70 import GHC.Base (MutableByteArray#)
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
71 import System.IO.Unsafe (unsafePerformIO)
72 import qualified Data.Text.Array as A
a70e3a0 @bos Split encoding support out into new modules
authored
73 import qualified Data.Text.Encoding.Fusion as E
2189300 @bos Write a faster UTF-8 decoder
authored
74 import qualified Data.Text.Encoding.Utf16 as U16
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
75 import qualified Data.Text.Fusion as F
a70e3a0 @bos Split encoding support out into new modules
authored
76
f97e59a @bos Many small documentation improvements.
authored
77 -- $strict
78 --
79 -- All of the single-parameter functions for decoding bytestrings
80 -- encoded in one of the Unicode Transformation Formats (UTF) operate
81 -- in a /strict/ mode: each will throw an exception if given invalid
82 -- input.
83 --
84 -- Each function has a variant, whose name is suffixed with -'With',
85 -- that gives greater control over the handling of decoding errors.
86 -- For instance, 'decodeUtf8' will throw an exception, but
87 -- 'decodeUtf8With' allows the programmer to determine what to do on a
88 -- decoding error.
89
9e9d83e @bos Mark the ASCII decoding functions as deprecated.
authored
90 -- | /Deprecated/. Decode a 'ByteString' containing 7-bit ASCII
91 -- encoded text.
92 --
93 -- This function is deprecated. Use 'decodeUtf8' instead.
a70e3a0 @bos Split encoding support out into new modules
authored
94 decodeASCII :: ByteString -> Text
9e9d83e @bos Mark the ASCII decoding functions as deprecated.
authored
95 decodeASCII = decodeUtf8
96 {-# DEPRECATED decodeASCII "Use decodeUtf8 instead" #-}
a70e3a0 @bos Split encoding support out into new modules
authored
97
f97e59a @bos Many small documentation improvements.
authored
98 -- | Decode a 'ByteString' containing UTF-8 encoded text.
8766bac @bos Add controllable error handling and recovery code.
authored
99 decodeUtf8With :: OnDecodeError -> ByteString -> Text
b131ab8 @bos Switch decodeUtf8 to runText
authored
100 decodeUtf8With onErr (PS fp off len) = runText $ \done -> do
101 let go dest = withForeignPtr fp $ \ptr ->
102 with (0::CSize) $ \destOffPtr -> do
103 let end = ptr `plusPtr` (off + len)
104 loop curPtr = do
105 curPtr' <- c_decode_utf8 (A.maBA dest) destOffPtr curPtr end
106 if curPtr' == end
107 then do
108 n <- peek destOffPtr
109 unsafeSTToIO (done dest (fromIntegral n))
110 else do
111 x <- peek curPtr'
112 case onErr desc (Just x) of
113 Nothing -> loop $ curPtr' `plusPtr` 1
114 Just c -> do
115 destOff <- peek destOffPtr
116 w <- unsafeSTToIO $
117 unsafeWrite dest (fromIntegral destOff) c
118 poke destOffPtr (destOff + fromIntegral w)
119 loop $ curPtr' `plusPtr` 1
120 loop (ptr `plusPtr` off)
121 (unsafeIOToST . go) =<< A.new len
2189300 @bos Write a faster UTF-8 decoder
authored
122 where
0c1bc82 @bos Speed up UTF-8 decoding by a little over 2x
authored
123 desc = "Data.Text.Encoding.decodeUtf8: Invalid UTF-8 stream"
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
124 {- INLINE[0] decodeUtf8With #-}
2189300 @bos Write a faster UTF-8 decoder
authored
125
3b0634b @bos Improve error message.
authored
126 -- | Decode a 'ByteString' containing UTF-8 encoded text that is known
127 -- to be valid.
f97e59a @bos Many small documentation improvements.
authored
128 --
129 -- If the input contains any invalid UTF-8 data, an exception will be
f8d5f17 @bos Add decodeUtf8'.
authored
130 -- thrown that cannot be caught in pure code. For more control over
131 -- the handling of invalid data, use 'decodeUtf8'' or
f97e59a @bos Many small documentation improvements.
authored
132 -- 'decodeUtf8With'.
4667a18 @bos Get rid of the old decode function
authored
133 decodeUtf8 :: ByteString -> Text
134 decodeUtf8 = decodeUtf8With strictDecode
135 {-# INLINE[0] decodeUtf8 #-}
136 {-# RULES "STREAM stream/decodeUtf8 fusion" [1]
137 forall bs. F.stream (decodeUtf8 bs) = E.streamUtf8 strictDecode bs #-}
2189300 @bos Write a faster UTF-8 decoder
authored
138
f8d5f17 @bos Add decodeUtf8'.
authored
139 -- | Decode a 'ByteString' containing UTF-8 encoded text..
140 --
141 -- If the input contains any invalid UTF-8 data, the relevant
142 -- exception will be returned, otherwise the decoded text.
143 decodeUtf8' :: ByteString -> Either UnicodeException Text
144 decodeUtf8' = unsafePerformIO . try . evaluate . decodeUtf8With strictDecode
145 {-# INLINE decodeUtf8' #-}
146
a70e3a0 @bos Split encoding support out into new modules
authored
147 -- | Encode text using UTF-8 encoding.
148 encodeUtf8 :: Text -> ByteString
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
149 encodeUtf8 (Text arr off len) = unsafePerformIO $ do
9dec3d6 @bos Oh noes! I was miscalculating the initial buffer size!
authored
150 let size0 = max len 4
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
151 mallocByteString size0 >>= start size0 off 0
152 where
153 start size n0 m0 fp = withForeignPtr fp $ loop n0 m0
154 where
155 loop n1 m1 ptr = go n1 m1
156 where
a31c0f7 @bos Improve ASCII encoding performance in a safer way.
authored
157 offLen = off + len
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
158 go !n !m
a31c0f7 @bos Improve ASCII encoding performance in a safer way.
authored
159 | n == offLen = return (PS fp 0 m)
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
160 | otherwise = do
161 let poke8 k v = poke (ptr `plusPtr` k) (fromIntegral v :: Word8)
4050227 @bos Eliminate unnecessary resizes from encodeUtf8.
authored
162 ensure k act
163 | size-m >= k = act
164 | otherwise = {-# SCC "resizeUtf8/ensure" #-} do
165 let newSize = size `shiftL` 1
166 fp' <- mallocByteString newSize
167 withForeignPtr fp' $ \ptr' ->
168 memcpy ptr' ptr (fromIntegral m)
169 start newSize n m fp'
170 {-# INLINE ensure #-}
171 case A.unsafeIndex arr n of
6571833 @bos Oops! Back out part of 59aad6977070 - it was wrong
authored
172 w| w <= 0x7F -> ensure 1 $ do
a31c0f7 @bos Improve ASCII encoding performance in a safer way.
authored
173 poke (ptr `plusPtr` m) (fromIntegral w :: Word8)
174 -- A single ASCII octet is likely to start a run of
175 -- them. We see better performance when we
176 -- special-case this assumption.
bd23332 @bos Reduce pointer arithmetic for better speed.
authored
177 let end = ptr `plusPtr` size
178 ascii !t !u
2ceb577 @bos Many improvements, all small.
authored
179 | t == offLen || u == end || v >= 0x80 =
180 go t (u `minusPtr` ptr)
a31c0f7 @bos Improve ASCII encoding performance in a safer way.
authored
181 | otherwise = do
bd23332 @bos Reduce pointer arithmetic for better speed.
authored
182 poke u (fromIntegral v :: Word8)
183 ascii (t+1) (u `plusPtr` 1)
a31c0f7 @bos Improve ASCII encoding performance in a safer way.
authored
184 where v = A.unsafeIndex arr t
bd23332 @bos Reduce pointer arithmetic for better speed.
authored
185 ascii (n+1) (ptr `plusPtr` (m+1))
4050227 @bos Eliminate unnecessary resizes from encodeUtf8.
authored
186 | w <= 0x7FF -> ensure 2 $ do
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
187 poke8 m $ (w `shiftR` 6) + 0xC0
188 poke8 (m+1) $ (w .&. 0x3f) + 0x80
189 go (n+1) (m+2)
4050227 @bos Eliminate unnecessary resizes from encodeUtf8.
authored
190 | 0xD800 <= w && w <= 0xDBFF -> ensure 4 $ do
2189300 @bos Write a faster UTF-8 decoder
authored
191 let c = ord $ U16.chr2 w (A.unsafeIndex arr (n+1))
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
192 poke8 m $ (c `shiftR` 18) + 0xF0
193 poke8 (m+1) $ ((c `shiftR` 12) .&. 0x3F) + 0x80
194 poke8 (m+2) $ ((c `shiftR` 6) .&. 0x3F) + 0x80
195 poke8 (m+3) $ (c .&. 0x3F) + 0x80
196 go (n+2) (m+4)
4050227 @bos Eliminate unnecessary resizes from encodeUtf8.
authored
197 | otherwise -> ensure 3 $ do
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
198 poke8 m $ (w `shiftR` 12) + 0xE0
199 poke8 (m+1) $ ((w `shiftR` 6) .&. 0x3F) + 0x80
200 poke8 (m+2) $ (w .&. 0x3F) + 0x80
201 go (n+1) (m+3)
a70e3a0 @bos Split encoding support out into new modules
authored
202
203 -- | Decode text from little endian UTF-16 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
204 decodeUtf16LEWith :: OnDecodeError -> ByteString -> Text
205 decodeUtf16LEWith onErr bs = F.unstream (E.streamUtf16LE onErr bs)
206 {-# INLINE decodeUtf16LEWith #-}
207
208 -- | Decode text from little endian UTF-16 encoding.
f97e59a @bos Many small documentation improvements.
authored
209 --
210 -- If the input contains any invalid little endian UTF-16 data, an
211 -- exception will be thrown. For more control over the handling of
212 -- invalid data, use 'decodeUtf16LEWith'.
a70e3a0 @bos Split encoding support out into new modules
authored
213 decodeUtf16LE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
214 decodeUtf16LE = decodeUtf16LEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
215 {-# INLINE decodeUtf16LE #-}
216
217 -- | Decode text from big endian UTF-16 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
218 decodeUtf16BEWith :: OnDecodeError -> ByteString -> Text
219 decodeUtf16BEWith onErr bs = F.unstream (E.streamUtf16BE onErr bs)
220 {-# INLINE decodeUtf16BEWith #-}
221
222 -- | Decode text from big endian UTF-16 encoding.
f97e59a @bos Many small documentation improvements.
authored
223 --
224 -- If the input contains any invalid big endian UTF-16 data, an
225 -- exception will be thrown. For more control over the handling of
226 -- invalid data, use 'decodeUtf16BEWith'.
a70e3a0 @bos Split encoding support out into new modules
authored
227 decodeUtf16BE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
228 decodeUtf16BE = decodeUtf16BEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
229 {-# INLINE decodeUtf16BE #-}
230
231 -- | Encode text using little endian UTF-16 encoding.
232 encodeUtf16LE :: Text -> ByteString
233 encodeUtf16LE txt = E.unstream (E.restreamUtf16LE (F.stream txt))
234 {-# INLINE encodeUtf16LE #-}
235
236 -- | Encode text using big endian UTF-16 encoding.
237 encodeUtf16BE :: Text -> ByteString
238 encodeUtf16BE txt = E.unstream (E.restreamUtf16BE (F.stream txt))
239 {-# INLINE encodeUtf16BE #-}
240
241 -- | Decode text from little endian UTF-32 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
242 decodeUtf32LEWith :: OnDecodeError -> ByteString -> Text
243 decodeUtf32LEWith onErr bs = F.unstream (E.streamUtf32LE onErr bs)
244 {-# INLINE decodeUtf32LEWith #-}
245
246 -- | Decode text from little endian UTF-32 encoding.
f97e59a @bos Many small documentation improvements.
authored
247 --
248 -- If the input contains any invalid little endian UTF-32 data, an
249 -- exception will be thrown. For more control over the handling of
250 -- invalid data, use 'decodeUtf32LEWith'.
a70e3a0 @bos Split encoding support out into new modules
authored
251 decodeUtf32LE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
252 decodeUtf32LE = decodeUtf32LEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
253 {-# INLINE decodeUtf32LE #-}
254
255 -- | Decode text from big endian UTF-32 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
256 decodeUtf32BEWith :: OnDecodeError -> ByteString -> Text
257 decodeUtf32BEWith onErr bs = F.unstream (E.streamUtf32BE onErr bs)
258 {-# INLINE decodeUtf32BEWith #-}
259
260 -- | Decode text from big endian UTF-32 encoding.
f97e59a @bos Many small documentation improvements.
authored
261 --
262 -- If the input contains any invalid big endian UTF-32 data, an
263 -- exception will be thrown. For more control over the handling of
264 -- invalid data, use 'decodeUtf32BEWith'.
a70e3a0 @bos Split encoding support out into new modules
authored
265 decodeUtf32BE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
266 decodeUtf32BE = decodeUtf32BEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
267 {-# INLINE decodeUtf32BE #-}
268
269 -- | Encode text using little endian UTF-32 encoding.
270 encodeUtf32LE :: Text -> ByteString
271 encodeUtf32LE txt = E.unstream (E.restreamUtf32LE (F.stream txt))
272 {-# INLINE encodeUtf32LE #-}
273
274 -- | Encode text using big endian UTF-32 encoding.
275 encodeUtf32BE :: Text -> ByteString
276 encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
277 {-# INLINE encodeUtf32BE #-}
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
278
279 foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
280 :: MutableByteArray# s -> Ptr CSize
281 -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
Something went wrong with that request. Please try again.