on master help sign in
streamDecodeUtf8With: accumulate undecoded chunks correctly

We had previously gotten the accounting and reporting wrong if an incomplete input was fed in over the course of several continuations, such that we'd report only the incomplete input seen by the most recent continuation. This fixes gh-70.

commit02b409c49b06f350d01154d26dd4df862c4ffa5c authorBryan O'Sullivan <bos@serpentine.com> dateWed 19 Feb 2014, 12:13 (13 years ago) parentf5dade2
1 file changed+9−5
Data/Text/Encoding.hs+9−5
Data/Text/Encoding.hs+9−5
@@ -238,12 +238,13 @@ streamDecodeUtf8 = streamDecodeUtf8With strictDecode
238238-- | Decode, in a stream oriented way, a 'ByteString' containing UTF-8
239239-- encoded text.
240240streamDecodeUtf8With :: OnDecodeError -> ByteString -> Decoding
241−streamDecodeUtf8With onErr = decodeChunk 0 0
241+streamDecodeUtf8With onErr = decodeChunk B.empty 0 0
242242 where
243243 -- We create a slightly larger than necessary buffer to accommodate a
244244 -- potential surrogate pair started in the last buffer
245− decodeChunk :: CodePoint -> DecoderState -> ByteString -> Decoding
246− decodeChunk codepoint0 state0 bs@(PS fp off len) =
245+ decodeChunk :: ByteString -> CodePoint -> DecoderState -> ByteString
246+ -> Decoding
247+ decodeChunk undecoded0 codepoint0 state0 bs@(PS fp off len) =
247248 runST $ (unsafeIOToST . decodeChunkToBuffer) =<< A.new (len+1)
248249 where
249250 decodeChunkToBuffer :: A.MArray s -> IO Decoding
@@ -281,8 +282,11 @@ streamDecodeUtf8With onErr = decodeChunk 0 0
281282 return $! textP arr 0 (fromIntegral n)
282283 lastPtr <- peek curPtrPtr
283284 let left = lastPtr `minusPtr` curPtr
284− return $ Some chunkText (B.drop left bs)
285− (decodeChunk codepoint state)
285+ undecoded = case state of
286+ UTF8_ACCEPT -> B.empty
287+ _ -> B.append undecoded0 (B.drop left bs)
288+ return $ Some chunkText undecoded
289+ (decodeChunk undecoded codepoint state)
286290 in loop (ptr `plusPtr` off)
287291 desc = "Data.Text.Internal.Encoding.streamDecodeUtf8With: Invalid UTF-8 stream"
288292