Skip to content

Commit

Permalink
added lazy streamLn
Browse files Browse the repository at this point in the history
  • Loading branch information
BebeSparkelSparkel committed Jun 4, 2024
1 parent bdb19ff commit aed6494
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/Data/Text/Internal/Fusion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ stream ::
HasCallStack =>
#endif
Text -> Stream Char
stream t = stream' t False
stream = stream' False
{-# INLINE [0] stream #-}

-- | /O(n)/ @'streamLn' t = 'stream' (t <> \'\\n\')@
Expand All @@ -90,15 +90,15 @@ streamLn ::
HasCallStack =>
#endif
Text -> Stream Char
streamLn t = stream' t True
streamLn = stream' True

-- | Shared implementation of 'stream' and 'streamLn'.
stream' ::
#if defined(ASSERTS)
HasCallStack =>
#endif
Text -> Bool -> Stream Char
stream' (Text arr off len) addNl = Stream next off (betweenSize (len `shiftR` 2) maxLen)
Bool -> Text -> Stream Char
stream' addNl (Text arr off len) = Stream next off (betweenSize (len `shiftR` 2) maxLen)
where
maxLen = if addNl then len + 1 else len
!end = off+len
Expand Down
28 changes: 25 additions & 3 deletions src/Data/Text/Internal/Lazy/Fusion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
module Data.Text.Internal.Lazy.Fusion
(
stream
, streamLn
, unstream
, unstreamChunks
, length
Expand Down Expand Up @@ -47,14 +48,35 @@ stream ::
HasCallStack =>
#endif
Text -> Stream Char
stream text = Stream next (text :*: 0) unknownSize
stream = stream' False
{-# INLINE [0] stream #-}

-- | /O(n)/ @'streamLn' t = 'stream' (t <> \'\\n\')@
--
-- @since ????
streamLn ::
#if defined(ASSERTS)
HasCallStack =>
#endif
Text -> Stream Char
streamLn = stream' True

-- | Shared implementation of 'stream' and 'streamLn'.
stream' ::
#if defined(ASSERTS)
HasCallStack =>
#endif
Bool -> Text -> Stream Char
stream' addNl text = Stream next (text :*: 0) unknownSize
where
next (Empty :*: _) = Done
next (Empty :*: i)
| addNl && i <= 0 = Yield '\n' (Empty :*: 1)
| otherwise = Done
next (txt@(Chunk t@(I.Text _ _ len) ts) :*: i)
| i >= len = next (ts :*: 0)
| otherwise = Yield c (txt :*: i+d)
where Iter c d = iter t i
{-# INLINE [0] stream #-}
{-# INLINE [0] stream' #-}

-- | /O(n)/ Convert a 'Stream Char' into a 'Text', using the given
-- chunk size.
Expand Down
11 changes: 4 additions & 7 deletions src/Data/Text/Lazy/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@ module Data.Text.Lazy.IO
import Data.Text.Lazy (Text)
import Prelude hiding (appendFile, getContents, getLine, interact,
putStr, putStrLn, readFile, writeFile)
import System.IO (Handle, IOMode(..), hPutChar, openFile, stdin, stdout,
import System.IO (Handle, IOMode(..), openFile, stdin, stdout,
withFile)
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as L
import qualified Control.Exception as E
import Control.Monad (when)
import Data.IORef (readIORef)
import Data.Text.Internal.IO (hGetLineWith, readChunk, hPutStream)
import Data.Text.Internal.Lazy (Text(..), chunk, empty)
import Data.Text.Internal.Lazy.Fusion (stream)
import Data.Text.Internal.Lazy (chunk, empty)
import Data.Text.Internal.Lazy.Fusion (stream, streamLn)
import GHC.IO.Buffer (isEmptyBuffer)
import GHC.IO.Exception (IOException(..), IOErrorType(..), ioException)
import GHC.IO.Handle.Internals (augmentIOError, hClose_help,
Expand Down Expand Up @@ -134,9 +133,7 @@ hPutStr h = hPutStream h . stream

-- | Write a string to a handle, followed by a newline.
hPutStrLn :: Handle -> Text -> IO ()
hPutStrLn h Empty = hPutChar h '\n'
hPutStrLn h (Chunk t Empty) = T.hPutStrLn h t -- print the newline after the last chunk atomically
hPutStrLn h t = hPutStream h (stream t) >> hPutChar h '\n'
hPutStrLn h t = hPutStream h $ streamLn t

-- | The 'interact' function takes a function of type @Text -> Text@
-- as its argument. The entire input from the standard input device is
Expand Down

0 comments on commit aed6494

Please sign in to comment.