@@ -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
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)
105+ curPtr' <- c_decode_utf8 (A.maBA dest) destOffPtr curPtr end
108+ n <- peek destOffPtr
109+ unsafeSTToIO (done dest (fromIntegral n))
112+ case onErr desc (Just x) of
113+ Nothing -> loop $ curPtr' `plusPtr` 1
115+ destOff <- peek destOffPtr
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
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)
107− curPtr' <- c_decode_utf8 (A.maBA dest) destOffPtr curPtr end
110− n <- peek destOffPtr
111− return (dest,fromIntegral n)
114− case onErr desc (Just x) of
115− Nothing -> loop $ curPtr' `plusPtr` 1
117− destOff <- peek destOffPtr
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 #-}
125-- | Decode a 'ByteString' containing UTF-8 encoded text that is known
126-- | Decode a 'ByteString' containing UTF-8 encoded text that is known