This source file includes following definitions.
- GetURLCharPriority
- CanonicalURLStringCompare
- HaveSameSchemeHostAndPort
- IsPathPrefix
- ToggleHTTPAndHTTPS
#include "chrome/browser/history/url_utils.h"
#include <algorithm>
namespace history {
namespace {
int GetURLCharPriority(char ch) {
switch (ch) {
case '\0': return 0;
case '?': return 1;
case '#': return 2;
case '/': return 3;
}
return 4;
}
}
bool CanonicalURLStringCompare(const std::string& s1, const std::string& s2) {
const std::string::value_type* ch1 = s1.c_str();
const std::string::value_type* ch2 = s2.c_str();
while (*ch1 && *ch2 && *ch1 == *ch2) {
++ch1;
++ch2;
}
int pri_diff = GetURLCharPriority(*ch1) - GetURLCharPriority(*ch2);
return (pri_diff != 0) ? pri_diff < 0 : *ch1 < *ch2;
}
bool HaveSameSchemeHostAndPort(const GURL&url1, const GURL& url2) {
return url1.scheme() == url2.scheme() && url1.host() == url2.host() &&
url1.port() == url2.port();
}
bool IsPathPrefix(const std::string& p1, const std::string& p2) {
if (p1.length() > p2.length())
return false;
std::pair<std::string::const_iterator, std::string::const_iterator>
first_diff = std::mismatch(p1.begin(), p1.end(), p2.begin());
if (first_diff.first != p1.end())
return false;
if (first_diff.second == p2.end())
return true;
if (!p1.empty() && *p1.rbegin() == '/')
return true;
return *(first_diff.second) == '/';
}
GURL ToggleHTTPAndHTTPS(const GURL& url) {
std::string new_scheme;
if (url.SchemeIs("http"))
new_scheme = "https";
else if (url.SchemeIs("https"))
new_scheme = "http";
else
return GURL::EmptyGURL();
url_parse::Component comp;
comp.len = new_scheme.length();
GURL::Replacements replacement;
replacement.SetScheme(new_scheme.c_str(), comp);
return url.ReplaceComponents(replacement);
}
}