on master help sign in
Fix up Data.Text.IO.hGetContents to work, and not be insane
9c3ebddBryan O'Sullivan
Data/Text/IO.hs+16−15
@@ -92,24 +92,25 @@ hGetContents = fmap decodeUtf8 . B.hGetContents
92#else
92#else
93hGetContents h = do
93hGetContents h = do
94 chooseGoodBuffering h
94 chooseGoodBuffering h
95− wantReadableHandle "hGetContents" h $ \hh -> do
95+ wantReadableHandle "hGetContents" h readAll
96− (hh',ts) <- readAll hh
97− return (hh'{haType=ClosedHandle},T.concat ts)
98 where
96 where
99 readAll hh@Handle__{..} = do
97 readAll hh@Handle__{..} = do
100− let readChunks = do
98+ let catchError e
99+ | isEOFError e = do
101− t <- readChunk hh =<< readIORef haCharBuffer
100+ buf <- readIORef haCharBuffer
102− (hh',ts) <- readChunks
101+ return $ if isEmptyBuffer buf
103− return (hh', t:ts)
102+ then T.empty
104− readChunks `catch` \e -> do
103+ else T.singleton '\r'
105− if isEOFError e
104+ | otherwise = throw (augmentIOError e "hGetContents" h)
106− then do
105+ readChunks = do
107− (hh', _) <- hClose_help hh
108 buf <- readIORef haCharBuffer
106 buf <- readIORef haCharBuffer
109− return $ if isEmptyBuffer buf
107+ t <- readChunk hh buf `catch` catchError
108+ if T.null t
110− then (hh', [])
109+ then return [t]
111− else (hh', [T.singleton '\r'])
110+ else (t:) `fmap` readChunks
112− else throw (augmentIOError e "hGetContents" h)
111+ ts <- readChunks
112+ (hh', _) <- hClose_help hh
113+ return (hh'{haType=ClosedHandle}, T.concat ts)
113#endif
114#endif
114
115
115-- | Use a more efficient buffer size if we're reading in
116-- | Use a more efficient buffer size if we're reading in