rewrite html_escape without boost replace

This commit is contained in:
Daniel Sipka 2015-04-24 01:02:25 +02:00
parent 8a32d98443
commit 7c5d88d914
2 changed files with 19 additions and 11 deletions

View file

@ -1,7 +1,5 @@
#include "utils.hpp" #include "utils.hpp"
#include <boost/algorithm/string/replace.hpp>
mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end) { mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end) {
for (auto it = begin; it != end; ++it) for (auto it = begin; it != end; ++it)
if (*it != ' ') return it; if (*it != ' ') return it;
@ -14,12 +12,22 @@ mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end) {
return --(end.base()); return --(end.base());
} }
std::string mstch::html_escape(std::string str) { std::string mstch::html_escape(const std::string& str) {
boost::replace_all(str, "&", "&amp;"); std::string out;
boost::replace_all(str, "'", "&#39;"); citer start = str.begin();
boost::replace_all(str, "\"", "&quot;"); auto add_escape = [&out, &start](const std::string& escaped, citer& it) {
boost::replace_all(str, "<", "&lt;"); out += std::string{start, it} + escaped;
boost::replace_all(str, ">", "&gt;"); start = it + 1;
boost::replace_all(str, "/", "&#x2F;"); };
return str; for (auto it = str.begin(); it != str.end(); ++it)
switch (*it) {
case '&': add_escape("&amp;", it); break;
case '\'': add_escape("&#39;", it); break;
case '"': add_escape("&quot;", it); break;
case '<': add_escape("&lt;", it); break;
case '>': add_escape("&gt;", it); break;
case '/': add_escape("&#x2F;", it); break;
default: break;
}
return out + std::string{start, str.end()};
} }

View file

@ -10,7 +10,7 @@ using criter = std::string::const_reverse_iterator;
citer first_not_ws(citer begin, citer end); citer first_not_ws(citer begin, citer end);
citer first_not_ws(criter begin, criter end); citer first_not_ws(criter begin, criter end);
std::string html_escape(std::string str); std::string html_escape(const std::string& str);
template<class... Args> template<class... Args>
auto visit(Args&&... args) -> decltype(boost::apply_visitor( auto visit(Args&&... args) -> decltype(boost::apply_visitor(