rewrite html_escape without boost replace
This commit is contained in:
parent
8a32d98443
commit
7c5d88d914
@ -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, "&", "&");
|
std::string out;
|
||||||
boost::replace_all(str, "'", "'");
|
citer start = str.begin();
|
||||||
boost::replace_all(str, "\"", """);
|
auto add_escape = [&out, &start](const std::string& escaped, citer& it) {
|
||||||
boost::replace_all(str, "<", "<");
|
out += std::string{start, it} + escaped;
|
||||||
boost::replace_all(str, ">", ">");
|
start = it + 1;
|
||||||
boost::replace_all(str, "/", "/");
|
};
|
||||||
return str;
|
for (auto it = str.begin(); it != str.end(); ++it)
|
||||||
|
switch (*it) {
|
||||||
|
case '&': add_escape("&", it); break;
|
||||||
|
case '\'': add_escape("'", it); break;
|
||||||
|
case '"': add_escape(""", it); break;
|
||||||
|
case '<': add_escape("<", it); break;
|
||||||
|
case '>': add_escape(">", it); break;
|
||||||
|
case '/': add_escape("/", it); break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
return out + std::string{start, str.end()};
|
||||||
}
|
}
|
||||||
|
@ -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(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user