on master help sign in
Merge
commit43afcf21810b9e91a1890e8b5487b4b0908ff428 authorBryan O'Sullivan <bos@serpentine.com> dateSat 8 Sep 2012, 22:51 (14 years ago) parentb8019c5abf20a8
1 file changed+17
Data/Text/IO.hs+17
Data/Text/IO.hs+17
@@ -26,6 +26,7 @@ module Data.Text.IO
2626 , appendFile
2727 -- * Operations on handles
2828 , hGetContents
29+ , hGetChunk
2930 , hGetLine
3031 , hPutStr
3132 , hPutStrLn
@@ -97,6 +98,22 @@ writeFile p = withFile p WriteMode . flip hPutStr
9798appendFile :: FilePath -> Text -> IO ()
9899appendFile p = withFile p AppendMode . flip hPutStr
99100
101+-- | Read a single chunk of strict text from a 'Handle'.
102+hGetChunk :: Handle -> IO Text
103+hGetChunk h = wantReadableHandle "hGetChunk" h readSingleChunk
104+ where
105+ readSingleChunk hh@Handle__{..} = do
106+ let catchError e
107+ | isEOFError e = do
108+ buf <- readIORef haCharBuffer
109+ return $ if isEmptyBuffer buf
110+ then T.empty
111+ else T.singleton '\r'
112+ | otherwise = throwIO (augmentIOError e "hGetChunk" h)
113+ buf <- readIORef haCharBuffer
114+ t <- readChunk hh buf `catch` catchError
115+ return (hh, t)
116+
100117-- | Read the remaining contents of a 'Handle' as a string. The
101118-- 'Handle' is closed once the contents have been read, or if an
102119-- exception is thrown.