Pfx.format
You're seeing just the function
format
, go back to Pfx module for more information.
Specs
Formats pfx
as a string, using several options:
:width
, field width (default 8):base
, howto turn a field into a string (default 10, use 16 for hex numbers):unit
, how many fields go into 1 section (default 1):ssep
, howto join the sections together (default "."):lsep
, howto join a mask if required (default "/"):mask
, whether to add a mask (default false):reverse
, whether to reverse fields before grouping/joining (default false):padding
, whether to pad out thepfx.bits
(default true)
The defaults are geared towards IPv4 prefixes, but the options should be able to accomodate other domains as well.
Notes:
- the prefix.bits-length is omitted if equal to the prefix.bits-size
- domain specific submodules probably implement their own formatter.
Examples
iex> format(%Pfx{bits: <<10, 11, 12>>, maxlen: 32})
"10.11.12.0/24"
iex> format({{10, 11, 12, 0}, 24})
"10.11.12.0/24"
iex> format({10, 11, 12, 0})
"10.11.12.0"
# non-sensical, but there you go
iex> format("10.11.12.0/24")
"10.11.12.0/24"
# bitstring, note that mask is applied when new creates the `pfx`
iex> format("1.2.3.4/24", width: 1, base: 2, unit: 8, mask: false)
"00000001.00000010.00000011.00000000"
# mask not appended as its redundant for a full-sized prefix
iex> format(%Pfx{bits: <<10, 11, 12, 13>>, maxlen: 32})
"10.11.12.13"
iex> pfx = new(<<0xacdc::16, 0x1976::16>>, 128)
iex> format(pfx, width: 16, base: 16, ssep: ":")
"acdc:1976:0:0:0:0:0:0/32"
#
# similar, but grouping 4 fields, each 4 bits wide, into a single section
#
iex> format(pfx, width: 4, base: 16, unit: 4, ssep: ":")
"acdc:1976:0000:0000:0000:0000:0000:0000/32"
#
# this time, omit the acutal pfx length
#
iex> format(pfx, width: 16, base: 16, ssep: ":", mask: false)
"acdc:1976:0:0:0:0:0:0"
#
# ptr for IPv6 using the nibble format:
# - dot-separated reversal of all hex digits in the expanded address
#
iex> pfx
...> |> format(width: 4, base: 16, mask: false, reverse: true)
...> |> String.downcase()
...> |> (fn x -> "#{x}.ip6.arpa." end).()
"0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.6.7.9.1.c.d.c.a.ip6.arpa."
# turn off padding to get reverse zone dns ptr record
iex> new(<<10, 11, 12>>, 32)
...> |> format(padding: false, reverse: true, mask: false)
...> |> (&"#{&1}.in-addr.arpa.").()
"12.11.10.in-addr.arpa."