@@ -70,6 +70,7 @@ module Streamly.Data.String
70 , stripStart
70 , stripStart
71 , foldLines
71 , foldLines
72 , foldWords
72 , foldWords
74 -- * Streams of Strings
75 -- * Streams of Strings
@@ -87,10 +88,13 @@ import Streamly (IsStream, MonadAsync)
87import Prelude hiding (String, lines, words, unlines, unwords)
88import Prelude hiding (String, lines, words, unlines, unwords)
88import Streamly.Fold (Fold)
89import Streamly.Fold (Fold)
89import Streamly.Memory.Array (Array)
90import Streamly.Memory.Array (Array)
91+import Streamly.Unfold (Unfold)
91−import qualified Streamly.Prelude as S
93+import qualified Streamly.Prelude.Internal as S
92import qualified Streamly.Memory.Array as A
94import qualified Streamly.Memory.Array as A
95+import qualified Streamly.Memory.Array.Types as A
93import qualified Streamly.Streams.StreamD as D
96import qualified Streamly.Streams.StreamD as D
97+import qualified Streamly.Unfold as UF
95-- type String = List Char
99-- type String = List Char
@@ -242,6 +246,15 @@ lines = foldLines A.write
242words :: (MonadIO m, IsStream t) => t m Char -> t m (Array Char)
246words :: (MonadIO m, IsStream t) => t m Char -> t m (Array Char)
243words = foldWords A.write
247words = foldWords A.write
249+-- | Unfold a stream to character streams using the supplied 'Unfold'
250+-- and concat the results suffixing a newline character @\\n@ to each stream.
252+-- > unfoldLines = S.intercalateSuffix UF.singleton '\n'
254+{-# INLINE unfoldLines #-}
255+unfoldLines :: (MonadIO m, IsStream t) => Unfold m a Char -> t m a -> t m Char
256+unfoldLines unf = S.intercalateSuffix UF.singleton '\n' unf
245-- | Flattens the stream of @Array Char@, after appending a terminating
258-- | Flattens the stream of @Array Char@, after appending a terminating
246-- newline to each string.
259-- newline to each string.
@@ -250,14 +263,14 @@ words = foldWords A.write
250-- >>> S.toList $ unlines $ S.fromList ["lines", "this", "string"]
263-- >>> S.toList $ unlines $ S.fromList ["lines", "this", "string"]
251-- "lines\nthis\nstring\n"
264-- "lines\nthis\nstring\n"
253−-- > unlines = A.concatWithSuffix '\n'
266+-- > unlines = unfoldLines A.readU
255-- Note that, in general
268-- Note that, in general
257-- > unlines . lines /= id
270-- > unlines . lines /= id
258{-# INLINE unlines #-}
271{-# INLINE unlines #-}
259unlines :: (MonadIO m, IsStream t) => t m (Array Char) -> t m Char
272unlines :: (MonadIO m, IsStream t) => t m (Array Char) -> t m Char
260−unlines = A.concatWithSuffix '\n'
273+unlines = unfoldLines A.readU
262-- | Flattens the stream of @Array Char@, after appending a separating
275-- | Flattens the stream of @Array Char@, after appending a separating
263-- space to each string.
276-- space to each string.