diff --git a/src/openrct2/core/String.hpp b/src/openrct2/core/String.hpp index 0629585f5b..1d0f463ede 100644 --- a/src/openrct2/core/String.hpp +++ b/src/openrct2/core/String.hpp @@ -111,4 +111,9 @@ namespace String * Converts a multi-byte string from one code page to another. */ std::string Convert(const std::string_view& src, sint32 srcCodePage, sint32 dstCodePage); -} // namespace String + + /** + * Returns an uppercased version of a UTF-8 string. + */ + std::string ToUpper(const utf8 * src); +} diff --git a/src/openrct2/localisation/Localisation.cpp b/src/openrct2/localisation/Localisation.cpp index 05d20b25e7..da03041103 100644 --- a/src/openrct2/localisation/Localisation.cpp +++ b/src/openrct2/localisation/Localisation.cpp @@ -31,6 +31,7 @@ #include "../common.h" #include "../config/Config.h" #include "../core/Math.hpp" +#include "../core/String.hpp" #include "../core/Util.hpp" #include "Date.h" #include "../Game.h" @@ -1215,12 +1216,16 @@ void format_string_to_upper(utf8 *dest, size_t size, rct_string_id format, void format_string(dest, size, format, args); - // Convert to upper case - utf8 *ch = dest; - while (*ch != '\0') { - *ch = toupper(*ch); - ch++; + std::string upperString = String::ToUpper(dest); + + if (upperString.size() + 1 >= size) { + upperString.resize(size - 1); + dest[size - 1] = '\0'; + log_warning("Truncating formatted string \"%s\" to %d bytes.", dest, size); } + + upperString.copy(dest, upperString.size()); + dest[upperString.size()] = '\0'; } money32 string_to_money(char * string_to_monetise) diff --git a/test/tests/StringTest.cpp b/test/tests/StringTest.cpp index c7f9704e5e..8dd1bd75ef 100644 --- a/test/tests/StringTest.cpp +++ b/test/tests/StringTest.cpp @@ -114,3 +114,18 @@ TEST_F(StringTest, Convert_Empty) } #endif + +/////////////////////////////////////////////////////////////////////////////// +// Tests for String::ToUpper +/////////////////////////////////////////////////////////////////////////////// + +TEST_F(StringTest, ToUpper_Basic) +{ + auto actual = String::ToUpper("test TEST tEsT 1234"); + ASSERT_STREQ(actual.c_str(), "TEST TEST TEST 1234"); +} +TEST_F(StringTest, ToUpper_Unicode) +{ + auto actual = String::ToUpper("éœǘξдȿ𞥃ꜳᲁ ÉŒǗΞДⱾ𞤡ꜲД 語"); + ASSERT_STREQ(actual.c_str(), "ÉŒǗΞДⱾ𞤡ꜲД ÉŒǗΞДⱾ𞤡ꜲД 語"); +}