]> Creatis software - cpPlugins.git/blob - lib/mstch/utils.cpp
Moved to version 1.0
[cpPlugins.git] / lib / mstch / utils.cpp
1 #include "utils.hpp"
2 #include "mstch/mstch.hpp"
3
4 mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end) {
5   for (auto it = begin; it != end; ++it)
6     if (*it != ' ') return it;
7   return end;
8 }
9
10 mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end) {
11   for (auto rit = begin; rit != end; ++rit)
12     if (*rit != ' ') return --(rit.base());
13   return --(end.base());
14 }
15
16 mstch::criter mstch::reverse(mstch::citer it) {
17   return std::reverse_iterator<mstch::citer>(it);
18 }
19
20 std::string mstch::html_escape(const std::string& str) {
21   if (mstch::config::escape)
22     return mstch::config::escape(str);
23   
24   std::string out;
25   citer start = str.begin();
26
27   auto add_escape = [&out, &start](const std::string& escaped, citer& it) {
28     out += std::string{start, it} + escaped;
29     start = it + 1;
30   };
31
32   for (auto it = str.begin(); it != str.end(); ++it)
33     switch (*it) {
34       case '&': add_escape("&amp;", it); break;
35       case '\'': add_escape("&#39;", it); break;
36       case '"': add_escape("&quot;", it); break;
37       case '<': add_escape("&lt;", it); break;
38       case '>': add_escape("&gt;", it); break;
39       case '/': add_escape("&#x2F;", it); break;
40       default: break;
41     }
42
43   return out + std::string{start, str.end()};
44 }