mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-08-19 12:39:11 +00:00
Codechange: Change internal format of encoded strings to improve robustness and allow expansion. (#13499)
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include "../3rdparty/catch2/catch.hpp"
|
||||
|
||||
#include "../string_func.h"
|
||||
#include "../table/control_codes.h"
|
||||
|
||||
/**** String compare/equals *****/
|
||||
|
||||
@@ -408,3 +409,67 @@ TEST_CASE("StrTrimView") {
|
||||
}
|
||||
}
|
||||
|
||||
extern void FixSCCEncoded(std::string &str, bool fix_code);
|
||||
|
||||
/* Helper to call FixSCCEncoded and return the result in a new string. */
|
||||
static std::string FixSCCEncodedWrapper(const std::string &str, bool fix_code)
|
||||
{
|
||||
std::string result = str;
|
||||
FixSCCEncoded(result, fix_code);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Helper to compose a string part from a unicode character */
|
||||
static void ComposePart(std::back_insert_iterator<std::string> &output, char32_t c)
|
||||
{
|
||||
Utf8Encode(output, c);
|
||||
}
|
||||
|
||||
/* Helper to compose a string part from a string. */
|
||||
static void ComposePart(std::back_insert_iterator<std::string> &output, const std::string &value)
|
||||
{
|
||||
for (const auto &c : value) *output = c;
|
||||
}
|
||||
|
||||
/* Helper to compose a string from unicde or string parts. */
|
||||
template <typename... Args>
|
||||
static std::string Compose(Args &&... args)
|
||||
{
|
||||
std::string result;
|
||||
auto output = std::back_inserter(result);
|
||||
(ComposePart(output, args), ...);
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST_CASE("FixSCCEncoded")
|
||||
{
|
||||
/* Test conversion of empty string. */
|
||||
CHECK(FixSCCEncodedWrapper("", false) == "");
|
||||
|
||||
/* Test conversion of old code to new code. */
|
||||
CHECK(FixSCCEncodedWrapper("\uE0280", true) == Compose(SCC_ENCODED, "0"));
|
||||
|
||||
/* Test conversion of two old codes to new codes. */
|
||||
CHECK(FixSCCEncodedWrapper("\uE0280:\uE0281", true) == Compose(SCC_ENCODED, "0", SCC_RECORD_SEPARATOR, SCC_ENCODED, "1"));
|
||||
|
||||
/* Test conversion with no parameter. */
|
||||
CHECK(FixSCCEncodedWrapper("\uE0001", false) == Compose(SCC_ENCODED, "1"));
|
||||
|
||||
/* Test conversion with one numeric parameter. */
|
||||
CHECK(FixSCCEncodedWrapper("\uE00022:1", false) == Compose(SCC_ENCODED, "22", SCC_RECORD_SEPARATOR, SCC_ENCODED_NUMERIC, "1"));
|
||||
|
||||
/* Test conversion with two numeric parameters. */
|
||||
CHECK(FixSCCEncodedWrapper("\uE0003:12:2", false) == Compose(SCC_ENCODED, "3", SCC_RECORD_SEPARATOR, SCC_ENCODED_NUMERIC, "12", SCC_RECORD_SEPARATOR, SCC_ENCODED_NUMERIC, "2"));
|
||||
|
||||
/* Test conversion with one string parameter. */
|
||||
CHECK(FixSCCEncodedWrapper("\uE0004:\"Foo\"", false) == Compose(SCC_ENCODED, "4", SCC_RECORD_SEPARATOR, SCC_ENCODED_STRING, "Foo"));
|
||||
|
||||
/* Test conversion with two string parameters. */
|
||||
CHECK(FixSCCEncodedWrapper("\uE00055:\"Foo\":\"Bar\"", false) == Compose(SCC_ENCODED, "55", SCC_RECORD_SEPARATOR, SCC_ENCODED_STRING, "Foo", SCC_RECORD_SEPARATOR, SCC_ENCODED_STRING, "Bar"));
|
||||
|
||||
/* Test conversion with two string parameters surrounding a numeric parameter. */
|
||||
CHECK(FixSCCEncodedWrapper("\uE0006:\"Foo\":7CA:\"Bar\"", false) == Compose(SCC_ENCODED, "6", SCC_RECORD_SEPARATOR, SCC_ENCODED_STRING, "Foo", SCC_RECORD_SEPARATOR, SCC_ENCODED_NUMERIC, "7CA", SCC_RECORD_SEPARATOR, SCC_ENCODED_STRING, "Bar"));
|
||||
|
||||
/* Test conversion with one sub-string and two string parameters. */
|
||||
CHECK(FixSCCEncodedWrapper("\uE000777:\uE0008888:\"Foo\":\"BarBaz\"", false) == Compose(SCC_ENCODED, "777", SCC_RECORD_SEPARATOR, SCC_ENCODED, "8888", SCC_RECORD_SEPARATOR, SCC_ENCODED_STRING, "Foo", SCC_RECORD_SEPARATOR, SCC_ENCODED_STRING, "BarBaz"));
|
||||
}
|
||||
|
Reference in New Issue
Block a user