on master help sign in
Add char encoder-decoder
3736914Ranjeet Kumar Ranjan
core/src/Streamly/Internal/Serialize/FromBytes.hs+11
@@ -37,11 +37,13 @@ module Streamly.Internal.Serialize.FromBytes
37 , float32le
37 , float32le
38 , double64be
38 , double64be
39 , double64le
39 , double64le
40+ , charLatin1
40 )
41 )
41where
42where
42
43
43import Control.Monad.IO.Class (MonadIO)
44import Control.Monad.IO.Class (MonadIO)
44import Data.Bits ((.|.), unsafeShiftL)
45import Data.Bits ((.|.), unsafeShiftL)
46+import Data.Char (chr)
45import Data.Int (Int8, Int16, Int32, Int64)
47import Data.Int (Int8, Int16, Int32, Int64)
46import GHC.Float (castWord32ToFloat, castWord64ToDouble)
48import GHC.Float (castWord32ToFloat, castWord64ToDouble)
47import Data.Word (Word8, Word16, Word32, Word64)
49import Data.Word (Word8, Word16, Word32, Word64)
@@ -361,6 +363,15 @@ double64be = castWord64ToDouble <$> word64be
361{-# INLINE double64le #-}
363{-# INLINE double64le #-}
362double64le :: MonadIO m => Parser Word8 m Double
364double64le :: MonadIO m => Parser Word8 m Double
363double64le = castWord64ToDouble <$> word64le
365double64le = 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+
364-------------------------------------------------------------------------------
375-------------------------------------------------------------------------------
365-- Host byte order
376-- Host byte order
366-------------------------------------------------------------------------------
377-------------------------------------------------------------------------------
core/src/Streamly/Internal/Serialize/ToBytes.hs+15−2
@@ -36,22 +36,25 @@ module Streamly.Internal.Serialize.ToBytes
36 , float32le
36 , float32le
37 , double64be
37 , double64be
38 , double64le
38 , double64le
39+ , charLatin1
40+ , charUtf8
39 )
41 )
40where
42where
41
43
42#include "MachDeps.h"
44#include "MachDeps.h"
43
45
44import Data.Bits (shiftR)
46import Data.Bits (shiftR)
47+import Data.Char (ord)
48+import Data.Int (Int8, Int16, Int32, Int64)
45import Data.Word (Word8, Word16, Word32, Word64)
49import Data.Word (Word8, Word16, Word32, Word64)
46import GHC.Float (castDoubleToWord64, castFloatToWord32)
50import GHC.Float (castDoubleToWord64, castFloatToWord32)
47import Streamly.Internal.Data.Stream.StreamD (Stream)
51import Streamly.Internal.Data.Stream.StreamD (Stream)
48import Streamly.Internal.Data.Stream.StreamD (Step(..))
52import Streamly.Internal.Data.Stream.StreamD (Step(..))
53+import Streamly.Internal.Unicode.Stream (readCharUtf8)
49
54
50import qualified Streamly.Internal.Data.Stream.StreamD as Stream
55import qualified Streamly.Internal.Data.Stream.StreamD as Stream
51import qualified Streamly.Internal.Data.Stream.StreamD as D
56import qualified Streamly.Internal.Data.Stream.StreamD as D
52
57
53−import Data.Int (Int8, Int16, Int32, Int64)
54−
55-- XXX Use StreamD directly?
58-- XXX Use StreamD directly?
56
59
57-- | A value of type '()' is encoded as @0@ in binary encoding.
60-- | A value of type '()' is encoded as @0@ in binary encoding.
@@ -335,6 +338,16 @@ double64be = word64beD . castDoubleToWord64
335double64le :: Monad m => Double -> Stream m Word8
338double64le :: Monad m => Double -> Stream m Word8
336double64le = word64leD . castDoubleToWord64
339double64le = word64leD . castDoubleToWord64
337
340
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+
338-------------------------------------------------------------------------------
351-------------------------------------------------------------------------------
339-- Host byte order
352-- Host byte order
340-------------------------------------------------------------------------------
353-------------------------------------------------------------------------------
core/src/Streamly/Internal/Unicode/Stream.hs+2
@@ -42,6 +42,8 @@ module Streamly.Internal.Unicode.Stream
42
42
43 -- ** UTF-8 Encoding
43 -- ** UTF-8 Encoding
44 , readCharUtf8'
44 , readCharUtf8'
45+ , readCharUtf8
46+ , readCharUtf8_
45 , encodeUtf8
47 , encodeUtf8
46 , encodeUtf8'
48 , encodeUtf8'
47 , encodeUtf8_
49 , encodeUtf8_