Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 269 lines (244 sloc) 10.013 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
da1bc90 @bos Update copyright
authored
5 -- Copyright : (c) 2008, 2009 Tom Harper,
6 -- (c) 2009, 2010 Bryan O'Sullivan,
7 -- (c) 2009 Duncan Coutts
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)
2189300 @bos Write a faster UTF-8 decoder
authored
60 import Data.Text.Internal (Text(..), textP)
61 import Data.Text.UnsafeChar (ord, unsafeWrite)
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
62 import Data.Text.UnsafeShift (shiftL, shiftR)
63 import Data.Word (Word8)
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
64 import Foreign.C.Types (CSize)
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
65 import Foreign.ForeignPtr (withForeignPtr)
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
66 import Foreign.Marshal.Utils (with)
67 import Foreign.Ptr (Ptr, plusPtr)
68 import Foreign.Storable (peek, poke)
69 import GHC.Base (MutableByteArray#)
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
70 import System.IO.Unsafe (unsafePerformIO)
71 import qualified Data.Text.Array as A
a70e3a0 @bos Split encoding support out into new modules
authored
72 import qualified Data.Text.Encoding.Fusion as E
2189300 @bos Write a faster UTF-8 decoder
authored
73 import qualified Data.Text.Encoding.Utf16 as U16
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
74 import qualified Data.Text.Fusion as F
a70e3a0 @bos Split encoding support out into new modules
authored
75
f97e59a @bos Many small documentation improvements.
authored
76 -- $strict
77 --
78 -- All of the single-parameter functions for decoding bytestrings
79 -- encoded in one of the Unicode Transformation Formats (UTF) operate
80 -- in a /strict/ mode: each will throw an exception if given invalid
81 -- input.
82 --
83 -- Each function has a variant, whose name is suffixed with -'With',
84 -- that gives greater control over the handling of decoding errors.
85 -- For instance, 'decodeUtf8' will throw an exception, but
86 -- 'decodeUtf8With' allows the programmer to determine what to do on a
87 -- decoding error.
88
9e9d83e @bos Mark the ASCII decoding functions as deprecated.
authored
89 -- | /Deprecated/. Decode a 'ByteString' containing 7-bit ASCII
90 -- encoded text.
91 --
92 -- This function is deprecated. Use 'decodeUtf8' instead.
a70e3a0 @bos Split encoding support out into new modules
authored
93 decodeASCII :: ByteString -> Text
9e9d83e @bos Mark the ASCII decoding functions as deprecated.
authored
94 decodeASCII = decodeUtf8
95 {-# DEPRECATED decodeASCII "Use decodeUtf8 instead" #-}
a70e3a0 @bos Split encoding support out into new modules
authored
96
f97e59a @bos Many small documentation improvements.
authored
97 -- | Decode a 'ByteString' containing UTF-8 encoded text.
8766bac @bos Add controllable error handling and recovery code.
authored
98 decodeUtf8With :: OnDecodeError -> ByteString -> Text
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
99 decodeUtf8With onErr (PS fp off len) = textP (fst a) 0 (snd a)
2189300 @bos Write a faster UTF-8 decoder
authored
100 where
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
101 a = A.run2 (A.new len >>= unsafeIOToST . go)
0c1bc82 @bos Speed up UTF-8 decoding by a little over 2x
authored
102 desc = "Data.Text.Encoding.decodeUtf8: Invalid UTF-8 stream"
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
103 go dest = withForeignPtr fp $ \ptr ->
104 with (0::CSize) $ \destOffPtr -> do
105 let end = ptr `plusPtr` (off + len)
106 loop curPtr = do
107 curPtr' <- c_decode_utf8 (A.maBA dest) destOffPtr curPtr end
108 if curPtr' == end
109 then do
110 n <- peek destOffPtr
111 return (dest,fromIntegral n)
112 else do
113 x <- peek curPtr'
114 case onErr desc (Just x) of
115 Nothing -> loop $ curPtr' `plusPtr` 1
116 Just c -> do
117 destOff <- peek destOffPtr
118 w <- unsafeSTToIO $
119 unsafeWrite dest (fromIntegral destOff) c
120 poke destOffPtr (destOff + fromIntegral w)
121 loop $ curPtr' `plusPtr` 1
122 loop (ptr `plusPtr` off)
123 {- INLINE[0] decodeUtf8With #-}
2189300 @bos Write a faster UTF-8 decoder
authored
124
3b0634b @bos Improve error message.
authored
125 -- | Decode a 'ByteString' containing UTF-8 encoded text that is known
126 -- to be valid.
f97e59a @bos Many small documentation improvements.
authored
127 --
128 -- If the input contains any invalid UTF-8 data, an exception will be
f8d5f17 @bos Add decodeUtf8'.
authored
129 -- thrown that cannot be caught in pure code. For more control over
130 -- the handling of invalid data, use 'decodeUtf8'' or
f97e59a @bos Many small documentation improvements.
authored
131 -- 'decodeUtf8With'.
4667a18 @bos Get rid of the old decode function
authored
132 decodeUtf8 :: ByteString -> Text
133 decodeUtf8 = decodeUtf8With strictDecode
134 {-# INLINE[0] decodeUtf8 #-}
135 {-# RULES "STREAM stream/decodeUtf8 fusion" [1]
136 forall bs. F.stream (decodeUtf8 bs) = E.streamUtf8 strictDecode bs #-}
2189300 @bos Write a faster UTF-8 decoder
authored
137
f8d5f17 @bos Add decodeUtf8'.
authored
138 -- | Decode a 'ByteString' containing UTF-8 encoded text..
139 --
140 -- If the input contains any invalid UTF-8 data, the relevant
141 -- exception will be returned, otherwise the decoded text.
142 decodeUtf8' :: ByteString -> Either UnicodeException Text
143 decodeUtf8' = unsafePerformIO . try . evaluate . decodeUtf8With strictDecode
144 {-# INLINE decodeUtf8' #-}
145
a70e3a0 @bos Split encoding support out into new modules
authored
146 -- | Encode text using UTF-8 encoding.
147 encodeUtf8 :: Text -> ByteString
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
148 encodeUtf8 (Text arr off len) = unsafePerformIO $ do
9dec3d6 @bos Oh noes! I was miscalculating the initial buffer size!
authored
149 let size0 = max len 4
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
150 mallocByteString size0 >>= start size0 off 0
151 where
152 start size n0 m0 fp = withForeignPtr fp $ loop n0 m0
153 where
154 loop n1 m1 ptr = go n1 m1
155 where
156 go !n !m
4050227 @bos Eliminate unnecessary resizes from encodeUtf8.
authored
157 | n-off == len = return (PS fp 0 m)
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
158 | otherwise = do
159 let poke8 k v = poke (ptr `plusPtr` k) (fromIntegral v :: Word8)
4050227 @bos Eliminate unnecessary resizes from encodeUtf8.
authored
160 ensure k act
161 | size-m >= k = act
162 | otherwise = {-# SCC "resizeUtf8/ensure" #-} do
163 let newSize = size `shiftL` 1
164 fp' <- mallocByteString newSize
165 withForeignPtr fp' $ \ptr' ->
166 memcpy ptr' ptr (fromIntegral m)
167 start newSize n m fp'
168 {-# INLINE ensure #-}
169 case A.unsafeIndex arr n of
170 w| w <= 0x7F -> ensure 1 $ do
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
171 poke8 m w
172 go (n+1) (m+1)
4050227 @bos Eliminate unnecessary resizes from encodeUtf8.
authored
173 | w <= 0x7FF -> ensure 2 $ do
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
174 poke8 m $ (w `shiftR` 6) + 0xC0
175 poke8 (m+1) $ (w .&. 0x3f) + 0x80
176 go (n+1) (m+2)
4050227 @bos Eliminate unnecessary resizes from encodeUtf8.
authored
177 | 0xD800 <= w && w <= 0xDBFF -> ensure 4 $ do
2189300 @bos Write a faster UTF-8 decoder
authored
178 let c = ord $ U16.chr2 w (A.unsafeIndex arr (n+1))
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
179 poke8 m $ (c `shiftR` 18) + 0xF0
180 poke8 (m+1) $ ((c `shiftR` 12) .&. 0x3F) + 0x80
181 poke8 (m+2) $ ((c `shiftR` 6) .&. 0x3F) + 0x80
182 poke8 (m+3) $ (c .&. 0x3F) + 0x80
183 go (n+2) (m+4)
4050227 @bos Eliminate unnecessary resizes from encodeUtf8.
authored
184 | otherwise -> ensure 3 $ do
8dbe7ad @bos Rewrite encodeUtf8 for speed
authored
185 poke8 m $ (w `shiftR` 12) + 0xE0
186 poke8 (m+1) $ ((w `shiftR` 6) .&. 0x3F) + 0x80
187 poke8 (m+2) $ (w .&. 0x3F) + 0x80
188 go (n+1) (m+3)
a70e3a0 @bos Split encoding support out into new modules
authored
189
190 -- | Decode text from little endian UTF-16 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
191 decodeUtf16LEWith :: OnDecodeError -> ByteString -> Text
192 decodeUtf16LEWith onErr bs = F.unstream (E.streamUtf16LE onErr bs)
193 {-# INLINE decodeUtf16LEWith #-}
194
195 -- | Decode text from little endian UTF-16 encoding.
f97e59a @bos Many small documentation improvements.
authored
196 --
197 -- If the input contains any invalid little endian UTF-16 data, an
198 -- exception will be thrown. For more control over the handling of
199 -- invalid data, use 'decodeUtf16LEWith'.
a70e3a0 @bos Split encoding support out into new modules
authored
200 decodeUtf16LE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
201 decodeUtf16LE = decodeUtf16LEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
202 {-# INLINE decodeUtf16LE #-}
203
204 -- | Decode text from big endian UTF-16 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
205 decodeUtf16BEWith :: OnDecodeError -> ByteString -> Text
206 decodeUtf16BEWith onErr bs = F.unstream (E.streamUtf16BE onErr bs)
207 {-# INLINE decodeUtf16BEWith #-}
208
209 -- | Decode text from big endian UTF-16 encoding.
f97e59a @bos Many small documentation improvements.
authored
210 --
211 -- If the input contains any invalid big endian UTF-16 data, an
212 -- exception will be thrown. For more control over the handling of
213 -- invalid data, use 'decodeUtf16BEWith'.
a70e3a0 @bos Split encoding support out into new modules
authored
214 decodeUtf16BE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
215 decodeUtf16BE = decodeUtf16BEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
216 {-# INLINE decodeUtf16BE #-}
217
218 -- | Encode text using little endian UTF-16 encoding.
219 encodeUtf16LE :: Text -> ByteString
220 encodeUtf16LE txt = E.unstream (E.restreamUtf16LE (F.stream txt))
221 {-# INLINE encodeUtf16LE #-}
222
223 -- | Encode text using big endian UTF-16 encoding.
224 encodeUtf16BE :: Text -> ByteString
225 encodeUtf16BE txt = E.unstream (E.restreamUtf16BE (F.stream txt))
226 {-# INLINE encodeUtf16BE #-}
227
228 -- | Decode text from little endian UTF-32 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
229 decodeUtf32LEWith :: OnDecodeError -> ByteString -> Text
230 decodeUtf32LEWith onErr bs = F.unstream (E.streamUtf32LE onErr bs)
231 {-# INLINE decodeUtf32LEWith #-}
232
233 -- | Decode text from little endian UTF-32 encoding.
f97e59a @bos Many small documentation improvements.
authored
234 --
235 -- If the input contains any invalid little endian UTF-32 data, an
236 -- exception will be thrown. For more control over the handling of
237 -- invalid data, use 'decodeUtf32LEWith'.
a70e3a0 @bos Split encoding support out into new modules
authored
238 decodeUtf32LE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
239 decodeUtf32LE = decodeUtf32LEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
240 {-# INLINE decodeUtf32LE #-}
241
242 -- | Decode text from big endian UTF-32 encoding.
8766bac @bos Add controllable error handling and recovery code.
authored
243 decodeUtf32BEWith :: OnDecodeError -> ByteString -> Text
244 decodeUtf32BEWith onErr bs = F.unstream (E.streamUtf32BE onErr bs)
245 {-# INLINE decodeUtf32BEWith #-}
246
247 -- | Decode text from big endian UTF-32 encoding.
f97e59a @bos Many small documentation improvements.
authored
248 --
249 -- If the input contains any invalid big endian UTF-32 data, an
250 -- exception will be thrown. For more control over the handling of
251 -- invalid data, use 'decodeUtf32BEWith'.
a70e3a0 @bos Split encoding support out into new modules
authored
252 decodeUtf32BE :: ByteString -> Text
8766bac @bos Add controllable error handling and recovery code.
authored
253 decodeUtf32BE = decodeUtf32BEWith strictDecode
a70e3a0 @bos Split encoding support out into new modules
authored
254 {-# INLINE decodeUtf32BE #-}
255
256 -- | Encode text using little endian UTF-32 encoding.
257 encodeUtf32LE :: Text -> ByteString
258 encodeUtf32LE txt = E.unstream (E.restreamUtf32LE (F.stream txt))
259 {-# INLINE encodeUtf32LE #-}
260
261 -- | Encode text using big endian UTF-32 encoding.
262 encodeUtf32BE :: Text -> ByteString
263 encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
264 {-# INLINE encodeUtf32BE #-}
efa8732 @bos Portable native UTF-8 decoder gives 3.7x faster decoding
authored
265
266 foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
267 :: MutableByteArray# s -> Ptr CSize
268 -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
Something went wrong with that request. Please try again.