{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Proto3.Wire.Builder
{-# DEPRECATED "This module is no longer used by the rest of the proto3-wire package." #-}
(
Builder
, byteString
, lazyByteString
, shortByteString
, word8
, word16BE
, word16LE
, word32BE
, word32LE
, word64BE
, word64LE
, word64Base128LEVar
, int8
, int16BE
, int16LE
, int32BE
, int32LE
, int64BE
, int64LE
, floatBE
, floatLE
, doubleBE
, doubleLE
, char7
, string7
, char8
, string8
, charUtf8
, stringUtf8
, builderLength
, rawBuilder
, toLazyByteString
, hPutBuilder
, unsafeMakeBuilder
) where
import Data.Bits ((.|.), shiftR)
import qualified Data.ByteString as B
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Builder.Extra as BB
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Short as BS
import Data.Char ( ord )
import Data.Int ( Int8, Int16, Int32, Int64 )
import Data.Semigroup ( Sum(..) )
import Data.Word ( Word8, Word16, Word32, Word64 )
import System.IO ( Handle )
data Builder = Builder {-# UNPACK #-} !(Sum Word) BB.Builder
instance Semigroup Builder where
Builder Sum Word
s Builder
b <> :: Builder -> Builder -> Builder
<> Builder Sum Word
s1 Builder
b1 = Sum Word -> Builder -> Builder
Builder (Sum Word
s Sum Word -> Sum Word -> Sum Word
forall a. Semigroup a => a -> a -> a
<> Sum Word
s1) (Builder
b Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
b1)
instance Monoid Builder where
mempty :: Builder
mempty = Sum Word -> Builder -> Builder
Builder Sum Word
forall a. Monoid a => a
mempty Builder
forall a. Monoid a => a
mempty
mappend :: Builder -> Builder -> Builder
mappend = Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
(<>)
instance Show Builder where
showsPrec :: Int -> Builder -> ShowS
showsPrec Int
prec Builder
builder =
Bool -> ShowS -> ShowS
showParen (Int
prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10)
(String -> ShowS
showString String
"Proto3.Wire.Builder.lazyByteString " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShowS
forall a. Show a => a -> ShowS
shows ByteString
bytes)
where
bytes :: ByteString
bytes = Builder -> ByteString
toLazyByteString Builder
builder
builderLength :: Builder -> Word
builderLength :: Builder -> Word
builderLength (Builder Sum Word
x Builder
_) = Sum Word -> Word
forall a. Sum a -> a
getSum Sum Word
x
rawBuilder :: Builder -> BB.Builder
rawBuilder :: Builder -> Builder
rawBuilder (Builder Sum Word
_ Builder
x) = Builder
x
unsafeMakeBuilder :: Word -> BB.Builder -> Builder
unsafeMakeBuilder :: Word -> Builder -> Builder
unsafeMakeBuilder Word
len Builder
bldr = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
len) Builder
bldr
toLazyByteString :: Builder -> BL.ByteString
toLazyByteString :: Builder -> ByteString
toLazyByteString (Builder (Sum Word
len) Builder
bb) =
AllocationStrategy -> ByteString -> Builder -> ByteString
BB.toLazyByteStringWith AllocationStrategy
strat ByteString
BL.empty Builder
bb
where
strat :: AllocationStrategy
strat = Int -> Int -> AllocationStrategy
BB.safeStrategy (Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
len) Int
BB.defaultChunkSize
{-# NOINLINE toLazyByteString #-}
hPutBuilder :: Handle -> Builder -> IO ()
hPutBuilder :: Handle -> Builder -> IO ()
hPutBuilder Handle
handle = Handle -> Builder -> IO ()
BB.hPutBuilder Handle
handle (Builder -> IO ()) -> (Builder -> Builder) -> Builder -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
rawBuilder
byteString :: B.ByteString -> Builder
byteString :: ByteString -> Builder
byteString ByteString
bs =
Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
bs))) (ByteString -> Builder
BB.byteStringCopy ByteString
bs)
lazyByteString :: BL.ByteString -> Builder
lazyByteString :: ByteString -> Builder
lazyByteString ByteString
bl =
Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum (Int64 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
BL.length ByteString
bl))) (ByteString -> Builder
BB.lazyByteStringCopy ByteString
bl)
shortByteString :: BS.ShortByteString -> Builder
shortByteString :: ShortByteString -> Builder
shortByteString ShortByteString
bs =
Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ShortByteString -> Int
BS.length ShortByteString
bs))) (ShortByteString -> Builder
BB.shortByteString ShortByteString
bs)
word8 :: Word8 -> Builder
word8 :: Word8 -> Builder
word8 Word8
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
1) (Word8 -> Builder
BB.word8 Word8
w)
int8 :: Int8 -> Builder
int8 :: Int8 -> Builder
int8 Int8
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
1) (Int8 -> Builder
BB.int8 Int8
w)
word16BE :: Word16 -> Builder
word16BE :: Word16 -> Builder
word16BE Word16
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
2) (Word16 -> Builder
BB.word16BE Word16
w)
word16LE :: Word16 -> Builder
word16LE :: Word16 -> Builder
word16LE Word16
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
2) (Word16 -> Builder
BB.word16LE Word16
w)
int16BE :: Int16 -> Builder
int16BE :: Int16 -> Builder
int16BE Int16
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
2) (Int16 -> Builder
BB.int16BE Int16
w)
int16LE :: Int16 -> Builder
int16LE :: Int16 -> Builder
int16LE Int16
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
2) (Int16 -> Builder
BB.int16LE Int16
w)
word32BE :: Word32 -> Builder
word32BE :: Word32 -> Builder
word32BE Word32
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
4) (Word32 -> Builder
BB.word32BE Word32
w)
word32LE :: Word32 -> Builder
word32LE :: Word32 -> Builder
word32LE Word32
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
4) (Word32 -> Builder
BB.word32LE Word32
w)
int32BE :: Int32 -> Builder
int32BE :: Int32 -> Builder
int32BE Int32
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
4) (Int32 -> Builder
BB.int32BE Int32
w)
int32LE :: Int32 -> Builder
int32LE :: Int32 -> Builder
int32LE Int32
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
4) (Int32 -> Builder
BB.int32LE Int32
w)
floatBE :: Float -> Builder
floatBE :: Float -> Builder
floatBE Float
f = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
4) (Float -> Builder
BB.floatBE Float
f)
floatLE :: Float -> Builder
floatLE :: Float -> Builder
floatLE Float
f = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
4) (Float -> Builder
BB.floatLE Float
f)
word64BE :: Word64 -> Builder
word64BE :: Word64 -> Builder
word64BE Word64
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
8) (Word64 -> Builder
BB.word64BE Word64
w)
word64LE :: Word64 -> Builder
word64LE :: Word64 -> Builder
word64LE Word64
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
8) (Word64 -> Builder
BB.word64LE Word64
w)
word64Base128LEVar :: Word64 -> Builder
word64Base128LEVar :: Word64 -> Builder
word64Base128LEVar Word64
i
| Word64
i Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0x80 = Word8 -> Builder
word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i)
| Word64
i Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0x4000 = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
2) (Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
7)))
| Word64
i Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0x200000 = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
3) (Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
7) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
14)))
| Word64
i Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0x10000000 = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
4) (Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
7) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
14) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
21)))
| Word64
i Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0x800000000 = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
5) (Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
7) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
14) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
21) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
28)))
| Word64
i Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0x40000000000 = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
6) (Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
7) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
14) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
21) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
28) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
35)))
| Word64
i Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0x2000000000000 = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
7) (Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
7) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
14) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
21) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
28) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
35) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
42)))
| Word64
i Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0x100000000000000 = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
8) (Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
7) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
14) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
21) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
28) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
35) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
42) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
49)))
| Word64
i Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0x8000000000000000 = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
9) (Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
7) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
14) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
21) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
28) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
35) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
42) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
49) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
56)))
| Bool
otherwise = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
10) (Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
7) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
14) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
21) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
28) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
35) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
42) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
49) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
56) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
Word8 -> Builder
BB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
i Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
63)))
int64BE :: Int64 -> Builder
int64BE :: Int64 -> Builder
int64BE Int64
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
8) (Int64 -> Builder
BB.int64BE Int64
w)
int64LE :: Int64 -> Builder
int64LE :: Int64 -> Builder
int64LE Int64
w = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
8) (Int64 -> Builder
BB.int64LE Int64
w)
doubleBE :: Double -> Builder
doubleBE :: Double -> Builder
doubleBE Double
f = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
8) (Double -> Builder
BB.doubleBE Double
f)
doubleLE :: Double -> Builder
doubleLE :: Double -> Builder
doubleLE Double
f = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
8) (Double -> Builder
BB.doubleLE Double
f)
char7 :: Char -> Builder
char7 :: Char -> Builder
char7 Char
c = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
1) (Char -> Builder
BB.char7 Char
c)
string7 :: String -> Builder
string7 :: String -> Builder
string7 String
s = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s))) (String -> Builder
BB.string7 String
s)
char8 :: Char -> Builder
char8 :: Char -> Builder
char8 Char
c = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum Word
1) (Char -> Builder
BB.char8 Char
c)
string8 :: String -> Builder
string8 :: String -> Builder
string8 String
s = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s))) (String -> Builder
BB.string8 String
s)
charUtf8 :: Char -> Builder
charUtf8 :: Char -> Builder
charUtf8 Char
c = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum (Char -> Word
utf8Width Char
c)) (Char -> Builder
BB.charUtf8 Char
c)
stringUtf8 :: String -> Builder
stringUtf8 :: String -> Builder
stringUtf8 String
s = Sum Word -> Builder -> Builder
Builder (Word -> Sum Word
forall a. a -> Sum a
Sum (Word -> String -> Word
len Word
0 String
s)) (String -> Builder
BB.stringUtf8 String
s)
where
len :: Word -> String -> Word
len !Word
n [] = Word
n
len !Word
n (Char
h : String
t) = Word -> String -> Word
len (Word
n Word -> Word -> Word
forall a. Num a => a -> a -> a
+ Char -> Word
utf8Width Char
h) String
t
{-# INLINABLE stringUtf8 #-}
utf8Width :: Char -> Word
utf8Width :: Char -> Word
utf8Width Char
c = case Char -> Int
ord Char
c of
Int
o | Int
o Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x007F -> Word
1
| Int
o Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x07FF -> Word
2
| Int
o Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0xFFFF -> Word
3
| Bool
otherwise -> Word
4
{-# INLINE utf8Width #-}