2015-04-12 21:35:13 +08:00
|
|
|
#include "utils.hpp"
|
2015-04-10 02:41:27 +08:00
|
|
|
|
2015-04-16 04:32:27 +08:00
|
|
|
mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end) {
|
2015-04-23 18:54:08 +08:00
|
|
|
for (auto it = begin; it != end; ++it)
|
|
|
|
if (*it != ' ') return it;
|
|
|
|
return end;
|
2015-04-16 04:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end) {
|
2015-04-23 18:54:08 +08:00
|
|
|
for (auto rit = begin; rit != end; ++rit)
|
|
|
|
if (*rit != ' ') return --(rit.base());
|
|
|
|
return --(end.base());
|
2015-04-16 04:32:27 +08:00
|
|
|
}
|
|
|
|
|
2015-05-04 17:27:51 +08:00
|
|
|
mstch::criter mstch::reverse(mstch::citer it) {
|
|
|
|
return std::reverse_iterator<mstch::citer>(it);
|
|
|
|
}
|
|
|
|
|
2015-04-24 07:02:25 +08:00
|
|
|
std::string mstch::html_escape(const std::string& str) {
|
|
|
|
std::string out;
|
|
|
|
citer start = str.begin();
|
2015-04-27 20:14:21 +08:00
|
|
|
|
2015-04-24 07:02:25 +08:00
|
|
|
auto add_escape = [&out, &start](const std::string& escaped, citer& it) {
|
|
|
|
out += std::string{start, it} + escaped;
|
|
|
|
start = it + 1;
|
|
|
|
};
|
2015-04-27 20:14:21 +08:00
|
|
|
|
2015-04-24 07:02:25 +08:00
|
|
|
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;
|
|
|
|
}
|
2015-04-27 20:14:21 +08:00
|
|
|
|
2015-04-24 07:02:25 +08:00
|
|
|
return out + std::string{start, str.end()};
|
2015-04-10 02:41:27 +08:00
|
|
|
}
|