fix unicode string literal escape

This commit is contained in:
MihailRis
2025-09-23 00:08:05 +03:00
parent 8b38d57966
commit 28c821006a
5 changed files with 63 additions and 6 deletions
+54
View File
@@ -1,4 +1,5 @@
#include "util/stringutil.hpp"
#include "coders/BasicParser.hpp"
#include <gtest/gtest.h>
@@ -16,6 +17,25 @@ TEST(stringutil, utf8) {
EXPECT_EQ(str, str2);
}
static std::wstring gen_random_unicode_wstring(int n) {
std::wstring str;
str.resize(n);
for (int i = 0; i < n; i++) {
// wstring is 16 bit in some systems
str[i] = rand() & 0xFFFF;
}
return str;
}
TEST(stringutil, utf8_random) {
srand(5436324);
auto str = gen_random_unicode_wstring(10'000);
auto utf8str = util::wstr2str_utf8(str);
auto back = util::str2wstr_utf8(utf8str);
EXPECT_EQ(str, back);
}
TEST(stringutil, base64) {
srand(2019);
for (size_t size = 0; size < 30; size++) {
@@ -47,3 +67,37 @@ TEST(stringutil, base64_urlsafe) {
}
}
}
class StringParser : BasicParser<char> {
public:
StringParser(std::string_view source) : BasicParser("<string>", source) {}
std::string parse() {
++pos;
return parseString(source[0], true);
}
};
TEST(stringutil, escape_cases) {
auto escaped = util::escape("тест5", true);
auto expected = "\"\\u0442\\u0435\\u0441\\u04425\"";
ASSERT_EQ(expected, escaped);
srand(345873458);
for (int i = 0; i < 36; i++) {
rand();
}
auto str = gen_random_unicode_wstring(40);
auto utf8str = util::wstr2str_utf8(str);
escaped = util::escape(utf8str, true);
StringParser parser(escaped);
auto restored = parser.parse();
for (int i = 0; i < utf8str.length(); i++) {
if (utf8str[i] != restored[i]) {
std::cout << i << ": " << (int)utf8str[i] << " " << (int)restored[i] << std::endl;
}
}
EXPECT_EQ(utf8str, restored);
}