on master help sign in
Switch decodeUtf8 to runText
b131ab8Bryan O'Sullivan
Data/Text/Encoding.hs+24−23
@@ -57,7 +57,8 @@ import Data.Bits ((.&.))
57import Data.ByteString as B
57import Data.ByteString as B
58import Data.ByteString.Internal as B
58import Data.ByteString.Internal as B
59import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
59import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
60−import Data.Text.Internal (Text(..), textP)
60+import Data.Text.Internal (Text(..))
61+import Data.Text.Private (runText)
61import Data.Text.UnsafeChar (ord, unsafeWrite)
62import Data.Text.UnsafeChar (ord, unsafeWrite)
62import Data.Text.UnsafeShift (shiftL, shiftR)
63import Data.Text.UnsafeShift (shiftL, shiftR)
63import Data.Word (Word8)
64import Data.Word (Word8)
@@ -96,30 +97,30 @@ decodeASCII = decodeUtf8
96
97
97-- | Decode a 'ByteString' containing UTF-8 encoded text.
98-- | Decode a 'ByteString' containing UTF-8 encoded text.
98decodeUtf8With :: OnDecodeError -> ByteString -> Text
99decodeUtf8With :: OnDecodeError -> ByteString -> Text
99−decodeUtf8With onErr (PS fp off len) = textP (fst a) 0 (snd a)
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
100 where
122 where
101− a = A.run2 (A.new len >>= unsafeIOToST . go)
102 desc = "Data.Text.Encoding.decodeUtf8: Invalid UTF-8 stream"
123 desc = "Data.Text.Encoding.decodeUtf8: Invalid UTF-8 stream"
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 #-}
124{- INLINE[0] decodeUtf8With #-}
124
125
125-- | Decode a 'ByteString' containing UTF-8 encoded text that is known
126-- | Decode a 'ByteString' containing UTF-8 encoded text that is known