module Data.Text.Builder.Linear.Hex (
(|>&),
(&<|),
) where
import Data.Bits (Bits (..), FiniteBits (..))
import Data.Text.Array qualified as A
import GHC.Exts (Int (..), (>#))
import GHC.ST (ST)
import Data.Text.Builder.Linear.Core
(|>&) ∷ (Integral a, FiniteBits a) ⇒ Buffer ⊸ a → Buffer
infixl 6 |>&
Buffer
buffer |>& :: forall a. (Integral a, FiniteBits a) => Buffer %1 -> a -> Buffer
|>& a
n =
Int
-> (forall s. MArray s -> Int -> ST s Int) -> Buffer %1 -> Buffer
appendBounded
(forall b. FiniteBits b => b -> Int
finiteBitSize a
n forall a. Bits a => a -> Int -> a
`shiftR` Int
2)
(\MArray s
dst Int
dstOff → forall a s.
(Integral a, FiniteBits a) =>
MArray s -> Int -> a -> ST s Int
unsafeAppendHex MArray s
dst Int
dstOff a
n)
Buffer
buffer
{-# INLINEABLE (|>&) #-}
(&<|) ∷ (Integral a, FiniteBits a) ⇒ a → Buffer ⊸ Buffer
infixr 6 &<|
a
n &<| :: forall a. (Integral a, FiniteBits a) => a -> Buffer %1 -> Buffer
&<| Buffer
buffer =
Int
-> (forall s. MArray s -> Int -> ST s Int)
-> (forall s. MArray s -> Int -> ST s Int)
-> Buffer
%1 -> Buffer
prependBounded
(forall b. FiniteBits b => b -> Int
finiteBitSize a
n forall a. Bits a => a -> Int -> a
`shiftR` Int
2)
(\MArray s
dst Int
dstOff → forall a s.
(Integral a, FiniteBits a) =>
MArray s -> Int -> a -> ST s Int
unsafePrependHex MArray s
dst Int
dstOff a
n)
(\MArray s
dst Int
dstOff → forall a s.
(Integral a, FiniteBits a) =>
MArray s -> Int -> a -> ST s Int
unsafeAppendHex MArray s
dst Int
dstOff a
n)
Buffer
buffer
{-# INLINEABLE (&<|) #-}
unsafeAppendHex ∷ (Integral a, FiniteBits a) ⇒ A.MArray s → Int → a → ST s Int
unsafeAppendHex :: forall a s.
(Integral a, FiniteBits a) =>
MArray s -> Int -> a -> ST s Int
unsafeAppendHex MArray s
marr !Int
off a
0 =
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr Int
off Word8
0x30 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
1
unsafeAppendHex MArray s
marr !Int
off a
n = forall {t}. (Integral t, Bits t) => Int -> t -> ST s Int
go (Int
off forall a. Num a => a -> a -> a
+ Int
len forall a. Num a => a -> a -> a
- Int
1) a
n
where
len :: Int
len = forall b. FiniteBits b => b -> Int
lengthAsHex a
n
go :: Int -> t -> ST s Int
go !Int
_ t
0 = forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
len
go !Int
o t
m = do
let nibble :: t
nibble = t
m forall a. Bits a => a -> a -> a
.&. t
0x0f
forall s. MArray s -> Int -> Int -> ST s ()
writeNibbleAsHex MArray s
marr Int
o (forall a b. (Integral a, Num b) => a -> b
fromIntegral t
nibble)
Int -> t -> ST s Int
go (Int
o forall a. Num a => a -> a -> a
- Int
1) (t
m forall a. Bits a => a -> Int -> a
`shiftR` Int
4)
{-# INLINEABLE unsafeAppendHex #-}
unsafePrependHex ∷ (Integral a, FiniteBits a) ⇒ A.MArray s → Int → a → ST s Int
unsafePrependHex :: forall a s.
(Integral a, FiniteBits a) =>
MArray s -> Int -> a -> ST s Int
unsafePrependHex MArray s
marr !Int
off a
0 =
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr (Int
off forall a. Num a => a -> a -> a
- Int
1) Word8
0x30 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
1
unsafePrependHex MArray s
marr !Int
off a
n = forall {t}. (Integral t, Bits t) => Int -> t -> ST s Int
go (Int
off forall a. Num a => a -> a -> a
- Int
1) a
n
where
go :: Int -> t -> ST s Int
go !Int
o t
0 = forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
off forall a. Num a => a -> a -> a
- Int
1 forall a. Num a => a -> a -> a
- Int
o)
go !Int
o t
m = do
let nibble :: t
nibble = t
m forall a. Bits a => a -> a -> a
.&. t
0x0f
forall s. MArray s -> Int -> Int -> ST s ()
writeNibbleAsHex MArray s
marr Int
o (forall a b. (Integral a, Num b) => a -> b
fromIntegral t
nibble)
Int -> t -> ST s Int
go (Int
o forall a. Num a => a -> a -> a
- Int
1) (t
m forall a. Bits a => a -> Int -> a
`shiftR` Int
4)
{-# INLINEABLE unsafePrependHex #-}
lengthAsHex ∷ FiniteBits a ⇒ a → Int
lengthAsHex :: forall b. FiniteBits b => b -> Int
lengthAsHex a
n = (forall b. FiniteBits b => b -> Int
finiteBitSize a
n forall a. Bits a => a -> Int -> a
`shiftR` Int
2) forall a. Num a => a -> a -> a
- (forall b. FiniteBits b => b -> Int
countLeadingZeros a
n forall a. Bits a => a -> Int -> a
`shiftR` Int
2)
{-# INLINEABLE lengthAsHex #-}
writeNibbleAsHex ∷ A.MArray s → Int → Int → ST s ()
writeNibbleAsHex :: forall s. MArray s -> Int -> Int -> ST s ()
writeNibbleAsHex MArray s
marr Int
off n :: Int
n@(I# Int#
n#) = forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr Int
off (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
hex)
where
hex :: Int
hex = Int
0x30 forall a. Num a => a -> a -> a
+ Int
n forall a. Num a => a -> a -> a
+ Int# -> Int
I# (Int#
n# Int# -> Int# -> Int#
># Int#
9#) forall a. Num a => a -> a -> a
* (Int
0x60 forall a. Num a => a -> a -> a
- Int
0x39)