@@ -34,6 +34,7 @@ module Streamly.Internal.Unicode.Stream
3434 , encodeUtf8
3535 , encodeUtf8'
3636 , encodeUtf8_
37+ , encodeStrings
3738 {-
3839 -- * Operations on character strings
3940 , strip -- (dropAround isSpace)
@@ -54,7 +55,7 @@ module Streamly.Internal.Unicode.Stream
5455 , decodeUtf8ArraysD_
5556
5657 -- * Transformation
57− , stripStart
58+ , stripHead
5859 , lines
5960 , words
6061 , unlines
@@ -85,18 +86,22 @@ import Streamly.Data.Array.Foreign (Array)
8586import Streamly.Internal.Data.Unfold (Unfold)
8687import Streamly.Internal.Data.SVar (adaptState)
8788import Streamly.Internal.Data.Stream.IsStream (IsStream)
89+import Streamly.Internal.Data.Stream.Serial (SerialT)
8890import Streamly.Internal.Data.Stream.StreamD (Stream(..), Step (..))
8991import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
9092
93+import qualified Streamly.Internal.Data.Unfold as Unfold
94+import qualified Streamly.Internal.Data.Stream.Serial as Serial
95+import qualified Streamly.Internal.Data.Array.Foreign as Array
9196import qualified Streamly.Internal.Data.Array.Foreign.Type as A
9297import qualified Streamly.Internal.Data.Stream.IsStream as S
9398import qualified Streamly.Internal.Data.Stream.StreamD as D
9499
95−import Prelude hiding (String, lines, words, unlines, unwords)
100+import Prelude hiding (lines, words, unlines, unwords)
96101
97102-- $setup
98103-- >>> :m
99−-- >>> import Prelude hiding (String, lines, words, unlines, unwords)
104+-- >>> import Prelude hiding (lines, words, unlines, unwords)
100105-- >>> import qualified Streamly.Prelude as Stream
101106-- >>> import qualified Streamly.Data.Fold as Fold
102107-- >>> import Streamly.Internal.Unicode.Stream
@@ -842,6 +847,39 @@ encodeUtf8_ = D.fromStreamD . encodeUtf8D_ . D.toStreamD
842847encodeUtf8Lax :: (IsStream t, Monad m) => t m Char -> t m Word8
843848encodeUtf8Lax = encodeUtf8
844849
850+-- | Encode a container to @Array Word8@ provided an unfold to covert it to a
851+-- Char stream and an encoding function.
852+--
853+-- /Internal/
854+{-# INLINE encodeObject #-}
855+encodeObject :: MonadIO m =>
856+ (SerialT m Char -> SerialT m Word8)
857+ -> Unfold m a Char
858+ -> a
859+ -> m (Array Word8)
860+encodeObject encode u = S.fold Array.write . encode . S.unfold u
861+
862+-- | Encode a stream of container objects using the supplied encoding scheme.
863+-- Each object is encoded as an @Array Word8@.
864+--
865+-- /Internal/
866+{-# INLINE encodeObjects #-}
867+encodeObjects :: (MonadIO m, IsStream t) =>
868+ (SerialT m Char -> SerialT m Word8)
869+ -> Unfold m a Char
870+ -> t m a
871+ -> t m (Array Word8)
872+encodeObjects encode u = Serial.mapM (encodeObject encode u)
873+
874+-- | Encode a stream of 'String' using the supplied encoding scheme. Each
875+-- string is encoded as an @Array Word8@.
876+--
877+-- @since 0.8.0
878+{-# INLINE encodeStrings #-}
879+encodeStrings :: (MonadIO m, IsStream t) =>
880+ (SerialT m Char -> SerialT m Word8) -> t m String -> t m (Array Word8)
881+encodeStrings encode = encodeObjects encode Unfold.fromList
882+
845883{-
846884-------------------------------------------------------------------------------
847885-- Utility operations on strings
@@ -850,18 +888,18 @@ encodeUtf8Lax = encodeUtf8
850888strip :: IsStream t => t m Char -> t m Char
851889strip = undefined
852890
853−stripEnd :: IsStream t => t m Char -> t m Char
854−stripEnd = undefined
891+stripTail :: IsStream t => t m Char -> t m Char
892+stripTail = undefined
855893-}
856894
857895-- | Remove leading whitespace from a string.
858896--
859−-- > stripStart = S.dropWhile isSpace
897+-- > stripHead = S.dropWhile isSpace
860898--
861899-- /Pre-release/
862−{-# INLINE stripStart #-}
863−stripStart :: (Monad m, IsStream t) => t m Char -> t m Char
864−stripStart = S.dropWhile isSpace
900+{-# INLINE stripHead #-}
901+stripHead :: (Monad m, IsStream t) => t m Char -> t m Char
902+stripHead = S.dropWhile isSpace
865903
866904-- | Fold each line of the stream using the supplied 'Fold'
867905-- and stream the result.
@@ -906,6 +944,7 @@ words = S.wordsBy isSpace
906944--
907945-- @
908946-- unlines = Stream.interposeSuffix '\n'
947+-- unlines = Stream.intercalateSuffix Unfold.fromList "\n"
909948-- @
910949--
911950-- /Pre-release/
@@ -919,6 +958,7 @@ unlines = S.interposeSuffix '\n'
919958--
920959-- @
921960-- unwords = Stream.interpose ' '
961+-- unwords = Stream.intercalate Unfold.fromList " "
922962-- @
923963--
924964-- /Pre-release/