CRAM.Ltf8 (cram v0.1.0)

Copy Markdown View Source

LTF-8 integer codec from CRAM v3 section 2.3.

An LTF-8 value is a signed 64-bit integer. The prefix works like ITF-8 but extends to nine bytes so a full 64-bit pattern fits. A first byte of 0xFF means the next eight bytes are the value. Encoding uses the shortest prefix that can hold the two's-complement bit pattern.

Summary

Types

Why decode/1 rejected the bytes. :truncated means the prefix promised more bytes than binary contains.

Functions

Decode one LTF-8 integer from the front of binary.

Decode one LTF-8 integer or raise CRAM.Error.

Encode a signed 64-bit integer.

Encode a signed 64-bit integer or raise CRAM.Error.

Types

decode_error()

@type decode_error() :: :truncated

Why decode/1 rejected the bytes. :truncated means the prefix promised more bytes than binary contains.

Functions

decode(binary)

@spec decode(binary()) :: {:ok, integer(), binary()} | {:error, decode_error()}

Decode one LTF-8 integer from the front of binary.

iex> {:ok, 128, <<9>>} = CRAM.Ltf8.decode(<<0x80, 128, 9>>)
{:ok, 128, <<9>>}

iex> CRAM.Ltf8.decode(<<0x80>>)
{:error, :truncated}

decode!(binary)

@spec decode!(binary()) :: {integer(), binary()}

Decode one LTF-8 integer or raise CRAM.Error.

Returns {value, rest}. A short prefix raises CRAM.Error with reason: :truncated.

iex> CRAM.Ltf8.decode!(<<0>>)
{0, <<>>}

iex> CRAM.Ltf8.decode!(<<0x80>>)
** (CRAM.Error) cram: truncated: LTF-8 input ended early

encode(value)

@spec encode(integer()) :: {:ok, binary()} | {:error, :overflow}

Encode a signed 64-bit integer.

iex> CRAM.Ltf8.encode(0)
{:ok, <<0>>}

iex> CRAM.Ltf8.encode(128)
{:ok, <<0x80, 128>>}

iex> CRAM.Ltf8.encode(9_223_372_036_854_775_808)
{:error, :overflow}

encode!(value)

@spec encode!(integer()) :: binary()

Encode a signed 64-bit integer or raise CRAM.Error.

value must fit in a signed 64-bit integer. A value outside that range raises CRAM.Error with reason: :overflow.

iex> CRAM.Ltf8.encode!(128)
<<0x80, 128>>

iex> CRAM.Ltf8.encode!(9_223_372_036_854_775_808)
** (CRAM.Error) cram: overflow: LTF-8 value is outside int64