#include <iostream>
#include <regex>
#include <string>
std::string extract_authority(std::string uri)
{
// See: Apendix B from http://w...content-available-to-author-only...f.org/rfc/rfc2396.txt for regular expression
std::regex uri_exp(R"xxx(^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)xxx");
std::smatch matches;
if (std::regex_match(uri, matches, uri_exp) && matches.size() >= 5)
return matches[4].str();
return std::string();
}
std::string trim_prefix(std::string s, std::string prefix)
{
return s.find(prefix) == 0 ? s.substr(prefix.length()) : s;
}
int main()
{
std::vector<std::string> examples =
{
R"(http://w...content-available-to-author-only...i.edu/pub/ietf/uri/#Related)",
R"(http://w...content-available-to-author-only...s.com/user/)",
R"(ftp://f...content-available-to-author-only...o.za/rfc/rfc1808.txt)",
R"(gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles")",
R"(http://w...content-available-to-author-only...o.no/faq/compression-faq/part1.html)",
R"(mailto:mduerst@ifi.unizh.ch)",
R"(news:comp.infosystems.www.servers.unix")",
R"(telnet://melvyl.ucop.edu/)",
};
for (auto example : examples)
{
auto auth = extract_authority(example);
std::cout << '"' << trim_prefix(auth, "www.") << "\" (" << example << ")\n";
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cmVnZXg+CiNpbmNsdWRlIDxzdHJpbmc+CgpzdGQ6OnN0cmluZyBleHRyYWN0X2F1dGhvcml0eShzdGQ6OnN0cmluZyB1cmkpCnsKICAgIC8vIFNlZTogQXBlbmRpeCBCIGZyb20gaHR0cDovL3cuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmYub3JnL3JmYy9yZmMyMzk2LnR4dCBmb3IgcmVndWxhciBleHByZXNzaW9uCiAgICBzdGQ6OnJlZ2V4IHVyaV9leHAoUiJ4eHgoXigoW146Lz8jXSspOik/KC8vKFteLz8jXSopKT8oW14/I10qKShcPyhbXiNdKikpPygjKC4qKSk/KXh4eCIpOwogICAgc3RkOjpzbWF0Y2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