fix utf8 support

This commit is contained in:
MihailRis 2025-11-23 19:56:12 +03:00
parent 6810f9a03a
commit 28589c4b3b
3 changed files with 14 additions and 14 deletions

View File

@ -89,8 +89,8 @@ static int l_escape(lua::State* L) {
}
static int l_escape_xml(lua::State* L) {
auto string = lua::require_lstring(L, 1);
return lua::pushstring(L, util::escape_xml(string));
auto string = lua::require_wstring(L, 1);
return lua::pushwstring(L, util::escape_xml(string));
}
const luaL_Reg utf8lib[] = {

View File

@ -60,24 +60,24 @@ std::string util::escape(std::string_view s, bool escapeUnicode) {
return ss.str();
}
std::string util::escape_xml(std::string_view s) {
std::stringstream ss;
for (char c : s) {
std::wstring util::escape_xml(std::wstring_view s) {
std::wstringstream ss;
for (wchar_t c : s) {
switch (c) {
case '&':
ss << "&amp;";
case L'&':
ss << L"&amp;";
break;
case '<':
ss << "&lt;";
case L'<':
ss << L"&lt;";
break;
case '>':
ss << "&gt;";
case L'>':
ss << L"&gt;";
break;
case '"':
ss << "&quot;";
ss << L"&quot;";
break;
case '\'':
ss << "&apos;";
ss << L"&apos;";
break;
default:
ss << c;

View File

@ -11,7 +11,7 @@ namespace util {
std::string escape(std::string_view s, bool escapeUnicode=true);
/// @brief Escape all special XML characters
std::string escape_xml(std::string_view s);
std::wstring escape_xml(std::wstring_view s);
/// @brief Function used for error messages
std::string quote(const std::string& s);