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) { static int l_escape_xml(lua::State* L) {
auto string = lua::require_lstring(L, 1); auto string = lua::require_wstring(L, 1);
return lua::pushstring(L, util::escape_xml(string)); return lua::pushwstring(L, util::escape_xml(string));
} }
const luaL_Reg utf8lib[] = { const luaL_Reg utf8lib[] = {

View File

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

View File

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