Bryan O'Sullivan avatar Bryan O'Sullivan committed 5d7f4d3

Complete the refactoring of hGetLine.

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, hGetLineLoop, unpack, unpack_nl)
+import Data.Text.IO.Internal (getSomeCharacters, hGetLineWith, unpack, unpack_nl)
 import Data.Text.Unsafe (inlinePerformIO)
 import Foreign.Storable (peekElemOff)
 import GHC.IO.Buffer (Buffer(..), BufferState(..), CharBufElem, CharBuffer,
 #if __GLASGOW_HASKELL__ <= 610
 hGetLine = fmap decodeUtf8 . B.hGetLine
 #else
-hGetLine h = wantReadableHandle_ "hGetLine" h go
-  where go hh@Handle__{..} = do
-          buf <- readIORef haCharBuffer
-          ts <- hGetLineLoop hh [] buf
-          return $! T.concat ts
+hGetLine = hGetLineWith T.concat
 #endif
 
 -- | Write a string to a handle.

Data/Text/IO/Internal.hs

 module Data.Text.IO.Internal
     (
 #if __GLASGOW_HASKELL__ >= 612
-      hGetLineLoop
-    , getSomeCharacters
+      getSomeCharacters
+    , hGetLineWith
     , unpack
     , unpack_nl
 #endif
 #if __GLASGOW_HASKELL__ >= 612
 import Control.Exception (throw)
 import Data.IORef (readIORef, writeIORef)
-import qualified Data.Text as T
 import Data.Text (Text)
 import Data.Text.Fusion (stream, unstream)
 import Data.Text.Fusion.Internal (Step(..), Stream(..))
 import GHC.IO.Handle.Text (commitBuffer')
 import GHC.IO.Handle.Types (BufferList(..), BufferMode(..), Handle__(..),
                             Newline(..))
+import System.IO (Handle)
 import System.IO.Error (isEOFError)
+import qualified Data.Text as T
+
+hGetLineWith :: ([Text] -> t) -> Handle -> IO t
+hGetLineWith f h = wantReadableHandle_ "hGetLine" h go
+  where go hh@Handle__{..} = do
+          buf <- readIORef haCharBuffer
+          ts <- hGetLineLoop hh [] buf
+          return (f ts)
 
 hGetLineLoop :: Handle__ -> [Text] -> CharBuffer -> IO [Text]
 hGetLineLoop hh@Handle__{..} ts buf@Buffer{ bufL=r0, bufR=w, bufRaw=raw0 } = do

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 qualified Data.Text.Lazy as L
 #endif
 
 -- | The 'readFile' function reads a file and returns the contents of
 #if __GLASGOW_HASKELL__ <= 610
 hGetLine = fmap decodeUtf8 . S8.hGetLine
 #else
-hGetLine h = undefined
+hGetLine = hGetLineWith L.fromChunks
 #endif
 
 -- | Write a string to a handle.
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.