on master help sign in
Factor out chunk reading from hGetContents.
0797768Bryan O'Sullivan
Data/Text/IO.hs+3−8
@@ -46,7 +46,7 @@ import qualified Data.Text as T
46import Data.Text.Fusion (stream, unstream)
46import Data.Text.Fusion (stream, unstream)
47import Data.Text.Fusion.Internal (Step(..), Stream(..))
47import Data.Text.Fusion.Internal (Step(..), Stream(..))
48import Data.Text.Fusion.Size (exactSize, maxSize)
48import 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)
50import Data.Text.Unsafe (inlinePerformIO)
50import Data.Text.Unsafe (inlinePerformIO)
51import Foreign.Storable (peekElemOff)
51import Foreign.Storable (peekElemOff)
52import GHC.IO.Buffer (Buffer(..), BufferState(..), CharBufElem, CharBuffer,
52import GHC.IO.Buffer (Buffer(..), BufferState(..), CharBufElem, CharBuffer,
@@ -99,12 +99,7 @@ hGetContents h = wantReadableHandle "hGetContents" h $ \hh -> do
99 readAll hh@Handle__{..} = do
99 readAll hh@Handle__{..} = do
100 buf <- readIORef haCharBuffer
100 buf <- readIORef haCharBuffer
101 let readChunks = do
101 let readChunks = do
102− buf'@Buffer{..} <- getSomeCharacters hh buf
102+ t <- readChunk 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')
108 (hh',ts) <- readAll hh
103 (hh',ts) <- readAll hh
109 return (hh', t:ts)
104 return (hh', t:ts)
110 readChunks `catch` \e -> do
105 readChunks `catch` \e -> do
@@ -115,7 +110,7 @@ hGetContents h = wantReadableHandle "hGetContents" h $ \hh -> do
115 else (hh', [T.singleton '\r'])
110 else (hh', [T.singleton '\r'])
116 else throw (augmentIOError e "hGetContents" h)
111 else throw (augmentIOError e "hGetContents" h)
117#endif
112#endif
118−
113+
119-- | Read a single line from a handle.
114-- | Read a single line from a handle.
120hGetLine :: Handle -> IO Text
115hGetLine :: Handle -> IO Text
121#if __GLASGOW_HASKELL__ <= 610
116#if __GLASGOW_HASKELL__ <= 610
Data/Text/IO/Internal.hs+12−4
@@ -13,10 +13,8 @@
13module Data.Text.IO.Internal
13module Data.Text.IO.Internal
14 (
14 (
15#if __GLASGOW_HASKELL__ >= 612
15#if __GLASGOW_HASKELL__ >= 612
16− getSomeCharacters
17− , hGetLineWith
16+ hGetLineWith
18− , unpack
17+ , readChunk
19− , unpack_nl
20#endif
18#endif
21 ) where
19 ) where
22
20
@@ -155,6 +153,16 @@ getSomeCharacters handle_@Handle__{..} buf@Buffer{..} =
155 -- buffer has some chars in it already: just return it
153 -- buffer has some chars in it already: just return it
156 _otherwise -> return buf
154 _otherwise -> return buf
157
155
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+
158sizeError :: String -> a
166sizeError :: String -> a
159sizeError loc = error $ "Data.Text.IO." ++ loc ++ ": bad internal buffer size"
167sizeError loc = error $ "Data.Text.IO." ++ loc ++ ": bad internal buffer size"
160#endif
168#endif
Data/Text/Lazy/IO.hs+1−1
@@ -43,7 +43,7 @@ import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
43import qualified Data.ByteString.Char8 as S8
43import qualified Data.ByteString.Char8 as S8
44import qualified Data.ByteString.Lazy.Char8 as L8
44import qualified Data.ByteString.Lazy.Char8 as L8
45#else
45#else
46−import Data.Text.IO.Internal (getSomeCharacters, hGetLineWith, unpack, unpack_nl)
46+import Data.Text.IO.Internal (hGetLineWith)
47#endif
47#endif
48
48
49-- | The 'readFile' function reads a file and returns the contents of
49-- | The 'readFile' function reads a file and returns the contents of