Changeset - r25649:96782a72248d
[Not reviewed]
master
0 2 0
rubidium42 - 3 years ago 2021-05-30 09:30:03
rubidium@openttd.org
Codechange: use StrStartsWith/StrEndsWith when finding settings
2 files changed with 14 insertions and 18 deletions:
0 comments (0 inline, 0 general)
src/settings.cpp
Show inline comments
 
@@ -1235,7 +1235,7 @@ static void HandleOldDiffCustom(bool sav
 
		if (has_no_town_council_tolerance && name == "town_council_tolerance") continue;
 

	
 
		std::string fullname = "difficulty." + name;
 
		const SettingDesc *sd = GetSettingFromName(fullname.c_str());
 
		const SettingDesc *sd = GetSettingFromName(fullname);
 

	
 
		/* Some settings are no longer in use; skip reading those. */
 
		if (sd == nullptr) {
 
@@ -1705,22 +1705,19 @@ void IntSettingDesc::ChangeValue(const v
 
 * @return Pointer to the setting description of setting \a name if it can be found,
 
 *         \c nullptr indicates failure to obtain the description.
 
 */
 
static const SettingDesc *GetSettingFromName(const char *name, const SettingTable &settings)
 
static const SettingDesc *GetSettingFromName(const std::string_view name, const SettingTable &settings)
 
{
 
	/* First check all full names */
 
	for (auto &sd : settings) {
 
		if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
 
		if (strcmp(sd->name, name) == 0) return sd.get();
 
		if (sd->name == name) return sd.get();
 
	}
 

	
 
	/* Then check the shortcut variant of the name. */
 
	std::string short_name_suffix = std::string{ "." }.append(name);
 
	for (auto &sd : settings) {
 
		if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
 
		const char *short_name = strchr(sd->name, '.');
 
		if (short_name != nullptr) {
 
			short_name++;
 
			if (strcmp(short_name, name) == 0) return sd.get();
 
		}
 
		if (StrEndsWith(sd->name, short_name_suffix)) return sd.get();
 
	}
 

	
 
	return nullptr;
 
@@ -1731,13 +1728,11 @@ static const SettingDesc *GetSettingFrom
 
 * @param prefix The prefix to look for.
 
 * @param saveloads A vector to store the result in.
 
 */
 
void GetSettingSaveLoadByPrefix(const char *prefix, std::vector<SaveLoad> &saveloads)
 
void GetSettingSaveLoadByPrefix(std::string_view prefix, std::vector<SaveLoad> &saveloads)
 
{
 
	size_t prefixlen = strlen(prefix);
 

	
 
	for (auto &sd : _settings) {
 
		if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
 
		if (strncmp(sd->name, prefix, prefixlen) == 0) saveloads.push_back(sd->save);
 
		if (StrStartsWith(sd->name, prefix)) saveloads.push_back(sd->save);
 
	}
 
}
 

	
 
@@ -1747,9 +1742,10 @@ void GetSettingSaveLoadByPrefix(const ch
 
 * @return Pointer to the setting description of setting \a name if it can be found,
 
 *         \c nullptr indicates failure to obtain the description.
 
 */
 
static const SettingDesc *GetCompanySettingFromName(const char *name)
 
static const SettingDesc *GetCompanySettingFromName(std::string_view name)
 
{
 
	if (strncmp(name, "company.", 8) == 0) name += 8;
 
	static const std::string_view company_prefix = "company.";
 
	if (StrStartsWith(name, company_prefix)) name.remove_prefix(company_prefix.size());
 
	return GetSettingFromName(name, _company_settings);
 
}
 

	
 
@@ -1759,7 +1755,7 @@ static const SettingDesc *GetCompanySett
 
 * @return Pointer to the setting description of setting \a name if it can be found,
 
 *         \c nullptr indicates failure to obtain the description.
 
 */
 
const SettingDesc *GetSettingFromName(const char *name)
 
const SettingDesc *GetSettingFromName(const std::string_view name)
 
{
 
	auto sd = GetSettingFromName(name, _settings);
 
	if (sd != nullptr) return sd;
 
@@ -1781,7 +1777,7 @@ const SettingDesc *GetSettingFromName(co
 
CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
 
{
 
	if (text.empty()) return CMD_ERROR;
 
	const SettingDesc *sd = GetSettingFromName(text.c_str());
 
	const SettingDesc *sd = GetSettingFromName(text);
 

	
 
	if (sd == nullptr) return CMD_ERROR;
 
	if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
src/settings_internal.h
Show inline comments
 
@@ -301,8 +301,8 @@ struct NullSettingDesc : SettingDesc {
 

	
 
typedef std::initializer_list<std::unique_ptr<const SettingDesc>> SettingTable;
 

	
 
const SettingDesc *GetSettingFromName(const char *name);
 
void GetSettingSaveLoadByPrefix(const char *prefix, std::vector<SaveLoad> &saveloads);
 
const SettingDesc *GetSettingFromName(const std::string_view name);
 
void GetSettingSaveLoadByPrefix(const std::string_view prefix, std::vector<SaveLoad> &saveloads);
 
bool SetSettingValue(const IntSettingDesc *sd, int32 value, bool force_newgame = false);
 
bool SetSettingValue(const StringSettingDesc *sd, const std::string value, bool force_newgame = false);
 

	
0 comments (0 inline, 0 general)