-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Build byte arrays -- -- This is similar to the builder facilities provided by -- Data.ByteString.Builder. It is intended to be used in -- situations where the following apply: -- --
-- import qualified Arithmetic.Lte as Lte -- -- buildNumber :: Either Double Word64 -> Builder 32 -- buildNumber = \case -- Left d -> doubleDec d -- Right w -> weaken (Lte.constant @19 @32) (word64Dec w) --weaken :: forall m n. (m <= n) -> Builder m -> Builder n -- | Replace the upper bound on size with an equal number. substitute :: forall m n. (m :=: n) -> Builder m -> Builder n -- | Requires up to 19 bytes. Encodes an unsigned 64-bit integer as -- decimal. This encoding never starts with a zero unless the argument -- was zero. word64Dec :: Word64 -> Builder 19 -- | Requires up to 10 bytes. Encodes an unsigned 32-bit integer as -- decimal. This encoding never starts with a zero unless the argument -- was zero. word32Dec :: Word32 -> Builder 10 -- | Requires up to 5 bytes. Encodes an unsigned 16-bit integer as decimal. -- This encoding never starts with a zero unless the argument was zero. word16Dec :: Word16 -> Builder 5 -- | Requires up to 3 bytes. Encodes an unsigned 8-bit integer as decimal. -- This encoding never starts with a zero unless the argument was zero. word8Dec :: Word8 -> Builder 3 -- | Requires up to 19 bytes. Encodes an unsigned machine-sized integer as -- decimal. This encoding never starts with a zero unless the argument -- was zero. wordDec :: Word -> Builder 19 -- | Requires up to 20 bytes. Encodes a signed 64-bit integer as decimal. -- This encoding never starts with a zero unless the argument was zero. -- Negative numbers are preceded by a minus sign. Positive numbers are -- not preceded by anything. int64Dec :: Int64 -> Builder 20 -- | Requires up to 11 bytes. Encodes a signed 32-bit integer as decimal. -- This encoding never starts with a zero unless the argument was zero. -- Negative numbers are preceded by a minus sign. Positive numbers are -- not preceded by anything. int32Dec :: Int32 -> Builder 11 -- | Requires up to 6 bytes. Encodes a signed 16-bit integer as decimal. -- This encoding never starts with a zero unless the argument was zero. -- Negative numbers are preceded by a minus sign. Positive numbers are -- not preceded by anything. int16Dec :: Int16 -> Builder 6 -- | Requires up to 4 bytes. Encodes a signed 8-bit integer as decimal. -- This encoding never starts with a zero unless the argument was zero. -- Negative numbers are preceded by a minus sign. Positive numbers are -- not preceded by anything. int8Dec :: Int8 -> Builder 4 -- | Requires up to 20 bytes. Encodes a signed machine-sized integer as -- decimal. This encoding never starts with a zero unless the argument -- was zero. Negative numbers are preceded by a minus sign. Positive -- numbers are not preceded by anything. intDec :: Int -> Builder 20 -- | Requires exactly 32 bytes. Encodes a 128-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 32 digits. This uses -- lowercase for the alphabetical digits. word128PaddedLowerHex :: Word128 -> Builder 32 -- | Requires exactly 32 bytes. Encodes a 128-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 32 digits. This uses -- uppercase for the alphabetical digits. word128PaddedUpperHex :: Word128 -> Builder 32 -- | Requires exactly 64 bytes. Encodes a 256-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 64 digits. This uses -- lowercase for the alphabetical digits. word256PaddedLowerHex :: Word256 -> Builder 64 -- | Requires exactly 64 bytes. Encodes a 256-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 64 digits. This uses -- uppercase for the alphabetical digits. word256PaddedUpperHex :: Word256 -> Builder 64 -- | Requires exactly 16 bytes. Encodes a 64-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 16 digits. This uses -- lowercase for the alphabetical digits. For example, this encodes the -- number 1022 as 00000000000003fe. word64PaddedLowerHex :: Word64 -> Builder 16 -- | Requires exactly 16 bytes. Encodes a 64-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 16 digits. This uses -- uppercase for the alphabetical digits. For example, this encodes the -- number 1022 as 00000000000003FE. word64PaddedUpperHex :: Word64 -> Builder 16 -- | Requires exactly 12 bytes. Discards the upper 16 bits of a 64-bit -- unsigned integer and then encodes the lower 48 bits as hexadecimal, -- zero-padding the encoding to 12 digits. This uses lowercase for the -- alphabetical digits. For example, this encodes the number 1022 as -- 0000000003fe. word48PaddedLowerHex :: Word64 -> Builder 12 -- | Requires exactly 8 bytes. Encodes a 32-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 8 digits. This uses -- lowercase for the alphabetical digits. word32PaddedLowerHex :: Word32 -> Builder 8 -- | Requires exactly 8 bytes. Encodes a 32-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 8 digits. This uses -- uppercase for the alphabetical digits. word32PaddedUpperHex :: Word32 -> Builder 8 -- | Requires exactly 4 bytes. Encodes a 16-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 4 digits. This uses -- lowercase for the alphabetical digits. -- --
-- >>> word16PaddedLowerHex 0xab0 -- 0ab0 --word16PaddedLowerHex :: Word16 -> Builder 4 -- | Requires exactly 4 bytes. Encodes a 16-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 4 digits. This uses -- uppercase for the alphabetical digits. -- --
-- >>> word16PaddedUpperHex 0xab0 -- 0AB0 --word16PaddedUpperHex :: Word16 -> Builder 4 -- | Requires at most 4 bytes. Encodes a 16-bit unsigned integer as -- hexadecimal. No leading zeroes are displayed. Letters are presented in -- lowercase. If the number is zero, a single zero digit is used. -- --
-- >>> word16LowerHex 0xab0 -- ab0 --word16LowerHex :: Word16 -> Builder 4 -- | Requires at most 4 bytes. Encodes a 16-bit unsigned integer as -- hexadecimal. No leading zeroes are displayed. Letters are presented in -- uppercase. If the number is zero, a single zero digit is used. -- --
-- >>> word16UpperHex 0xab0 -- AB0 --word16UpperHex :: Word16 -> Builder 4 -- | Requires exactly 2 bytes. Encodes a 8-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 2 digits. This uses -- lowercase for the alphabetical digits. word8PaddedLowerHex :: Word8 -> Builder 2 -- | Requires exactly 2 bytes. Encodes a 8-bit unsigned integer as -- hexadecimal, zero-padding the encoding to 2 digits. This uses -- uppercase for the alphabetical digits. word8PaddedUpperHex :: Word8 -> Builder 2 -- | Requires at most 2 bytes. Encodes a 8-bit unsigned integer as -- hexadecimal. No leading zeroes are displayed. If the number is zero, a -- single zero digit is used. word8LowerHex :: Word8 -> Builder 2 -- | Encode an ASCII character. Precondition: Input must be an ASCII -- character. This is not checked. ascii :: Char -> Builder 1 -- | Encode two ASCII characters. Precondition: Must be an ASCII -- characters. This is not checked. ascii2 :: Char -> Char -> Builder 2 -- | Encode three ASCII characters. Precondition: Must be an ASCII -- characters. This is not checked. ascii3 :: Char -> Char -> Char -> Builder 3 -- | Encode four ASCII characters. Precondition: Must be an ASCII -- characters. This is not checked. ascii4 :: Char -> Char -> Char -> Char -> Builder 4 -- | Encode five ASCII characters. Precondition: Must be an ASCII -- characters. This is not checked. ascii5 :: Char -> Char -> Char -> Char -> Char -> Builder 5 -- | Encode six ASCII characters. Precondition: Must be an ASCII -- characters. This is not checked. ascii6 :: Char -> Char -> Char -> Char -> Char -> Char -> Builder 6 -- | Encode seven ASCII characters. Precondition: Must be an ASCII -- characters. This is not checked. ascii7 :: Char -> Char -> Char -> Char -> Char -> Char -> Char -> Builder 7 -- | Encode eight ASCII characters. Precondition: Must be an ASCII -- characters. This is not checked. ascii8 :: Char -> Char -> Char -> Char -> Char -> Char -> Char -> Char -> Builder 8 -- | Encode a character as UTF-8. This only uses as much space as is -- required. char :: Char -> Builder 4 -- | Encode a number less than 100 as a decimal number, zero-padding it to -- two digits. For example: 0 is encoded as 00, 5 is encoded as -- 05, and 73 is encoded as 73. -- -- Precondition: Argument must be less than 100. Failure to satisfy this -- precondition will not result in a segfault, but the resulting bytes -- are undefined. The implemention uses a heuristic for division that is -- inaccurate for large numbers. wordPaddedDec2 :: Word -> Builder 2 -- | Encode a number less than 10000 as a decimal number, zero-padding it -- to two digits. For example: 0 is encoded as 0000, 5 is -- encoded as 0005, and 73 is encoded as 0073. -- -- Precondition: Argument must be less than 10000. Failure to satisfy -- this precondition will not result in a segfault, but the resulting -- bytes are undefined. The implemention uses a heuristic for division -- that is inaccurate for large numbers. wordPaddedDec4 :: Word -> Builder 4 -- | Encode a number less than 1e9 as a decimal number, zero-padding it to -- nine digits. For example: 0 is encoded as 000000000 and 5 is -- encoded as 000000005. -- -- Precondition: Argument must be less than 1e9. Failure to satisfy this -- precondition will not result in a segfault, but the resulting bytes -- are undefined. The implemention uses a heuristic for division that is -- inaccurate for large numbers. wordPaddedDec9 :: Word -> Builder 9 word8 :: Word8 -> Builder 1 word256BE :: Word256 -> Builder 32 word128BE :: Word128 -> Builder 16 -- | Requires exactly 8 bytes. Dump the octets of a 64-bit word in a -- big-endian fashion. word64BE :: Word64 -> Builder 8 -- | Requires exactly 4 bytes. Dump the octets of a 32-bit word in a -- big-endian fashion. word32BE :: Word32 -> Builder 4 -- | Requires exactly 2 bytes. Dump the octets of a 16-bit word in a -- big-endian fashion. word16BE :: Word16 -> Builder 2 int64BE :: Int64 -> Builder 8 int32BE :: Int32 -> Builder 4 int16BE :: Int16 -> Builder 2 word256LE :: Word256 -> Builder 32 word128LE :: Word128 -> Builder 16 -- | Requires exactly 8 bytes. Dump the octets of a 64-bit word in a -- little-endian fashion. word64LE :: Word64 -> Builder 8 -- | Requires exactly 4 bytes. Dump the octets of a 32-bit word in a -- little-endian fashion. word32LE :: Word32 -> Builder 4 -- | Requires exactly 2 bytes. Dump the octets of a 16-bit word in a -- little-endian fashion. word16LE :: Word16 -> Builder 2 int64LE :: Int64 -> Builder 8 int32LE :: Int32 -> Builder 4 int16LE :: Int16 -> Builder 2 -- | Encode a machine-sized word with LEB-128. wordLEB128 :: Word -> Builder 10 -- | Encode a 32-bit word with LEB-128. word32LEB128 :: Word32 -> Builder 5 -- | Encode a 64-bit word with LEB-128. word64LEB128 :: Word64 -> Builder 10 -- | Encode a double-floating-point number, using decimal notation or -- scientific notation depending on the magnitude. This has undefined -- behavior when representing +inf, -inf, and -- NaN. It will not crash, but the generated numbers will be -- nonsense. doubleDec :: Double -> Builder 32 module Data.Bytes.Builder.Bounded.Class -- | Variant of To that can be encoded as a builder. Human-readable -- encodings are used when possible. For example, numbers are encoded an -- ascii-encoded decimal characters. UTF-8 is preferred for textual -- types. For types that represent arbitrary bytes (e.g. Bytes, -- ByteString), the bytes are preserved. -- -- The goal of this typeclass is to reduce the size of builders produced -- by quasiquotation. class ToBoundedBuilder a where { type BoundedBuilderLength a :: Nat; } toBuilder :: ToBoundedBuilder a => a -> Builder (BoundedBuilderLength a) instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder (Data.Bytes.Builder.Bounded.Unsafe.Builder n) instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder GHC.Int.Int64 instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder GHC.Int.Int32 instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder GHC.Int.Int16 instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder GHC.Int.Int8 instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder GHC.Types.Int instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder GHC.Word.Word64 instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder GHC.Word.Word32 instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder GHC.Word.Word16 instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder GHC.Word.Word8 instance Data.Bytes.Builder.Bounded.Class.ToBoundedBuilder GHC.Types.Word module Data.Bytes.Builder.Unsafe -- | An unmaterialized sequence of bytes that may be pasted into a mutable -- byte array. newtype Builder Builder :: (forall s. MutableByteArray# s -> Int# -> Int# -> Commits s -> State# s -> (# State# s, MutableByteArray# s, Int#, Int#, Commits s #)) -> Builder -- | A list of committed chunks along with the chunk currently being -- written to. This is kind of like a non-empty variant of -- Commmits but with the additional invariant that the head -- chunk is a mutable byte array. data BuilderState s BuilderState :: MutableByteArray# s -> Int# -> Int# -> !Commits s -> BuilderState s data Commits s Mutable :: MutableByteArray# s -> Int# -> !Commits s -> Commits s Immutable :: ByteArray# -> Int# -> Int# -> !Commits s -> Commits s Initial :: Commits s -- | Run a builder, performing an in-place update on the state. The -- BuilderState argument must not be reused after being passed -- to this function. That is, its use must be affine. pasteST :: Builder -> BuilderState s -> ST s (BuilderState s) -- | Variant of pasteST that runs in IO. pasteIO :: Builder -> BuilderState RealWorld -> IO (BuilderState RealWorld) fromEffect :: Int -> (forall s. MutableByteArray s -> Int -> ST s Int) -> Builder -- | Create an empty BuilderState with a buffer of the given size. newBuilderState :: Int -> ST s (BuilderState s) -- | Push the active chunk onto the top of the commits. The -- BuilderState argument must not be reused after being passed -- to this function. That is, its use must be affine. closeBuilderState :: BuilderState s -> Commits s -- | Cons the chunks from a list of Commits onto an initial -- Chunks list (this argument is often ChunksNil). This -- reverses the order of the chunks, which is desirable since builders -- assemble Commits with the chunks backwards. This performs an -- in-place shrink and freezes any mutable byte arrays it encounters. -- Consequently, these must not be reused. reverseCommitsOntoChunks :: Chunks -> Commits s -> ST s Chunks -- | Variant of reverseCommitsOntoChunks that does not reverse the -- order of the commits. Since commits are built backwards by consing, -- this means that the chunks appended to the front will be backwards. -- Within each chunk, however, the bytes will be in the correct order. -- -- Unlike reverseCommitsOntoChunks, this function is not tail -- recursive. commitsOntoChunks :: Chunks -> Commits s -> ST s Chunks -- | Copy the contents of the chunks into a mutable array, reversing the -- order of the chunks. Precondition: The destination must have enough -- space to house the contents. This is not checked. copyReverseCommits :: MutableByteArray s -> Int -> Commits s -> ST s Int -- | Add the total number of bytes in the commits to first argument. addCommitsLength :: Int -> Commits s -> Int -- | Compute the number of bytes between the last byte and the offset -- specified in a chunk. Precondition: the chunk must exist in the list -- of committed chunks. This relies on mutable byte arrays having -- identity (e.g. it uses sameMutableByteArray#). commitDistance :: MutableByteArray# s -> Int# -> Commits s -> Int# -- | Variant of commitDistance where you get to supply a head of the commit -- list that has not yet been committed. commitDistance1 :: MutableByteArray# s -> Int# -> MutableByteArray# s -> Int# -> Commits s -> Int# -- | Create a builder from a cons-list of Char. These must be UTF-8 -- encoded. stringUtf8 :: String -> Builder -- | Create a builder from a NUL-terminated CString. This -- ignores any textual encoding, copying bytes until NUL is -- reached. cstring :: CString -> Builder instance Data.String.IsString Data.Bytes.Builder.Unsafe.Builder instance GHC.Base.Semigroup Data.Bytes.Builder.Unsafe.Builder instance GHC.Base.Monoid Data.Bytes.Builder.Unsafe.Builder module Data.Bytes.Builder -- | An unmaterialized sequence of bytes that may be pasted into a mutable -- byte array. data Builder -- | Convert a bounded builder to an unbounded one. If the size is a -- constant, use Arithmetic.Nat.constant as the first argument -- to let GHC conjure up this value for you. fromBounded :: Nat n -> Builder n -> Builder -- | Run a builder. run :: Int -> Builder -> Chunks -- | Run a builder. The resulting chunks are consed onto the beginning of -- an existing sequence of chunks. runOnto :: Int -> Builder -> Chunks -> Chunks -- | Variant of runOnto that conses the additional chunks in reverse -- order. reversedOnto :: Int -> Builder -> Chunks -> Chunks -- | Run a builder against lots of elements. This fills the same underlying -- buffer over and over again. Do not let the argument to the callback -- escape from the callback (i.e. do not write it to an IORef). -- Also, do not unsafeFreezeByteArray any of the mutable byte -- arrays in the callback. The intent is that the callback will write the -- buffer out. putMany :: Foldable f => Int -> (a -> Builder) -> f a -> (MutableBytes RealWorld -> IO b) -> IO () -- | Variant of putMany that prefixes each pushed array of chunks -- with the number of bytes that the chunks in each batch required. (This -- excludes the bytes required to encode the length itself.) This is -- useful for chunked HTTP encoding. putManyConsLength :: (Foldable f, MonadIO m) => Nat n -> (Int -> Builder n) -> Int -> (a -> Builder) -> f a -> (MutableBytes RealWorld -> m b) -> m () -- | Create a builder from a sliced byte sequence. The variants copy -- and insert provide more control over whether or not the byte -- sequence is copied or aliased. This function is preferred when the -- user does not know the size of the byte sequence. bytes :: Bytes -> Builder -- | Create a builder from a byte sequence. This always results in a call -- to memcpy. This is beneficial when the byte sequence is known -- to be small (less than 256 bytes). copy :: Bytes -> Builder -- | Variant of copy that additionally pastes an extra byte in front -- of the bytes. copyCons :: Word8 -> Bytes -> Builder -- | Create a builder from two byte sequences. This always results in two -- calls to memcpy. This is beneficial when the byte sequences -- are known to be small (less than 256 bytes). copy2 :: Bytes -> Bytes -> Builder -- | Create a builder from a byte sequence. This never calls -- memcpy. Instead, it pushes a chunk that references the -- argument byte sequence. This wastes the remaining space in the active -- chunk, so it may adversely affect performance if used carelessly. See -- flush for a way to mitigate this problem. This functions is -- most beneficial when the byte sequence is known to be large (more than -- 8192 bytes). insert :: Bytes -> Builder -- | Create a builder from an unsliced byte sequence. Implemented with -- bytes. byteArray :: ByteArray -> Builder -- | Create a builder from a short bytestring. Implemented with -- bytes. shortByteString :: ShortByteString -> Builder -- | Create a builder from text. The text will be UTF-8 encoded. shortTextUtf8 :: ShortText -> Builder -- | Create a builder from text. The text will be UTF-8 encoded, and JSON -- special characters will be escaped. Additionally, the result is -- surrounded by double quotes. For example: -- --
-- forall (n :: Int) (x :: Builder). -- let sz = sizeofByteArray (run n (consLength32BE x)) -- consLength32BE x === word32BE (fromIntegral sz) <> x ---- -- However, using consLength32BE is much more efficient here since -- it only materializes the ByteArray once. consLength32BE :: Builder -> Builder -- | Prefix a builder with its size in bytes. This size is presented as a -- big-endian 64-bit word. See consLength32BE. consLength64BE :: Builder -> Builder -- | Encode a double-floating-point number, using decimal notation or -- scientific notation depending on the magnitude. This has undefined -- behavior when representing +inf, -inf, and -- NaN. It will not crash, but the generated numbers will be -- nonsense. doubleDec :: Double -> Builder -- | Replicate a byte the given number of times. replicate :: Int -> Word8 -> Builder -- | Push the buffer currently being filled onto the chunk list, allocating -- a new active buffer of the requested size. This is helpful when a -- small builder is sandwhiched between two large zero-copy builders: -- --
-- insert bigA <> flush 1 <> word8 0x42 <> insert bigB ---- -- Without flush 1, word8 0x42 would see the zero-byte -- active buffer that insert returned, decide that it needed more -- space, and allocate a 4080-byte buffer to which only a single byte -- would be written. flush :: Int -> Builder -- | This function and the documentation for it are copied from Takano -- Akio's fast-builder library. -- -- rebuild b is equivalent to b, but it allows -- GHC to assume that b will be run at most once. This can -- enable various optimizations that greately improve performance. -- -- There are two types of typical situations where a use of -- rebuild is often a win: -- --
-- [templ|[WARN] Partition `partition` has invalid data.\n|] ---- -- This expression has type Builder and expands to: -- --
-- Builder.cstringLen (Ptr "[WARN] Partition "#, 17) <> -- Builder.toBuilder partition <> -- Builder.cstringLen (Ptr " has invalid data.\n"#, 19) ---- -- The ToBuilder instance for Word32 uses decimal -- encoding, so this would result in the following if partition -- was 42 (with a newline character at the end): -- --
-- [WARN] Partition 42 has invalid data. ---- -- In the future, a more sophisticated bbldr variant will be -- added that will support expressions where the maximum length of the -- entire builder can be computed at compile time. bldr :: QuasiQuoter