on master help sign in
Add encodeStrings, remove writeStrings
0cfc423Harendra Kumar
src/Streamly/Internal/Network/Socket.hs−14
@@ -56,7 +56,6 @@ module Streamly.Internal.Network.Socket
56 , writeChunk
56 , writeChunk
57 , writeChunks
57 , writeChunks
58 , writeChunksWithBufferOf
58 , writeChunksWithBufferOf
59− , writeStrings
60
59
61 -- reading/writing datagrams
60 -- reading/writing datagrams
62 )
61 )
@@ -98,7 +97,6 @@ import Streamly.Data.Fold (Fold)
98
97
99import qualified Streamly.Data.Fold as FL
98import qualified Streamly.Data.Fold as FL
100import qualified Streamly.Internal.Data.Unfold as UF
99import qualified Streamly.Internal.Data.Unfold as UF
101−import qualified Streamly.Internal.Data.Array.Foreign as IA
102import qualified Streamly.Data.Array.Foreign as A
100import qualified Streamly.Data.Array.Foreign as A
103import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
101import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
104import qualified Streamly.Internal.Data.Array.Foreign.Type as A
102import qualified Streamly.Internal.Data.Array.Foreign.Type as A
@@ -471,18 +469,6 @@ writeChunksWithBufferOf :: (MonadIO m, Storable a)
471 => Int -> Socket -> Fold m (Array a) ()
469 => Int -> Socket -> Fold m (Array a) ()
472writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
470writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
473
471
474−
475−-- | Write a stream of strings to a socket in Latin1 encoding. Output is
476−-- flushed to the socket for each string.
477−--
478−-- /Pre-release/
479−--
480−{-# INLINE writeStrings #-}
481−writeStrings :: MonadIO m
482− => (SerialT m Char -> SerialT m Word8) -> Socket -> Fold m String ()
483−writeStrings encode h =
484− FL.lmapM (IA.fromStream . encode . S.fromList) (writeChunks h)
485−
486-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
472-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
487--
473--
488-- XXX test this
474-- XXX test this
src/Streamly/Internal/Unicode/Stream.hs+49−9
@@ -34,6 +34,7 @@ module Streamly.Internal.Unicode.Stream
34 , encodeUtf8
34 , encodeUtf8
35 , encodeUtf8'
35 , encodeUtf8'
36 , encodeUtf8_
36 , encodeUtf8_
37+ , encodeStrings
37 {-
38 {-
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_
55
56
56 -- * Transformation
57 -- * Transformation
57− , stripStart
58+ , stripHead
58 , lines
59 , lines
59 , words
60 , words
60 , unlines
61 , unlines
@@ -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'(..))
90
92
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
94
99
95−import Prelude hiding (String, lines, words, unlines, unwords)
100+import Prelude hiding (lines, words, unlines, unwords)
96
101
97-- $setup
102-- $setup
98-- >>> :m
103-- >>> :m
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
844
849
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+
845{-
883{-
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
852
890
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
855-}
893-}
856
894
857-- | Remove leading whitespace from a string.
895-- | Remove leading whitespace from a string.
858--
896--
859−-- > stripStart = S.dropWhile isSpace
897+-- > stripHead = S.dropWhile isSpace
860--
898--
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
865
903
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
906--
944--
907-- @
945-- @
908-- unlines = Stream.interposeSuffix '\n'
946-- unlines = Stream.interposeSuffix '\n'
947+-- unlines = Stream.intercalateSuffix Unfold.fromList "\n"
909-- @
948-- @
910--
949--
911-- /Pre-release/
950-- /Pre-release/
@@ -919,6 +958,7 @@ unlines = S.interposeSuffix '\n'
919--
958--
920-- @
959-- @
921-- unwords = Stream.interpose ' '
960-- unwords = Stream.interpose ' '
961+-- unwords = Stream.intercalate Unfold.fromList " "
922-- @
962-- @
923--
963--
924-- /Pre-release/
964-- /Pre-release/
src/Streamly/Unicode/Stream.hs+1
@@ -86,6 +86,7 @@ module Streamly.Unicode.Stream
86 , encodeLatin1'
86 , encodeLatin1'
87 , encodeUtf8
87 , encodeUtf8
88 , encodeUtf8'
88 , encodeUtf8'
89+ , encodeStrings
89 {-
90 {-
90 -- * Operations on character strings
91 -- * Operations on character strings
91 , strip -- (dropAround isSpace)
92 , strip -- (dropAround isSpace)