ex_bencode v1.0.0 ExBencode View Source

Documentation for ExBencode.

Link to this section Summary

Functions

Decode the bencoded binary value

Link to this section Functions

Decode the bencoded binary value.

Examples

Decoding integers

iex> ExBencode.decode("i10e")
{:ok, 10}

iex> ExBencode.decode("i-10e")
{:ok, -10}

Scientific notation is not supported

iex> ExBencode.decode("i1.5e7e")
{:error, :not_bencoded_form}

Decoding strings

iex> ExBencode.decode("4:spam")
{:ok, "spam"}

iex> ExBencode.decode("4:too much spam")
{:error, :not_bencoded_form}

Bytes are handled using the string type, with the preceding number representing the byte size, not the string length.

iex> ExBencode.decode(<<?3, ?:, 1, 2, 3>>)
{:ok, <<1, 2, 3>>}

iex> ExBencode.decode("7:hełło")
{:ok, "hełło"}

iex> ExBencode.decode("5:hełło")
{:error, :not_bencoded_form}

Decoding lists

iex> ExBencode.decode("le")
{:ok, []}

iex> ExBencode.decode("l4:spam4:eggse")
{:ok, ["spam", "eggs"]}

Decoding Dictionaries

iex> ExBencode.decode("de")
{:ok, %{}}

iex> ExBencode.decode("d3:cow3:mooe")
{:ok, %{"cow" => "moo"}}

iex> ExBencode.decode("d8:shoppingl4:eggs4:milkee")
{:ok, %{"shopping" => ["eggs", "milk"]}}