Pfx.insert
You're seeing just the function
insert
, go back to Pfx module for more information.
Specs
Inserts bits
into pfx
-s bitstring, starting at position
.
The resulting bitstring is silently clipped to the pfx.maxlen
.
Valid bit positions are bits_size(pfx.bits) .. min(pfx.maxlen-1, bit_size(pfx.bits))
. A position of 0
will prepend the bits
, while a
position of bit_size(pfx.bits)
will append the bits
as long as the prefix
is not a full length prefix already.
A negative position is taken relative to the end. Note that this cannot
be used for appending bits, since -1
refers to the last actual bit and
there is no such thing as -0
..
Examples
# prepend bits
iex> insert("0.0.0.0/0", <<255>>, 0)
"255.0.0.0/8"
# append bits
iex> insert("255.255.0.0/16", <<255>>, 16)
"255.255.255.0/24"
# cannot append to a full prefix, positions go from 0..31
iex> insert("1.2.3.4", <<255>>, 32)
** (ArgumentError) invalid bit position, got 32
# but inserting inside the bitstring, is ok
iex> insert("1.2.3.4", <<255>>, 16)
"1.2.255.3"
# turn EUI48 into a modified EUI64
iex> new("0088.8888.8888")
...> |> new(64)
...> |> flip(6)
...> |> insert(<<0xFF, 0xFE>>, 24)
%Pfx{bits: <<0x02, 0x88, 0x88, 0xFF, 0xFE, 0x88, 0x88, 0x88>>, maxlen: 64}
# sliently clips to pfx's maxlen
iex> insert("1.2.3.0/24", <<255, 255, 255, 255>>, 0)
"255.255.255.255"
iex> insert("1.2.3.0/24", <<255, 255, 255, 255>>, 24)
"1.2.3.255"