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:

url u = format("{}://{}:{}/rfc/{}", "https", "", 80, "rfc2396.txt");
assert(u.buffer() == "");

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:

url u = format("https://{}/{}", "", "Hello world!");
assert(u.buffer() == "!");

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:

url u = format("{}:{}", "mailto", "");
assert(u.buffer() == "");
assert(u.scheme() == "mailto");
assert(u.path() == "");
url u = format("{}{}", "mailto:", "");
assert(u.buffer() == "");
assert(u.path() == "");
assert(u.encoded_path() == "");

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

static_url<50> u;
format_to(u, "{}://{}:{}/rfc/{}", "https", "", 80, "rfc2396.txt");
assert(u.buffer() == "");

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

url u = format("{0}://{2}:{1}/{3}{4}{3}", "https", 80, "", "abra", "cad");
assert(u.buffer() == "");

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

url u = format("{username}", arg("username", "mark"));
assert(u.buffer() == "");

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:

boost::core::string_view fmt = "{scheme}://{host}:{port}/{dir}/{file}";
url u = format(fmt, {{"scheme", "https"}, {"port", 80}, {"host", ""}, {"dir", "path/to"}, {"file", "file.txt"}});
assert(u.buffer() == "");