@@ -34,6 +34,7 @@ module Streamly.Internal.Unicode.Stream
34 , encodeUtf8
34 , encodeUtf8
35 , encodeUtf8'
35 , encodeUtf8'
36 , encodeUtf8_
36 , encodeUtf8_
38 -- * Operations on character strings
39 -- * Operations on character strings
39 , strip -- (dropAround isSpace)
40 , strip -- (dropAround isSpace)
@@ -54,7 +55,7 @@ module Streamly.Internal.Unicode.Stream
54 , decodeUtf8ArraysD_
55 , decodeUtf8ArraysD_
56 -- * Transformation
57 -- * Transformation
57− , stripStart
58+ , stripHead
@@ -85,18 +86,22 @@ import Streamly.Data.Array.Foreign (Array)
85import Streamly.Internal.Data.Unfold (Unfold)
86import Streamly.Internal.Data.Unfold (Unfold)
86import Streamly.Internal.Data.SVar (adaptState)
87import Streamly.Internal.Data.SVar (adaptState)
87import Streamly.Internal.Data.Stream.IsStream (IsStream)
88import Streamly.Internal.Data.Stream.IsStream (IsStream)
89+import Streamly.Internal.Data.Stream.Serial (SerialT)
88import Streamly.Internal.Data.Stream.StreamD (Stream(..), Step (..))
90import Streamly.Internal.Data.Stream.StreamD (Stream(..), Step (..))
89import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
91import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
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
91import qualified Streamly.Internal.Data.Array.Foreign.Type as A
96import qualified Streamly.Internal.Data.Array.Foreign.Type as A
92import qualified Streamly.Internal.Data.Stream.IsStream as S
97import qualified Streamly.Internal.Data.Stream.IsStream as S
93import qualified Streamly.Internal.Data.Stream.StreamD as D
98import qualified Streamly.Internal.Data.Stream.StreamD as D
95−import Prelude hiding (String, lines, words, unlines, unwords)
100+import Prelude hiding (lines, words, unlines, unwords)
99−-- >>> import Prelude hiding (String, lines, words, unlines, unwords)
104+-- >>> import Prelude hiding (lines, words, unlines, unwords)
100-- >>> import qualified Streamly.Prelude as Stream
105-- >>> import qualified Streamly.Prelude as Stream
101-- >>> import qualified Streamly.Data.Fold as Fold
106-- >>> import qualified Streamly.Data.Fold as Fold
102-- >>> import Streamly.Internal.Unicode.Stream
107-- >>> import Streamly.Internal.Unicode.Stream
@@ -842,6 +847,39 @@ encodeUtf8_ = D.fromStreamD . encodeUtf8D_ . D.toStreamD
842encodeUtf8Lax :: (IsStream t, Monad m) => t m Char -> t m Word8
847encodeUtf8Lax :: (IsStream t, Monad m) => t m Char -> t m Word8
843encodeUtf8Lax = encodeUtf8
848encodeUtf8Lax = encodeUtf8
850+-- | Encode a container to @Array Word8@ provided an unfold to covert it to a
851+-- Char stream and an encoding function.
854+{-# INLINE encodeObject #-}
855+encodeObject :: MonadIO m =>
856+ (SerialT m Char -> SerialT m Word8)
860+encodeObject encode u = S.fold Array.write . encode . S.unfold u
862+-- | Encode a stream of container objects using the supplied encoding scheme.
863+-- Each object is encoded as an @Array Word8@.
866+{-# INLINE encodeObjects #-}
867+encodeObjects :: (MonadIO m, IsStream t) =>
868+ (SerialT m Char -> SerialT m Word8)
871+ -> t m (Array Word8)
872+encodeObjects encode u = Serial.mapM (encodeObject encode u)
874+-- | Encode a stream of 'String' using the supplied encoding scheme. Each
875+-- string is encoded as an @Array Word8@.
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
846-------------------------------------------------------------------------------
884-------------------------------------------------------------------------------
847-- Utility operations on strings
885-- Utility operations on strings
@@ -850,18 +888,18 @@ encodeUtf8Lax = encodeUtf8
850strip :: IsStream t => t m Char -> t m Char
888strip :: IsStream t => t m Char -> t m Char
851strip = undefined
889strip = undefined
853−stripEnd :: IsStream t => t m Char -> t m Char
891+stripTail :: IsStream t => t m Char -> t m Char
854−stripEnd = undefined
892+stripTail = undefined
857-- | Remove leading whitespace from a string.
895-- | Remove leading whitespace from a string.
859−-- > stripStart = S.dropWhile isSpace
897+-- > stripHead = S.dropWhile isSpace
861-- /Pre-release/
899-- /Pre-release/
862−{-# INLINE stripStart #-}
900+{-# INLINE stripHead #-}
863−stripStart :: (Monad m, IsStream t) => t m Char -> t m Char
901+stripHead :: (Monad m, IsStream t) => t m Char -> t m Char
864−stripStart = S.dropWhile isSpace
902+stripHead = S.dropWhile isSpace
866-- | Fold each line of the stream using the supplied 'Fold'
904-- | Fold each line of the stream using the supplied 'Fold'
867-- and stream the result.
905-- and stream the result.
@@ -906,6 +944,7 @@ words = S.wordsBy isSpace
908-- unlines = Stream.interposeSuffix '\n'
946-- unlines = Stream.interposeSuffix '\n'
947+-- unlines = Stream.intercalateSuffix Unfold.fromList "\n"
911-- /Pre-release/
950-- /Pre-release/
@@ -919,6 +958,7 @@ unlines = S.interposeSuffix '\n'
921-- unwords = Stream.interpose ' '
960-- unwords = Stream.interpose ' '
961+-- unwords = Stream.intercalate Unfold.fromList " "
924-- /Pre-release/
964-- /Pre-release/