Dotted-decimal IPv4 literal parser.


parseIpv4Literal

(host -- address valid) Parses host as a dotted-decimal IPv4 literal and returns its address and whether host is such a literal.
  • host is a Text, String, or StringView.
  • A literal is four octets separated by dots, and nothing else; an octet is a number from 0 to 255 in ASCII digits without leading zeros.
  • address is a host-order Nat32 with the first dotted octet in the most significant byte: 1.2.3.4 gives 0x01020304n32.
  • valid is a Cond; when it is FALSE, address has no meaning.

Examples

Example: valid and invalid literals

The address of a literal is printed; a leading zero, an octet above 255, and a missing octet are refused.

"String"         use
"control"        use
"ipAddressUtils" use

show: [
  host:;
  address: valid: host parseIpv4Literal;;
  valid [(host " = " address LF) printList] [(host " is not an IPv4 literal" LF) printList] if
];

{} Int32 {} [
  "1.2.3.4" show
  "01.2.3.4" show
  "256.1.1.1" show
  "1.2.3" show
  0
] "main" exportFunction

Expected Output

1.2.3.4 = 16909060
01.2.3.4 is not an IPv4 literal
256.1.1.1 is not an IPv4 literal
1.2.3 is not an IPv4 literal

See also