Codechange: move saveload string fixing code to saveload

This commit is contained in:
Rubidium 2023-06-08 17:15:32 +02:00 committed by rubidium42
parent 3f35787458
commit 97dd84d1e0
3 changed files with 22 additions and 28 deletions

View File

@ -890,6 +890,27 @@ static inline size_t SlCalcStdStringLen(const void *ptr)
return len + SlGetArrayLength(len); // also include the length of the index
}
/**
* Scan the string for old values of SCC_ENCODED and fix it to it's new, value.
* Note that at the moment this runs, the string has not been validated yet
* because the validation looks for SCC_ENCODED. If there is something invalid,
* just bail out and do not continue trying to replace the tokens.
* @param str the string to fix.
*/
static void FixSCCEncoded(std::string &str)
{
for (size_t i = 0; i < str.size(); /* nothing. */) {
size_t len = Utf8EncodedCharLen(str[i]);
if (len == 0 || i + len > str.size()) break;
WChar c;
Utf8Decode(&c, &str[i]);
if (c == 0xE028 || c == 0xE02A) Utf8Encode(&str[i], SCC_ENCODED);
i += len;
}
}
/**
* Save/Load a \c std::string.
* @param ptr the string being manipulated
@ -921,9 +942,7 @@ static void SlStdString(void *ptr, VarType conv)
StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK;
if ((conv & SLF_ALLOW_CONTROL) != 0) {
settings = settings | SVS_ALLOW_CONTROL_CODE;
if (IsSavegameVersionBefore(SLV_169)) {
str_fix_scc_encoded(str->data(), str->data() + len);
}
if (IsSavegameVersionBefore(SLV_169)) FixSCCEncoded(*str);
}
if ((conv & SLF_ALLOW_NEWLINE) != 0) {
settings = settings | SVS_ALLOW_NEWLINE;

View File

@ -116,30 +116,6 @@ std::string FormatArrayAsHex(span<const byte> data)
return str;
}
/**
* Scan the string for old values of SCC_ENCODED and fix it to
* it's new, static value.
* @param str the string to scan
* @param last the last valid character of str
*/
void str_fix_scc_encoded(char *str, const char *last)
{
while (str <= last && *str != '\0') {
size_t len = Utf8EncodedCharLen(*str);
if ((len == 0 && str + 4 > last) || str + len > last) break;
WChar c;
Utf8Decode(&c, str);
if (c == '\0') break;
if (c == 0xE028 || c == 0xE02A) {
c = SCC_ENCODED;
}
str += Utf8Encode(str, c);
}
*str = '\0';
}
template <class T>
static void StrMakeValidInPlace(T &dst, const char *str, const char *last, StringValidationSettings settings)

View File

@ -41,7 +41,6 @@ void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings s
[[nodiscard]] std::string StrMakeValid(std::string_view str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
void str_fix_scc_encoded(char *str, const char *last) NOACCESS(2);
bool strtolower(std::string &str, std::string::size_type offs = 0);
[[nodiscard]] bool StrValid(const char *str, const char *last) NOACCESS(2);