module Data.Text.Builder.Linear.Core (
Buffer,
runBuffer,
runBufferBS,
dupBuffer,
consumeBuffer,
eraseBuffer,
byteSizeOfBuffer,
lengthOfBuffer,
dropBuffer,
takeBuffer,
appendBounded,
appendExact,
prependBounded,
prependExact,
(><),
) where
import Data.ByteString.Internal (ByteString (..))
import Data.Text qualified as T
import Data.Text.Array qualified as A
import Data.Text.Internal (Text (..))
import GHC.Exts (Int (..), Levity (..), RuntimeRep (..), TYPE, byteArrayContents#, isByteArrayPinned#, isTrue#, plusAddr#, sizeofByteArray#, unsafeCoerce#)
import GHC.ForeignPtr (ForeignPtr (..), ForeignPtrContents (..))
import GHC.ST (ST (..), runST)
data Buffer ∷ TYPE ('BoxedRep 'Unlifted) where
Buffer ∷ {-# UNPACK #-} !Text → Buffer
unBuffer ∷ Buffer ⊸ Text
unBuffer :: Buffer %1 -> Text
unBuffer (Buffer Text
x) = Text
x
runBuffer ∷ (Buffer ⊸ Buffer) ⊸ Text
runBuffer :: (Buffer %1 -> Buffer) %1 -> Text
runBuffer Buffer %1 -> Buffer
f = Buffer %1 -> Text
unBuffer (Buffer %1 -> Buffer
shrinkBuffer (Buffer %1 -> Buffer
f (Text -> Buffer
Buffer forall a. Monoid a => a
mempty)))
runBufferBS ∷ (Buffer ⊸ Buffer) ⊸ ByteString
runBufferBS :: (Buffer %1 -> Buffer) %1 -> ByteString
runBufferBS Buffer %1 -> Buffer
f = case Buffer %1 -> Buffer
shrinkBuffer (Buffer %1 -> Buffer
f (Text -> Buffer
Buffer Text
memptyPinned)) of
Buffer (Text (A.ByteArray ByteArray#
arr) (I# Int#
from) Int
len) → ForeignPtr Word8 -> Int -> ByteString
BS forall {a}. ForeignPtr a
fp Int
len
where
addr# :: Addr#
addr# = ByteArray# -> Addr#
byteArrayContents# ByteArray#
arr Addr# -> Int# -> Addr#
`plusAddr#` Int#
from
fp :: ForeignPtr a
fp = forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
addr# (MutableByteArray# RealWorld -> ForeignPtrContents
PlainPtr (unsafeCoerce# :: forall a b. a -> b
unsafeCoerce# ByteArray#
arr))
shrinkBuffer ∷ Buffer ⊸ Buffer
shrinkBuffer :: Buffer %1 -> Buffer
shrinkBuffer (Buffer (Text ByteArray
arr Int
from Int
len)) = Text -> Buffer
Buffer forall a b. (a -> b) -> a -> b
$ forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
MArray s
arrM ← forall s. ByteArray -> ST s (MArray s)
unsafeThaw ByteArray
arr
forall s. MArray s -> Int -> ST s ()
A.shrinkM MArray s
arrM (Int
from forall a. Num a => a -> a -> a
+ Int
len)
ByteArray
arr' ← forall s. MArray s -> ST s ByteArray
A.unsafeFreeze MArray s
arrM
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ByteArray -> Int -> Int -> Text
Text ByteArray
arr' Int
from Int
len
memptyPinned ∷ Text
memptyPinned :: Text
memptyPinned = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
MArray s
marr ← forall s. Int -> ST s (MArray s)
A.newPinned Int
0
ByteArray
arr ← forall s. MArray s -> ST s ByteArray
A.unsafeFreeze MArray s
marr
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ByteArray -> Int -> Int -> Text
Text ByteArray
arr Int
0 Int
0
dupBuffer ∷ Buffer ⊸ (# Buffer, Buffer #)
dupBuffer :: Buffer %1 -> (# Buffer, Buffer #)
dupBuffer (Buffer Text
x) = (# Text -> Buffer
Buffer Text
x, Text -> Buffer
Buffer (Text -> Text
T.copy Text
x) #)
consumeBuffer ∷ Buffer ⊸ ()
consumeBuffer :: Buffer %1 -> ()
consumeBuffer Buffer {} = ()
eraseBuffer ∷ Buffer ⊸ Buffer
eraseBuffer :: Buffer %1 -> Buffer
eraseBuffer (Buffer (Text ByteArray
arr Int
_ Int
_)) =
Text -> Buffer
Buffer (if ByteArray -> Bool
isPinned ByteArray
arr then Text
memptyPinned else forall a. Monoid a => a
mempty)
byteSizeOfBuffer ∷ Buffer ⊸ (# Buffer, Word #)
byteSizeOfBuffer :: Buffer %1 -> (# Buffer, Word #)
byteSizeOfBuffer (Buffer t :: Text
t@(Text ByteArray
_ Int
_ Int
len)) = (# Text -> Buffer
Buffer Text
t, forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len #)
lengthOfBuffer ∷ Buffer ⊸ (# Buffer, Word #)
lengthOfBuffer :: Buffer %1 -> (# Buffer, Word #)
lengthOfBuffer (Buffer Text
t) = (# Text -> Buffer
Buffer Text
t, forall a b. (Integral a, Num b) => a -> b
fromIntegral (Text -> Int
T.length Text
t) #)
dropBuffer ∷ Word → Buffer ⊸ Buffer
dropBuffer :: Word -> Buffer %1 -> Buffer
dropBuffer Word
nChar (Buffer t :: Text
t@(Text ByteArray
arr Int
off Int
len))
| Int
nByte forall a. Ord a => a -> a -> Bool
<= Int
0 = Text -> Buffer
Buffer (ByteArray -> Int -> Int -> Text
Text ByteArray
arr (Int
off forall a. Num a => a -> a -> a
+ Int
len) Int
0)
| Bool
otherwise = Text -> Buffer
Buffer (ByteArray -> Int -> Int -> Text
Text ByteArray
arr (Int
off forall a. Num a => a -> a -> a
+ Int
nByte) (Int
len forall a. Num a => a -> a -> a
- Int
nByte))
where
nByte :: Int
nByte = Int -> Text -> Int
T.measureOff (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
nChar) Text
t
takeBuffer ∷ Word → Buffer ⊸ Buffer
takeBuffer :: Word -> Buffer %1 -> Buffer
takeBuffer Word
nChar (Buffer t :: Text
t@(Text ByteArray
arr Int
off Int
_))
| Int
nByte forall a. Ord a => a -> a -> Bool
<= Int
0 = Text -> Buffer
Buffer Text
t
| Bool
otherwise = Text -> Buffer
Buffer (ByteArray -> Int -> Int -> Text
Text ByteArray
arr Int
off Int
nByte)
where
nByte :: Int
nByte = Int -> Text -> Int
T.measureOff (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
nChar) Text
t
appendBounded
∷ Int
→ (∀ s. A.MArray s → Int → ST s Int)
→ Buffer
⊸ Buffer
appendBounded :: Int
-> (forall s. MArray s -> Int -> ST s Int) -> Buffer %1 -> Buffer
appendBounded Int
maxSrcLen forall s. MArray s -> Int -> ST s Int
appender (Buffer (Text ByteArray
dst Int
dstOff Int
dstLen)) = Text -> Buffer
Buffer forall a b. (a -> b) -> a -> b
$ forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
let dstFullLen :: Int
dstFullLen = ByteArray -> Int
sizeofByteArray ByteArray
dst
newFullLen :: Int
newFullLen = Int
dstOff forall a. Num a => a -> a -> a
+ Int
2 forall a. Num a => a -> a -> a
* (Int
dstLen forall a. Num a => a -> a -> a
+ Int
maxSrcLen)
MArray s
newM ←
if Int
dstOff forall a. Num a => a -> a -> a
+ Int
dstLen forall a. Num a => a -> a -> a
+ Int
maxSrcLen forall a. Ord a => a -> a -> Bool
<= Int
dstFullLen
then forall s. ByteArray -> ST s (MArray s)
unsafeThaw ByteArray
dst
else do
MArray s
tmpM ← (if ByteArray -> Bool
isPinned ByteArray
dst then forall s. Int -> ST s (MArray s)
A.newPinned else forall s. Int -> ST s (MArray s)
A.new) Int
newFullLen
forall s. Int -> MArray s -> Int -> ByteArray -> Int -> ST s ()
A.copyI Int
dstLen MArray s
tmpM Int
dstOff ByteArray
dst Int
dstOff
forall (f :: * -> *) a. Applicative f => a -> f a
pure MArray s
tmpM
Int
srcLen ← forall s. MArray s -> Int -> ST s Int
appender MArray s
newM (Int
dstOff forall a. Num a => a -> a -> a
+ Int
dstLen)
ByteArray
new ← forall s. MArray s -> ST s ByteArray
A.unsafeFreeze MArray s
newM
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ByteArray -> Int -> Int -> Text
Text ByteArray
new Int
dstOff (Int
dstLen forall a. Num a => a -> a -> a
+ Int
srcLen)
{-# INLINE appendBounded #-}
appendExact
∷ Int
→ (∀ s. A.MArray s → Int → ST s ())
→ Buffer
⊸ Buffer
appendExact :: Int
-> (forall s. MArray s -> Int -> ST s ()) -> Buffer %1 -> Buffer
appendExact Int
srcLen forall s. MArray s -> Int -> ST s ()
appender =
Int
-> (forall s. MArray s -> Int -> ST s Int) -> Buffer %1 -> Buffer
appendBounded
Int
srcLen
(\MArray s
dst Int
dstOff → forall s. MArray s -> Int -> ST s ()
appender MArray s
dst Int
dstOff forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
srcLen)
{-# INLINE appendExact #-}
prependBounded
∷ Int
→ (∀ s. A.MArray s → Int → ST s Int)
→ (∀ s. A.MArray s → Int → ST s Int)
→ Buffer
⊸ Buffer
prependBounded :: Int
-> (forall s. MArray s -> Int -> ST s Int)
-> (forall s. MArray s -> Int -> ST s Int)
-> Buffer
%1 -> Buffer
prependBounded Int
maxSrcLen forall s. MArray s -> Int -> ST s Int
prepender forall s. MArray s -> Int -> ST s Int
appender (Buffer (Text ByteArray
dst Int
dstOff Int
dstLen))
| Int
maxSrcLen forall a. Ord a => a -> a -> Bool
<= Int
dstOff = Text -> Buffer
Buffer forall a b. (a -> b) -> a -> b
$ forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
MArray s
newM ← forall s. ByteArray -> ST s (MArray s)
unsafeThaw ByteArray
dst
Int
srcLen ← forall s. MArray s -> Int -> ST s Int
prepender MArray s
newM Int
dstOff
ByteArray
new ← forall s. MArray s -> ST s ByteArray
A.unsafeFreeze MArray s
newM
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ByteArray -> Int -> Int -> Text
Text ByteArray
new (Int
dstOff forall a. Num a => a -> a -> a
- Int
srcLen) (Int
srcLen forall a. Num a => a -> a -> a
+ Int
dstLen)
| Bool
otherwise = Text -> Buffer
Buffer forall a b. (a -> b) -> a -> b
$ forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
let dstFullLen :: Int
dstFullLen = ByteArray -> Int
sizeofByteArray ByteArray
dst
newOff :: Int
newOff = Int
dstLen forall a. Num a => a -> a -> a
+ Int
maxSrcLen
newFullLen :: Int
newFullLen = Int
2 forall a. Num a => a -> a -> a
* Int
newOff forall a. Num a => a -> a -> a
+ (Int
dstFullLen forall a. Num a => a -> a -> a
- Int
dstOff forall a. Num a => a -> a -> a
- Int
dstLen)
MArray s
newM ← (if ByteArray -> Bool
isPinned ByteArray
dst then forall s. Int -> ST s (MArray s)
A.newPinned else forall s. Int -> ST s (MArray s)
A.new) Int
newFullLen
Int
srcLen ← forall s. MArray s -> Int -> ST s Int
appender MArray s
newM Int
newOff
forall s. Int -> MArray s -> Int -> ByteArray -> Int -> ST s ()
A.copyI Int
dstLen MArray s
newM (Int
newOff forall a. Num a => a -> a -> a
+ Int
srcLen) ByteArray
dst Int
dstOff
ByteArray
new ← forall s. MArray s -> ST s ByteArray
A.unsafeFreeze MArray s
newM
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ByteArray -> Int -> Int -> Text
Text ByteArray
new Int
newOff (Int
dstLen forall a. Num a => a -> a -> a
+ Int
srcLen)
{-# INLINE prependBounded #-}
prependExact
∷ Int
→ (∀ s. A.MArray s → Int → ST s ())
→ Buffer
⊸ Buffer
prependExact :: Int
-> (forall s. MArray s -> Int -> ST s ()) -> Buffer %1 -> Buffer
prependExact Int
srcLen forall s. MArray s -> Int -> ST s ()
appender =
Int
-> (forall s. MArray s -> Int -> ST s Int)
-> (forall s. MArray s -> Int -> ST s Int)
-> Buffer
%1 -> Buffer
prependBounded
Int
srcLen
(\MArray s
dst Int
dstOff → forall s. MArray s -> Int -> ST s ()
appender MArray s
dst (Int
dstOff forall a. Num a => a -> a -> a
- Int
srcLen) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
srcLen)
(\MArray s
dst Int
dstOff → forall s. MArray s -> Int -> ST s ()
appender MArray s
dst Int
dstOff forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
srcLen)
{-# INLINE prependExact #-}
unsafeThaw ∷ A.Array → ST s (A.MArray s)
unsafeThaw :: forall s. ByteArray -> ST s (MArray s)
unsafeThaw (A.ByteArray ByteArray#
a) = forall s a. STRep s a -> ST s a
ST forall a b. (a -> b) -> a -> b
$ \State# s
s# →
(# State# s
s#, forall s. MutableByteArray# s -> MutableByteArray s
A.MutableByteArray (unsafeCoerce# :: forall a b. a -> b
unsafeCoerce# ByteArray#
a) #)
sizeofByteArray ∷ A.Array → Int
sizeofByteArray :: ByteArray -> Int
sizeofByteArray (A.ByteArray ByteArray#
a) = Int# -> Int
I# (ByteArray# -> Int#
sizeofByteArray# ByteArray#
a)
isPinned ∷ A.Array → Bool
isPinned :: ByteArray -> Bool
isPinned (A.ByteArray ByteArray#
a) = Int# -> Bool
isTrue# (ByteArray# -> Int#
isByteArrayPinned# ByteArray#
a)
(><) ∷ Buffer ⊸ Buffer ⊸ Buffer
infix 6 ><
Buffer (Text ByteArray
left Int
leftOff Int
leftLen) >< :: Buffer %1 -> Buffer %1 -> Buffer
>< Buffer (Text ByteArray
right Int
rightOff Int
rightLen) = Text -> Buffer
Buffer forall a b. (a -> b) -> a -> b
$ forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
let leftFullLen :: Int
leftFullLen = ByteArray -> Int
sizeofByteArray ByteArray
left
rightFullLen :: Int
rightFullLen = ByteArray -> Int
sizeofByteArray ByteArray
right
canCopyToLeft :: Bool
canCopyToLeft = Int
leftOff forall a. Num a => a -> a -> a
+ Int
leftLen forall a. Num a => a -> a -> a
+ Int
rightLen forall a. Ord a => a -> a -> Bool
<= Int
leftFullLen
canCopyToRight :: Bool
canCopyToRight = Int
leftLen forall a. Ord a => a -> a -> Bool
<= Int
rightOff
shouldCopyToLeft :: Bool
shouldCopyToLeft = Bool
canCopyToLeft Bool -> Bool -> Bool
&& (Bool -> Bool
not Bool
canCopyToRight Bool -> Bool -> Bool
|| Int
leftLen forall a. Ord a => a -> a -> Bool
>= Int
rightLen)
if Bool
shouldCopyToLeft
then do
MArray s
newM ← forall s. ByteArray -> ST s (MArray s)
unsafeThaw ByteArray
left
forall s. Int -> MArray s -> Int -> ByteArray -> Int -> ST s ()
A.copyI Int
rightLen MArray s
newM (Int
leftOff forall a. Num a => a -> a -> a
+ Int
leftLen) ByteArray
right Int
rightOff
ByteArray
new ← forall s. MArray s -> ST s ByteArray
A.unsafeFreeze MArray s
newM
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ByteArray -> Int -> Int -> Text
Text ByteArray
new Int
leftOff (Int
leftLen forall a. Num a => a -> a -> a
+ Int
rightLen)
else
if Bool
canCopyToRight
then do
MArray s
newM ← forall s. ByteArray -> ST s (MArray s)
unsafeThaw ByteArray
right
forall s. Int -> MArray s -> Int -> ByteArray -> Int -> ST s ()
A.copyI Int
leftLen MArray s
newM (Int
rightOff forall a. Num a => a -> a -> a
- Int
leftLen) ByteArray
left Int
leftOff
ByteArray
new ← forall s. MArray s -> ST s ByteArray
A.unsafeFreeze MArray s
newM
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ByteArray -> Int -> Int -> Text
Text ByteArray
new (Int
rightOff forall a. Num a => a -> a -> a
- Int
leftLen) (Int
leftLen forall a. Num a => a -> a -> a
+ Int
rightLen)
else do
let fullLen :: Int
fullLen = Int
leftOff forall a. Num a => a -> a -> a
+ Int
leftLen forall a. Num a => a -> a -> a
+ Int
rightLen forall a. Num a => a -> a -> a
+ (Int
rightFullLen forall a. Num a => a -> a -> a
- Int
rightOff forall a. Num a => a -> a -> a
- Int
rightLen)
MArray s
newM ← (if ByteArray -> Bool
isPinned ByteArray
left Bool -> Bool -> Bool
|| ByteArray -> Bool
isPinned ByteArray
right then forall s. Int -> ST s (MArray s)
A.newPinned else forall s. Int -> ST s (MArray s)
A.new) Int
fullLen
forall s. Int -> MArray s -> Int -> ByteArray -> Int -> ST s ()
A.copyI Int
leftLen MArray s
newM Int
leftOff ByteArray
left Int
leftOff
forall s. Int -> MArray s -> Int -> ByteArray -> Int -> ST s ()
A.copyI Int
rightLen MArray s
newM (Int
leftOff forall a. Num a => a -> a -> a
+ Int
leftLen) ByteArray
right Int
rightOff
ByteArray
new ← forall s. MArray s -> ST s ByteArray
A.unsafeFreeze MArray s
newM
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ByteArray -> Int -> Int -> Text
Text ByteArray
new Int
leftOff (Int
leftLen forall a. Num a => a -> a -> a
+ Int
rightLen)