on master help sign in
Add encodeStrings, remove writeStrings

Fix some documentation and rename stripStart to stripHead. Implement encodeObject(s) for encodeStrings encodeObjects can be used to encode streams of any container type in general.

commit0cfc42394d56fffc1f4589c1b7aae44fff2b3c3d authorHarendra Kumar <harendra@composewell.com> dateThu 22 Apr 2021, 16:29 (5 years ago) parent99f676a
3 files changed+50−23
src/Streamly/Internal/Network/Socket.hs−14 src/Streamly/Internal/Unicode/Stream.hs+49−9 src/Streamly/Unicode/Stream.hs+1
src/Streamly/Internal/Network/Socket.hs−14
@@ -56,7 +56,6 @@ module Streamly.Internal.Network.Socket
5656 , writeChunk
5757 , writeChunks
5858 , writeChunksWithBufferOf
59− , writeStrings
6059
6160 -- reading/writing datagrams
6261 )
@@ -98,7 +97,6 @@ import Streamly.Data.Fold (Fold)
9897
9998import qualified Streamly.Data.Fold as FL
10099import qualified Streamly.Internal.Data.Unfold as UF
101−import qualified Streamly.Internal.Data.Array.Foreign as IA
102100import qualified Streamly.Data.Array.Foreign as A
103101import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
104102import qualified Streamly.Internal.Data.Array.Foreign.Type as A
@@ -471,18 +469,6 @@ writeChunksWithBufferOf :: (MonadIO m, Storable a)
471469 => Int -> Socket -> Fold m (Array a) ()
472470writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
473471
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−
486472-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
487473--
488474-- XXX test this
src/Streamly/Internal/Unicode/Stream.hs+49−9
@@ -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/
src/Streamly/Unicode/Stream.hs+1
@@ -86,6 +86,7 @@ module Streamly.Unicode.Stream
8686 , encodeLatin1'
8787 , encodeUtf8
8888 , encodeUtf8'
89+ , encodeStrings
8990 {-
9091 -- * Operations on character strings
9192 , strip -- (dropAround isSpace)