Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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