@@ -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-------------------------------------------------------------------------------