on master help sign in
Fix up Data.Text.IO.hGetContents to work, and not be insane

--HG-- extra : convert_revision : 423c75e213dd4aed47c4d99fef294740a31b7e0b

commit9c3ebdd73cf56c0b9849ef53fa92157a66e12182 authorBryan O'Sullivan <bos@serpentine.com> dateThu 26 Aug 2010, 18:30 (16 years ago) parentdb99fe3
1 file changed+16−15
Data/Text/IO.hs+16−15
Data/Text/IO.hs+16−15
@@ -92,24 +92,25 @@ hGetContents = fmap decodeUtf8 . B.hGetContents
9292#else
9393hGetContents h = do
9494 chooseGoodBuffering h
95− wantReadableHandle "hGetContents" h $ \hh -> do
96− (hh',ts) <- readAll hh
97− return (hh'{haType=ClosedHandle},T.concat ts)
95+ wantReadableHandle "hGetContents" h readAll
9896 where
9997 readAll hh@Handle__{..} = do
100− let readChunks = do
101− t <- readChunk hh =<< readIORef haCharBuffer
102− (hh',ts) <- readChunks
103− return (hh', t:ts)
104− readChunks `catch` \e -> do
105− if isEOFError e
106− then do
107− (hh', _) <- hClose_help hh
98+ let catchError e
99+ | isEOFError e = do
100+ buf <- readIORef haCharBuffer
101+ return $ if isEmptyBuffer buf
102+ then T.empty
103+ else T.singleton '\r'
104+ | otherwise = throw (augmentIOError e "hGetContents" h)
105+ readChunks = do
108106 buf <- readIORef haCharBuffer
109− return $ if isEmptyBuffer buf
110− then (hh', [])
111− else (hh', [T.singleton '\r'])
112− else throw (augmentIOError e "hGetContents" h)
107+ t <- readChunk hh buf `catch` catchError
108+ if T.null t
109+ then return [t]
110+ else (t:) `fmap` readChunks
111+ ts <- readChunks
112+ (hh', _) <- hClose_help hh
113+ return (hh'{haType=ClosedHandle}, T.concat ts)
113114#endif
114115
115116-- | Use a more efficient buffer size if we're reading in