From 5447e2abdd342f9892b2a8e61dbc85fc9f713bfe Mon Sep 17 00:00:00 2001 From: simonmar Date: Tue, 18 Jun 2002 13:01:43 +0000 Subject: [PATCH] [project @ 2002-06-18 13:01:43 by simonmar] Fix bug in the implementation of hGetLine: on finding the EOF when we have a partial line in our hands, we weren't resetting the state of the buffer to empty, so the same partial line would be returned for each subsequent call to hGetLine. --- GHC/IO.hs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/GHC/IO.hs b/GHC/IO.hs index 9467c53..b3d590a 100644 --- a/GHC/IO.hs +++ b/GHC/IO.hs @@ -168,10 +168,13 @@ hGetLineBufferedLoop handle_ ref #endif xs <- unpack raw r off + + -- if eol == True, then off is the offset of the '\n' + -- otherwise off == w and the buffer is now empty. if eol - then do if w == off + 1 - then writeIORef ref buf{ bufRPtr=0, bufWPtr=0 } - else writeIORef ref buf{ bufRPtr = off + 1 } + then do if (w == off + 1) + then writeIORef ref buf{ bufRPtr=0, bufWPtr=0 } + else writeIORef ref buf{ bufRPtr = off + 1 } return (concat (reverse (xs:xss))) else do maybe_buf <- maybeFillReadBuffer (haFD handle_) True (haIsStream handle_) @@ -179,10 +182,12 @@ hGetLineBufferedLoop handle_ ref case maybe_buf of -- Nothing indicates we caught an EOF, and we may have a -- partial line to return. - Nothing -> let str = concat (reverse (xs:xss)) in - if not (null str) - then return str - else ioe_EOF + Nothing -> do + writeIORef ref buf{ bufRPtr=0, bufWPtr=0 } + let str = concat (reverse (xs:xss)) + if not (null str) + then return str + else ioe_EOF Just new_buf -> hGetLineBufferedLoop handle_ ref new_buf (xs:xss) -- 1.7.10.4