diff --git a/src/utils.cpp b/src/utils.cpp index 610f429..832c9ca 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,7 +1,5 @@ #include "utils.hpp" -#include - mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end) { for (auto it = begin; it != end; ++it) if (*it != ' ') return it; @@ -14,12 +12,22 @@ mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end) { return --(end.base()); } -std::string mstch::html_escape(std::string str) { - boost::replace_all(str, "&", "&"); - boost::replace_all(str, "'", "'"); - boost::replace_all(str, "\"", """); - boost::replace_all(str, "<", "<"); - boost::replace_all(str, ">", ">"); - boost::replace_all(str, "/", "/"); - return str; +std::string mstch::html_escape(const std::string& str) { + std::string out; + citer start = str.begin(); + auto add_escape = [&out, &start](const std::string& escaped, citer& it) { + out += std::string{start, it} + escaped; + start = it + 1; + }; + 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()}; } diff --git a/src/utils.hpp b/src/utils.hpp index 51e2bd6..9b05da8 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -10,7 +10,7 @@ using criter = std::string::const_reverse_iterator; citer first_not_ws(citer begin, citer 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 auto visit(Args&&... args) -> decltype(boost::apply_visitor(