aboutsummaryrefslogtreecommitdiff
path: root/lib/mstch/src/utils.cpp
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-01-16 15:43:04 +0800
committerMistivia <i@mistivia.com>2025-01-16 15:53:58 +0800
commit8689a7c78c50676ea739f52fbcee9f091709f5c0 (patch)
treebbb72c68e5e1753f751133941a402caa43e29a16 /lib/mstch/src/utils.cpp
parent00afb767ae37488d99f78363d031b898b1932354 (diff)
add mstch
Diffstat (limited to 'lib/mstch/src/utils.cpp')
-rw-r--r--lib/mstch/src/utils.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/mstch/src/utils.cpp b/lib/mstch/src/utils.cpp
new file mode 100644
index 0000000..1646170
--- /dev/null
+++ b/lib/mstch/src/utils.cpp
@@ -0,0 +1,44 @@
+#include "utils.hpp"
+#include "mstch/mstch.hpp"
+
+mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end) {
+ for (auto it = begin; it != end; ++it)
+ if (*it != ' ') return it;
+ return end;
+}
+
+mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end) {
+ for (auto rit = begin; rit != end; ++rit)
+ if (*rit != ' ') return --(rit.base());
+ return --(end.base());
+}
+
+mstch::criter mstch::reverse(mstch::citer it) {
+ return std::reverse_iterator<mstch::citer>(it);
+}
+
+std::string mstch::html_escape(const std::string& str) {
+ if (mstch::config::escape)
+ return mstch::config::escape(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("&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;
+ }
+
+ return out + std::string{start, str.end()};
+}