Skip to content

Commit aed6494

Browse files
added lazy streamLn
1 parent bdb19ff commit aed6494

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/Data/Text/Internal/Fusion.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ stream ::
7979
HasCallStack =>
8080
#endif
8181
Text -> Stream Char
82-
stream t = stream' t False
82+
stream = stream' False
8383
{-# INLINE [0] stream #-}
8484

8585
-- | /O(n)/ @'streamLn' t = 'stream' (t <> \'\\n\')@
@@ -90,15 +90,15 @@ streamLn ::
9090
HasCallStack =>
9191
#endif
9292
Text -> Stream Char
93-
streamLn t = stream' t True
93+
streamLn = stream' True
9494

9595
-- | Shared implementation of 'stream' and 'streamLn'.
9696
stream' ::
9797
#if defined(ASSERTS)
9898
HasCallStack =>
9999
#endif
100-
Text -> Bool -> Stream Char
101-
stream' (Text arr off len) addNl = Stream next off (betweenSize (len `shiftR` 2) maxLen)
100+
Bool -> Text -> Stream Char
101+
stream' addNl (Text arr off len) = Stream next off (betweenSize (len `shiftR` 2) maxLen)
102102
where
103103
maxLen = if addNl then len + 1 else len
104104
!end = off+len

src/Data/Text/Internal/Lazy/Fusion.hs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
module Data.Text.Internal.Lazy.Fusion
1818
(
1919
stream
20+
, streamLn
2021
, unstream
2122
, unstreamChunks
2223
, length
@@ -47,14 +48,35 @@ stream ::
4748
HasCallStack =>
4849
#endif
4950
Text -> Stream Char
50-
stream text = Stream next (text :*: 0) unknownSize
51+
stream = stream' False
52+
{-# INLINE [0] stream #-}
53+
54+
-- | /O(n)/ @'streamLn' t = 'stream' (t <> \'\\n\')@
55+
--
56+
-- @since ????
57+
streamLn ::
58+
#if defined(ASSERTS)
59+
HasCallStack =>
60+
#endif
61+
Text -> Stream Char
62+
streamLn = stream' True
63+
64+
-- | Shared implementation of 'stream' and 'streamLn'.
65+
stream' ::
66+
#if defined(ASSERTS)
67+
HasCallStack =>
68+
#endif
69+
Bool -> Text -> Stream Char
70+
stream' addNl text = Stream next (text :*: 0) unknownSize
5171
where
52-
next (Empty :*: _) = Done
72+
next (Empty :*: i)
73+
| addNl && i <= 0 = Yield '\n' (Empty :*: 1)
74+
| otherwise = Done
5375
next (txt@(Chunk t@(I.Text _ _ len) ts) :*: i)
5476
| i >= len = next (ts :*: 0)
5577
| otherwise = Yield c (txt :*: i+d)
5678
where Iter c d = iter t i
57-
{-# INLINE [0] stream #-}
79+
{-# INLINE [0] stream' #-}
5880

5981
-- | /O(n)/ Convert a 'Stream Char' into a 'Text', using the given
6082
-- chunk size.

src/Data/Text/Lazy/IO.hs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,15 @@ module Data.Text.Lazy.IO
4141
import Data.Text.Lazy (Text)
4242
import Prelude hiding (appendFile, getContents, getLine, interact,
4343
putStr, putStrLn, readFile, writeFile)
44-
import System.IO (Handle, IOMode(..), hPutChar, openFile, stdin, stdout,
44+
import System.IO (Handle, IOMode(..), openFile, stdin, stdout,
4545
withFile)
46-
import qualified Data.Text.IO as T
4746
import qualified Data.Text.Lazy as L
4847
import qualified Control.Exception as E
4948
import Control.Monad (when)
5049
import Data.IORef (readIORef)
5150
import Data.Text.Internal.IO (hGetLineWith, readChunk, hPutStream)
52-
import Data.Text.Internal.Lazy (Text(..), chunk, empty)
53-
import Data.Text.Internal.Lazy.Fusion (stream)
51+
import Data.Text.Internal.Lazy (chunk, empty)
52+
import Data.Text.Internal.Lazy.Fusion (stream, streamLn)
5453
import GHC.IO.Buffer (isEmptyBuffer)
5554
import GHC.IO.Exception (IOException(..), IOErrorType(..), ioException)
5655
import GHC.IO.Handle.Internals (augmentIOError, hClose_help,
@@ -134,9 +133,7 @@ hPutStr h = hPutStream h . stream
134133

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

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

0 commit comments

Comments
 (0)