Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests that rely on UTF-8 literals #14490

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/filesys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ bool PathStartsWith(const std::string &path, const std::string &prefix)
char pathchar = path[pathpos+len];
char prefixchar = prefix[prefixpos+len];
if(FILESYS_CASE_INSENSITIVE){
pathchar = tolower(pathchar);
prefixchar = tolower(prefixchar);
pathchar = my_tolower(pathchar);
prefixchar = my_tolower(prefixchar);
}
if(pathchar != prefixchar)
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/unittest/test_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void TestSettings::runTests(IGameDef *gamedef)
////////////////////////////////////////////////////////////////////////////////

const char *TestSettings::config_text_before =
"leet = 1337\n"
u8"leet = 1337\n"
"leetleet = 13371337\n"
"leetleet_neg = -13371337\n"
"floaty_thing = 1.1\n"
Expand All @@ -77,7 +77,7 @@ const char *TestSettings::config_text_before =
"[dummy_eof_end_tag]\n";

const std::string TestSettings::config_text_after =
"leet = 1337\n"
u8"leet = 1337\n"
"leetleet = 13371337\n"
"leetleet_neg = -13371337\n"
"floaty_thing = 1.1\n"
Expand Down Expand Up @@ -154,7 +154,7 @@ void TestSettings::testAllSettings()

// Not sure if 1.1 is an exact value as a float, but doesn't matter
UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
UASSERT(s.get("stringy_thing") == u8"asd /( ¤%&(/\" BLÖÄRP");
UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
Expand Down
27 changes: 16 additions & 11 deletions src/unittest/test_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ void TestUtilities::testWrapDegrees_0_360_v3f()

void TestUtilities::testLowercase()
{
UASSERT(lowercase("Foo bAR") == "foo bar");
UASSERT(lowercase("eeeeeeaaaaaaaaaaaààààà") == "eeeeeeaaaaaaaaaaaààààà");
UASSERT(lowercase("MINETEST-powa") == "minetest-powa");
UASSERTEQ(auto, lowercase("Foo bAR"), "foo bar");
UASSERTEQ(auto, lowercase(u8"eeeeeeaaaaaaaaaaaààààà"), u8"eeeeeeaaaaaaaaaaaààààà");
// intentionally won't handle Unicode, regardless of locale
UASSERTEQ(auto, lowercase(u8"ÜÜ"), u8"ÜÜ");
UASSERTEQ(auto, lowercase("MINETEST-powa"), "minetest-powa");
}


Expand Down Expand Up @@ -306,18 +308,21 @@ void TestUtilities::testAsciiPrintableHelper()

void TestUtilities::testUTF8()
{
UASSERT(utf8_to_wide("¤") == L"¤");
UASSERT(utf8_to_wide(u8"¤") == L"¤");

UASSERT(wide_to_utf8(L"¤") == "¤");
UASSERTEQ(std::string, wide_to_utf8(L"¤"), u8"¤");

UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("")), "");
UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("the shovel dug a crumbly node!")),
"the shovel dug a crumbly node!");
UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("-ä-")),
"-ä-");
UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("-\xF0\xA0\x80\x8B-")),
"-\xF0\xA0\x80\x8B-");

UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide(u8"-ä-")),
u8"-ä-");
UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide(u8"-\U0002000b-")),
u8"-\U0002000b-");
if constexpr (sizeof(wchar_t) == 4) {
const auto *literal = U"-\U0002000b-";
UASSERT(utf8_to_wide(u8"-\U0002000b-") == reinterpret_cast<const wchar_t*>(literal));
}
}

void TestUtilities::testRemoveEscapes()
Expand Down Expand Up @@ -640,7 +645,7 @@ void TestUtilities::testSanitizeDirName()
UASSERTEQ(auto, sanitizeDirName(" a ", "~"), "_a_");
UASSERTEQ(auto, sanitizeDirName("COM1", "~"), "~COM1");
UASSERTEQ(auto, sanitizeDirName("COM1", ":"), "_COM1");
UASSERTEQ(auto, sanitizeDirName("cOm\u00B2", "~"), "~cOm\u00B2");
UASSERTEQ(auto, sanitizeDirName(u8"cOm\u00B2", "~"), u8"~cOm\u00B2");
UASSERTEQ(auto, sanitizeDirName("cOnIn$", "~"), "~cOnIn$");
UASSERTEQ(auto, sanitizeDirName(" cOnIn$ ", "~"), "_cOnIn$_");
}
Expand Down
23 changes: 19 additions & 4 deletions src/util/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ bool parseColorString(const std::string &value, video::SColor &color, bool quiet
unsigned char default_alpha = 0xff);
std::string encodeHexColorString(video::SColor color);

/**
* Converts a letter to lowercase, with safe handling of the char type and non-ASCII.
* @param c input letter
* @returns same letter but lowercase
*/
inline char my_tolower(char c)
{
// By design this function cannot handle any Unicode (codepoints don't fit into char),
// but make sure to pass it through unchanged.
// tolower() can mangle it if the POSIX locale is not UTF-8.
if (static_cast<unsigned char>(c) > 0x7f)
return c;
grorp marked this conversation as resolved.
Show resolved Hide resolved
// toupper(3): "If the argument c is of type char, it must be cast to unsigned char"
return tolower(static_cast<unsigned char>(c));
}

/**
* Returns a copy of \p str with spaces inserted at the right hand side to ensure
Expand Down Expand Up @@ -173,7 +188,7 @@ inline bool str_equal(std::basic_string_view<T> s1,
return false;

for (size_t i = 0; i < s1.size(); ++i)
if(tolower(s1[i]) != tolower(s2[i]))
if (my_tolower(s1[i]) != my_tolower(s2[i]))
return false;

return true;
Expand Down Expand Up @@ -212,7 +227,7 @@ inline bool str_starts_with(std::basic_string_view<T> str,
return str.compare(0, prefix.size(), prefix) == 0;

for (size_t i = 0; i < prefix.size(); ++i)
if (tolower(str[i]) != tolower(prefix[i]))
if (my_tolower(str[i]) != my_tolower(prefix[i]))
return false;
return true;
}
Expand Down Expand Up @@ -253,7 +268,7 @@ inline bool str_ends_with(std::basic_string_view<T> str,
return str.compare(start, suffix.size(), suffix) == 0;

for (size_t i = 0; i < suffix.size(); ++i)
if (tolower(str[start + i]) != tolower(suffix[i]))
if (my_tolower(str[start + i]) != my_tolower(suffix[i]))
return false;
return true;
}
Expand Down Expand Up @@ -305,7 +320,7 @@ inline std::string lowercase(std::string_view str)
std::string s2;
s2.resize(str.size());
for (size_t i = 0; i < str.size(); i++)
s2[i] = tolower(str[i]);
s2[i] = my_tolower(str[i]);
return s2;
}

Expand Down