on master help sign in
Complete the refactoring of hGetLine.
b0f3a93Bryan O'Sullivan
Data/Text/IO.hs+2−6
@@ -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, hGetLineLoop, unpack, unpack_nl)
49+import Data.Text.IO.Internal (getSomeCharacters, hGetLineWith, unpack, unpack_nl)
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,
@@ -121,11 +121,7 @@ hGetLine :: Handle -> IO Text
121#if __GLASGOW_HASKELL__ <= 610
121#if __GLASGOW_HASKELL__ <= 610
122hGetLine = fmap decodeUtf8 . B.hGetLine
122hGetLine = fmap decodeUtf8 . B.hGetLine
123#else
123#else
124−hGetLine h = wantReadableHandle_ "hGetLine" h go
124+hGetLine = hGetLineWith T.concat
125− where go hh@Handle__{..} = do
126− buf <- readIORef haCharBuffer
127− ts <- hGetLineLoop hh [] buf
128− return $! T.concat ts
129#endif
125#endif
130
126
131-- | Write a string to a handle.
127-- | Write a string to a handle.
Data/Text/IO/Internal.hs+11−3
@@ -13,8 +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− hGetLineLoop
16+ getSomeCharacters
17− , getSomeCharacters
17+ , hGetLineWith
18 , unpack
18 , unpack
19 , unpack_nl
19 , unpack_nl
20#endif
20#endif
@@ -23,7 +23,6 @@ module Data.Text.IO.Internal
23#if __GLASGOW_HASKELL__ >= 612
23#if __GLASGOW_HASKELL__ >= 612
24import Control.Exception (throw)
24import Control.Exception (throw)
25import Data.IORef (readIORef, writeIORef)
25import Data.IORef (readIORef, writeIORef)
26−import qualified Data.Text as T
27import Data.Text (Text)
26import Data.Text (Text)
28import Data.Text.Fusion (stream, unstream)
27import Data.Text.Fusion (stream, unstream)
29import Data.Text.Fusion.Internal (Step(..), Stream(..))
28import Data.Text.Fusion.Internal (Step(..), Stream(..))
@@ -40,7 +39,16 @@ import GHC.IO.Handle.Internals (augmentIOError, ioe_EOF, readTextDevice,
40import GHC.IO.Handle.Text (commitBuffer')
39import GHC.IO.Handle.Text (commitBuffer')
41import GHC.IO.Handle.Types (BufferList(..), BufferMode(..), Handle__(..),
40import GHC.IO.Handle.Types (BufferList(..), BufferMode(..), Handle__(..),
42 Newline(..))
41 Newline(..))
42+import System.IO (Handle)
43import System.IO.Error (isEOFError)
43import System.IO.Error (isEOFError)
44+import qualified Data.Text as T
45+
46+hGetLineWith :: ([Text] -> t) -> Handle -> IO t
47+hGetLineWith f h = wantReadableHandle_ "hGetLine" h go
48+ where go hh@Handle__{..} = do
49+ buf <- readIORef haCharBuffer
50+ ts <- hGetLineLoop hh [] buf
51+ return (f ts)
44
52
45hGetLineLoop :: Handle__ -> [Text] -> CharBuffer -> IO [Text]
53hGetLineLoop :: Handle__ -> [Text] -> CharBuffer -> IO [Text]
46hGetLineLoop hh@Handle__{..} ts buf@Buffer{ bufL=r0, bufR=w, bufRaw=raw0 } = do
54hGetLineLoop hh@Handle__{..} ts buf@Buffer{ bufL=r0, bufR=w, bufRaw=raw0 } = do
Data/Text/Lazy/IO.hs+3−1
@@ -41,6 +41,8 @@ import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
41import qualified Data.ByteString.Char8 as S8
41import qualified Data.ByteString.Char8 as S8
42import qualified Data.ByteString.Lazy.Char8 as L8
42import qualified Data.ByteString.Lazy.Char8 as L8
43#else
43#else
44+import Data.Text.IO.Internal (getSomeCharacters, hGetLineWith, unpack, unpack_nl)
45+import qualified Data.Text.Lazy as L
44#endif
46#endif
45
47
46-- | The 'readFile' function reads a file and returns the contents of
48-- | The 'readFile' function reads a file and returns the contents of
@@ -70,7 +72,7 @@ hGetLine :: Handle -> IO Text
70#if __GLASGOW_HASKELL__ <= 610
72#if __GLASGOW_HASKELL__ <= 610
71hGetLine = fmap decodeUtf8 . S8.hGetLine
73hGetLine = fmap decodeUtf8 . S8.hGetLine
72#else
74#else
73−hGetLine h = undefined
75+hGetLine = hGetLineWith L.fromChunks
74#endif
76#endif
75
77
76-- | Write a string to a handle.
78-- | Write a string to a handle.