on master help sign in
implement getBytes and writeStrings
8504b93Harendra Kumar
src/Streamly/Internal/FileSystem/Handle.hs+19−6
@@ -1,6 +1,7 @@
1{-# OPTIONS_HADDOCK hide #-}
1{-# OPTIONS_HADDOCK hide #-}
2{-# LANGUAGE CPP #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE BangPatterns #-}
4+{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE MagicHash #-}
5{-# LANGUAGE MagicHash #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE UnboxedTuples #-}
7{-# LANGUAGE UnboxedTuples #-}
@@ -27,6 +28,7 @@ module Streamly.Internal.FileSystem.Handle
27
28
28 , toBytes
29 , toBytes
29 , toBytesWithBufferOf
30 , toBytesWithBufferOf
31+ , getBytes
30
32
31 -- -- * Array Read
33 -- -- * Array Read
32 -- , readArrayUpto
34 -- , readArrayUpto
@@ -44,6 +46,7 @@ module Streamly.Internal.FileSystem.Handle
44 -- , writeUtf8
46 -- , writeUtf8
45 -- , writeUtf8ByLines
47 -- , writeUtf8ByLines
46 -- , writeByFrames
48 -- , writeByFrames
49+ -- , writeLines
47 , writeWithBufferOf
50 , writeWithBufferOf
48
51
49 -- Byte stream write (Streams)
52 -- Byte stream write (Streams)
@@ -60,6 +63,7 @@ module Streamly.Internal.FileSystem.Handle
60 , fromChunks
63 , fromChunks
61 , putChunks
64 , putChunks
62 , putStrings
65 , putStrings
66+ -- , putLines
63
67
64 -- -- * Random Access (Seek)
68 -- -- * Random Access (Seek)
65 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
69 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
@@ -111,6 +115,7 @@ import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
111import System.IO (Handle, hGetBufSome, hPutBuf, stdin, stdout)
115import System.IO (Handle, hGetBufSome, hPutBuf, stdin, stdout)
112import Prelude hiding (read)
116import Prelude hiding (read)
113
117
118+import Streamly (MonadAsync)
114import Streamly.Data.Fold (Fold)
119import Streamly.Data.Fold (Fold)
115import Streamly.Internal.Data.Unfold.Types (Unfold(..))
120import Streamly.Internal.Data.Unfold.Types (Unfold(..))
116import Streamly.Internal.Memory.Array.Types
121import Streamly.Internal.Memory.Array.Types
@@ -124,6 +129,7 @@ import qualified Streamly.Data.Fold as FL
124import qualified Streamly.Internal.Data.Fold.Types as FL
129import qualified Streamly.Internal.Data.Fold.Types as FL
125import qualified Streamly.Internal.Data.Unicode.Stream as U
130import qualified Streamly.Internal.Data.Unicode.Stream as U
126import qualified Streamly.Internal.Data.Unfold as UF
131import qualified Streamly.Internal.Data.Unfold as UF
132+import qualified Streamly.Internal.Memory.Array as IA
127import qualified Streamly.Internal.Memory.ArrayStream as AS
133import qualified Streamly.Internal.Memory.ArrayStream as AS
128import qualified Streamly.Internal.Prelude as S
134import qualified Streamly.Internal.Prelude as S
129import qualified Streamly.Memory.Array as A
135import qualified Streamly.Memory.Array as A
@@ -245,6 +251,16 @@ toChunks = toChunksWithBufferOf defaultChunkSize
245getChunks :: (IsStream t, MonadIO m) => t m (Array Word8)
251getChunks :: (IsStream t, MonadIO m) => t m (Array Word8)
246getChunks = toChunks stdin
252getChunks = toChunks stdin
247
253
254+-- | Read a stream of bytes from standard input.
255+--
256+-- > getBytes = toBytes stdin
257+--
258+-- /Internal/
259+--
260+{-# INLINE getBytes #-}
261+getBytes :: (IsStream t, MonadIO m) => t m Word8
262+getBytes = toBytes stdin
263+
248-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
264-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
249-- device are performed using a buffer of size
265-- device are performed using a buffer of size
250-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
266-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
@@ -344,16 +360,13 @@ fromChunks h m = S.mapM_ (liftIO . writeArray h) m
344putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
360putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
345putChunks = fromChunks stdout
361putChunks = fromChunks stdout
346
362
347−-- XXX this is currently buffered with 32K buffer. We need to use a timeout
348−-- based scheme instead.
349−--
350−-- | Write a stream of strings to standard output.
363+-- | Write a stream of strings to standard output using Latin1 encoding.
351--
364--
352-- /Internal/
365-- /Internal/
353--
366--
354{-# INLINE putStrings #-}
367{-# INLINE putStrings #-}
355−putStrings :: MonadIO m => SerialT m String -> m ()
368+putStrings :: MonadAsync m => SerialT m String -> m ()
356−putStrings = fromBytes stdout . U.encodeUtf8 . S.concatUnfold UF.fromList
369+putStrings = putChunks . S.mapM (IA.fromStream . U.encodeLatin1 . S.fromList)
357
370
358-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
371-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
359-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
372-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
src/Streamly/Internal/Network/Socket.hs+12
@@ -62,6 +62,7 @@ module Streamly.Internal.Network.Socket
62 -- -- * Array Write
62 -- -- * Array Write
63 , writeArray
63 , writeArray
64 , writeChunks
64 , writeChunks
65+ , writeStrings
65
66
66 -- reading/writing datagrams
67 -- reading/writing datagrams
67 )
68 )
@@ -100,8 +101,10 @@ import Streamly.Data.Fold (Fold)
100-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
101-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
101
102
102import qualified Streamly.Data.Fold as FL
103import qualified Streamly.Data.Fold as FL
104+import qualified Streamly.Data.Unicode.Stream as U
103import qualified Streamly.Internal.Data.Fold.Types as FL
105import qualified Streamly.Internal.Data.Fold.Types as FL
104import qualified Streamly.Internal.Data.Unfold as UF
106import qualified Streamly.Internal.Data.Unfold as UF
107+import qualified Streamly.Internal.Memory.Array as IA
105import qualified Streamly.Memory.Array as A
108import qualified Streamly.Memory.Array as A
106import qualified Streamly.Internal.Memory.ArrayStream as AS
109import qualified Streamly.Internal.Memory.ArrayStream as AS
107import qualified Streamly.Internal.Memory.Array.Types as A
110import qualified Streamly.Internal.Memory.Array.Types as A
@@ -415,6 +418,15 @@ fromChunks h m = S.mapM_ (liftIO . writeArray h) m
415writeChunks :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
418writeChunks :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
416writeChunks h = FL.drainBy (liftIO . writeArray h)
419writeChunks h = FL.drainBy (liftIO . writeArray h)
417
420
421+-- | Write a stream of strings to a socket in Latin1 encoding.
422+--
423+-- /Internal/
424+--
425+{-# INLINE writeStrings #-}
426+writeStrings :: MonadIO m => Socket -> Fold m String ()
427+writeStrings h =
428+ FL.lmapM (IA.fromStream . U.encodeLatin1 . S.fromList) (writeChunks h)
429+
418-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
430-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
419--
431--
420-- XXX test this
432-- XXX test this