on master help sign in
Factor out chunk reading from hGetContents.

--HG-- extra : convert_revision : 43256b5ba815882fd694a53671b51a154e8ebf6e

commit07977687ab0ee36cf0884b0c054a2230db1def37 authorBryan O'Sullivan <bos@serpentine.com> dateTue 15 Dec 2009, 00:53 (17 years ago) parentf831ab7
3 files changed+16−13
Data/Text/IO.hs+3−8 Data/Text/IO/Internal.hs+12−4 Data/Text/Lazy/IO.hs+1−1
Data/Text/IO.hs+3−8
@@ -46,7 +46,7 @@ import qualified Data.Text as T
4646import Data.Text.Fusion (stream, unstream)
4747import Data.Text.Fusion.Internal (Step(..), Stream(..))
4848import Data.Text.Fusion.Size (exactSize, maxSize)
49−import Data.Text.IO.Internal (getSomeCharacters, hGetLineWith, unpack, unpack_nl)
49+import Data.Text.IO.Internal (hGetLineWith, readChunk)
5050import Data.Text.Unsafe (inlinePerformIO)
5151import Foreign.Storable (peekElemOff)
5252import GHC.IO.Buffer (Buffer(..), BufferState(..), CharBufElem, CharBuffer,
@@ -99,12 +99,7 @@ hGetContents h = wantReadableHandle "hGetContents" h $ \hh -> do
9999 readAll hh@Handle__{..} = do
100100 buf <- readIORef haCharBuffer
101101 let readChunks = do
102− buf'@Buffer{..} <- getSomeCharacters hh buf
103− (t,r) <- if haInputNL == CRLF
104− then unpack_nl bufRaw bufL bufR
105− else do t <- unpack bufRaw bufL bufR
106− return (t,bufR)
107− writeIORef haCharBuffer (bufferAdjustL r buf')
102+ t <- readChunk hh buf
108103 (hh',ts) <- readAll hh
109104 return (hh', t:ts)
110105 readChunks `catch` \e -> do
@@ -115,7 +110,7 @@ hGetContents h = wantReadableHandle "hGetContents" h $ \hh -> do
115110 else (hh', [T.singleton '\r'])
116111 else throw (augmentIOError e "hGetContents" h)
117112#endif
118−
113+
119114-- | Read a single line from a handle.
120115hGetLine :: Handle -> IO Text
121116#if __GLASGOW_HASKELL__ <= 610
Data/Text/IO/Internal.hs+12−4
@@ -13,10 +13,8 @@
1313module Data.Text.IO.Internal
1414 (
1515#if __GLASGOW_HASKELL__ >= 612
16− getSomeCharacters
17− , hGetLineWith
18− , unpack
19− , unpack_nl
16+ hGetLineWith
17+ , readChunk
2018#endif
2119 ) where
2220
@@ -155,6 +153,16 @@ getSomeCharacters handle_@Handle__{..} buf@Buffer{..} =
155153 -- buffer has some chars in it already: just return it
156154 _otherwise -> return buf
157155
156+readChunk :: Handle__ -> CharBuffer -> IO Text
157+readChunk hh@Handle__{..} buf = do
158+ buf'@Buffer{..} <- getSomeCharacters hh buf
159+ (t,r) <- if haInputNL == CRLF
160+ then unpack_nl bufRaw bufL bufR
161+ else do t <- unpack bufRaw bufL bufR
162+ return (t,bufR)
163+ writeIORef haCharBuffer (bufferAdjustL r buf')
164+ return t
165+
158166sizeError :: String -> a
159167sizeError loc = error $ "Data.Text.IO." ++ loc ++ ": bad internal buffer size"
160168#endif
Data/Text/Lazy/IO.hs+1−1
@@ -43,7 +43,7 @@ import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
4343import qualified Data.ByteString.Char8 as S8
4444import qualified Data.ByteString.Lazy.Char8 as L8
4545#else
46−import Data.Text.IO.Internal (getSomeCharacters, hGetLineWith, unpack, unpack_nl)
46+import Data.Text.IO.Internal (hGetLineWith)
4747#endif
4848
4949-- | The 'readFile' function reads a file and returns the contents of