{-# LANGUAGE CPP, BangPatterns, ScopedTypeVariables #-}
{-# LANGUAGE MagicHash, UnboxedTuples, PatternGuards #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
#if __GLASGOW_HASKELL__ == 700
{-# LANGUAGE MonoPatBinds #-}
#endif
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
module Data.ByteString.Builder.Prim (
BoundedPrim
, emptyB
, (>*<)
, (>$<)
, eitherB
, condB
, primBounded
, primMapListBounded
, primUnfoldrBounded
, primMapByteStringBounded
, primMapLazyByteStringBounded
, FixedPrim
, emptyF
, liftFixedToBounded
, primFixed
, primMapListFixed
, primUnfoldrFixed
, primMapByteStringFixed
, primMapLazyByteStringFixed
, module Data.ByteString.Builder.Prim.Binary
, module Data.ByteString.Builder.Prim.ASCII
, char8
, charUtf8
, cstring
, cstringUtf8
) where
import Data.ByteString.Builder.Internal
import qualified Data.ByteString as S
import qualified Data.ByteString.Internal as S
import qualified Data.ByteString.Lazy.Internal as L
import Data.Monoid
import Data.Char (chr, ord)
import Control.Monad ((<=<), unless)
import Data.ByteString.Builder.Prim.Internal hiding (size, sizeBound)
import qualified Data.ByteString.Builder.Prim.Internal as I (size, sizeBound)
import Data.ByteString.Builder.Prim.Binary
import Data.ByteString.Builder.Prim.ASCII
#if MIN_VERSION_base(4,4,0)
#if MIN_VERSION_base(4,7,0)
import Foreign
import Foreign.C.Types
#else
import Foreign hiding (unsafeForeignPtrToPtr)
#endif
import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
#else
import Foreign
#endif
import GHC.Word (Word8 (..))
import GHC.Exts
import GHC.IO
{-# INLINE primFixed #-}
primFixed :: FixedPrim a -> (a -> Builder)
primFixed :: forall a. FixedPrim a -> a -> Builder
primFixed = BoundedPrim a -> a -> Builder
forall a. BoundedPrim a -> a -> Builder
primBounded (BoundedPrim a -> a -> Builder)
-> (FixedPrim a -> BoundedPrim a) -> FixedPrim a -> a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedPrim a -> BoundedPrim a
forall a. FixedPrim a -> BoundedPrim a
toB
{-# INLINE primMapListFixed #-}
primMapListFixed :: FixedPrim a -> ([a] -> Builder)
primMapListFixed :: forall a. FixedPrim a -> [a] -> Builder
primMapListFixed = BoundedPrim a -> [a] -> Builder
forall a. BoundedPrim a -> [a] -> Builder
primMapListBounded (BoundedPrim a -> [a] -> Builder)
-> (FixedPrim a -> BoundedPrim a) -> FixedPrim a -> [a] -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedPrim a -> BoundedPrim a
forall a. FixedPrim a -> BoundedPrim a
toB
{-# INLINE primUnfoldrFixed #-}
primUnfoldrFixed :: FixedPrim b -> (a -> Maybe (b, a)) -> a -> Builder
primUnfoldrFixed :: forall b a. FixedPrim b -> (a -> Maybe (b, a)) -> a -> Builder
primUnfoldrFixed = BoundedPrim b -> (a -> Maybe (b, a)) -> a -> Builder
forall b a. BoundedPrim b -> (a -> Maybe (b, a)) -> a -> Builder
primUnfoldrBounded (BoundedPrim b -> (a -> Maybe (b, a)) -> a -> Builder)
-> (FixedPrim b -> BoundedPrim b)
-> FixedPrim b
-> (a -> Maybe (b, a))
-> a
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedPrim b -> BoundedPrim b
forall a. FixedPrim a -> BoundedPrim a
toB
{-# INLINE primMapByteStringFixed #-}
primMapByteStringFixed :: FixedPrim Word8 -> (S.ByteString -> Builder)
primMapByteStringFixed :: FixedPrim Word8 -> ByteString -> Builder
primMapByteStringFixed = BoundedPrim Word8 -> ByteString -> Builder
primMapByteStringBounded (BoundedPrim Word8 -> ByteString -> Builder)
-> (FixedPrim Word8 -> BoundedPrim Word8)
-> FixedPrim Word8
-> ByteString
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedPrim Word8 -> BoundedPrim Word8
forall a. FixedPrim a -> BoundedPrim a
toB
{-# INLINE primMapLazyByteStringFixed #-}
primMapLazyByteStringFixed :: FixedPrim Word8 -> (L.ByteString -> Builder)
primMapLazyByteStringFixed :: FixedPrim Word8 -> ByteString -> Builder
primMapLazyByteStringFixed = BoundedPrim Word8 -> ByteString -> Builder
primMapLazyByteStringBounded (BoundedPrim Word8 -> ByteString -> Builder)
-> (FixedPrim Word8 -> BoundedPrim Word8)
-> FixedPrim Word8
-> ByteString
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedPrim Word8 -> BoundedPrim Word8
forall a. FixedPrim a -> BoundedPrim a
toB
{-# INLINE[1] primBounded #-}
primBounded :: BoundedPrim a -> (a -> Builder)
primBounded :: forall a. BoundedPrim a -> a -> Builder
primBounded BoundedPrim a
w a
x =
Int -> Builder
ensureFree (BoundedPrim a -> Int
forall a. BoundedPrim a -> Int
I.sizeBound BoundedPrim a
w) Builder -> Builder -> Builder
forall a. Monoid a => a -> a -> a
`mappend` (forall r. BuildStep r -> BuildStep r) -> Builder
builder forall {b}. (BufferRange -> IO b) -> BufferRange -> IO b
forall r. BuildStep r -> BuildStep r
step
where
step :: (BufferRange -> IO b) -> BufferRange -> IO b
step BufferRange -> IO b
k (BufferRange Ptr Word8
op Ptr Word8
ope) = do
Ptr Word8
op' <- BoundedPrim a -> a -> Ptr Word8 -> IO (Ptr Word8)
forall a. BoundedPrim a -> a -> Ptr Word8 -> IO (Ptr Word8)
runB BoundedPrim a
w a
x Ptr Word8
op
let !br' :: BufferRange
br' = Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange Ptr Word8
op' Ptr Word8
ope
BufferRange -> IO b
k BufferRange
br'
{-# RULES
"append/primBounded" forall w1 w2 x1 x2.
append (primBounded w1 x1) (primBounded w2 x2)
= primBounded (pairB w1 w2) (x1, x2)
"append/primBounded/assoc_r" forall w1 w2 x1 x2 b.
append (primBounded w1 x1) (append (primBounded w2 x2) b)
= append (primBounded (pairB w1 w2) (x1, x2)) b
"append/primBounded/assoc_l" forall w1 w2 x1 x2 b.
append (append b (primBounded w1 x1)) (primBounded w2 x2)
= append b (primBounded (pairB w1 w2) (x1, x2))
#-}
{-# INLINE primMapListBounded #-}
primMapListBounded :: BoundedPrim a -> [a] -> Builder
primMapListBounded :: forall a. BoundedPrim a -> [a] -> Builder
primMapListBounded BoundedPrim a
w [a]
xs0 =
(forall r. BuildStep r -> BuildStep r) -> Builder
builder ((forall r. BuildStep r -> BuildStep r) -> Builder)
-> (forall r. BuildStep r -> BuildStep r) -> Builder
forall a b. (a -> b) -> a -> b
$ [a]
-> (BufferRange -> IO (BuildSignal r))
-> BufferRange
-> IO (BuildSignal r)
forall {a}.
[a]
-> (BufferRange -> IO (BuildSignal a))
-> BufferRange
-> IO (BuildSignal a)
step [a]
xs0
where
step :: [a]
-> (BufferRange -> IO (BuildSignal a))
-> BufferRange
-> IO (BuildSignal a)
step [a]
xs1 BufferRange -> IO (BuildSignal a)
k (BufferRange Ptr Word8
op0 Ptr Word8
ope0) =
[a] -> Ptr Word8 -> IO (BuildSignal a)
go [a]
xs1 Ptr Word8
op0
where
go :: [a] -> Ptr Word8 -> IO (BuildSignal a)
go [] !Ptr Word8
op = BufferRange -> IO (BuildSignal a)
k (Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange Ptr Word8
op Ptr Word8
ope0)
go xs :: [a]
xs@(a
x':[a]
xs') !Ptr Word8
op
| Ptr Word8
op Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
bound Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr Word8
ope0 = BoundedPrim a -> a -> Ptr Word8 -> IO (Ptr Word8)
forall a. BoundedPrim a -> a -> Ptr Word8 -> IO (Ptr Word8)
runB BoundedPrim a
w a
x' Ptr Word8
op IO (Ptr Word8)
-> (Ptr Word8 -> IO (BuildSignal a)) -> IO (BuildSignal a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [a] -> Ptr Word8 -> IO (BuildSignal a)
go [a]
xs'
| Bool
otherwise =
BuildSignal a -> IO (BuildSignal a)
forall (m :: * -> *) a. Monad m => a -> m a
return (BuildSignal a -> IO (BuildSignal a))
-> BuildSignal a -> IO (BuildSignal a)
forall a b. (a -> b) -> a -> b
$ Int
-> Ptr Word8
-> (BufferRange -> IO (BuildSignal a))
-> BuildSignal a
forall a. Int -> Ptr Word8 -> BuildStep a -> BuildSignal a
bufferFull Int
bound Ptr Word8
op ([a]
-> (BufferRange -> IO (BuildSignal a))
-> BufferRange
-> IO (BuildSignal a)
step [a]
xs BufferRange -> IO (BuildSignal a)
k)
bound :: Int
bound = BoundedPrim a -> Int
forall a. BoundedPrim a -> Int
I.sizeBound BoundedPrim a
w
{-# INLINE primUnfoldrBounded #-}
primUnfoldrBounded :: BoundedPrim b -> (a -> Maybe (b, a)) -> a -> Builder
primUnfoldrBounded :: forall b a. BoundedPrim b -> (a -> Maybe (b, a)) -> a -> Builder
primUnfoldrBounded BoundedPrim b
w a -> Maybe (b, a)
f a
x0 =
(forall r. BuildStep r -> BuildStep r) -> Builder
builder ((forall r. BuildStep r -> BuildStep r) -> Builder)
-> (forall r. BuildStep r -> BuildStep r) -> Builder
forall a b. (a -> b) -> a -> b
$ a
-> (BufferRange -> IO (BuildSignal r))
-> BufferRange
-> IO (BuildSignal r)
forall {a}.
a
-> (BufferRange -> IO (BuildSignal a))
-> BufferRange
-> IO (BuildSignal a)
fillWith a
x0
where
fillWith :: a
-> (BufferRange -> IO (BuildSignal a))
-> BufferRange
-> IO (BuildSignal a)
fillWith a
x BufferRange -> IO (BuildSignal a)
k (BufferRange Ptr Word8
op0 Ptr Word8
ope0) =
Maybe (b, a) -> Ptr Word8 -> IO (BuildSignal a)
go (a -> Maybe (b, a)
f a
x) Ptr Word8
op0
where
go :: Maybe (b, a) -> Ptr Word8 -> IO (BuildSignal a)
go Maybe (b, a)
Nothing !Ptr Word8
op = BufferRange -> IO (BuildSignal a)
k (Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange Ptr Word8
op Ptr Word8
ope0)
go (Just (b
y, a
x')) !Ptr Word8
op
| Ptr Word8
op Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
bound Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr Word8
ope0 = BoundedPrim b -> b -> Ptr Word8 -> IO (Ptr Word8)
forall a. BoundedPrim a -> a -> Ptr Word8 -> IO (Ptr Word8)
runB BoundedPrim b
w b
y Ptr Word8
op IO (Ptr Word8)
-> (Ptr Word8 -> IO (BuildSignal a)) -> IO (BuildSignal a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe (b, a) -> Ptr Word8 -> IO (BuildSignal a)
go (a -> Maybe (b, a)
f a
x')
| Bool
otherwise = BuildSignal a -> IO (BuildSignal a)
forall (m :: * -> *) a. Monad m => a -> m a
return (BuildSignal a -> IO (BuildSignal a))
-> BuildSignal a -> IO (BuildSignal a)
forall a b. (a -> b) -> a -> b
$ Int
-> Ptr Word8
-> (BufferRange -> IO (BuildSignal a))
-> BuildSignal a
forall a. Int -> Ptr Word8 -> BuildStep a -> BuildSignal a
bufferFull Int
bound Ptr Word8
op ((BufferRange -> IO (BuildSignal a)) -> BuildSignal a)
-> (BufferRange -> IO (BuildSignal a)) -> BuildSignal a
forall a b. (a -> b) -> a -> b
$
\(BufferRange Ptr Word8
opNew Ptr Word8
opeNew) -> do
!Ptr Word8
opNew' <- BoundedPrim b -> b -> Ptr Word8 -> IO (Ptr Word8)
forall a. BoundedPrim a -> a -> Ptr Word8 -> IO (Ptr Word8)
runB BoundedPrim b
w b
y Ptr Word8
opNew
a
-> (BufferRange -> IO (BuildSignal a))
-> BufferRange
-> IO (BuildSignal a)
fillWith a
x' BufferRange -> IO (BuildSignal a)
k (Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange Ptr Word8
opNew' Ptr Word8
opeNew)
bound :: Int
bound = BoundedPrim b -> Int
forall a. BoundedPrim a -> Int
I.sizeBound BoundedPrim b
w
{-# INLINE primMapByteStringBounded #-}
primMapByteStringBounded :: BoundedPrim Word8 -> S.ByteString -> Builder
primMapByteStringBounded :: BoundedPrim Word8 -> ByteString -> Builder
primMapByteStringBounded BoundedPrim Word8
w =
\ByteString
bs -> (forall r. BuildStep r -> BuildStep r) -> Builder
builder ((forall r. BuildStep r -> BuildStep r) -> Builder)
-> (forall r. BuildStep r -> BuildStep r) -> Builder
forall a b. (a -> b) -> a -> b
$ ByteString
-> (BufferRange -> IO (BuildSignal r))
-> BufferRange
-> IO (BuildSignal r)
forall {a}.
ByteString
-> (BufferRange -> IO (BuildSignal a))
-> BufferRange
-> IO (BuildSignal a)
step ByteString
bs
where
bound :: Int
bound = BoundedPrim Word8 -> Int
forall a. BoundedPrim a -> Int
I.sizeBound BoundedPrim Word8
w
step :: ByteString
-> (BufferRange -> IO (BuildSignal a))
-> BufferRange
-> IO (BuildSignal a)
step (S.BS ForeignPtr Word8
ifp Int
isize) !BufferRange -> IO (BuildSignal a)
k =
Ptr Word8 -> BufferRange -> IO (BuildSignal a)
goBS (ForeignPtr Word8 -> Ptr Word8
forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr Word8
ifp)
where
!ipe :: Ptr b
ipe = ForeignPtr Word8 -> Ptr Word8
forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr Word8
ifp Ptr Word8 -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
isize
goBS :: Ptr Word8 -> BufferRange -> IO (BuildSignal a)
goBS !Ptr Word8
ip0 br :: BufferRange
br@(BufferRange Ptr Word8
op0 Ptr Word8
ope)
| Ptr Word8
ip0 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr Word8
forall {b}. Ptr b
ipe = do
ForeignPtr Word8 -> IO ()
forall a. ForeignPtr a -> IO ()
touchForeignPtr ForeignPtr Word8
ifp
BufferRange -> IO (BuildSignal a)
k BufferRange
br
| Ptr Word8
op0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
bound Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Ptr Word8
ope =
Ptr Word8 -> IO (BuildSignal a)
goPartial (Ptr Word8
ip0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
outRemaining Int
inpRemaining)
| Bool
otherwise = BuildSignal a -> IO (BuildSignal a)
forall (m :: * -> *) a. Monad m => a -> m a
return (BuildSignal a -> IO (BuildSignal a))
-> BuildSignal a -> IO (BuildSignal a)
forall a b. (a -> b) -> a -> b
$ Int
-> Ptr Word8
-> (BufferRange -> IO (BuildSignal a))
-> BuildSignal a
forall a. Int -> Ptr Word8 -> BuildStep a -> BuildSignal a
bufferFull Int
bound Ptr Word8
op0 (Ptr Word8 -> BufferRange -> IO (BuildSignal a)
goBS Ptr Word8
ip0)
where
outRemaining :: Int
outRemaining = (Ptr Word8
ope Ptr Word8 -> Ptr Word8 -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
op0) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
bound
inpRemaining :: Int
inpRemaining = Ptr Any
forall {b}. Ptr b
ipe Ptr Any -> Ptr Word8 -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
ip0
goPartial :: Ptr Word8 -> IO (BuildSignal a)
goPartial !Ptr Word8
ipeTmp = Ptr Word8 -> Ptr Word8 -> IO (BuildSignal a)
go Ptr Word8
ip0 Ptr Word8
op0
where
go :: Ptr Word8 -> Ptr Word8 -> IO (BuildSignal a)
go !Ptr Word8
ip !Ptr Word8
op
| Ptr Word8
ip Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Ptr Word8
ipeTmp = do
Word8
x <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
ip
Ptr Word8
op' <- BoundedPrim Word8 -> Word8 -> Ptr Word8 -> IO (Ptr Word8)
forall a. BoundedPrim a -> a -> Ptr Word8 -> IO (Ptr Word8)
runB BoundedPrim Word8
w Word8
x Ptr Word8
op
Ptr Word8 -> Ptr Word8 -> IO (BuildSignal a)
go (Ptr Word8
ip Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) Ptr Word8
op'
| Bool
otherwise =
Ptr Word8 -> BufferRange -> IO (BuildSignal a)
goBS Ptr Word8
ip (Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange Ptr Word8
op Ptr Word8
ope)
{-# INLINE primMapLazyByteStringBounded #-}
primMapLazyByteStringBounded :: BoundedPrim Word8 -> L.ByteString -> Builder
primMapLazyByteStringBounded :: BoundedPrim Word8 -> ByteString -> Builder
primMapLazyByteStringBounded BoundedPrim Word8
w =
(ByteString -> Builder -> Builder)
-> Builder -> ByteString -> Builder
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
L.foldrChunks (\ByteString
x Builder
b -> BoundedPrim Word8 -> ByteString -> Builder
primMapByteStringBounded BoundedPrim Word8
w ByteString
x Builder -> Builder -> Builder
forall a. Monoid a => a -> a -> a
`mappend` Builder
b) Builder
forall a. Monoid a => a
mempty
#if !MIN_VERSION_base(4,7,0)
isTrue# :: Bool -> Bool
isTrue# x = x
#endif
cstring :: Addr# -> Builder
cstring :: Addr# -> Builder
cstring =
\Addr#
addr0 -> (forall r. BuildStep r -> BuildStep r) -> Builder
builder ((forall r. BuildStep r -> BuildStep r) -> Builder)
-> (forall r. BuildStep r -> BuildStep r) -> Builder
forall a b. (a -> b) -> a -> b
$ Addr# -> BuildStep r -> BuildStep r
forall r. Addr# -> BuildStep r -> BuildStep r
step Addr#
addr0
where
step :: Addr# -> BuildStep r -> BuildStep r
step :: forall r. Addr# -> BuildStep r -> BuildStep r
step !Addr#
addr !BuildStep r
k br :: BufferRange
br@(BufferRange op0 :: Ptr Word8
op0@(Ptr Addr#
op0#) Ptr Word8
ope)
| Word8# -> Word8
W8# Word8#
ch Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0 = BuildStep r
k BufferRange
br
| Ptr Word8
op0 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Word8
ope =
BuildSignal r -> IO (BuildSignal r)
forall (m :: * -> *) a. Monad m => a -> m a
return (BuildSignal r -> IO (BuildSignal r))
-> BuildSignal r -> IO (BuildSignal r)
forall a b. (a -> b) -> a -> b
$ Int -> Ptr Word8 -> BuildStep r -> BuildSignal r
forall a. Int -> Ptr Word8 -> BuildStep a -> BuildSignal a
bufferFull Int
defaultChunkSize Ptr Word8
op0 (Addr# -> BuildStep r -> BuildStep r
forall r. Addr# -> BuildStep r -> BuildStep r
step Addr#
addr BuildStep r
k)
| Bool
otherwise = do
(State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s -> case Addr# -> Int# -> Word8# -> State# RealWorld -> State# RealWorld
forall d. Addr# -> Int# -> Word8# -> State# d -> State# d
writeWord8OffAddr# Addr#
op0# Int#
0# Word8#
ch State# RealWorld
s of
State# RealWorld
s' -> (# State# RealWorld
s', () #)
let br' :: BufferRange
br' = Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange (Ptr Word8
op0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) Ptr Word8
ope
Addr# -> BuildStep r -> BuildStep r
forall r. Addr# -> BuildStep r -> BuildStep r
step (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) BuildStep r
k BufferRange
br'
where
!ch :: Word8#
ch = Addr# -> Int# -> Word8#
indexWord8OffAddr# Addr#
addr Int#
0#
cstringUtf8 :: Addr# -> Builder
cstringUtf8 :: Addr# -> Builder
cstringUtf8 =
\Addr#
addr0 -> (forall r. BuildStep r -> BuildStep r) -> Builder
builder ((forall r. BuildStep r -> BuildStep r) -> Builder)
-> (forall r. BuildStep r -> BuildStep r) -> Builder
forall a b. (a -> b) -> a -> b
$ Addr# -> BuildStep r -> BuildStep r
forall r. Addr# -> BuildStep r -> BuildStep r
step Addr#
addr0
where
step :: Addr# -> BuildStep r -> BuildStep r
step :: forall r. Addr# -> BuildStep r -> BuildStep r
step !Addr#
addr !BuildStep r
k br :: BufferRange
br@(BufferRange op0 :: Ptr Word8
op0@(Ptr Addr#
op0#) Ptr Word8
ope)
| Word8# -> Word8
W8# Word8#
ch Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0 = BuildStep r
k BufferRange
br
| Ptr Word8
op0 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Word8
ope =
BuildSignal r -> IO (BuildSignal r)
forall (m :: * -> *) a. Monad m => a -> m a
return (BuildSignal r -> IO (BuildSignal r))
-> BuildSignal r -> IO (BuildSignal r)
forall a b. (a -> b) -> a -> b
$ Int -> Ptr Word8 -> BuildStep r -> BuildSignal r
forall a. Int -> Ptr Word8 -> BuildStep a -> BuildSignal a
bufferFull Int
defaultChunkSize Ptr Word8
op0 (Addr# -> BuildStep r -> BuildStep r
forall r. Addr# -> BuildStep r -> BuildStep r
step Addr#
addr BuildStep r
k)
| Word8# -> Word8
W8# Word8#
ch Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0xc0
, Word8# -> Word8
W8# (Addr# -> Int# -> Word8#
indexWord8OffAddr# Addr#
addr Int#
1#) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x80 = do
let !(W8# Word8#
nullByte#) = Word8
0
(State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s -> case Addr# -> Int# -> Word8# -> State# RealWorld -> State# RealWorld
forall d. Addr# -> Int# -> Word8# -> State# d -> State# d
writeWord8OffAddr# Addr#
op0# Int#
0# Word8#
nullByte# State# RealWorld
s of
State# RealWorld
s' -> (# State# RealWorld
s', () #)
let br' :: BufferRange
br' = Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange (Ptr Word8
op0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) Ptr Word8
ope
Addr# -> BuildStep r -> BuildStep r
forall r. Addr# -> BuildStep r -> BuildStep r
step (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
2#) BuildStep r
k BufferRange
br'
| Bool
otherwise = do
(State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s -> case Addr# -> Int# -> Word8# -> State# RealWorld -> State# RealWorld
forall d. Addr# -> Int# -> Word8# -> State# d -> State# d
writeWord8OffAddr# Addr#
op0# Int#
0# Word8#
ch State# RealWorld
s of
State# RealWorld
s' -> (# State# RealWorld
s', () #)
let br' :: BufferRange
br' = Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange (Ptr Word8
op0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) Ptr Word8
ope
Addr# -> BuildStep r -> BuildStep r
forall r. Addr# -> BuildStep r -> BuildStep r
step (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) BuildStep r
k BufferRange
br'
where
!ch :: Word8#
ch = Addr# -> Int# -> Word8#
indexWord8OffAddr# Addr#
addr Int#
0#
{-# INLINE char8 #-}
char8 :: FixedPrim Char
char8 :: FixedPrim Char
char8 = (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> (Char -> Int) -> Char -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord) (Char -> Word8) -> FixedPrim Word8 -> FixedPrim Char
forall (f :: * -> *) b a. Contravariant f => (b -> a) -> f a -> f b
>$< FixedPrim Word8
word8
{-# INLINE charUtf8 #-}
charUtf8 :: BoundedPrim Char
charUtf8 :: BoundedPrim Char
charUtf8 = Int -> (Char -> Ptr Word8 -> IO (Ptr Word8)) -> BoundedPrim Char
forall a.
Int -> (a -> Ptr Word8 -> IO (Ptr Word8)) -> BoundedPrim a
boundedPrim Int
4 ((Word8 -> Ptr Word8 -> IO (Ptr Word8))
-> (Word8 -> Word8 -> Ptr Word8 -> IO (Ptr Word8))
-> (Word8 -> Word8 -> Word8 -> Ptr Word8 -> IO (Ptr Word8))
-> (Word8
-> Word8 -> Word8 -> Word8 -> Ptr Word8 -> IO (Ptr Word8))
-> Char
-> Ptr Word8
-> IO (Ptr Word8)
forall a.
(Word8 -> a)
-> (Word8 -> Word8 -> a)
-> (Word8 -> Word8 -> Word8 -> a)
-> (Word8 -> Word8 -> Word8 -> Word8 -> a)
-> Char
-> a
encodeCharUtf8 Word8 -> Ptr Word8 -> IO (Ptr Word8)
forall {a} {b} {b}. Storable a => a -> Ptr b -> IO (Ptr b)
f1 Word8 -> Word8 -> Ptr Word8 -> IO (Ptr Word8)
forall {a} {a} {b} {b}.
(Storable a, Storable a) =>
a -> a -> Ptr b -> IO (Ptr b)
f2 Word8 -> Word8 -> Word8 -> Ptr Word8 -> IO (Ptr Word8)
forall {a} {a} {a} {b} {b}.
(Storable a, Storable a, Storable a) =>
a -> a -> a -> Ptr b -> IO (Ptr b)
f3 Word8 -> Word8 -> Word8 -> Word8 -> Ptr Word8 -> IO (Ptr Word8)
forall {a} {a} {a} {a} {b} {b}.
(Storable a, Storable a, Storable a, Storable a) =>
a -> a -> a -> a -> Ptr b -> IO (Ptr b)
f4)
where
pokeN :: Int -> (Ptr a -> m a) -> Ptr a -> m (Ptr b)
pokeN Int
n Ptr a -> m a
io Ptr a
op = Ptr a -> m a
io Ptr a
op m a -> m (Ptr b) -> m (Ptr b)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr b -> m (Ptr b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr a
op Ptr a -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
n)
f1 :: a -> Ptr b -> IO (Ptr b)
f1 a
x1 = Int -> (Ptr b -> IO ()) -> Ptr b -> IO (Ptr b)
forall {m :: * -> *} {a} {a} {b}.
Monad m =>
Int -> (Ptr a -> m a) -> Ptr a -> m (Ptr b)
pokeN Int
1 ((Ptr b -> IO ()) -> Ptr b -> IO (Ptr b))
-> (Ptr b -> IO ()) -> Ptr b -> IO (Ptr b)
forall a b. (a -> b) -> a -> b
$ \Ptr b
op -> Ptr b -> Int -> a -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr b
op Int
0 a
x1
f2 :: a -> a -> Ptr b -> IO (Ptr b)
f2 a
x1 a
x2 = Int -> (Ptr b -> IO ()) -> Ptr b -> IO (Ptr b)
forall {m :: * -> *} {a} {a} {b}.
Monad m =>
Int -> (Ptr a -> m a) -> Ptr a -> m (Ptr b)
pokeN Int
2 ((Ptr b -> IO ()) -> Ptr b -> IO (Ptr b))
-> (Ptr b -> IO ()) -> Ptr b -> IO (Ptr b)
forall a b. (a -> b) -> a -> b
$ \Ptr b
op -> do Ptr b -> Int -> a -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr b
op Int
0 a
x1
Ptr b -> Int -> a -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr b
op Int
1 a
x2
f3 :: a -> a -> a -> Ptr b -> IO (Ptr b)
f3 a
x1 a
x2 a
x3 = Int -> (Ptr b -> IO ()) -> Ptr b -> IO (Ptr b)
forall {m :: * -> *} {a} {a} {b}.
Monad m =>
Int -> (Ptr a -> m a) -> Ptr a -> m (Ptr b)
pokeN Int
3 ((Ptr b -> IO ()) -> Ptr b -> IO (Ptr b))
-> (Ptr b -> IO ()) -> Ptr b -> IO (Ptr b)
forall a b. (a -> b) -> a -> b
$ \Ptr b
op -> do Ptr b -> Int -> a -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr b
op Int
0 a
x1
Ptr b -> Int -> a -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr b
op Int
1 a
x2
Ptr b -> Int -> a -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr b
op Int
2 a
x3
f4 :: a -> a -> a -> a -> Ptr b -> IO (Ptr b)
f4 a
x1 a
x2 a
x3 a
x4 = Int -> (Ptr b -> IO ()) -> Ptr b -> IO (Ptr b)
forall {m :: * -> *} {a} {a} {b}.
Monad m =>
Int -> (Ptr a -> m a) -> Ptr a -> m (Ptr b)
pokeN Int
4 ((Ptr b -> IO ()) -> Ptr b -> IO (Ptr b))
-> (Ptr b -> IO ()) -> Ptr b -> IO (Ptr b)
forall a b. (a -> b) -> a -> b
$ \Ptr b
op -> do Ptr b -> Int -> a -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr b
op Int
0 a
x1
Ptr b -> Int -> a -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr b
op Int
1 a
x2
Ptr b -> Int -> a -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr b
op Int
2 a
x3
Ptr b -> Int -> a -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr b
op Int
3 a
x4
{-# INLINE encodeCharUtf8 #-}
encodeCharUtf8 :: (Word8 -> a)
-> (Word8 -> Word8 -> a)
-> (Word8 -> Word8 -> Word8 -> a)
-> (Word8 -> Word8 -> Word8 -> Word8 -> a)
-> Char
-> a
encodeCharUtf8 :: forall a.
(Word8 -> a)
-> (Word8 -> Word8 -> a)
-> (Word8 -> Word8 -> Word8 -> a)
-> (Word8 -> Word8 -> Word8 -> Word8 -> a)
-> Char
-> a
encodeCharUtf8 Word8 -> a
f1 Word8 -> Word8 -> a
f2 Word8 -> Word8 -> Word8 -> a
f3 Word8 -> Word8 -> Word8 -> Word8 -> a
f4 Char
c = case Char -> Int
ord Char
c of
Int
x | Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x7F -> Word8 -> a
f1 (Word8 -> a) -> Word8 -> a
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x
| Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x07FF ->
let x1 :: Word8
x1 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ (Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
6) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0xC0
x2 :: Word8
x2 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ (Int
x Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0x80
in Word8 -> Word8 -> a
f2 Word8
x1 Word8
x2
| Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0xFFFF ->
let x1 :: Word8
x1 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ (Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
12) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0xE0
x2 :: Word8
x2 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ ((Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
6) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0x80
x3 :: Word8
x3 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ (Int
x Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0x80
in Word8 -> Word8 -> Word8 -> a
f3 Word8
x1 Word8
x2 Word8
x3
| Bool
otherwise ->
let x1 :: Word8
x1 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ (Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
18) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0xF0
x2 :: Word8
x2 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ ((Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
12) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0x80
x3 :: Word8
x3 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ ((Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
6) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0x80
x4 :: Word8
x4 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ (Int
x Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0x80
in Word8 -> Word8 -> Word8 -> Word8 -> a
f4 Word8
x1 Word8
x2 Word8
x3 Word8
x4