on master help sign in
Add char encoder-decoder
commit373691429d72def27be75a43fd61d56bef2dcf51 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateThu 22 Dec 2022, 17:19 (4 years ago) parent38dda06
3 files changed+28−2
core/src/Streamly/Internal/Serialize/FromBytes.hs+11 core/src/Streamly/Internal/Serialize/ToBytes.hs+15−2 core/src/Streamly/Internal/Unicode/Stream.hs+2
core/src/Streamly/Internal/Serialize/FromBytes.hs+11
@@ -37,11 +37,13 @@ module Streamly.Internal.Serialize.FromBytes
3737 , float32le
3838 , double64be
3939 , double64le
40+ , charLatin1
4041 )
4142where
4243
4344import Control.Monad.IO.Class (MonadIO)
4445import Data.Bits ((.|.), unsafeShiftL)
46+import Data.Char (chr)
4547import Data.Int (Int8, Int16, Int32, Int64)
4648import GHC.Float (castWord32ToFloat, castWord64ToDouble)
4749import Data.Word (Word8, Word16, Word32, Word64)
@@ -361,6 +363,15 @@ double64be = castWord64ToDouble <$> word64be
361363{-# INLINE double64le #-}
362364double64le :: MonadIO m => Parser Word8 m Double
363365double64le = castWord64ToDouble <$> word64le
366+
367+-- | Accept any byte.
368+--
369+-- /Pre-release/
370+--
371+{-# INLINE charLatin1 #-}
372+charLatin1 :: Monad m => Parser Word8 m Char
373+charLatin1 = fmap (chr . fromIntegral) word8
374+
364375-------------------------------------------------------------------------------
365376-- Host byte order
366377-------------------------------------------------------------------------------
core/src/Streamly/Internal/Serialize/ToBytes.hs+15−2
@@ -36,22 +36,25 @@ module Streamly.Internal.Serialize.ToBytes
3636 , float32le
3737 , double64be
3838 , double64le
39+ , charLatin1
40+ , charUtf8
3941 )
4042where
4143
4244#include "MachDeps.h"
4345
4446import Data.Bits (shiftR)
47+import Data.Char (ord)
48+import Data.Int (Int8, Int16, Int32, Int64)
4549import Data.Word (Word8, Word16, Word32, Word64)
4650import GHC.Float (castDoubleToWord64, castFloatToWord32)
4751import Streamly.Internal.Data.Stream.StreamD (Stream)
4852import Streamly.Internal.Data.Stream.StreamD (Step(..))
53+import Streamly.Internal.Unicode.Stream (readCharUtf8)
4954
5055import qualified Streamly.Internal.Data.Stream.StreamD as Stream
5156import qualified Streamly.Internal.Data.Stream.StreamD as D
5257
53−import Data.Int (Int8, Int16, Int32, Int64)
54−
5558-- XXX Use StreamD directly?
5659
5760-- | A value of type '()' is encoded as @0@ in binary encoding.
@@ -335,6 +338,16 @@ double64be = word64beD . castDoubleToWord64
335338double64le :: Monad m => Double -> Stream m Word8
336339double64le = word64leD . castDoubleToWord64
337340
341+-- | Encode a Unicode character to stream of bytes in 0-255 range.
342+--
343+{-# INLINE charLatin1 #-}
344+charLatin1 :: Applicative m => Char -> Stream m Word8
345+charLatin1 = Stream.fromPure . fromIntegral . ord
346+
347+{-# INLINE charUtf8 #-}
348+charUtf8 :: Monad m => Char -> Stream m Word8
349+charUtf8 = Stream.unfold readCharUtf8
350+
338351-------------------------------------------------------------------------------
339352-- Host byte order
340353-------------------------------------------------------------------------------
core/src/Streamly/Internal/Unicode/Stream.hs+2
@@ -42,6 +42,8 @@ module Streamly.Internal.Unicode.Stream
4242
4343 -- ** UTF-8 Encoding
4444 , readCharUtf8'
45+ , readCharUtf8
46+ , readCharUtf8_
4547 , encodeUtf8
4648 , encodeUtf8'
4749 , encodeUtf8_