@@ -95,15 +95,23 @@ writeFile p = withFile p WriteMode . flip hPutStr
95
95
appendFile :: FilePath -> Text -> IO ()
96
96
appendFile p = withFile p AppendMode . flip hPutStr
97
97
98
- catchError :: String -> Handle -> Handle__ -> IOError -> IO Text
98
+ catchError :: String -> Handle -> Handle__ -> IOError -> IO ( Text , Bool )
99
99
catchError caller h Handle__ {.. } err
100
100
| isEOFError err = do
101
101
buf <- readIORef haCharBuffer
102
102
return $ if isEmptyBuffer buf
103
- then T. empty
104
- else T. singleton ' \r '
103
+ then ( T. empty, True )
104
+ else ( T. singleton ' \r ' , True )
105
105
| otherwise = E. throwIO (augmentIOError err caller h)
106
106
107
+ -- | Wrap readChunk and return a value indicating if we're reached the EOF.
108
+ -- This is needed because unpack_nl is unable to discern the difference
109
+ -- between a buffer with just \r due to EOF or because not enough data was left
110
+ -- for decoding. e.g. the final character decoded from the byte buffer was \r.
111
+ readChunkEof :: Handle__ -> CharBuffer -> IO (Text , Bool )
112
+ readChunkEof hh buf = do t <- readChunk hh buf
113
+ return (t, False )
114
+
107
115
-- | /Experimental./ Read a single chunk of strict text from a
108
116
-- 'Handle'. The size of the chunk depends on the amount of input
109
117
-- currently buffered.
@@ -116,7 +124,7 @@ hGetChunk h = wantReadableHandle "hGetChunk" h readSingleChunk
116
124
where
117
125
readSingleChunk hh@ Handle__ {.. } = do
118
126
buf <- readIORef haCharBuffer
119
- t <- readChunk hh buf `E.catch` catchError " hGetChunk" h hh
127
+ (t, _) <- readChunkEof hh buf `E.catch` catchError " hGetChunk" h hh
120
128
return (hh, t)
121
129
122
130
-- | Read the remaining contents of a 'Handle' as a string. The
@@ -138,8 +146,9 @@ hGetContents h = do
138
146
readAll hh@ Handle__ {.. } = do
139
147
let readChunks = do
140
148
buf <- readIORef haCharBuffer
141
- t <- readChunk hh buf `E.catch` catchError " hGetContents" h hh
142
- if T. null t
149
+ (t, eof) <- readChunkEof hh buf
150
+ `E.catch` catchError " hGetContents" h hh
151
+ if eof
143
152
then return [t]
144
153
else (t: ) `fmap` readChunks
145
154
ts <- readChunks
0 commit comments