-- |
-- Module      : Streamly.Internal.Unicode.Stream
-- Copyright   : (c) 2018 Composewell Technologies
--               (c) Bjoern Hoehrmann 2008-2009
--
-- License     : BSD-3-Clause
-- Maintainer  : streamly@composewell.com
-- Stability   : experimental
-- Portability : GHC
--

module Streamly.Internal.Unicode.Stream
    (
    -- * Construction (Decoding)
      decodeLatin1

    -- ** UTF-8 Decoding
    , CodingFailureMode(..)
    , writeCharUtf8'
    , parseCharUtf8With
    , decodeUtf8
    , decodeUtf8'
    , decodeUtf8_

    -- ** Resumable UTF-8 Decoding
    , DecodeError(..)
    , DecodeState
    , CodePoint
    , decodeUtf8Either
    , resumeDecodeUtf8Either

    -- ** UTF-8 Array Stream Decoding
    , decodeUtf8Arrays
    , decodeUtf8Arrays'
    , decodeUtf8Arrays_

    -- * Elimination (Encoding)
    -- ** Latin1 Encoding
    , encodeLatin1
    , encodeLatin1'
    , encodeLatin1_

    -- ** UTF-8 Encoding
    , readCharUtf8'
    , encodeUtf8
    , encodeUtf8'
    , encodeUtf8_
    , encodeStrings
    {-
    -- * Operations on character strings
    , strip -- (dropAround isSpace)
    , stripEnd
    -}

    -- * Transformation
    , stripHead
    , lines
    , words
    , unlines
    , unwords

    -- * StreamD UTF8 Encoding / Decoding transformations.
    , decodeUtf8D
    , decodeUtf8D'
    , decodeUtf8D_
    , encodeUtf8D
    , encodeUtf8D'
    , encodeUtf8D_
    , decodeUtf8EitherD
    , resumeDecodeUtf8EitherD
    , decodeUtf8ArraysD
    , decodeUtf8ArraysD'
    , decodeUtf8ArraysD_

    -- * Deprecations
    , decodeUtf8Lax
    , encodeLatin1Lax
    , encodeUtf8Lax
    )
where

#include "inline.hs"

import Control.Monad.Catch (MonadThrow, MonadCatch)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Bits (shiftR, shiftL, (.|.), (.&.))
import Data.Char (chr, ord)
import Data.Word (Word8)
import Foreign.Storable (Storable(..))
import Fusion.Plugin.Types (Fuse(..))
import GHC.Base (assert, unsafeChr)
import GHC.IO.Encoding.Failure (isSurrogate)
import GHC.Ptr (Ptr (..), plusPtr)
import System.IO.Unsafe (unsafePerformIO)
import Streamly.Internal.Data.Array.Foreign (Array)
import Streamly.Internal.Data.Array.Foreign.Mut.Type (ArrayContents, touch)
import Streamly.Internal.Data.Fold (Fold)
import Streamly.Internal.Data.Stream.IsStream.Type
    (IsStream, fromStreamD, toStreamD, adapt)
import Streamly.Internal.Data.Stream.Serial (SerialT)
import Streamly.Internal.Data.Stream.StreamD (Stream(..), Step (..))
import Streamly.Internal.Data.SVar (adaptState)
import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import Streamly.Internal.System.IO (unsafeInlineIO)

import qualified Streamly.Internal.Data.Unfold as Unfold
import qualified Streamly.Internal.Data.Parser as Parser
import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
import qualified Streamly.Internal.Data.Parser.ParserK.Type as ParserK
import qualified Streamly.Internal.Data.Stream.Serial as Serial
import qualified Streamly.Internal.Data.Array.Foreign as Array
import qualified Streamly.Internal.Data.Array.Foreign.Type as A
import qualified Streamly.Internal.Data.Stream.IsStream as S
import qualified Streamly.Internal.Data.Stream.StreamD as D

import Prelude hiding (lines, words, unlines, unwords)

-- $setup
-- >>> :m
-- >>> import Prelude hiding (lines, words, unlines, unwords)
-- >>> import qualified Streamly.Prelude as Stream
-- >>> import qualified Streamly.Data.Fold as Fold
-- >>> import Streamly.Internal.Unicode.Stream

-------------------------------------------------------------------------------
-- Latin1 decoding
-------------------------------------------------------------------------------

-- | Decode a stream of bytes to Unicode characters by mapping each byte to a
-- corresponding Unicode 'Char' in 0-255 range.
--
-- /Since: 0.7.0 ("Streamly.Data.Unicode.Stream")/
--
-- @since 0.8.0
{-# INLINE decodeLatin1 #-}
decodeLatin1 :: (IsStream t, Monad m) => t m Word8 -> t m Char
decodeLatin1 :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m Word8 -> t m Char
decodeLatin1 = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
S.map (Int -> Char
unsafeChr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral)

-------------------------------------------------------------------------------
-- Latin1 encoding
-------------------------------------------------------------------------------

-- | Encode a stream of Unicode characters to bytes by mapping each character
-- to a byte in 0-255 range. Throws an error if the input stream contains
-- characters beyond 255.
--
-- @since 0.8.0
{-# INLINE encodeLatin1' #-}
encodeLatin1' :: (IsStream t, Monad m) => t m Char -> t m Word8
encodeLatin1' :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m Char -> t m Word8
encodeLatin1' = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
S.map forall {a}. Num a => Char -> a
convert
    where
    convert :: Char -> a
convert Char
c =
        let codepoint :: Int
codepoint = Char -> Int
ord Char
c
        in if Int
codepoint forall a. Ord a => a -> a -> Bool
> Int
255
           then forall a. (?callStack::CallStack) => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Streamly.Unicode.encodeLatin1 invalid " forall a. [a] -> [a] -> [a]
++
                      [Char]
"input char codepoint " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
codepoint
           else forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
codepoint

-- XXX Should we instead replace the invalid chars by NUL or whitespace or some
-- other control char? That may affect the perf a bit but may be a better
-- behavior.
--
-- | Like 'encodeLatin1'' but silently maps input codepoints beyond 255 to
-- arbitrary Latin1 chars in 0-255 range. No error or exception is thrown when
-- such mapping occurs.
--
-- /Since: 0.7.0 ("Streamly.Data.Unicode.Stream")/
--
-- /Since: 0.8.0 (Lenient Behaviour)/
{-# INLINE encodeLatin1 #-}
encodeLatin1 :: (IsStream t, Monad m) => t m Char -> t m Word8
encodeLatin1 :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m Char -> t m Word8
encodeLatin1 = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
S.map (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord)

-- | Like 'encodeLatin1' but drops the input characters beyond 255.
--
-- @since 0.8.0
{-# INLINE encodeLatin1_ #-}
encodeLatin1_ :: (IsStream t, Monad m) => t m Char -> t m Word8
encodeLatin1_ :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m Char -> t m Word8
encodeLatin1_ = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b) -> t m a -> t m b
S.map (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
(a -> Bool) -> t m a -> t m a
S.filter (forall a. Ord a => a -> a -> Bool
<= Int -> Char
chr Int
255)

-- | Same as 'encodeLatin1'
--
{-# DEPRECATED encodeLatin1Lax "Please use 'encodeLatin1' instead" #-}
{-# INLINE encodeLatin1Lax #-}
encodeLatin1Lax :: (IsStream t, Monad m) => t m Char -> t m Word8
encodeLatin1Lax :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m Char -> t m Word8
encodeLatin1Lax = forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m Char -> t m Word8
encodeLatin1

-------------------------------------------------------------------------------
-- UTF-8 decoding
-------------------------------------------------------------------------------

-- Int helps in cheaper conversion from Int to Char
type CodePoint = Int
type DecodeState = Word8

-- We can divide the errors in three general categories:
-- * A non-starter was encountered in a begin state
-- * A starter was encountered without completing a codepoint
-- * The last codepoint was not complete (input underflow)
--
-- Need to separate resumable and non-resumable error. In case of non-resumable
-- error we can also provide the failing byte. In case of resumable error the
-- state can be opaque.
--
data DecodeError = DecodeError !DecodeState !CodePoint deriving Int -> DecodeError -> ShowS
[DecodeError] -> ShowS
DecodeError -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [DecodeError] -> ShowS
$cshowList :: [DecodeError] -> ShowS
show :: DecodeError -> [Char]
$cshow :: DecodeError -> [Char]
showsPrec :: Int -> DecodeError -> ShowS
$cshowsPrec :: Int -> DecodeError -> ShowS
Show

-- See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.

-- XXX Use names decodeSuccess = 0, decodeFailure = 12

decodeTable :: [Word8]
decodeTable :: [Word8]
decodeTable = [
   -- The first part of the table maps bytes to character classes that
   -- to reduce the size of the transition table and create bitmasks.
   Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,  Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,
   Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,  Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,
   Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,  Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,
   Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,  Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,Word8
0,
   Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,Word8
1,  Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,Word8
9,
   Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,  Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,Word8
7,
   Word8
8,Word8
8,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,  Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,Word8
2,
  Word8
10,Word8
3,Word8
3,Word8
3,Word8
3,Word8
3,Word8
3,Word8
3,Word8
3,Word8
3,Word8
3,Word8
3,Word8
3,Word8
4,Word8
3,Word8
3, Word8
11,Word8
6,Word8
6,Word8
6,Word8
5,Word8
8,Word8
8,Word8
8,Word8
8,Word8
8,Word8
8,Word8
8,Word8
8,Word8
8,Word8
8,Word8
8,

   -- The second part is a transition table that maps a combination
   -- of a state of the automaton and a character class to a state.
   Word8
0,Word8
12,Word8
24,Word8
36,Word8
60,Word8
96,Word8
84,Word8
12,Word8
12,Word8
12,Word8
48,Word8
72, Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,
  Word8
12, Word8
0,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12, Word8
0,Word8
12, Word8
0,Word8
12,Word8
12, Word8
12,Word8
24,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
24,Word8
12,Word8
24,Word8
12,Word8
12,
  Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
24,Word8
12,Word8
12,Word8
12,Word8
12, Word8
12,Word8
24,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
24,Word8
12,Word8
12,
  Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
36,Word8
12,Word8
36,Word8
12,Word8
12, Word8
12,Word8
36,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
36,Word8
12,Word8
36,Word8
12,Word8
12,
  Word8
12,Word8
36,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12,Word8
12
  ]

{-# NOINLINE utf8d #-}
utf8d :: A.Array Word8
utf8d :: Array Word8
utf8d =
      forall a. IO a -> a
unsafePerformIO
    -- Aligning to cacheline makes a barely noticeable difference
    -- XXX currently alignment is not implemented for unmanaged allocation
    forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m b
D.fold (forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Int -> Fold m a (Array a)
A.writeNAlignedUnmanaged Int
64 (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Word8]
decodeTable))
              (forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [Word8]
decodeTable)

-- | Return element at the specified index without checking the bounds.
-- and without touching the foreign ptr.
{-# INLINE_NORMAL unsafePeekElemOff #-}
unsafePeekElemOff :: forall a. Storable a => Ptr a -> Int -> a
unsafePeekElemOff :: forall a. Storable a => Ptr a -> Int -> a
unsafePeekElemOff Ptr a
p Int
i = let !x :: a
x = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
p Int
i in a
x

-- decode is split into two separate cases to avoid branching instructions.
-- From the higher level flow we already know which case we are in so we can
-- call the appropriate decode function.
--
-- When the state is 0
{-# INLINE decode0 #-}
decode0 :: Ptr Word8 -> Word8 -> Tuple' DecodeState CodePoint
decode0 :: Ptr Word8 -> Word8 -> Tuple' Word8 Int
decode0 Ptr Word8
table Word8
byte =
    let !t :: Word8
t = Ptr Word8
table forall a. Storable a => Ptr a -> Int -> a
`unsafePeekElemOff` forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
byte
        !codep' :: Int
codep' = (Int
0xff forall a. Bits a => a -> Int -> a
`shiftR` forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
t) forall a. Bits a => a -> a -> a
.&. forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
byte
        !state' :: Word8
state' = Ptr Word8
table forall a. Storable a => Ptr a -> Int -> a
`unsafePeekElemOff` (Int
256 forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
t)
     in forall a. (?callStack::CallStack) => Bool -> a -> a
assert ((Word8
byte forall a. Ord a => a -> a -> Bool
> Word8
0x7f Bool -> Bool -> Bool
|| forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
showByte)
                Bool -> Bool -> Bool
&& (Word8
state' forall a. Eq a => a -> a -> Bool
/= Word8
0 Bool -> Bool -> Bool
|| forall a. (?callStack::CallStack) => [Char] -> a
error ([Char]
showByte forall a. [a] -> [a] -> [a]
++ [Char]
showTable)))
               (forall a b. a -> b -> Tuple' a b
Tuple' Word8
state' Int
codep')

    where

    utf8table :: Array Word8
utf8table =
        let end :: Ptr b
end = Ptr Word8
table forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
364
        in forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
A.Array forall a. (?callStack::CallStack) => a
undefined Ptr Word8
table forall {b}. Ptr b
end :: A.Array Word8
    showByte :: [Char]
showByte = [Char]
"Streamly: decode0: byte: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Word8
byte
    showTable :: [Char]
showTable = [Char]
" table: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Array Word8
utf8table

-- When the state is not 0
{-# INLINE decode1 #-}
decode1
    :: Ptr Word8
    -> DecodeState
    -> CodePoint
    -> Word8
    -> Tuple' DecodeState CodePoint
decode1 :: Ptr Word8 -> Word8 -> Int -> Word8 -> Tuple' Word8 Int
decode1 Ptr Word8
table Word8
state Int
codep Word8
byte =
    -- Remember codep is Int type!
    -- Can it be unsafe to convert the resulting Int to Char?
    let !t :: Word8
t = Ptr Word8
table forall a. Storable a => Ptr a -> Int -> a
`unsafePeekElemOff` forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
byte
        !codep' :: Int
codep' = (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
byte forall a. Bits a => a -> a -> a
.&. Int
0x3f) forall a. Bits a => a -> a -> a
.|. (Int
codep forall a. Bits a => a -> Int -> a
`shiftL` Int
6)
        !state' :: Word8
state' = Ptr Word8
table forall a. Storable a => Ptr a -> Int -> a
`unsafePeekElemOff`
                    (Int
256 forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
state forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
t)
     in forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
codep' forall a. Ord a => a -> a -> Bool
<= Int
0x10FFFF
                    Bool -> Bool -> Bool
|| forall a. (?callStack::CallStack) => [Char] -> a
error ([Char]
showByte forall a. [a] -> [a] -> [a]
++ forall {a} {a}. (Show a, Show a) => a -> a -> [Char]
showState Word8
state Int
codep))
               (forall a b. a -> b -> Tuple' a b
Tuple' Word8
state' Int
codep')
    where

    utf8table :: Array Word8
utf8table =
        let end :: Ptr b
end = Ptr Word8
table forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
364
        in forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
A.Array forall a. (?callStack::CallStack) => a
undefined Ptr Word8
table forall {b}. Ptr b
end :: A.Array Word8
    showByte :: [Char]
showByte = [Char]
"Streamly: decode1: byte: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Word8
byte
    showState :: a -> a -> [Char]
showState a
st a
cp =
        [Char]
" state: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show a
st forall a. [a] -> [a] -> [a]
++
        [Char]
" codepoint: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show a
cp forall a. [a] -> [a] -> [a]
++
        [Char]
" table: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Array Word8
utf8table

-------------------------------------------------------------------------------
-- Resumable UTF-8 decoding
-------------------------------------------------------------------------------

-- Strangely, GHCJS hangs linking template-haskell with this
#ifndef __GHCJS__
{-# ANN type UTF8DecodeState Fuse #-}
#endif
data UTF8DecodeState s a
    = UTF8DecodeInit s
    | UTF8DecodeInit1 s Word8
    | UTF8DecodeFirst s Word8
    | UTF8Decoding s !DecodeState !CodePoint
    | YieldAndContinue a (UTF8DecodeState s a)
    | Done

{-# INLINE_NORMAL resumeDecodeUtf8EitherD #-}
resumeDecodeUtf8EitherD
    :: Monad m
    => DecodeState
    -> CodePoint
    -> Stream m Word8
    -> Stream m (Either DecodeError Char)
resumeDecodeUtf8EitherD :: forall (m :: * -> *).
Monad m =>
Word8
-> Int -> Stream m Word8 -> Stream m (Either DecodeError Char)
resumeDecodeUtf8EitherD Word8
dst Int
codep (Stream State Stream m Word8 -> s -> m (Step s Word8)
step s
state) =
    let A.Array ArrayContents
_ Ptr Word8
p Ptr Word8
_ = Array Word8
utf8d
        !ptr :: Ptr Word8
ptr = Ptr Word8
p
        stt :: UTF8DecodeState s a
stt =
            if Word8
dst forall a. Eq a => a -> a -> Bool
== Word8
0
            then forall s a. s -> UTF8DecodeState s a
UTF8DecodeInit s
state
            else forall s a. s -> Word8 -> Int -> UTF8DecodeState s a
UTF8Decoding s
state Word8
dst Int
codep
    in forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
Stream (forall {m :: * -> *} {a}.
Ptr Word8
-> State Stream m a
-> UTF8DecodeState s (Either DecodeError Char)
-> m (Step
        (UTF8DecodeState s (Either DecodeError Char))
        (Either DecodeError Char))
step' Ptr Word8
ptr) forall {a}. UTF8DecodeState s a
stt
  where
    {-# INLINE_LATE step' #-}
    step' :: Ptr Word8
-> State Stream m a
-> UTF8DecodeState s (Either DecodeError Char)
-> m (Step
        (UTF8DecodeState s (Either DecodeError Char))
        (Either DecodeError Char))
step' Ptr Word8
_ State Stream m a
gst (UTF8DecodeInit s
st) = do
        Step s Word8
r <- State Stream m Word8 -> s -> m (Step s Word8)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s Word8
r of
            Yield Word8
x s
s -> forall s a. s -> Step s a
Skip (forall s a. s -> Word8 -> UTF8DecodeState s a
UTF8DecodeInit1 s
s Word8
x)
            Skip s
s -> forall s a. s -> Step s a
Skip (forall s a. s -> UTF8DecodeState s a
UTF8DecodeInit s
s)
            Step s Word8
Stop   -> forall s a. s -> Step s a
Skip forall s a. UTF8DecodeState s a
Done

    step' Ptr Word8
_ State Stream m a
_ (UTF8DecodeInit1 s
st Word8
x) = do
        -- Note: It is important to use a ">" instead of a "<=" test
        -- here for GHC to generate code layout for default branch
        -- prediction for the common case. This is fragile and might
        -- change with the compiler versions, we need a more reliable
        -- "likely" primitive to control branch predication.
        case Word8
x forall a. Ord a => a -> a -> Bool
> Word8
0x7f of
            Bool
False ->
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. a -> UTF8DecodeState s a -> UTF8DecodeState s a
YieldAndContinue
                    (forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Int -> Char
unsafeChr (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
x))
                    (forall s a. s -> UTF8DecodeState s a
UTF8DecodeInit s
st)
            -- Using a separate state here generates a jump to a
            -- separate code block in the core which seems to perform
            -- slightly better for the non-ascii case.
            Bool
True -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. s -> Word8 -> UTF8DecodeState s a
UTF8DecodeFirst s
st Word8
x

    -- XXX should we merge it with UTF8DecodeInit1?
    step' Ptr Word8
table State Stream m a
_ (UTF8DecodeFirst s
st Word8
x) = do
        let (Tuple' Word8
sv Int
cp) = Ptr Word8 -> Word8 -> Tuple' Word8 Int
decode0 Ptr Word8
table Word8
x
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
            case Word8
sv of
                Word8
12 ->
                    forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. a -> UTF8DecodeState s a -> UTF8DecodeState s a
YieldAndContinue (forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Word8 -> Int -> DecodeError
DecodeError Word8
0 (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
x))
                                            (forall s a. s -> UTF8DecodeState s a
UTF8DecodeInit s
st)
                Word8
0 -> forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"unreachable state"
                Word8
_ -> forall s a. s -> Step s a
Skip (forall s a. s -> Word8 -> Int -> UTF8DecodeState s a
UTF8Decoding s
st Word8
sv Int
cp)

    -- We recover by trying the new byte x a starter of a new codepoint.
    -- XXX on error need to report the next byte "x" as well.
    -- XXX need to use the same recovery in array decoding routine as well
    step' Ptr Word8
table State Stream m a
gst (UTF8Decoding s
st Word8
statePtr Int
codepointPtr) = do
        Step s Word8
r <- State Stream m Word8 -> s -> m (Step s Word8)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
        case Step s Word8
r of
            Yield Word8
x s
s -> do
                let (Tuple' Word8
sv Int
cp) = Ptr Word8 -> Word8 -> Int -> Word8 -> Tuple' Word8 Int
decode1 Ptr Word8
table Word8
statePtr Int
codepointPtr Word8
x
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
                    case Word8
sv of
                        Word8
0 -> forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. a -> UTF8DecodeState s a -> UTF8DecodeState s a
YieldAndContinue (forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Int -> Char
unsafeChr Int
cp)
                                        (forall s a. s -> UTF8DecodeState s a
UTF8DecodeInit s
s)
                        Word8
12 ->
                            forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. a -> UTF8DecodeState s a -> UTF8DecodeState s a
YieldAndContinue (forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Word8 -> Int -> DecodeError
DecodeError Word8
statePtr Int
codepointPtr)
                                        (forall s a. s -> Word8 -> UTF8DecodeState s a
UTF8DecodeInit1 s
s Word8
x)
                        Word8
_ -> forall s a. s -> Step s a
Skip (forall s a. s -> Word8 -> Int -> UTF8DecodeState s a
UTF8Decoding s
s Word8
sv Int
cp)
            Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall s a. s -> Word8 -> Int -> UTF8DecodeState s a
UTF8Decoding s
s Word8
statePtr Int
codepointPtr)
            Step s Word8
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. a -> UTF8DecodeState s a -> UTF8DecodeState s a
YieldAndContinue (forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Word8 -> Int -> DecodeError
DecodeError Word8
statePtr Int
codepointPtr) forall s a. UTF8DecodeState s a
Done

    step' Ptr Word8
_ State Stream m a
_ (YieldAndContinue Either DecodeError Char
c UTF8DecodeState s (Either DecodeError Char)
s) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield Either DecodeError Char
c UTF8DecodeState s (Either DecodeError Char)
s
    step' Ptr Word8
_ State Stream m a
_ UTF8DecodeState s (Either DecodeError Char)
Done = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop

-- XXX We can use just one API, and define InitState = 0 and InitCodePoint = 0
-- to use as starting state.
--
{-# INLINE_NORMAL decodeUtf8EitherD #-}
decodeUtf8EitherD :: Monad m
    => Stream m Word8 -> Stream m (Either DecodeError Char)
decodeUtf8EitherD :: forall (m :: * -> *).
Monad m =>
Stream m Word8 -> Stream m (Either DecodeError Char)
decodeUtf8EitherD = forall (m :: * -> *).
Monad m =>
Word8
-> Int -> Stream m Word8 -> Stream m (Either DecodeError Char)
resumeDecodeUtf8EitherD Word8
0 Int
0

-- |
--
-- /Pre-release/
{-# INLINE decodeUtf8Either #-}
decodeUtf8Either :: (Monad m, IsStream t)
    => t m Word8 -> t m (Either DecodeError Char)
decodeUtf8Either :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Word8 -> t m (Either DecodeError Char)
decodeUtf8Either = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
Monad m =>
Stream m Word8 -> Stream m (Either DecodeError Char)
decodeUtf8EitherD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

-- |
--
-- /Pre-release/
{-# INLINE resumeDecodeUtf8Either #-}
resumeDecodeUtf8Either
    :: (Monad m, IsStream t)
    => DecodeState
    -> CodePoint
    -> t m Word8
    -> t m (Either DecodeError Char)
resumeDecodeUtf8Either :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
Word8 -> Int -> t m Word8 -> t m (Either DecodeError Char)
resumeDecodeUtf8Either Word8
st Int
cp =
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
Monad m =>
Word8
-> Int -> Stream m Word8 -> Stream m (Either DecodeError Char)
resumeDecodeUtf8EitherD Word8
st Int
cp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

-------------------------------------------------------------------------------
-- One shot decoding
-------------------------------------------------------------------------------

data CodingFailureMode
    = TransliterateCodingFailure
    | ErrorOnCodingFailure
    | DropOnCodingFailure
    deriving (Int -> CodingFailureMode -> ShowS
[CodingFailureMode] -> ShowS
CodingFailureMode -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [CodingFailureMode] -> ShowS
$cshowList :: [CodingFailureMode] -> ShowS
show :: CodingFailureMode -> [Char]
$cshow :: CodingFailureMode -> [Char]
showsPrec :: Int -> CodingFailureMode -> ShowS
$cshowsPrec :: Int -> CodingFailureMode -> ShowS
Show)

{-# INLINE replacementChar #-}
replacementChar :: Char
replacementChar :: Char
replacementChar = Char
'\xFFFD'

data UTF8CharDecodeState a
    = UTF8CharDecodeInit
    | UTF8CharDecoding !DecodeState !CodePoint

{-# INLINE parseCharUtf8WithD #-}
parseCharUtf8WithD ::
       Monad m => CodingFailureMode -> ParserD.Parser m Word8 Char
parseCharUtf8WithD :: forall (m :: * -> *).
Monad m =>
CodingFailureMode -> Parser m Word8 Char
parseCharUtf8WithD CodingFailureMode
cfm =
    let A.Array ArrayContents
_ Ptr Word8
ptr Ptr Word8
_ = Array Word8
utf8d
    in forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Initial s b) -> (s -> m b) -> Parser m a b
ParserD.Parser (forall {m :: * -> *} {a} {a}.
Monad m =>
Ptr Word8
-> UTF8CharDecodeState a
-> Word8
-> m (Step (UTF8CharDecodeState a) Char)
step' Ptr Word8
ptr) forall {a} {b}. m (Initial (UTF8CharDecodeState a) b)
initial forall {p} {a}. p -> a
extract

    where

    prefix :: [Char]
prefix = [Char]
"Streamly.Internal.Data.Stream.parseCharUtf8WithD:"

    {-# INLINE initial #-}
    initial :: m (Initial (UTF8CharDecodeState a) b)
initial = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. s -> Initial s b
ParserD.IPartial forall a. UTF8CharDecodeState a
UTF8CharDecodeInit

    handleError :: [Char] -> Bool -> Step (UTF8CharDecodeState a) Char
handleError [Char]
err Bool
souldBackTrack =
        case CodingFailureMode
cfm of
            CodingFailureMode
ErrorOnCodingFailure -> forall s b. [Char] -> Step s b
ParserD.Error [Char]
err
            CodingFailureMode
TransliterateCodingFailure ->
                case Bool
souldBackTrack of
                    Bool
True -> forall s b. Int -> b -> Step s b
ParserD.Done Int
1 Char
replacementChar
                    Bool
False -> forall s b. Int -> b -> Step s b
ParserD.Done Int
0 Char
replacementChar
            CodingFailureMode
DropOnCodingFailure ->
                case Bool
souldBackTrack of
                    Bool
True -> forall s b. Int -> s -> Step s b
ParserD.Continue Int
1 forall a. UTF8CharDecodeState a
UTF8CharDecodeInit
                    Bool
False -> forall s b. Int -> s -> Step s b
ParserD.Continue Int
0 forall a. UTF8CharDecodeState a
UTF8CharDecodeInit

    {-# INLINE step' #-}
    step' :: Ptr Word8
-> UTF8CharDecodeState a
-> Word8
-> m (Step (UTF8CharDecodeState a) Char)
step' Ptr Word8
table UTF8CharDecodeState a
UTF8CharDecodeInit Word8
x =
        -- Note: It is important to use a ">" instead of a "<=" test
        -- here for GHC to generate code layout for default branch
        -- prediction for the common case. This is fragile and might
        -- change with the compiler versions, we need a more reliable
        -- "likely" primitive to control branch predication.
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Word8
x forall a. Ord a => a -> a -> Bool
> Word8
0x7f of
            Bool
False -> forall s b. Int -> b -> Step s b
ParserD.Done Int
0 forall a b. (a -> b) -> a -> b
$ Int -> Char
unsafeChr forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
x
            Bool
True ->
                let (Tuple' Word8
sv Int
cp) = Ptr Word8 -> Word8 -> Tuple' Word8 Int
decode0 Ptr Word8
table Word8
x
                 in case Word8
sv of
                        Word8
12 ->
                            let msg :: [Char]
msg = [Char]
prefix
                                    forall a. [a] -> [a] -> [a]
++ [Char]
"Invalid first UTF8 byte" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Word8
x
                             in forall {a}. [Char] -> Bool -> Step (UTF8CharDecodeState a) Char
handleError [Char]
msg Bool
False
                        Word8
0 -> forall a. (?callStack::CallStack) => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
prefix forall a. [a] -> [a] -> [a]
++ [Char]
"unreachable state"
                        Word8
_ -> forall s b. Int -> s -> Step s b
ParserD.Continue Int
0 (forall a. Word8 -> Int -> UTF8CharDecodeState a
UTF8CharDecoding Word8
sv Int
cp)

    step' Ptr Word8
table (UTF8CharDecoding Word8
statePtr Int
codepointPtr) Word8
x = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        let (Tuple' Word8
sv Int
cp) = Ptr Word8 -> Word8 -> Int -> Word8 -> Tuple' Word8 Int
decode1 Ptr Word8
table Word8
statePtr Int
codepointPtr Word8
x
         in case Word8
sv of
            Word8
0 -> forall s b. Int -> b -> Step s b
ParserD.Done Int
0 forall a b. (a -> b) -> a -> b
$ Int -> Char
unsafeChr Int
cp
            Word8
12 ->
                let msg :: [Char]
msg = [Char]
prefix
                        forall a. [a] -> [a] -> [a]
++ [Char]
"Invalid subsequent UTF8 byte"
                        forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Word8
x
                        forall a. [a] -> [a] -> [a]
++ [Char]
"in state"
                        forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Word8
statePtr
                        forall a. [a] -> [a] -> [a]
++ [Char]
"accumulated value"
                        forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
codepointPtr
                 in forall {a}. [Char] -> Bool -> Step (UTF8CharDecodeState a) Char
handleError [Char]
msg Bool
True
            Word8
_ -> forall s b. Int -> s -> Step s b
ParserD.Continue Int
0 (forall a. Word8 -> Int -> UTF8CharDecodeState a
UTF8CharDecoding Word8
sv Int
cp)

    {-# INLINE extract #-}
    extract :: p -> a
extract p
_ = forall a. (?callStack::CallStack) => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
prefix forall a. [a] -> [a] -> [a]
++ [Char]
"Not enough input"

-- XXX This should ideally accept a "CodingFailureMode" and perform appropriate
-- error handling. This isn't possible now as "TransliterateCodingFailure"'s
-- workflow requires backtracking 1 element. This can be revisited once "Fold"
-- supports backtracking.
{-# INLINE writeCharUtf8' #-}
writeCharUtf8' :: MonadThrow m => Fold m Word8 Char
writeCharUtf8' :: forall (m :: * -> *). MonadThrow m => Fold m Word8 Char
writeCharUtf8' =  forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Fold m a b
ParserD.toFold (forall (m :: * -> *).
Monad m =>
CodingFailureMode -> Parser m Word8 Char
parseCharUtf8WithD CodingFailureMode
ErrorOnCodingFailure)

-- XXX The initial idea was to have "parseCharUtf8" and offload the error
-- handling to another parser. So, say we had "parseCharUtf8'",
--
-- >>> parseCharUtf8Smart = parseCharUtf8' <|> Parser.fromPure replacementChar
--
-- But unfortunately parseCharUtf8Smart used in conjunction with "parseMany" -
-- that is "parseMany parseCharUtf8Smart" on a stream causes the heap to
-- overflow. Even a heap size of 500 MB was not sufficient.
--
-- This needs to be investigated futher.
{-# INLINE parseCharUtf8With #-}
parseCharUtf8With ::
       MonadCatch m => CodingFailureMode -> Parser.Parser m Word8 Char
parseCharUtf8With :: forall (m :: * -> *).
MonadCatch m =>
CodingFailureMode -> Parser m Word8 Char
parseCharUtf8With = forall (m :: * -> *) a b.
MonadCatch m =>
Parser m a b -> Parser m a b
ParserK.toParserK forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
Monad m =>
CodingFailureMode -> Parser m Word8 Char
parseCharUtf8WithD

-- XXX write it as a parser and use parseMany to decode a stream, need to check
-- if that preserves the same performance. Or we can use a resumable parser
-- that parses a chunk at a time.
--
-- XXX Implement this in terms of decodeUtf8Either. Need to make sure that
-- decodeUtf8Either preserves the performance characterstics.
--
{-# INLINE_NORMAL decodeUtf8WithD #-}
decodeUtf8WithD :: Monad m
    => CodingFailureMode -> Stream m Word8 -> Stream m Char
decodeUtf8WithD :: forall (m :: * -> *).
Monad m =>
CodingFailureMode -> Stream m Word8 -> Stream m Char
decodeUtf8WithD CodingFailureMode
cfm (Stream State Stream m Word8 -> s -> m (Step s Word8)
step s
state) =
    let A.Array ArrayContents
_ Ptr Word8
ptr Ptr Word8
_ = Array Word8
utf8d
    in forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
Stream (forall {m :: * -> *} {a}.
Ptr Word8
-> State Stream m a
-> UTF8DecodeState s Char
-> m (Step (UTF8DecodeState s Char) Char)
step' Ptr Word8
ptr) (forall s a. s -> UTF8DecodeState s a
UTF8DecodeInit s
state)

    where

    prefix :: [Char]
prefix = [Char]
"Streamly.Internal.Data.Stream.StreamD.decodeUtf8With: "

    {-# INLINE handleError #-}
    handleError :: [Char] -> UTF8DecodeState s Char -> UTF8DecodeState s Char
handleError [Char]
e UTF8DecodeState s Char
s =
        case CodingFailureMode
cfm of
            CodingFailureMode
ErrorOnCodingFailure -> forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
e
            CodingFailureMode
TransliterateCodingFailure -> forall s a. a -> UTF8DecodeState s a -> UTF8DecodeState s a
YieldAndContinue Char
replacementChar UTF8DecodeState s Char
s
            CodingFailureMode
DropOnCodingFailure -> UTF8DecodeState s Char
s

    {-# INLINE handleUnderflow #-}
    handleUnderflow :: UTF8DecodeState s Char
handleUnderflow =
        case CodingFailureMode
cfm of
            CodingFailureMode
ErrorOnCodingFailure -> forall a. (?callStack::CallStack) => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
prefix forall a. [a] -> [a] -> [a]
++ [Char]
"Not enough input"
            CodingFailureMode
TransliterateCodingFailure -> forall s a. a -> UTF8DecodeState s a -> UTF8DecodeState s a
YieldAndContinue Char
replacementChar forall s a. UTF8DecodeState s a
Done
            CodingFailureMode
DropOnCodingFailure -> forall s a. UTF8DecodeState s a
Done

    {-# INLINE_LATE step' #-}
    step' :: Ptr Word8
-> State Stream m a
-> UTF8DecodeState s Char
-> m (Step (UTF8DecodeState s Char) Char)
step' Ptr Word8
_ State Stream m a
gst (UTF8DecodeInit s
st) = do
        Step s Word8
r <- State Stream m Word8 -> s -> m (Step s Word8)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s Word8
r of
            Yield Word8
x s
s -> forall s a. s -> Step s a
Skip (forall s a. s -> Word8 -> UTF8DecodeState s a
UTF8DecodeInit1 s
s Word8
x)
            Skip s
s -> forall s a. s -> Step s a
Skip (forall s a. s -> UTF8DecodeState s a
UTF8DecodeInit s
s)
            Step s Word8
Stop   -> forall s a. s -> Step s a
Skip forall s a. UTF8DecodeState s a
Done

    step' Ptr Word8
_ State Stream m a
_ (UTF8DecodeInit1 s
st Word8
x) = do
        -- Note: It is important to use a ">" instead of a "<=" test
        -- here for GHC to generate code layout for default branch
        -- prediction for the common case. This is fragile and might
        -- change with the compiler versions, we need a more reliable
        -- "likely" primitive to control branch predication.
        case Word8
x forall a. Ord a => a -> a -> Bool
> Word8
0x7f of
            Bool
False ->
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. a -> UTF8DecodeState s a -> UTF8DecodeState s a
YieldAndContinue
                    (Int -> Char
unsafeChr (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
x))
                    (forall s a. s -> UTF8DecodeState s a
UTF8DecodeInit s
st)
            -- Using a separate state here generates a jump to a
            -- separate code block in the core which seems to perform
            -- slightly better for the non-ascii case.
            Bool
True -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. s -> Word8 -> UTF8DecodeState s a
UTF8DecodeFirst s
st Word8
x

    -- XXX should we merge it with UTF8DecodeInit1?
    step' Ptr Word8
table State Stream m a
_ (UTF8DecodeFirst s
st Word8
x) = do
        let (Tuple' Word8
sv Int
cp) = Ptr Word8 -> Word8 -> Tuple' Word8 Int
decode0 Ptr Word8
table Word8
x
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
            case Word8
sv of
                Word8
12 ->
                    let msg :: [Char]
msg = [Char]
prefix forall a. [a] -> [a] -> [a]
++ [Char]
"Invalid first UTF8 byte " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Word8
x
                     in forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall {s}.
[Char] -> UTF8DecodeState s Char -> UTF8DecodeState s Char
handleError [Char]
msg (forall s a. s -> UTF8DecodeState s a
UTF8DecodeInit s
st)
                Word8
0 -> forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"unreachable state"
                Word8
_ -> forall s a. s -> Step s a
Skip (forall s a. s -> Word8 -> Int -> UTF8DecodeState s a
UTF8Decoding s
st Word8
sv Int
cp)

    -- We recover by trying the new byte x as a starter of a new codepoint.
    -- XXX need to use the same recovery in array decoding routine as well
    step' Ptr Word8
table State Stream m a
gst (UTF8Decoding s
st Word8
statePtr Int
codepointPtr) = do
        Step s Word8
r <- State Stream m Word8 -> s -> m (Step s Word8)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
        case Step s Word8
r of
            Yield Word8
x s
s -> do
                let (Tuple' Word8
sv Int
cp) = Ptr Word8 -> Word8 -> Int -> Word8 -> Tuple' Word8 Int
decode1 Ptr Word8
table Word8
statePtr Int
codepointPtr Word8
x
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Word8
sv of
                    Word8
0 -> forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. a -> UTF8DecodeState s a -> UTF8DecodeState s a
YieldAndContinue
                            (Int -> Char
unsafeChr Int
cp) (forall s a. s -> UTF8DecodeState s a
UTF8DecodeInit s
s)
                    Word8
12 ->
                        let msg :: [Char]
msg = [Char]
prefix
                                forall a. [a] -> [a] -> [a]
++ [Char]
"Invalid subsequent UTF8 byte "
                                forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Word8
x
                                forall a. [a] -> [a] -> [a]
++ [Char]
" in state "
                                forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Word8
statePtr
                                forall a. [a] -> [a] -> [a]
++ [Char]
" accumulated value "
                                forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
codepointPtr
                         in forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall {s}.
[Char] -> UTF8DecodeState s Char -> UTF8DecodeState s Char
handleError [Char]
msg (forall s a. s -> Word8 -> UTF8DecodeState s a
UTF8DecodeInit1 s
s Word8
x)
                    Word8
_ -> forall s a. s -> Step s a
Skip (forall s a. s -> Word8 -> Int -> UTF8DecodeState s a
UTF8Decoding s
s Word8
sv Int
cp)
            Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
                forall s a. s -> Step s a
Skip (forall s a. s -> Word8 -> Int -> UTF8DecodeState s a
UTF8Decoding s
s Word8
statePtr Int
codepointPtr)
            Step s Word8
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall {s}. UTF8DecodeState s Char
handleUnderflow

    step' Ptr Word8
_ State Stream m a
_ (YieldAndContinue Char
c UTF8DecodeState s Char
s) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield Char
c UTF8DecodeState s Char
s
    step' Ptr Word8
_ State Stream m a
_ UTF8DecodeState s Char
Done = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop

{-# INLINE decodeUtf8D #-}
decodeUtf8D :: Monad m => Stream m Word8 -> Stream m Char
decodeUtf8D :: forall (m :: * -> *). Monad m => Stream m Word8 -> Stream m Char
decodeUtf8D = forall (m :: * -> *).
Monad m =>
CodingFailureMode -> Stream m Word8 -> Stream m Char
decodeUtf8WithD CodingFailureMode
TransliterateCodingFailure

-- | Decode a UTF-8 encoded bytestream to a stream of Unicode characters.
-- Any invalid codepoint encountered is replaced with the unicode replacement
-- character.
--
-- /Since: 0.7.0 ("Streamly.Data.Unicode.Stream")/
--
-- /Since: 0.8.0 (Lenient Behaviour)/
{-# INLINE decodeUtf8 #-}
decodeUtf8 :: (Monad m, IsStream t) => t m Word8 -> t m Char
decodeUtf8 :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Word8 -> t m Char
decodeUtf8 = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). Monad m => Stream m Word8 -> Stream m Char
decodeUtf8D forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

{-# INLINE decodeUtf8D' #-}
decodeUtf8D' :: Monad m => Stream m Word8 -> Stream m Char
decodeUtf8D' :: forall (m :: * -> *). Monad m => Stream m Word8 -> Stream m Char
decodeUtf8D' = forall (m :: * -> *).
Monad m =>
CodingFailureMode -> Stream m Word8 -> Stream m Char
decodeUtf8WithD CodingFailureMode
ErrorOnCodingFailure

-- | Decode a UTF-8 encoded bytestream to a stream of Unicode characters.
-- The function throws an error if an invalid codepoint is encountered.
--
-- @since 0.8.0
{-# INLINE decodeUtf8' #-}
decodeUtf8' :: (Monad m, IsStream t) => t m Word8 -> t m Char
decodeUtf8' :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Word8 -> t m Char
decodeUtf8' = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). Monad m => Stream m Word8 -> Stream m Char
decodeUtf8D' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

{-# INLINE decodeUtf8D_ #-}
decodeUtf8D_ :: Monad m => Stream m Word8 -> Stream m Char
decodeUtf8D_ :: forall (m :: * -> *). Monad m => Stream m Word8 -> Stream m Char
decodeUtf8D_ = forall (m :: * -> *).
Monad m =>
CodingFailureMode -> Stream m Word8 -> Stream m Char
decodeUtf8WithD CodingFailureMode
DropOnCodingFailure

-- | Decode a UTF-8 encoded bytestream to a stream of Unicode characters.
-- Any invalid codepoint encountered is dropped.
--
-- @since 0.8.0
{-# INLINE decodeUtf8_ #-}
decodeUtf8_ :: (Monad m, IsStream t) => t m Word8 -> t m Char
decodeUtf8_ :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Word8 -> t m Char
decodeUtf8_ = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). Monad m => Stream m Word8 -> Stream m Char
decodeUtf8D_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

-- | Same as 'decodeUtf8'
--
{-# DEPRECATED decodeUtf8Lax "Please use 'decodeUtf8' instead" #-}
{-# INLINE decodeUtf8Lax #-}
decodeUtf8Lax :: (IsStream t, Monad m) => t m Word8 -> t m Char
decodeUtf8Lax :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m Word8 -> t m Char
decodeUtf8Lax = forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Word8 -> t m Char
decodeUtf8

-------------------------------------------------------------------------------
-- Decoding Array Streams
-------------------------------------------------------------------------------

#ifndef __GHCJS__
{-# ANN type FlattenState Fuse #-}
#endif
data FlattenState s a
    = OuterLoop s !(Maybe (DecodeState, CodePoint))
    | InnerLoopDecodeInit s ArrayContents !(Ptr a) !(Ptr a)
    | InnerLoopDecodeFirst s ArrayContents !(Ptr a) !(Ptr a) Word8
    | InnerLoopDecoding s ArrayContents !(Ptr a) !(Ptr a)
        !DecodeState !CodePoint
    | YAndC !Char (FlattenState s a) -- These constructors can be
                                     -- encoded in the UTF8DecodeState
                                     -- type, I prefer to keep these
                                     -- flat even though that means
                                     -- coming up with new names
    | D

-- The normal decodeUtf8 above should fuse with flattenArrays
-- to create this exact code but it doesn't for some reason, as of now this
-- remains the fastest way I could figure out to decodeUtf8.
--
-- XXX Add Proper error messages
{-# INLINE_NORMAL decodeUtf8ArraysWithD #-}
decodeUtf8ArraysWithD ::
       MonadIO m
    => CodingFailureMode
    -> Stream m (A.Array Word8)
    -> Stream m Char
decodeUtf8ArraysWithD :: forall (m :: * -> *).
MonadIO m =>
CodingFailureMode -> Stream m (Array Word8) -> Stream m Char
decodeUtf8ArraysWithD CodingFailureMode
cfm (Stream State Stream m (Array Word8) -> s -> m (Step s (Array Word8))
step s
state) =
    let A.Array ArrayContents
_ Ptr Word8
ptr Ptr Word8
_ = Array Word8
utf8d
    in forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
Stream (forall {m :: * -> *} {a}.
Ptr Word8
-> State Stream m a
-> FlattenState s Word8
-> m (Step (FlattenState s Word8) Char)
step' Ptr Word8
ptr) (forall s a. s -> Maybe (Word8, Int) -> FlattenState s a
OuterLoop s
state forall a. Maybe a
Nothing)
  where
    {-# INLINE transliterateOrError #-}
    transliterateOrError :: [Char] -> FlattenState s a -> FlattenState s a
transliterateOrError [Char]
e FlattenState s a
s =
        case CodingFailureMode
cfm of
            CodingFailureMode
ErrorOnCodingFailure -> forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
e
            CodingFailureMode
TransliterateCodingFailure -> forall s a. Char -> FlattenState s a -> FlattenState s a
YAndC Char
replacementChar FlattenState s a
s
            CodingFailureMode
DropOnCodingFailure -> FlattenState s a
s
    {-# INLINE inputUnderflow #-}
    inputUnderflow :: FlattenState s a
inputUnderflow =
        case CodingFailureMode
cfm of
            CodingFailureMode
ErrorOnCodingFailure ->
                forall a. (?callStack::CallStack) => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
                forall a. Show a => a -> [Char]
show [Char]
"Streamly.Internal.Data.Stream.StreamD."
                forall a. [a] -> [a] -> [a]
++ [Char]
"decodeUtf8ArraysWith: Input Underflow"
            CodingFailureMode
TransliterateCodingFailure -> forall s a. Char -> FlattenState s a -> FlattenState s a
YAndC Char
replacementChar forall s a. FlattenState s a
D
            CodingFailureMode
DropOnCodingFailure -> forall s a. FlattenState s a
D
    {-# INLINE_LATE step' #-}
    step' :: Ptr Word8
-> State Stream m a
-> FlattenState s Word8
-> m (Step (FlattenState s Word8) Char)
step' Ptr Word8
_ State Stream m a
gst (OuterLoop s
st Maybe (Word8, Int)
Nothing) = do
        Step s (Array Word8)
r <- State Stream m (Array Word8) -> s -> m (Step s (Array Word8))
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
            case Step s (Array Word8)
r of
                Yield A.Array {Ptr Word8
ArrayContents
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
aEnd :: Ptr Word8
arrStart :: Ptr Word8
arrContents :: ArrayContents
..} s
s ->
                     forall s a. s -> Step s a
Skip (forall s a.
s -> ArrayContents -> Ptr a -> Ptr a -> FlattenState s a
InnerLoopDecodeInit s
s ArrayContents
arrContents Ptr Word8
arrStart Ptr Word8
aEnd)
                Skip s
s -> forall s a. s -> Step s a
Skip (forall s a. s -> Maybe (Word8, Int) -> FlattenState s a
OuterLoop s
s forall a. Maybe a
Nothing)
                Step s (Array Word8)
Stop -> forall s a. s -> Step s a
Skip forall s a. FlattenState s a
D
    step' Ptr Word8
_ State Stream m a
gst (OuterLoop s
st dst :: Maybe (Word8, Int)
dst@(Just (Word8
ds, Int
cp))) = do
        Step s (Array Word8)
r <- State Stream m (Array Word8) -> s -> m (Step s (Array Word8))
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
gst) s
st
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
            case Step s (Array Word8)
r of
                Yield A.Array {Ptr Word8
ArrayContents
aEnd :: Ptr Word8
arrStart :: Ptr Word8
arrContents :: ArrayContents
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} s
s ->
                     forall s a. s -> Step s a
Skip (forall s a.
s
-> ArrayContents
-> Ptr a
-> Ptr a
-> Word8
-> Int
-> FlattenState s a
InnerLoopDecoding s
s ArrayContents
arrContents Ptr Word8
arrStart Ptr Word8
aEnd Word8
ds Int
cp)
                Skip s
s -> forall s a. s -> Step s a
Skip (forall s a. s -> Maybe (Word8, Int) -> FlattenState s a
OuterLoop s
s Maybe (Word8, Int)
dst)
                Step s (Array Word8)
Stop -> forall s a. s -> Step s a
Skip forall s a. FlattenState s a
inputUnderflow
    step' Ptr Word8
_ State Stream m a
_ (InnerLoopDecodeInit s
st ArrayContents
startf Ptr Word8
p Ptr Word8
end)
        | Ptr Word8
p forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = do
            forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ ArrayContents -> IO ()
touch ArrayContents
startf
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. s -> Maybe (Word8, Int) -> FlattenState s a
OuterLoop s
st forall a. Maybe a
Nothing
    step' Ptr Word8
_ State Stream m a
_ (InnerLoopDecodeInit s
st ArrayContents
startf Ptr Word8
p Ptr Word8
end) = do
        Word8
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
p
        -- Note: It is important to use a ">" instead of a "<=" test here for
        -- GHC to generate code layout for default branch prediction for the
        -- common case. This is fragile and might change with the compiler
        -- versions, we need a more reliable "likely" primitive to control
        -- branch predication.
        case Word8
x forall a. Ord a => a -> a -> Bool
> Word8
0x7f of
            Bool
False ->
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. Char -> FlattenState s a -> FlattenState s a
YAndC
                    (Int -> Char
unsafeChr (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
x))
                    (forall s a.
s -> ArrayContents -> Ptr a -> Ptr a -> FlattenState s a
InnerLoopDecodeInit s
st ArrayContents
startf (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) Ptr Word8
end)
            -- Using a separate state here generates a jump to a separate code
            -- block in the core which seems to perform slightly better for the
            -- non-ascii case.
            Bool
True -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a.
s -> ArrayContents -> Ptr a -> Ptr a -> Word8 -> FlattenState s a
InnerLoopDecodeFirst s
st ArrayContents
startf Ptr Word8
p Ptr Word8
end Word8
x

    step' Ptr Word8
table State Stream m a
_ (InnerLoopDecodeFirst s
st ArrayContents
startf Ptr Word8
p Ptr Word8
end Word8
x) = do
        let (Tuple' Word8
sv Int
cp) = Ptr Word8 -> Word8 -> Tuple' Word8 Int
decode0 Ptr Word8
table Word8
x
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
            case Word8
sv of
                Word8
12 ->
                    forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$
                    forall {s} {a}. [Char] -> FlattenState s a -> FlattenState s a
transliterateOrError
                        (
                           [Char]
"Streamly.Internal.Data.Stream.StreamD."
                        forall a. [a] -> [a] -> [a]
++ [Char]
"decodeUtf8ArraysWith: Invalid UTF8"
                        forall a. [a] -> [a] -> [a]
++ [Char]
" codepoint encountered"
                        )
                        (forall s a.
s -> ArrayContents -> Ptr a -> Ptr a -> FlattenState s a
InnerLoopDecodeInit s
st ArrayContents
startf (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) Ptr Word8
end)
                Word8
0 -> forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"unreachable state"
                Word8
_ -> forall s a. s -> Step s a
Skip (forall s a.
s
-> ArrayContents
-> Ptr a
-> Ptr a
-> Word8
-> Int
-> FlattenState s a
InnerLoopDecoding s
st ArrayContents
startf (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) Ptr Word8
end Word8
sv Int
cp)
    step' Ptr Word8
_ State Stream m a
_ (InnerLoopDecoding s
st ArrayContents
startf Ptr Word8
p Ptr Word8
end Word8
sv Int
cp)
        | Ptr Word8
p forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = do
            forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ ArrayContents -> IO ()
touch ArrayContents
startf
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall s a. s -> Maybe (Word8, Int) -> FlattenState s a
OuterLoop s
st (forall a. a -> Maybe a
Just (Word8
sv, Int
cp))
    step' Ptr Word8
table State Stream m a
_ (InnerLoopDecoding s
st ArrayContents
startf Ptr Word8
p Ptr Word8
end Word8
statePtr Int
codepointPtr) = do
        Word8
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
p
        let (Tuple' Word8
sv Int
cp) = Ptr Word8 -> Word8 -> Int -> Word8 -> Tuple' Word8 Int
decode1 Ptr Word8
table Word8
statePtr Int
codepointPtr Word8
x
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
            case Word8
sv of
                Word8
0 ->
                    forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$
                    forall s a. Char -> FlattenState s a -> FlattenState s a
YAndC
                        (Int -> Char
unsafeChr Int
cp)
                        (forall s a.
s -> ArrayContents -> Ptr a -> Ptr a -> FlattenState s a
InnerLoopDecodeInit s
st ArrayContents
startf (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) Ptr Word8
end)
                Word8
12 ->
                    forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$
                    forall {s} {a}. [Char] -> FlattenState s a -> FlattenState s a
transliterateOrError
                        (
                           [Char]
"Streamly.Internal.Data.Stream.StreamD."
                        forall a. [a] -> [a] -> [a]
++ [Char]
"decodeUtf8ArraysWith: Invalid UTF8"
                        forall a. [a] -> [a] -> [a]
++ [Char]
" codepoint encountered"
                        )
                        (forall s a.
s -> ArrayContents -> Ptr a -> Ptr a -> FlattenState s a
InnerLoopDecodeInit s
st ArrayContents
startf (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) Ptr Word8
end)
                Word8
_ ->
                    forall s a. s -> Step s a
Skip
                    (forall s a.
s
-> ArrayContents
-> Ptr a
-> Ptr a
-> Word8
-> Int
-> FlattenState s a
InnerLoopDecoding s
st ArrayContents
startf (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) Ptr Word8
end Word8
sv Int
cp)
    step' Ptr Word8
_ State Stream m a
_ (YAndC Char
c FlattenState s Word8
s) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield Char
c FlattenState s Word8
s
    step' Ptr Word8
_ State Stream m a
_ FlattenState s Word8
D = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop

{-# INLINE decodeUtf8ArraysD #-}
decodeUtf8ArraysD ::
       MonadIO m
    => Stream m (A.Array Word8)
    -> Stream m Char
decodeUtf8ArraysD :: forall (m :: * -> *).
MonadIO m =>
Stream m (Array Word8) -> Stream m Char
decodeUtf8ArraysD = forall (m :: * -> *).
MonadIO m =>
CodingFailureMode -> Stream m (Array Word8) -> Stream m Char
decodeUtf8ArraysWithD CodingFailureMode
TransliterateCodingFailure

-- |
--
-- /Pre-release/
{-# INLINE decodeUtf8Arrays #-}
decodeUtf8Arrays ::
       (MonadIO m, IsStream t) => t m (Array Word8) -> t m Char
decodeUtf8Arrays :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(MonadIO m, IsStream t) =>
t m (Array Word8) -> t m Char
decodeUtf8Arrays =
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadIO m =>
Stream m (Array Word8) -> Stream m Char
decodeUtf8ArraysD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

{-# INLINE decodeUtf8ArraysD' #-}
decodeUtf8ArraysD' ::
       MonadIO m
    => Stream m (A.Array Word8)
    -> Stream m Char
decodeUtf8ArraysD' :: forall (m :: * -> *).
MonadIO m =>
Stream m (Array Word8) -> Stream m Char
decodeUtf8ArraysD' = forall (m :: * -> *).
MonadIO m =>
CodingFailureMode -> Stream m (Array Word8) -> Stream m Char
decodeUtf8ArraysWithD CodingFailureMode
ErrorOnCodingFailure

-- |
--
-- /Pre-release/
{-# INLINE decodeUtf8Arrays' #-}
decodeUtf8Arrays' :: (MonadIO m, IsStream t) => t m (Array Word8) -> t m Char
decodeUtf8Arrays' :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(MonadIO m, IsStream t) =>
t m (Array Word8) -> t m Char
decodeUtf8Arrays' = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadIO m =>
Stream m (Array Word8) -> Stream m Char
decodeUtf8ArraysD' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

{-# INLINE decodeUtf8ArraysD_ #-}
decodeUtf8ArraysD_ ::
       MonadIO m
    => Stream m (A.Array Word8)
    -> Stream m Char
decodeUtf8ArraysD_ :: forall (m :: * -> *).
MonadIO m =>
Stream m (Array Word8) -> Stream m Char
decodeUtf8ArraysD_ = forall (m :: * -> *).
MonadIO m =>
CodingFailureMode -> Stream m (Array Word8) -> Stream m Char
decodeUtf8ArraysWithD CodingFailureMode
DropOnCodingFailure

-- |
--
-- /Pre-release/
{-# INLINE decodeUtf8Arrays_ #-}
decodeUtf8Arrays_ ::
       (MonadIO m, IsStream t) => t m (Array Word8) -> t m Char
decodeUtf8Arrays_ :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(MonadIO m, IsStream t) =>
t m (Array Word8) -> t m Char
decodeUtf8Arrays_ =
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadIO m =>
Stream m (Array Word8) -> Stream m Char
decodeUtf8ArraysD_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

-------------------------------------------------------------------------------
-- Encoding Unicode (UTF-8) Characters
-------------------------------------------------------------------------------

data WList = WCons !Word8 !WList | WNil

-- UTF-8 primitives, Lifted from GHC.IO.Encoding.UTF8.

{-# INLINE ord2 #-}
ord2 :: Char -> WList
ord2 :: Char -> WList
ord2 Char
c = forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
>= Int
0x80 Bool -> Bool -> Bool
&& Int
n forall a. Ord a => a -> a -> Bool
<= Int
0x07ff) (Word8 -> WList -> WList
WCons Word8
x1 (Word8 -> WList -> WList
WCons Word8
x2 WList
WNil))
  where
    n :: Int
n = Char -> Int
ord Char
c
    x1 :: Word8
x1 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int
n forall a. Bits a => a -> Int -> a
`shiftR` Int
6) forall a. Num a => a -> a -> a
+ Int
0xC0
    x2 :: Word8
x2 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int
n forall a. Bits a => a -> a -> a
.&. Int
0x3F) forall a. Num a => a -> a -> a
+ Int
0x80

{-# INLINE ord3 #-}
ord3 :: Char -> WList
ord3 :: Char -> WList
ord3 Char
c = forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
>= Int
0x0800 Bool -> Bool -> Bool
&& Int
n forall a. Ord a => a -> a -> Bool
<= Int
0xffff) (Word8 -> WList -> WList
WCons Word8
x1 (Word8 -> WList -> WList
WCons Word8
x2 (Word8 -> WList -> WList
WCons Word8
x3 WList
WNil)))
  where
    n :: Int
n = Char -> Int
ord Char
c
    x1 :: Word8
x1 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int
n forall a. Bits a => a -> Int -> a
`shiftR` Int
12) forall a. Num a => a -> a -> a
+ Int
0xE0
    x2 :: Word8
x2 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ((Int
n forall a. Bits a => a -> Int -> a
`shiftR` Int
6) forall a. Bits a => a -> a -> a
.&. Int
0x3F) forall a. Num a => a -> a -> a
+ Int
0x80
    x3 :: Word8
x3 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int
n forall a. Bits a => a -> a -> a
.&. Int
0x3F) forall a. Num a => a -> a -> a
+ Int
0x80

{-# INLINE ord4 #-}
ord4 :: Char -> WList
ord4 :: Char -> WList
ord4 Char
c = forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
>= Int
0x10000)  (Word8 -> WList -> WList
WCons Word8
x1 (Word8 -> WList -> WList
WCons Word8
x2 (Word8 -> WList -> WList
WCons Word8
x3 (Word8 -> WList -> WList
WCons Word8
x4 WList
WNil))))
  where
    n :: Int
n = Char -> Int
ord Char
c
    x1 :: Word8
x1 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int
n forall a. Bits a => a -> Int -> a
`shiftR` Int
18) forall a. Num a => a -> a -> a
+ Int
0xF0
    x2 :: Word8
x2 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ((Int
n forall a. Bits a => a -> Int -> a
`shiftR` Int
12) forall a. Bits a => a -> a -> a
.&. Int
0x3F) forall a. Num a => a -> a -> a
+ Int
0x80
    x3 :: Word8
x3 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ((Int
n forall a. Bits a => a -> Int -> a
`shiftR` Int
6) forall a. Bits a => a -> a -> a
.&. Int
0x3F) forall a. Num a => a -> a -> a
+ Int
0x80
    x4 :: Word8
x4 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Int
n forall a. Bits a => a -> a -> a
.&. Int
0x3F) forall a. Num a => a -> a -> a
+ Int
0x80

{-# INLINE_NORMAL readCharUtf8With #-}
readCharUtf8With :: Monad m => WList -> Unfold m Char Word8
readCharUtf8With :: forall (m :: * -> *). Monad m => WList -> Unfold m Char Word8
readCharUtf8With WList
surr = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {m :: * -> *}. Monad m => WList -> m (Step WList Word8)
step forall {m :: * -> *}. Monad m => Char -> m WList
inject

    where

    inject :: Char -> m WList
inject Char
c =
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Char -> Int
ord Char
c of
            Int
x | Int
x forall a. Ord a => a -> a -> Bool
<= Int
0x7F -> forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x Word8 -> WList -> WList
`WCons` WList
WNil
              | Int
x forall a. Ord a => a -> a -> Bool
<= Int
0x7FF -> Char -> WList
ord2 Char
c
              | Int
x forall a. Ord a => a -> a -> Bool
<= Int
0xFFFF -> if Char -> Bool
isSurrogate Char
c then WList
surr else Char -> WList
ord3 Char
c
              | Bool
otherwise -> Char -> WList
ord4 Char
c

    {-# INLINE_LATE step #-}
    step :: WList -> m (Step WList Word8)
step WList
WNil = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
    step (WCons Word8
x WList
xs) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield Word8
x WList
xs

{-# INLINE_NORMAL readCharUtf8' #-}
readCharUtf8' :: Monad m => Unfold m Char Word8
readCharUtf8' :: forall (m :: * -> *). Monad m => Unfold m Char Word8
readCharUtf8' =
    forall (m :: * -> *). Monad m => WList -> Unfold m Char Word8
readCharUtf8With forall a b. (a -> b) -> a -> b
$
        forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"Streamly.Internal.Unicode.readCharUtf8': Encountered a surrogate"

-- More yield points improve performance, but I am not sure if they can cause
-- too much code bloat or some trouble with fusion. So keeping only two yield
-- points for now, one for the ascii chars (fast path) and one for all other
-- paths (slow path).
{-# INLINE_NORMAL encodeUtf8D' #-}
encodeUtf8D' :: Monad m => Stream m Char -> Stream m Word8
encodeUtf8D' :: forall (m :: * -> *). Monad m => Stream m Char -> Stream m Word8
encodeUtf8D' = forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
D.unfoldMany forall (m :: * -> *). Monad m => Unfold m Char Word8
readCharUtf8'

-- | Encode a stream of Unicode characters to a UTF-8 encoded bytestream. When
-- any invalid character (U+D800-U+D8FF) is encountered in the input stream the
-- function errors out.
--
-- @since 0.8.0
{-# INLINE encodeUtf8' #-}
encodeUtf8' :: (Monad m, IsStream t) => t m Char -> t m Word8
encodeUtf8' :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Char -> t m Word8
encodeUtf8' = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). Monad m => Stream m Char -> Stream m Word8
encodeUtf8D' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

{-# INLINE_NORMAL readCharUtf8 #-}
readCharUtf8 :: Monad m => Unfold m Char Word8
readCharUtf8 :: forall (m :: * -> *). Monad m => Unfold m Char Word8
readCharUtf8 = forall (m :: * -> *). Monad m => WList -> Unfold m Char Word8
readCharUtf8With forall a b. (a -> b) -> a -> b
$ Word8 -> WList -> WList
WCons Word8
239 (Word8 -> WList -> WList
WCons Word8
191 (Word8 -> WList -> WList
WCons Word8
189 WList
WNil))

-- | See section "3.9 Unicode Encoding Forms" in
-- https://www.unicode.org/versions/Unicode13.0.0/UnicodeStandard-13.0.pdf
--
{-# INLINE_NORMAL encodeUtf8D #-}
encodeUtf8D :: Monad m => Stream m Char -> Stream m Word8
encodeUtf8D :: forall (m :: * -> *). Monad m => Stream m Char -> Stream m Word8
encodeUtf8D = forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
D.unfoldMany forall (m :: * -> *). Monad m => Unfold m Char Word8
readCharUtf8

-- | Encode a stream of Unicode characters to a UTF-8 encoded bytestream. Any
-- Invalid characters (U+D800-U+D8FF) in the input stream are replaced by the
-- Unicode replacement character U+FFFD.
--
-- /Since: 0.7.0 ("Streamly.Data.Unicode.Stream")/
--
-- /Since: 0.8.0 (Lenient Behaviour)/
{-# INLINE encodeUtf8 #-}
encodeUtf8 :: (Monad m, IsStream t) => t m Char -> t m Word8
encodeUtf8 :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Char -> t m Word8
encodeUtf8 = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). Monad m => Stream m Char -> Stream m Word8
encodeUtf8D forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

{-# INLINE_NORMAL readCharUtf8_ #-}
readCharUtf8_ :: Monad m => Unfold m Char Word8
readCharUtf8_ :: forall (m :: * -> *). Monad m => Unfold m Char Word8
readCharUtf8_ = forall (m :: * -> *). Monad m => WList -> Unfold m Char Word8
readCharUtf8With WList
WNil

{-# INLINE_NORMAL encodeUtf8D_ #-}
encodeUtf8D_ :: Monad m => Stream m Char -> Stream m Word8
encodeUtf8D_ :: forall (m :: * -> *). Monad m => Stream m Char -> Stream m Word8
encodeUtf8D_ = forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
D.unfoldMany forall (m :: * -> *). Monad m => Unfold m Char Word8
readCharUtf8_

-- | Encode a stream of Unicode characters to a UTF-8 encoded bytestream. Any
-- Invalid characters (U+D800-U+D8FF) in the input stream are dropped.
--
-- @since 0.8.0
{-# INLINE encodeUtf8_ #-}
encodeUtf8_ :: (Monad m, IsStream t) => t m Char -> t m Word8
encodeUtf8_ :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Char -> t m Word8
encodeUtf8_ = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). Monad m => Stream m Char -> Stream m Word8
encodeUtf8D_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD

-- | Same as 'encodeUtf8'
--
{-# DEPRECATED encodeUtf8Lax "Please use 'encodeUtf8' instead" #-}
{-# INLINE encodeUtf8Lax #-}
encodeUtf8Lax :: (IsStream t, Monad m) => t m Char -> t m Word8
encodeUtf8Lax :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m Char -> t m Word8
encodeUtf8Lax = forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Char -> t m Word8
encodeUtf8

-------------------------------------------------------------------------------
-- Encode streams of containers
-------------------------------------------------------------------------------

-- | Encode a container to @Array Word8@ provided an unfold to covert it to a
-- Char stream and an encoding function.
--
-- /Internal/
{-# INLINE encodeObject #-}
encodeObject :: MonadIO m =>
       (SerialT m Char -> SerialT m Word8)
    -> Unfold m a Char
    -> a
    -> m (Array Word8)
encodeObject :: forall (m :: * -> *) a.
MonadIO m =>
(SerialT m Char -> SerialT m Word8)
-> Unfold m a Char -> a -> m (Array Word8)
encodeObject SerialT m Char -> SerialT m Word8
encode Unfold m a Char
u = forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> SerialT m a -> m b
S.fold forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Fold m a (Array a)
Array.write forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialT m Char -> SerialT m Word8
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
Unfold m a b -> a -> t m b
S.unfold Unfold m a Char
u

-- | Encode a stream of container objects using the supplied encoding scheme.
-- Each object is encoded as an @Array Word8@.
--
-- /Internal/
{-# INLINE encodeObjects #-}
encodeObjects :: (MonadIO m, IsStream t) =>
       (SerialT m Char -> SerialT m Word8)
    -> Unfold m a Char
    -> t m a
    -> t m (Array Word8)
encodeObjects :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(MonadIO m, IsStream t) =>
(SerialT m Char -> SerialT m Word8)
-> Unfold m a Char -> t m a -> t m (Array Word8)
encodeObjects SerialT m Char -> SerialT m Word8
encode Unfold m a Char
u = forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(IsStream t1, IsStream t2) =>
t1 m a -> t2 m a
adapt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SerialT m a -> SerialT m b
Serial.mapM (forall (m :: * -> *) a.
MonadIO m =>
(SerialT m Char -> SerialT m Word8)
-> Unfold m a Char -> a -> m (Array Word8)
encodeObject SerialT m Char -> SerialT m Word8
encode Unfold m a Char
u) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(IsStream t1, IsStream t2) =>
t1 m a -> t2 m a
adapt

-- | Encode a stream of 'String' using the supplied encoding scheme. Each
-- string is encoded as an @Array Word8@.
--
-- @since 0.8.0
{-# INLINE encodeStrings #-}
encodeStrings :: (MonadIO m, IsStream t) =>
    (SerialT m Char -> SerialT m Word8) -> t m String -> t m (Array Word8)
encodeStrings :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(MonadIO m, IsStream t) =>
(SerialT m Char -> SerialT m Word8)
-> t m [Char] -> t m (Array Word8)
encodeStrings SerialT m Char -> SerialT m Word8
encode = forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(MonadIO m, IsStream t) =>
(SerialT m Char -> SerialT m Word8)
-> Unfold m a Char -> t m a -> t m (Array Word8)
encodeObjects SerialT m Char -> SerialT m Word8
encode forall (m :: * -> *) a. Monad m => Unfold m [a] a
Unfold.fromList

{-
-------------------------------------------------------------------------------
-- Utility operations on strings
-------------------------------------------------------------------------------

strip :: IsStream t => t m Char -> t m Char
strip = undefined

stripTail :: IsStream t => t m Char -> t m Char
stripTail = undefined
-}

-- | Remove leading whitespace from a string.
--
-- > stripHead = S.dropWhile isSpace
--
-- /Pre-release/
{-# INLINE stripHead #-}
stripHead :: (Monad m, IsStream t) => t m Char -> t m Char
stripHead :: forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Char -> t m Char
stripHead = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
(a -> Bool) -> t m a -> t m a
S.dropWhile Char -> Bool
isSpace

-- | Fold each line of the stream using the supplied 'Fold'
-- and stream the result.
--
-- >>> Stream.toList $ lines Fold.toList (Stream.fromList "lines\nthis\nstring\n\n\n")
-- ["lines","this","string","",""]
--
-- > lines = S.splitOnSuffix (== '\n')
--
-- /Pre-release/
{-# INLINE lines #-}
lines :: (Monad m, IsStream t) => Fold m Char b -> t m Char -> t m b
lines :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) b.
(Monad m, IsStream t) =>
Fold m Char b -> t m Char -> t m b
lines = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> Bool) -> Fold m a b -> t m a -> t m b
S.splitOnSuffix (forall a. Eq a => a -> a -> Bool
== Char
'\n')

foreign import ccall unsafe "u_iswspace"
  iswspace :: Int -> Int

-- | Code copied from base/Data.Char to INLINE it
{-# INLINE isSpace #-}
isSpace :: Char -> Bool
isSpace :: Char -> Bool
isSpace Char
c
  | Word
uc forall a. Ord a => a -> a -> Bool
<= Word
0x377 = Word
uc forall a. Eq a => a -> a -> Bool
== Word
32 Bool -> Bool -> Bool
|| Word
uc forall a. Num a => a -> a -> a
- Word
0x9 forall a. Ord a => a -> a -> Bool
<= Word
4 Bool -> Bool -> Bool
|| Word
uc forall a. Eq a => a -> a -> Bool
== Word
0xa0
  | Bool
otherwise = Int -> Int
iswspace (Char -> Int
ord Char
c) forall a. Eq a => a -> a -> Bool
/= Int
0
  where
    uc :: Word
uc = forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Int
ord Char
c) :: Word

-- | Fold each word of the stream using the supplied 'Fold'
-- and stream the result.
--
-- >>>  Stream.toList $ words Fold.toList (Stream.fromList "fold these     words")
-- ["fold","these","words"]
--
-- > words = S.wordsBy isSpace
--
-- /Pre-release/
{-# INLINE words #-}
words :: (Monad m, IsStream t) => Fold m Char b -> t m Char -> t m b
words :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) b.
(Monad m, IsStream t) =>
Fold m Char b -> t m Char -> t m b
words = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> Bool) -> Fold m a b -> t m a -> t m b
S.wordsBy Char -> Bool
isSpace

-- | Unfold a stream to character streams using the supplied 'Unfold'
-- and concat the results suffixing a newline character @\\n@ to each stream.
--
-- @
-- unlines = Stream.interposeSuffix '\n'
-- unlines = Stream.intercalateSuffix Unfold.fromList "\n"
-- @
--
-- /Pre-release/
{-# INLINE unlines #-}
unlines :: (MonadIO m, IsStream t) => Unfold m a Char -> t m a -> t m Char
unlines :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(MonadIO m, IsStream t) =>
Unfold m a Char -> t m a -> t m Char
unlines = forall (t :: (* -> *) -> * -> *) (m :: * -> *) c b.
(IsStream t, Monad m) =>
c -> Unfold m b c -> t m b -> t m c
S.interposeSuffix Char
'\n'

-- | Unfold the elements of a stream to character streams using the supplied
-- 'Unfold' and concat the results with a whitespace character infixed between
-- the streams.
--
-- @
-- unwords = Stream.interpose ' '
-- unwords = Stream.intercalate Unfold.fromList " "
-- @
--
-- /Pre-release/
{-# INLINE unwords #-}
unwords :: (MonadIO m, IsStream t) => Unfold m a Char -> t m a -> t m Char
unwords :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(MonadIO m, IsStream t) =>
Unfold m a Char -> t m a -> t m Char
unwords = forall (t :: (* -> *) -> * -> *) (m :: * -> *) c b.
(IsStream t, Monad m) =>
c -> Unfold m b c -> t m b -> t m c
S.interpose Char
' '