Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/libmustache/utils.cpp

62 lines
1.3 KiB
C++
Raw Normal View History

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("&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;
2017-10-11 18:48:27 +05:00
}
return out + std::string{start, str.end()};
}