diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 1d5e214d8d..728ffafaa8 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -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; diff --git a/src/string.cpp b/src/string.cpp index c4f8278843..800efecb22 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -116,30 +116,6 @@ std::string FormatArrayAsHex(span 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 static void StrMakeValidInPlace(T &dst, const char *str, const char *last, StringValidationSettings settings) diff --git a/src/string_func.h b/src/string_func.h index 35a150fb88..517ced814d 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -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);