2017-10-11 18:48:27 +05:00
|
|
|
#include "utils.hpp"
|
|
|
|
#include "mstch/mstch.hpp"
|
|
|
|
|
2017-10-17 19:29:56 +05:00
|
|
|
mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end)
|
|
|
|
{
|
2017-10-11 18:48:27 +05:00
|
|
|
for (auto it = begin; it != end; ++it)
|
2017-10-17 19:29:56 +05:00
|
|
|
if (*it != ' ')
|
|
|
|
return it;
|
2017-10-11 18:48:27 +05:00
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2017-10-17 19:29:56 +05:00
|
|
|
mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end)
|
|
|
|
{
|
2017-10-11 18:48:27 +05:00
|
|
|
for (auto rit = begin; rit != end; ++rit)
|
2017-10-17 19:29:56 +05:00
|
|
|
if (*rit != ' ')
|
|
|
|
return --(rit.base());
|
2017-10-11 18:48:27 +05:00
|
|
|
return --(end.base());
|
|
|
|
}
|
|
|
|
|
2017-10-17 19:29:56 +05:00
|
|
|
mstch::criter mstch::reverse(mstch::citer it) { return std::reverse_iterator<mstch::citer>(it); }
|
2017-10-11 18:48:27 +05:00
|
|
|
|
2017-10-17 19:29:56 +05:00
|
|
|
std::string mstch::html_escape(const std::string &str)
|
|
|
|
{
|
2017-10-11 18:48:27 +05:00
|
|
|
if (mstch::config::escape)
|
|
|
|
return mstch::config::escape(str);
|
2017-10-17 19:29:56 +05:00
|
|
|
|
2017-10-11 18:48:27 +05:00
|
|
|
std::string out;
|
|
|
|
citer start = str.begin();
|
|
|
|
|
2017-10-17 19:29:56 +05:00
|
|
|
auto add_escape = [&out, &start](const std::string &escaped, citer &it) {
|
2017-10-11 18:48:27 +05:00
|
|
|
out += std::string{start, it} + escaped;
|
|
|
|
start = it + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto it = str.begin(); it != str.end(); ++it)
|
2017-10-17 19:29:56 +05:00
|
|
|
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;
|
2017-10-11 18:48:27 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
return out + std::string{start, str.end()};
|
|
|
|
}
|