Implement ICU support for uppercasing, with tests.

This commit is contained in:
Fusxfaranto 2018-05-14 21:35:48 +02:00 committed by Aaron van Geffen
parent f29b42cc26
commit a91dd6a356
3 changed files with 31 additions and 6 deletions

View File

@ -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);
}

View File

@ -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)

View File

@ -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(), "ÉŒǗΞДⱾ𞤡ꜲД ÉŒǗΞДⱾ𞤡ꜲД 語");
}