BSV.VarInt (BSV v2.1.0) View Source

A VarInt is an integer encoded as a variable length binary value. It is a format used throughout Bitcoin to represent the length of binary data in a compact form.

Link to this section Summary

Types

t()

VarInt binary

Functions

Decodes the given VarInt binary into an integer.

Decodes the given VarInt binary into an integer.

Returns a binary of the length specified by the VarInt in the first bytes of the binary. Any remaining bytes are ignored.

Returns a binary of the length specified by the VarInt in the first bytes of the binary.

Encodes the given integer into a VarInt binary.

Prepends the given binary with a VarInt representing the length of the binary.

Parses the given binary, returning a tuple with a binary of the length specified by the VarInt in the first bytes of the binary, and a binary of any remaining bytes.

Parses the given binary, returning a tuple with an integer decoded from the VarInt in the first bytes of the binary, and a binary of any remaining bytes.

Parses the given binary into a list of the length specified by the VarInt in the first bytes of the binary. Each item is parsed according to the specified Serializable.t/0.

Link to this section Types

Specs

t() :: binary()

VarInt binary

Link to this section Functions

Specs

decode(binary()) :: {:ok, integer()} | {:error, term()}

Decodes the given VarInt binary into an integer.

Returns the result in an :ok / :error tuple pair.

Examples

iex> BSV.VarInt.decode(<<253, 4, 1>>)
{:ok, 260}

iex> BSV.VarInt.decode(<<254, 0, 225, 245, 5>>)
{:ok, 100_000_000}

Specs

decode!(binary()) :: integer()

Decodes the given VarInt binary into an integer.

As decode/1 but returns the result or raises an exception.

Specs

decode_binary(binary()) :: {:ok, binary()} | {:error, term()}

Returns a binary of the length specified by the VarInt in the first bytes of the binary. Any remaining bytes are ignored.

Returns the result in an :ok / :error tuple pair.

Examples

iex> BSV.VarInt.decode_binary(<<5, 104, 101, 108, 108, 111, 99, 99>>)
{:ok, "hello"}

Specs

decode_binary!(binary()) :: binary()

Returns a binary of the length specified by the VarInt in the first bytes of the binary.

As decode_binary/1 but returns the result or raises an exception.

Specs

encode(integer()) :: binary()

Encodes the given integer into a VarInt binary.

Examples

iex> BSV.VarInt.encode(260)
<<253, 4, 1>>

iex> BSV.VarInt.encode(100_000_000)
<<254, 0, 225, 245, 5>>

Specs

encode_binary(binary()) :: binary()

Prepends the given binary with a VarInt representing the length of the binary.

Examples

iex> BSV.VarInt.encode_binary("hello")
<<5, 104, 101, 108, 108, 111>>

Specs

parse_data(binary()) :: {:ok, binary(), binary()} | {:error, term()}

Parses the given binary, returning a tuple with a binary of the length specified by the VarInt in the first bytes of the binary, and a binary of any remaining bytes.

Examples

iex> BSV.VarInt.parse_data(<<5, 104, 101, 108, 108, 111, 1, 2, 3>>)
{:ok, "hello", <<1, 2, 3>>}

Specs

parse_int(binary()) :: {:ok, integer(), binary()} | {:error, term()}

Parses the given binary, returning a tuple with an integer decoded from the VarInt in the first bytes of the binary, and a binary of any remaining bytes.

Examples

iex> BSV.VarInt.parse_int(<<5, 104, 101, 108, 108, 111, 1, 2, 3>>)
{:ok, 5, <<104, 101, 108, 108, 111, 1, 2, 3>>}

Specs

parse_items(binary(), BSV.Serializable.t()) ::
  {:ok, [BSV.Serializable.t()], binary()} | {:error, term()}

Parses the given binary into a list of the length specified by the VarInt in the first bytes of the binary. Each item is parsed according to the specified Serializable.t/0.