Changeset - r27556:4cb7661a5c73
[Not reviewed]
master
0 3 0
Rubidium - 12 months ago 2023-06-08 15:15:32
rubidium@openttd.org
Codechange: move saveload string fixing code to saveload
3 files changed with 22 insertions and 28 deletions:
0 comments (0 inline, 0 general)
src/saveload/saveload.cpp
Show inline comments
 
@@ -890,6 +890,27 @@ static inline size_t SlCalcStdStringLen(
 
	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, VarTy
 
			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;
src/string.cpp
Show inline comments
 
@@ -116,30 +116,6 @@ std::string FormatArrayAsHex(span<const 
 
	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)
src/string_func.h
Show inline comments
 
@@ -41,7 +41,6 @@ void StrMakeValidInPlace(char *str, cons
 
[[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);
0 comments (0 inline, 0 general)