Bryan O'Sullivan avatar Bryan O'Sullivan committed d92fcbd

Factor out chunk reading from hGetContents.

Comments (0)

Files changed (3)

 import Data.Text.Fusion (stream, unstream)
 import Data.Text.Fusion.Internal (Step(..), Stream(..))
 import Data.Text.Fusion.Size (exactSize, maxSize)
-import Data.Text.IO.Internal (getSomeCharacters, hGetLineWith, unpack, unpack_nl)
+import Data.Text.IO.Internal (hGetLineWith, readChunk)
 import Data.Text.Unsafe (inlinePerformIO)
 import Foreign.Storable (peekElemOff)
 import GHC.IO.Buffer (Buffer(..), BufferState(..), CharBufElem, CharBuffer,
   readAll hh@Handle__{..} = do
     buf <- readIORef haCharBuffer
     let readChunks = do
-          buf'@Buffer{..} <- getSomeCharacters hh buf
-          (t,r) <- if haInputNL == CRLF
-                   then unpack_nl bufRaw bufL bufR
-                   else do t <- unpack bufRaw bufL bufR
-                           return (t,bufR)
-          writeIORef haCharBuffer (bufferAdjustL r buf')
+          t <- readChunk hh buf
           (hh',ts) <- readAll hh
           return (hh', t:ts)
     readChunks `catch` \e -> do
                       else (hh', [T.singleton '\r'])
         else throw (augmentIOError e "hGetContents" h)
 #endif
-
+  
 -- | Read a single line from a handle.
 hGetLine :: Handle -> IO Text
 #if __GLASGOW_HASKELL__ <= 610

Data/Text/IO/Internal.hs

 module Data.Text.IO.Internal
     (
 #if __GLASGOW_HASKELL__ >= 612
-      getSomeCharacters
-    , hGetLineWith
-    , unpack
-    , unpack_nl
+      hGetLineWith
+    , readChunk
 #endif
     ) where
 
     -- buffer has some chars in it already: just return it
     _otherwise -> return buf
 
+readChunk :: Handle__ -> CharBuffer -> IO Text
+readChunk hh@Handle__{..} buf = do
+  buf'@Buffer{..} <- getSomeCharacters hh buf
+  (t,r) <- if haInputNL == CRLF
+           then unpack_nl bufRaw bufL bufR
+           else do t <- unpack bufRaw bufL bufR
+                   return (t,bufR)
+  writeIORef haCharBuffer (bufferAdjustL r buf')
+  return t
+
 sizeError :: String -> a
 sizeError loc = error $ "Data.Text.IO." ++ loc ++ ": bad internal buffer size"
 #endif

Data/Text/Lazy/IO.hs

 import qualified Data.ByteString.Char8 as S8
 import qualified Data.ByteString.Lazy.Char8 as L8
 #else
-import Data.Text.IO.Internal (getSomeCharacters, hGetLineWith, unpack, unpack_nl)
+import Data.Text.IO.Internal (hGetLineWith)
 #endif
 
 -- | The 'readFile' function reads a file and returns the contents of
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.