X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2Fmstch%2Futils.cpp;fp=lib%2Fmstch%2Futils.cpp;h=1646170198f146e7bb46716399f70351f9ea7d87;hb=2e142df11d6f312a2a2b5097b8da73571ed523e8;hp=0000000000000000000000000000000000000000;hpb=61b3659afe961ed248f30e26f9ca8f28fcfafddc;p=cpPlugins.git diff --git a/lib/mstch/utils.cpp b/lib/mstch/utils.cpp new file mode 100644 index 0000000..1646170 --- /dev/null +++ b/lib/mstch/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(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("&", 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; + } + + return out + std::string{start, str.end()}; +}