Changeset - r25554:3bffaeffba17
[Not reviewed]
master
0 22 0
Patric Stout - 3 years ago 2021-05-29 09:21:38
truebrain@openttd.org
Codechange: rename str_validate to StrMakeValid(InPlace) (#9304)

This to be more explicit the function changes the value, and not
returns yes/no.
22 files changed with 63 insertions and 62 deletions:
0 comments (0 inline, 0 general)
src/console.cpp
Show inline comments
 
@@ -111,7 +111,7 @@ void IConsolePrint(TextColour colour_cod
 
	 * characters and (when applicable) assign it to the console buffer */
 
	str = stredup(string);
 
	str_strip_colours(str);
 
	str_validate(str, str + strlen(str));
 
	StrMakeValidInPlace(str);
 

	
 
	if (_network_dedicated) {
 
		NetworkAdminConsole("console", str);
src/fios.cpp
Show inline comments
 
@@ -342,7 +342,7 @@ bool FiosFileScanner::AddFile(const std:
 
		t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1);
 
	}
 
	strecpy(fios->title, t, lastof(fios->title));
 
	str_validate(fios->title, lastof(fios->title));
 
	StrMakeValidInPlace(fios->title, lastof(fios->title));
 

	
 
	return true;
 
}
 
@@ -394,7 +394,7 @@ static void FiosGetFileList(SaveLoadOper
 
				std::string dirname = std::string(d_name) + PATHSEP;
 
				SetDParamStr(0, dirname);
 
				GetString(fios->title, STR_SAVELOAD_DIRECTORY, lastof(fios->title));
 
				str_validate(fios->title, lastof(fios->title));
 
				StrMakeValidInPlace(fios->title, lastof(fios->title));
 
			}
 
		}
 
		closedir(dir);
 
@@ -446,7 +446,7 @@ static void GetFileTitle(const std::stri
 
	size_t read = fread(title, 1, last - title, f);
 
	assert(title + read <= last);
 
	title[read] = '\0';
 
	str_validate(title, last);
 
	StrMakeValidInPlace(title, last);
 
	FioFCloseFile(f);
 
}
 

	
src/highscore.cpp
Show inline comments
 
@@ -170,7 +170,7 @@ void LoadFromHighScore()
 
					i = SP_SAVED_HIGHSCORE_END;
 
					break;
 
				}
 
				str_validate(hs->company, lastof(hs->company), SVS_NONE);
 
				StrMakeValidInPlace(hs->company, lastof(hs->company), SVS_NONE);
 
				hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
 
			}
 
		}
src/ini_load.cpp
Show inline comments
 
@@ -22,7 +22,7 @@
 
 */
 
IniItem::IniItem(IniGroup *parent, const std::string &name) : next(nullptr)
 
{
 
	this->name = str_validate(name);
 
	this->name = StrMakeValid(name);
 

	
 
	*parent->last_item = this;
 
	parent->last_item = &this->next;
 
@@ -50,7 +50,7 @@ void IniItem::SetValue(const std::string
 
 */
 
IniGroup::IniGroup(IniLoadFile *parent, const std::string &name) : next(nullptr), type(IGT_VARIABLES), item(nullptr)
 
{
 
	this->name = str_validate(name);
 
	this->name = StrMakeValid(name);
 

	
 
	this->last_item = &this->item;
 
	*parent->last_group = this;
 
@@ -288,7 +288,7 @@ void IniLoadFile::LoadFromDisk(const std
 
			if (!quoted && e == t) {
 
				item->value.reset();
 
			} else {
 
				item->value = str_validate(std::string(t));
 
				item->value = StrMakeValid(std::string(t));
 
			}
 
		} else {
 
			/* it's an orphan item */
src/misc_gui.cpp
Show inline comments
 
@@ -997,7 +997,7 @@ struct QueryStringWindow : public Window
 
	{
 
		char *last_of = &this->editbox.text.buf[this->editbox.text.max_bytes - 1];
 
		GetString(this->editbox.text.buf, str, last_of);
 
		str_validate(this->editbox.text.buf, last_of, SVS_NONE);
 
		StrMakeValidInPlace(this->editbox.text.buf, last_of, SVS_NONE);
 

	
 
		/* Make sure the name isn't too long for the text buffer in the number of
 
		 * characters (not bytes). max_chars also counts the '\0' characters. */
src/network/core/packet.cpp
Show inline comments
 
@@ -394,7 +394,7 @@ void Packet::Recv_string(char *buffer, s
 
	assert(pos <= std::numeric_limits<PacketSize>::max());
 
	this->pos = static_cast<PacketSize>(pos);
 

	
 
	str_validate(bufp, last, settings);
 
	StrMakeValidInPlace(bufp, last, settings);
 
}
 

	
 
/**
 
@@ -423,7 +423,7 @@ std::string Packet::Recv_string(size_t l
 
		while (this->Recv_uint8() != '\0') {}
 
	}
 

	
 
	return str_validate(str, settings);
 
	return StrMakeValid(str, settings);
 
}
 

	
 
/**
src/newgrf.cpp
Show inline comments
 
@@ -2598,7 +2598,7 @@ static std::string ReadDWordAsString(Byt
 
	char output[5];
 
	for (int i = 0; i < 4; i++) output[i] = reader->ReadByte();
 
	output[4] = '\0';
 
	str_validate(output, lastof(output));
 
	StrMakeValidInPlace(output, lastof(output));
 

	
 
	return std::string(output);
 
}
src/os/os2/os2.cpp
Show inline comments
 
@@ -174,7 +174,7 @@ int CDECL main(int argc, char *argv[])
 
	SetRandomSeed(time(nullptr));
 

	
 
	/* Make sure our arguments contain only valid UTF-8 characters. */
 
	for (int i = 0; i < argc; i++) ValidateString(argv[i]);
 
	for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);
 

	
 
	return openttd_main(argc, argv);
 
}
src/os/unix/unix.cpp
Show inline comments
 
@@ -243,7 +243,7 @@ void CocoaReleaseAutoreleasePool();
 
int CDECL main(int argc, char *argv[])
 
{
 
	/* Make sure our arguments contain only valid UTF-8 characters. */
 
	for (int i = 0; i < argc; i++) ValidateString(argv[i]);
 
	for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);
 

	
 
#ifdef WITH_COCOA
 
	CocoaSetupAutoreleasePool();
src/os/windows/win32.cpp
Show inline comments
 
@@ -429,7 +429,7 @@ int APIENTRY WinMain(HINSTANCE hInstance
 
	argc = ParseCommandLine(cmdline, argv, lengthof(argv));
 

	
 
	/* Make sure our arguments contain only valid UTF-8 characters. */
 
	for (int i = 0; i < argc; i++) ValidateString(argv[i]);
 
	for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);
 

	
 
	openttd_main(argc, argv);
 

	
src/saveload/oldloader.cpp
Show inline comments
 
@@ -234,7 +234,7 @@ static inline bool CheckOldSavegameType(
 

	
 
	bool ret = VerifyOldNameChecksum(temp, len);
 
	temp[len - 2] = '\0'; // name is null-terminated in savegame, but it's better to be sure
 
	str_validate(temp, last);
 
	StrMakeValidInPlace(temp, last);
 

	
 
	return ret;
 
}
src/saveload/saveload.cpp
Show inline comments
 
@@ -972,7 +972,7 @@ static void SlString(void *ptr, size_t l
 
			if ((conv & SLF_ALLOW_NEWLINE) != 0) {
 
				settings = settings | SVS_ALLOW_NEWLINE;
 
			}
 
			str_validate((char *)ptr, (char *)ptr + len, settings);
 
			StrMakeValidInPlace((char *)ptr, (char *)ptr + len, settings);
 
			break;
 
		}
 
		case SLA_PTRS: break;
 
@@ -1016,7 +1016,7 @@ static void SlStdString(void *ptr, VarTy
 
			if ((conv & SLF_ALLOW_NEWLINE) != 0) {
 
				settings = settings | SVS_ALLOW_NEWLINE;
 
			}
 
			str_validate(buf, buf + len, settings);
 
			StrMakeValidInPlace(buf, buf + len, settings);
 

	
 
			// Store sanitized string.
 
			str->assign(buf);
src/script/api/script_object.cpp
Show inline comments
 
@@ -283,7 +283,7 @@ ScriptObject::ActiveInstance::~ActiveIns
 
{
 
	char buffer[64];
 
	::GetString(buffer, string, lastof(buffer));
 
	::str_validate(buffer, lastof(buffer), SVS_NONE);
 
	::StrMakeValidInPlace(buffer, lastof(buffer), SVS_NONE);
 
	return ::stredup(buffer);
 
}
 

	
 
@@ -312,7 +312,7 @@ ScriptObject::ActiveInstance::~ActiveIns
 
	if (!StrEmpty(text) && (GetCommandFlags(cmd) & CMD_STR_CTRL) == 0) {
 
		/* The string must be valid, i.e. not contain special codes. Since some
 
		 * can be made with GSText, make sure the control codes are removed. */
 
		::str_validate(const_cast<char *>(text), text + strlen(text), SVS_NONE);
 
		::StrMakeValidInPlace(text, SVS_NONE);
 
	}
 

	
 
	/* Set the default callback to return a true/false result of the DoCommand */
src/script/api/script_text.cpp
Show inline comments
 
@@ -82,7 +82,7 @@ SQInteger ScriptText::_SetParam(int para
 
			sq_getstring(vm, -1, &value);
 

	
 
			this->params[parameter] = stredup(value);
 
			ValidateString(this->params[parameter]);
 
			StrMakeValidInPlace(this->params[parameter]);
 
			break;
 
		}
 

	
 
@@ -157,7 +157,7 @@ SQInteger ScriptText::_set(HSQUIRRELVM v
 
	if (sq_gettype(vm, 2) == OT_STRING) {
 
		const SQChar *key_string;
 
		sq_getstring(vm, 2, &key_string);
 
		ValidateString(key_string);
 
		StrMakeValidInPlace(key_string);
 

	
 
		if (strncmp(key_string, "param_", 6) != 0 || strlen(key_string) > 8) return SQ_ERROR;
 
		k = atoi(key_string + 6);
src/script/script_info.cpp
Show inline comments
 
@@ -122,14 +122,14 @@ SQInteger ScriptInfo::AddSetting(HSQUIRR
 
	while (SQ_SUCCEEDED(sq_next(vm, -2))) {
 
		const SQChar *key;
 
		if (SQ_FAILED(sq_getstring(vm, -2, &key))) return SQ_ERROR;
 
		ValidateString(key);
 
		StrMakeValidInPlace(key);
 

	
 
		if (strcmp(key, "name") == 0) {
 
			const SQChar *sqvalue;
 
			if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
 
			char *name = stredup(sqvalue);
 
			char *s;
 
			ValidateString(name);
 
			StrMakeValidInPlace(name);
 

	
 
			/* Don't allow '=' and ',' in configure setting names, as we need those
 
			 *  2 chars to nicely store the settings as a string. */
 
@@ -141,7 +141,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRR
 
			const SQChar *sqdescription;
 
			if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
 
			config.description = stredup(sqdescription);
 
			ValidateString(config.description);
 
			StrMakeValidInPlace(config.description);
 
			items |= 0x002;
 
		} else if (strcmp(key, "min_value") == 0) {
 
			SQInteger res;
 
@@ -226,7 +226,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRE
 
{
 
	const SQChar *setting_name;
 
	if (SQ_FAILED(sq_getstring(vm, -2, &setting_name))) return SQ_ERROR;
 
	ValidateString(setting_name);
 
	StrMakeValidInPlace(setting_name);
 

	
 
	ScriptConfigItem *config = nullptr;
 
	for (auto &item : this->config_list) {
 
@@ -253,7 +253,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRE
 
		/* Because squirrel doesn't support identifiers starting with a digit,
 
		 * we skip the first character. */
 
		int key = atoi(key_string + 1);
 
		ValidateString(label);
 
		StrMakeValidInPlace(label);
 

	
 
		/* !Contains() prevents stredup from leaking. */
 
		if (!config->labels->Contains(key)) config->labels->Insert(key, stredup(label));
src/script/squirrel.cpp
Show inline comments
 
@@ -448,7 +448,7 @@ bool Squirrel::CallStringMethodStrdup(HS
 
	if (!this->CallMethod(instance, method_name, &ret, suspend)) return false;
 
	if (ret._type != OT_STRING) return false;
 
	*res = stredup(ObjectToString(&ret));
 
	ValidateString(*res);
 
	StrMakeValidInPlace(*res);
 
	return true;
 
}
 

	
src/script/squirrel_helper.hpp
Show inline comments
 
@@ -116,7 +116,7 @@ namespace SQConvert {
 
		char *tmp_str = stredup(tmp);
 
		sq_poptop(vm);
 
		ptr->push_back((void *)tmp_str);
 
		str_validate(tmp_str, tmp_str + strlen(tmp_str));
 
		StrMakeValidInPlace(tmp_str);
 
		return tmp_str;
 
	}
 

	
src/settings.cpp
Show inline comments
 
@@ -497,7 +497,7 @@ void StringSettingDesc::MakeValueValid(s
 
	 * includes the '\0' termination for network transfer purposes.
 
	 * Also ensure the string is valid after chopping of some bytes. */
 
	std::string stdstr(str, this->max_length - 1);
 
	str.assign(str_validate(stdstr, SVS_NONE));
 
	str.assign(StrMakeValid(stdstr, SVS_NONE));
 
}
 

	
 
/**
src/string.cpp
Show inline comments
 
@@ -186,7 +186,7 @@ void str_fix_scc_encoded(char *str, cons
 

	
 

	
 
template <class T>
 
static void str_validate(T &dst, const char *str, const char *last, StringValidationSettings settings)
 
static void StrMakeValidInPlace(T &dst, const char *str, const char *last, StringValidationSettings settings)
 
{
 
	/* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
 

	
 
@@ -246,50 +246,51 @@ static void str_validate(T &dst, const c
 
}
 

	
 
/**
 
 * Scans the string for valid characters and if it finds invalid ones,
 
 * replaces them with a question mark '?' (if not ignored)
 
 * @param str the string to validate
 
 * @param last the last valid character of str
 
 * @param settings the settings for the string validation.
 
 * Scans the string for invalid characters and replaces then with a
 
 * question mark '?' (if not ignored).
 
 * @param str The string to validate.
 
 * @param last The last valid character of str.
 
 * @param settings The settings for the string validation.
 
 */
 
void str_validate(char *str, const char *last, StringValidationSettings settings)
 
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
 
{
 
	char *dst = str;
 
	str_validate(dst, str, last, settings);
 
	StrMakeValidInPlace(dst, str, last, settings);
 
	*dst = '\0';
 
}
 

	
 
/**
 
 * Scans the string for valid characters and if it finds invalid ones,
 
 * replaces them with a question mark '?' (if not ignored)
 
 * @param str the string to validate
 
 * @param settings the settings for the string validation.
 
 * Scans the string for invalid characters and replaces then with a
 
 * question mark '?' (if not ignored).
 
 * Only use this function when you are sure the string ends with a '\0';
 
 * otherwise use StrMakeValidInPlace(str, last, settings) variant.
 
 * @param str The string (of which you are sure ends with '\0') to validate.
 
 */
 
std::string str_validate(const std::string &str, StringValidationSettings settings)
 
void StrMakeValidInPlace(const char *str, StringValidationSettings settings)
 
{
 
	/* We know it is '\0' terminated. */
 
	StrMakeValidInPlace(const_cast<char *>(str), str + strlen(str), settings);
 
}
 

	
 
/**
 
 * Scans the string for invalid characters and replaces then with a
 
 * question mark '?' (if not ignored).
 
 * @param str The string to validate.
 
 * @param settings The settings for the string validation.
 
 */
 
std::string StrMakeValid(const std::string &str, StringValidationSettings settings)
 
{
 
	auto buf = str.data();
 
	auto last = buf + str.size();
 

	
 
	std::ostringstream dst;
 
	std::ostreambuf_iterator<char> dst_iter(dst);
 
	str_validate(dst_iter, buf, last, settings);
 
	StrMakeValidInPlace(dst_iter, buf, last, settings);
 

	
 
	return dst.str();
 
}
 

	
 
/**
 
 * Scans the string for valid characters and if it finds invalid ones,
 
 * replaces them with a question mark '?'.
 
 * @param str the string to validate
 
 */
 
void ValidateString(const char *str)
 
{
 
	/* We know it is '\0' terminated. */
 
	str_validate(const_cast<char *>(str), str + strlen(str) + 1);
 
}
 

	
 

	
 
/**
 
 * Checks whether the given string is valid, i.e. contains only
 
 * valid (printable) characters and is properly terminated.
 
 * @param str  The string to validate.
src/string_func.h
Show inline comments
 
@@ -39,9 +39,9 @@ int CDECL vseprintf(char *str, const cha
 

	
 
char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
 

	
 
void str_validate(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
 
[[nodiscard]] std::string str_validate(const std::string &str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
 
void ValidateString(const char *str);
 
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
 
[[nodiscard]] std::string StrMakeValid(const std::string &str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
 
void StrMakeValidInPlace(const char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
 

	
 
void str_fix_scc_encoded(char *str, const char *last) NOACCESS(2);
 
void str_strip_colours(char *str);
src/textfile_gui.cpp
Show inline comments
 
@@ -375,7 +375,7 @@ static void Xunzip(byte **bufp, size_t *
 
	this->text = ReallocT(this->text, filesize + 1);
 
	this->text[filesize] = '\0';
 

	
 
	/* Replace tabs and line feeds with a space since str_validate removes those. */
 
	/* Replace tabs and line feeds with a space since StrMakeValidInPlace removes those. */
 
	for (char *p = this->text; *p != '\0'; p++) {
 
		if (*p == '\t' || *p == '\r') *p = ' ';
 
	}
 
@@ -384,7 +384,7 @@ static void Xunzip(byte **bufp, size_t *
 
	char *p = this->text + (strncmp(u8"\ufeff", this->text, 3) == 0 ? 3 : 0);
 

	
 
	/* Make sure the string is a valid UTF-8 sequence. */
 
	str_validate(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
 
	StrMakeValidInPlace(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
 

	
 
	/* Split the string on newlines. */
 
	int row = 0;
src/video/dedicated_v.cpp
Show inline comments
 
@@ -229,7 +229,7 @@ static void DedicatedHandleKeyInput()
 
			break;
 
		}
 
	}
 
	str_validate(input_line, lastof(input_line));
 
	StrMakeValidInPlace(input_line, lastof(input_line));
 

	
 
	IConsoleCmdExec(input_line); // execute command
 
}
0 comments (0 inline, 0 general)