Rewrite cookie_helper::reverseDomainLevel()

pull/46/head
Tatsuhiro Tsujikawa 2013-02-16 22:51:58 +09:00
parent 61665d2c26
commit b3acf4d393
1 changed files with 20 additions and 10 deletions

View File

@ -402,18 +402,28 @@ bool pathMatch(const std::string& requestPath, const std::string& path)
std::string reverseDomainLevel(const std::string& domain)
{
std::string r;
for(std::string::const_iterator i = domain.begin(), eoi = domain.end();
i != eoi;) {
std::string::const_iterator j = std::find(i, eoi, '.');
r.insert(r.begin(), '.');
r.insert(r.begin(), i, j);
i = j;
if(j != eoi) {
++i;
if(domain.empty()) {
return r;
}
r.reserve(domain.size());
// Cut trailing dots
std::string::const_iterator s = domain.end() - 1;
for(; *s == '.'; --s) {
if(s == domain.begin()) {
return r;
}
}
if(!r.empty()) {
r.erase(r.size()-1, 1);
std::string::const_iterator t = s + 1;
for(; ; --s) {
if(*s == '.') {
r.append(s + 1, t);
r += '.';
t = s;
}
if(s == domain.begin()) {
r.append(s, t);
break;
}
}
return r;
}