Optimize htmlEscape implementation a bit

pull/122/head
Nils Maier 2013-08-24 21:54:56 +02:00
parent 1b8de6bb18
commit b2da75ca33
1 changed files with 24 additions and 17 deletions

View File

@ -1533,25 +1533,32 @@ getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len)
std::string htmlEscape(const std::string& src) std::string htmlEscape(const std::string& src)
{ {
std::string dest; std::string rv(src);
for(std::string::const_iterator i = src.begin(), eoi = src.end(); std::string::size_type pos = 0;
i != eoi; ++i) { while ((pos = rv.find_first_of("<>&\"'", pos)) != std::string::npos) {
char ch = *i; auto ch = rv[pos];
if (ch == '<') { if (ch == '<') {
dest += "&lt;"; rv.replace(pos, 1, "&lt;");
} else if(ch == '>') { pos += 4;
dest += "&gt;"; }
} else if(ch == '&') { else if (ch == '>') {
dest += "&amp;"; rv.replace(pos, 1, "&gt;");
} else if(ch == '\'') { pos += 4;
dest += "&#39;"; }
} else if(ch == '"') { else if (ch == '&') {
dest += "&quot;"; rv.replace(pos, 1, "&amp;");
} else { pos += 5;
dest += ch; }
else if (ch == '"') {
rv.replace(pos, 1, "&quot;");
pos += 6;
}
else { // '\''
rv.replace(pos, 1, "&#39;");
pos += 5;
} }
} }
return dest; return rv;
} }
std::pair<size_t, std::string> std::pair<size_t, std::string>