on master help sign in
Complete the refactoring of hGetLine.

--HG-- extra : convert_revision : 518daff38e13800eb61b02c6f714ecbde12e8214

commitb0f3a933a52f1f1b01c911e5b0ab3543965215ba authorBryan O'Sullivan <bos@serpentine.com> dateTue 15 Dec 2009, 00:37 (17 years ago) parent57f6c8f
3 files changed+16−10
Data/Text/IO.hs+2−6 Data/Text/IO/Internal.hs+11−3 Data/Text/Lazy/IO.hs+3−1
Data/Text/IO.hs+2−6
@@ -46,7 +46,7 @@ import qualified Data.Text as T
4646import Data.Text.Fusion (stream, unstream)
4747import Data.Text.Fusion.Internal (Step(..), Stream(..))
4848import 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)
5050import Data.Text.Unsafe (inlinePerformIO)
5151import Foreign.Storable (peekElemOff)
5252import GHC.IO.Buffer (Buffer(..), BufferState(..), CharBufElem, CharBuffer,
@@ -121,11 +121,7 @@ hGetLine :: Handle -> IO Text
121121#if __GLASGOW_HASKELL__ <= 610
122122hGetLine = fmap decodeUtf8 . B.hGetLine
123123#else
124−hGetLine h = wantReadableHandle_ "hGetLine" h go
125− where go hh@Handle__{..} = do
126− buf <- readIORef haCharBuffer
127− ts <- hGetLineLoop hh [] buf
128− return $! T.concat ts
124+hGetLine = hGetLineWith T.concat
129125#endif
130126
131127-- | Write a string to a handle.
Data/Text/IO/Internal.hs+11−3
@@ -13,8 +13,8 @@
1313module Data.Text.IO.Internal
1414 (
1515#if __GLASGOW_HASKELL__ >= 612
16− hGetLineLoop
17− , getSomeCharacters
16+ getSomeCharacters
17+ , hGetLineWith
1818 , unpack
1919 , unpack_nl
2020#endif
@@ -23,7 +23,6 @@ module Data.Text.IO.Internal
2323#if __GLASGOW_HASKELL__ >= 612
2424import Control.Exception (throw)
2525import Data.IORef (readIORef, writeIORef)
26−import qualified Data.Text as T
2726import Data.Text (Text)
2827import Data.Text.Fusion (stream, unstream)
2928import Data.Text.Fusion.Internal (Step(..), Stream(..))
@@ -40,7 +39,16 @@ import GHC.IO.Handle.Internals (augmentIOError, ioe_EOF, readTextDevice,
4039import GHC.IO.Handle.Text (commitBuffer')
4140import GHC.IO.Handle.Types (BufferList(..), BufferMode(..), Handle__(..),
4241 Newline(..))
42+import System.IO (Handle)
4343import 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)
4452
4553hGetLineLoop :: Handle__ -> [Text] -> CharBuffer -> IO [Text]
4654hGetLineLoop 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)
4141import qualified Data.ByteString.Char8 as S8
4242import qualified Data.ByteString.Lazy.Char8 as L8
4343#else
44+import Data.Text.IO.Internal (getSomeCharacters, hGetLineWith, unpack, unpack_nl)
45+import qualified Data.Text.Lazy as L
4446#endif
4547
4648-- | The 'readFile' function reads a file and returns the contents of
@@ -70,7 +72,7 @@ hGetLine :: Handle -> IO Text
7072#if __GLASGOW_HASKELL__ <= 610
7173hGetLine = fmap decodeUtf8 . S8.hGetLine
7274#else
73−hGetLine h = undefined
75+hGetLine = hGetLineWith L.fromChunks
7476#endif
7577
7678-- | Write a string to a handle.