Formatting

Algorithms to format URLs construct a mutable URL by parsing and applying arguments to a URL template. The following example uses the format function to construct an absolute URL:

// snippet_format_1

The rules for a format URL string are the same as for a std::format_string, where replacement fields are delimited by curly braces. The URL type is inferred from the format string.

The URL components to which replacement fields belong are identified before replacement is applied and any invalid characters for that formatted argument are percent-escaped:

// snippet_format_2

Delimiters in the URL template, such as ":", "//", "?", and "#", unambiguously associate each replacement field to a URL component. All other characters are normalized to ensure the URL is valid:

// snippet_format_3a
// snippet_format_3b

The function format_to can be used to format URLs into any modifiable URL container.

// snippet_format_4

As with std::format, positional and named arguments are supported.

// snippet_format_5a

The arg function can be used to associate names with arguments:

// snippet_format_5b

A second overload based on std::initializer_list is provided for both format and format_to. These overloads can help with lists of named arguments:

// snippet_format_5c