@@ -238,12 +238,13 @@ streamDecodeUtf8 = streamDecodeUtf8With strictDecode
238-- | Decode, in a stream oriented way, a 'ByteString' containing UTF-8
238-- | Decode, in a stream oriented way, a 'ByteString' containing UTF-8
239-- encoded text.
239-- encoded text.
240streamDecodeUtf8With :: OnDecodeError -> ByteString -> Decoding
240streamDecodeUtf8With :: OnDecodeError -> ByteString -> Decoding
241−streamDecodeUtf8With onErr = decodeChunk 0 0
241+streamDecodeUtf8With onErr = decodeChunk B.empty 0 0
243 -- We create a slightly larger than necessary buffer to accommodate a
243 -- We create a slightly larger than necessary buffer to accommodate a
244 -- potential surrogate pair started in the last buffer
244 -- potential surrogate pair started in the last buffer
245− decodeChunk :: CodePoint -> DecoderState -> ByteString -> Decoding
245+ decodeChunk :: ByteString -> CodePoint -> DecoderState -> ByteString
246− decodeChunk codepoint0 state0 bs@(PS fp off len) =
247+ decodeChunk undecoded0 codepoint0 state0 bs@(PS fp off len) =
247 runST $ (unsafeIOToST . decodeChunkToBuffer) =<< A.new (len+1)
248 runST $ (unsafeIOToST . decodeChunkToBuffer) =<< A.new (len+1)
249 decodeChunkToBuffer :: A.MArray s -> IO Decoding
250 decodeChunkToBuffer :: A.MArray s -> IO Decoding
@@ -281,8 +282,11 @@ streamDecodeUtf8With onErr = decodeChunk 0 0
281 return $! textP arr 0 (fromIntegral n)
282 return $! textP arr 0 (fromIntegral n)
282 lastPtr <- peek curPtrPtr
283 lastPtr <- peek curPtrPtr
283 let left = lastPtr `minusPtr` curPtr
284 let left = lastPtr `minusPtr` curPtr
285+ undecoded = case state of
286+ UTF8_ACCEPT -> B.empty
287+ _ -> B.append undecoded0 (B.drop left bs)
284− return $ Some chunkText (B.drop left bs)
288+ return $ Some chunkText undecoded
285− (decodeChunk codepoint state)
289+ (decodeChunk undecoded codepoint state)
286 in loop (ptr `plusPtr` off)
290 in loop (ptr `plusPtr` off)
287 desc = "Data.Text.Internal.Encoding.streamDecodeUtf8With: Invalid UTF-8 stream"
291 desc = "Data.Text.Internal.Encoding.streamDecodeUtf8With: Invalid UTF-8 stream"