Commits

Bryan O'Sullivan  committed 5d7f4d3

Complete the refactoring of hGetLine.

  • Participants
  • Parent commits bdce891

Comments (0)

Files changed (3)

File Data/Text/IO.hs

 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.

File 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

File 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.