replaced boost by manual xml writing, to improve serialization perfs

This commit is contained in:
emeric
2025-09-19 00:34:37 +02:00
parent 2b85de7939
commit c65b7b3e0b
8 changed files with 335 additions and 141 deletions
+21 -1
View File
@@ -44,10 +44,20 @@ namespace lms::core::stringUtils
constexpr std::pair<char, std::string_view> jsonEscapeChars[]{
{ '\\', "\\\\" },
{ '"', "\\\"" },
{ '\b', "\\b" },
{ '\f', "\\f" },
{ '\n', "\\n" },
{ '\r', "\\r" },
{ '\t', "\\t" },
{ '"', "\\\"" },
};
constexpr std::pair<char, std::string_view> xmlEscapeChars[]{
{ '&', "&amp;" },
{ '<', "&lt;" },
{ '>', "&gt;" },
{ '\'', "&apos;" },
{ '"', "&quot;" },
};
template<std::size_t N>
@@ -442,6 +452,16 @@ namespace lms::core::stringUtils
details::writeEscapedString(os, str, details::jsonEscapeChars);
}
std::string xmlEscape(std::string_view str)
{
return details::escape(str, details::xmlEscapeChars);
}
void writeXmlEscapedString(std::ostream& os, std::string_view str)
{
details::writeEscapedString(os, str, details::xmlEscapeChars);
}
std::string escapeString(std::string_view str, std::string_view charsToEscape, char escapeChar)
{
std::string res;
+3
View File
@@ -105,6 +105,9 @@ namespace lms::core::stringUtils
void writeJSEscapedString(std::ostream& os, std::string_view str);
void writeJsonEscapedString(std::ostream& os, std::string_view str);
[[nodiscard]] std::string xmlEscape(std::string_view str);
void writeXmlEscapedString(std::ostream& os, std::string_view str);
[[nodiscard]] std::string escapeString(std::string_view str, std::string_view charsToEscape, char escapeChar);
[[nodiscard]] std::string unescapeString(std::string_view str, char escapeChar);
+21
View File
@@ -221,6 +221,27 @@ namespace lms::core::stringUtils::tests
EXPECT_EQ(jsonEscape(R"(Test'.mp3)"), R"(Test'.mp3)");
EXPECT_EQ(jsonEscape(R"(Test"".mp3)"), R"(Test\"\".mp3)");
EXPECT_EQ(jsonEscape(R"(\Test\.mp3)"), R"(\\Test\\.mp3)");
EXPECT_EQ(jsonEscape("Line1\nLine2"), R"(Line1\nLine2)");
EXPECT_EQ(jsonEscape("Line1\rLine2"), R"(Line1\rLine2)");
EXPECT_EQ(jsonEscape("Col1\tCol2"), R"(Col1\tCol2)");
EXPECT_EQ(jsonEscape("Hello\bWorld"), R"(Hello\bWorld)");
EXPECT_EQ(jsonEscape("Hello\fWorld"), R"(Hello\fWorld)");
EXPECT_EQ(jsonEscape("Hello\nWorld"), R"(Hello\nWorld)");
}
TEST(StringUtils, escapeXmlString)
{
EXPECT_EQ(xmlEscape(""), "");
EXPECT_EQ(xmlEscape("Test.mp3"), "Test.mp3");
EXPECT_EQ(xmlEscape("A & B"), "A &amp; B");
EXPECT_EQ(xmlEscape("<tag>"), "&lt;tag&gt;");
EXPECT_EQ(xmlEscape(R"(He said "Hello")"), "He said &quot;Hello&quot;");
EXPECT_EQ(xmlEscape("It's fine"), "It&apos;s fine");
EXPECT_EQ(xmlEscape(R"(<tag attr="val & val2">O'Hara</tag>)"), "&lt;tag attr=&quot;val &amp; val2&quot;&gt;O&apos;Hara&lt;/tag&gt;");
EXPECT_EQ(xmlEscape(R"(\Test\.mp3)"), R"(\Test\.mp3)");
EXPECT_EQ(xmlEscape("Café & Tea"), "Café &amp; Tea");
EXPECT_EQ(xmlEscape(R"(&<>'")"), "&amp;&lt;&gt;&apos;&quot;");
EXPECT_EQ(xmlEscape("Line1\nLine2"), "Line1\nLine2");
}
TEST(StringUtils, escapeString)