SSX3LobbyServer/lib/base/html_escape.cpp

62 lines
1.9 KiB
C++

//! See [Houdini](https://github.com/vmg/houdini/blob/master/houdini_html_e.c) for where this code
//! originally came from.
#include <base/html_escape.hpp>
// clang-format off
constexpr static u8 HTML_ESCAPE_TABLE[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 4,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
constexpr static std::string_view HTML_ESCAPES[] = {
"",
"&quot;",
"&amp;",
"&#39;",
"&#47;",
"&lt;",
"&gt;"
};
// clang-format on
namespace base {
std::string HtmlEscape(const std::span<u8> input) {
std::string out;
// TODO: rewrite this underlying implementation to be
// UTF-8 safe, so we can allow UTF-8 input.
for(auto& c : input) {
const u8 escape = HTML_ESCAPE_TABLE[c];
if(escape == 0) {
out.push_back(c);
} else {
out += HTML_ESCAPES[escape];
}
}
return out;
}
std::string HtmlEscape(const std::string_view input) {
return HtmlEscape(std::span<u8> { std::bit_cast<u8*>(input.data()), input.length() });
}
std::string HtmlEscape(const std::string& input) {
return HtmlEscape(std::span<u8> { std::bit_cast<u8*>(input.data()), input.length() });
}
} // namespace base