Pfx.cut

You're seeing just the function cut, go back to Pfx module for more information.

Specs

cut(prefix(), integer(), integer()) :: prefix()

Cut out a series of bits and turn it into its own Pfx.

This basically uses &bits/3 to extract the bits and wraps it in a Pfx.t/0 with its maxlen set to the length of the bits extracted.

Examples

As per example on wikipedia for an IPv6 address 2001:0000:4136:e378:8000:63bf:3fff:fdd2 that refers to a Teredo client:

iex> teredo = new(<<0x2001::16, 0::16, 0x4136::16, 0xe378::16,
...>  0x8000::16, 0x63bf::16, 0x3fff::16, 0xfdd2::16>>, 128)
iex>
iex> # client
iex> cut(teredo, 96, 32) |> bnot() |> format()
"192.0.2.45"
iex>
iex>
iex> # udp port
iex> cut(teredo, 80, 16) |> bnot() |> cast()
40000
iex>
iex> # teredo server
iex> cut(teredo, 32, 32) |> format()
"65.54.227.120"
iex>
iex> # flags
iex> cut(teredo, 64, 16) |> digits(1) |> elem(0)
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

"Missing" bits are considered to be zero.

# extract 2nd and 3rd byte:
iex> %Pfx{bits: <<255, 255>>, maxlen: 32} |> cut(8, 16)
%Pfx{bits: <<255, 0>>, maxlen: 16}

Extraction must stay within maxlen of given pfx.

# cannot exceed boundaries though:
iex> %Pfx{bits: <<255, 255>>, maxlen: 32} |> cut(8, 32)
** (ArgumentError) invalid index range: {8, 32}