Mojoler.Dsl.Bytes (mojoler v0.1.0)

Copy Markdown View Source

Binary examples generated from tools/dsl/decls.json.

shift/1 copies the payload and adds 1 to each byte, wrapping at 255. borrow_len/1 reads the size of the caller's binary and does not copy it. The byte function lives in native/examples/bytes_user.mojo.

Summary

Functions

Returns the byte size of bin without copying the payload.

Loads priv/native/libdsl_bytes.so.

Returns a copy of bin with each byte increased by 1, modulo 256.

Functions

borrow_len(bin)

@spec borrow_len(binary()) :: {:ok, non_neg_integer()} | {:error, :not_binary}

Returns the byte size of bin without copying the payload.

Returns

  • {:ok, size}
  • {:error, :not_binary} for a list or a bitstring that is not a binary

Examples

iex> Mojoler.Dsl.Bytes.borrow_len(<<1, 2, 3, 4>>)
{:ok, 4}

iex> Mojoler.Dsl.Bytes.borrow_len(<<1::7>>)
{:error, :not_binary}

load_nif()

@spec load_nif() :: :ok | {:error, term()}

Loads priv/native/libdsl_bytes.so.

A second call returns {:error, {:reload, charlist}}.

Examples

iex> Mojoler.Dsl.Bytes.load_nif()
{:error, {:reload, ~c"NIF library already loaded (reload disallowed since OTP 20)."}}

shift(bin)

@spec shift(binary()) :: {:ok, binary(), non_neg_integer()} | {:error, :not_binary}

Returns a copy of bin with each byte increased by 1, modulo 256.

bin must be a binary. A bitstring that is not a binary is an error.

Returns

  • {:ok, shifted, nbytes} where nbytes is the byte size
  • {:error, :not_binary}

Examples

iex> Mojoler.Dsl.Bytes.shift(<<1, 2, 255>>)
{:ok, <<2, 3, 0>>, 3}

iex> Mojoler.Dsl.Bytes.shift(<<>>)
{:ok, <<>>, 0}

iex> Mojoler.Dsl.Bytes.shift([1, 2])
{:error, :not_binary}