HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulebytebuild-0.3.16.2Haskell2010

Data.Bytes.Builder.Bounded

The functions in this module are explict about the maximum number of bytes they require.

  • 1 type
  • 71 values

Builder

1 declaration
newtypenewtype Builder (a :: Nat) where
#

A builder parameterized by the maximum number of bytes it uses when executed.

Instances2ToBoundedBuilder, BoundedBuilderLength

Execute

3 declarations
valuerun
  1. :: Nat n
  2. -> Builder n

    Builder

  3. -> ByteArray
#

Execute the bounded builder. If the size is a constant, use Arithmetic.Nat.constant as the first argument to let GHC conjure up this value for you.

valuepasteGrowST
  1. :: Nat n
  2. -> Builder n
  3. -> MutableByteArrayOffset s

    Initial buffer, used linearly. Do not reuse this argument.

  4. -> ST s (MutableByteArrayOffset s)

    Final buffer that accomodated the builder.

#

Paste the builder into the byte array starting at offset zero. This reallocates the byte array if it cannot accomodate the builder, growing it by the minimum amount necessary.

Combine

2 declarations

Bounds Manipulation

2 declarations
valueweaken :: m <= n -> Builder m -> Builder n
#

Weaken the bound on the maximum number of bytes required. For example, to use two builders with unequal bounds in a disjunctive setting:

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)

Encode Integral Types

0 declarations

Human-Readable

valueword64Dec :: Word64 -> Builder 19
#

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.

valueword32Dec :: Word32 -> Builder 10
#

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.

valueword16Dec :: Word16 -> Builder 5
#

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.

valueword8Dec :: Word8 -> Builder 3
#

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.

valuewordDec :: Word -> Builder 19
#

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.

valueint64Dec :: Int64 -> Builder 20
#

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.

valueint32Dec :: Int32 -> Builder 11
#

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.

valueint16Dec :: Int16 -> Builder 6
#

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.

valueint8Dec :: Int8 -> Builder 4
#

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.

valueintDec :: Int -> Builder 20
#

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.

Unsigned Words

0 declarations

Wide Words

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.

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.

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.

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.

64-bit

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.

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.

48-bit

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.

32-bit

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.

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.

16-bit

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.

Example1 expression
word16PaddedLowerHex 0xab00ab0

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.

Example1 expression
word16PaddedUpperHex 0xab00AB0
valueword16LowerHex :: 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.

Example1 expression
word16LowerHex 0xab0ab0
valueword16UpperHex :: 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.

Example1 expression
word16UpperHex 0xab0AB0

8-bit

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.

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.

valueword8LowerHex :: 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.

valueascii :: Char -> Builder 1
#

Encode an ASCII character. Precondition: Input must be an ASCII character. This is not checked.

valueascii2 :: Char -> Char -> Builder 2
#

Encode two ASCII characters. Precondition: Must be an ASCII characters. This is not checked.

valueascii3 :: Char -> Char -> Char -> Builder 3
#

Encode three ASCII characters. Precondition: Must be an ASCII characters. This is not checked.

valueascii4 :: Char -> Char -> Char -> Char -> Builder 4
#

Encode four ASCII characters. Precondition: Must be an ASCII characters. This is not checked.

valuechar :: Char -> Builder 4
#

Encode a character as UTF-8. This only uses as much space as is required.

Native

valuewordPaddedDec2 :: Word -> Builder 2
#

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.

valuewordPaddedDec4 :: Word -> Builder 4
#

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.

valuewordPaddedDec9 :: Word -> Builder 9
#

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.

Machine-Readable

One

Big Endian

valueword64BE :: Word64 -> Builder 8
#

Requires exactly 8 bytes. Dump the octets of a 64-bit word in a big-endian fashion.

valueword32BE :: Word32 -> Builder 4
#

Requires exactly 4 bytes. Dump the octets of a 32-bit word in a big-endian fashion.

valueword16BE :: Word16 -> Builder 2
#

Requires exactly 2 bytes. Dump the octets of a 16-bit word in a big-endian fashion.

Little Endian

valueword64LE :: Word64 -> Builder 8
#

Requires exactly 8 bytes. Dump the octets of a 64-bit word in a little-endian fashion.

valueword32LE :: Word32 -> Builder 4
#

Requires exactly 4 bytes. Dump the octets of a 32-bit word in a little-endian fashion.

valueword16LE :: Word16 -> Builder 2
#

Requires exactly 2 bytes. Dump the octets of a 16-bit word in a little-endian fashion.

LEB128

LEB128 encodes an integer in 7-bit units, least significant bits first, with the high bit of each output byte set to 1 in all bytes except for the final byte.

VLQ

VLQ (also known as VByte, Varint, VInt) encodes an integer in 7-bit units, most significant bits first, with the high bit of each output byte set to 1 in all bytes except for the final byte.

valuewordVlq :: Word -> Builder 10
#

Encode a machine-sized word with VLQ (also known as VByte, Varint, VInt).

Encode Floating-Point Types

1 declaration
valuedoubleDec :: Double -> Builder 32
#

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.