Skip to content

Commit

Permalink
String: Replace various const string & parameters with string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
JGRennison committed Oct 5, 2024
1 parent 8dbeaa6 commit cfbacdf
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 32 deletions.
16 changes: 9 additions & 7 deletions src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,20 @@ bool GetArgumentInteger(uint32_t *value, const char *arg)
* @param name String to remove the underscores from.
* @return A copy of \a name, without underscores.
*/
std::string RemoveUnderscores(std::string name)
std::string RemoveUnderscores(std::string_view name)
{
name.erase(std::remove(name.begin(), name.end(), '_'), name.end());
return name;
std::string output;
output.reserve(name.size());
std::copy_if(std::begin(name), std::end(name), std::back_inserter(output), [](char c) { return c != '_'; });
return output;
}

/**
* Register a new command to be used in the console
* @param name name of the command that will be used
* @param proc function that will be called upon execution of command
*/
/* static */ void IConsole::CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook, bool unlisted)
/* static */ void IConsole::CmdRegister(std::string_view name, IConsoleCmdProc *proc, IConsoleHook *hook, bool unlisted)
{
IConsole::Commands().try_emplace(RemoveUnderscores(name), name, proc, hook, unlisted);
}
Expand All @@ -171,7 +173,7 @@ std::string RemoveUnderscores(std::string name)
* @param name command to be found
* @return return Cmdstruct of the found command, or nullptr on failure
*/
/* static */ IConsoleCmd *IConsole::CmdGet(const std::string &name)
/* static */ IConsoleCmd *IConsole::CmdGet(std::string_view name)
{
auto item = IConsole::Commands().find(RemoveUnderscores(name));
if (item != IConsole::Commands().end()) return &item->second;
Expand All @@ -183,7 +185,7 @@ std::string RemoveUnderscores(std::string name)
* @param name name of the alias that will be used
* @param cmd name of the command that 'name' will be alias of
*/
/* static */ void IConsole::AliasRegister(const std::string &name, const std::string &cmd)
/* static */ void IConsole::AliasRegister(std::string_view name, std::string_view cmd)
{
auto result = IConsole::Aliases().try_emplace(RemoveUnderscores(name), name, cmd);
if (!result.second) IConsolePrint(CC_ERROR, "An alias with the name '{}' already exists.", name);
Expand All @@ -194,7 +196,7 @@ std::string RemoveUnderscores(std::string name)
* @param name alias to be found
* @return return Aliasstruct of the found alias, or nullptr on failure
*/
/* static */ IConsoleAlias *IConsole::AliasGet(const std::string &name)
/* static */ IConsoleAlias *IConsole::AliasGet(std::string_view name)
{
auto item = IConsole::Aliases().find(RemoveUnderscores(name));
if (item != IConsole::Aliases().end()) return &item->second;
Expand Down
2 changes: 1 addition & 1 deletion src/console_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ static void IConsoleTabCompletion()
match_input.prefix = std::string(input, cmdptr - input);
if (match_input.prefix.empty()) return;

extern std::string RemoveUnderscores(std::string name);
extern std::string RemoveUnderscores(std::string_view name);
match_input_no_underscores.prefix = RemoveUnderscores(match_input.prefix);
if (match_input_no_underscores.prefix.empty()) return;

Expand Down
12 changes: 6 additions & 6 deletions src/console_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enum ConsoleHookResult {
typedef bool IConsoleCmdProc(uint8_t argc, char *argv[]);
typedef ConsoleHookResult IConsoleHook(bool echo);
struct IConsoleCmd {
IConsoleCmd(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook, bool unlisted) : name(name), proc(proc), hook(hook), unlisted(unlisted) {}
IConsoleCmd(std::string_view name, IConsoleCmdProc *proc, IConsoleHook *hook, bool unlisted) : name(name), proc(proc), hook(hook), unlisted(unlisted) {}

std::string name; ///< name of command
IConsoleCmdProc *proc; ///< process executed when command is typed
Expand All @@ -55,7 +55,7 @@ struct IConsoleCmd {
* - ";" allows for combining commands (see example 'ng')
*/
struct IConsoleAlias {
IConsoleAlias(const std::string &name, const std::string &cmdline) : name(name), cmdline(cmdline) {}
IConsoleAlias(std::string_view name, std::string_view cmdline) : name(name), cmdline(cmdline) {}

std::string name; ///< name of the alias
std::string cmdline; ///< command(s) that is/are being aliased
Expand All @@ -71,10 +71,10 @@ struct IConsole
static AliasList &Aliases();

/* Commands */
static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook = nullptr, bool unlisted = false);
static IConsoleCmd *CmdGet(const std::string &name);
static void AliasRegister(const std::string &name, const std::string &cmd);
static IConsoleAlias *AliasGet(const std::string &name);
static void CmdRegister(std::string_view name, IConsoleCmdProc *proc, IConsoleHook *hook = nullptr, bool unlisted = false);
static IConsoleCmd *CmdGet(std::string_view name);
static void AliasRegister(std::string_view name, std::string_view cmd);
static IConsoleAlias *AliasGet(std::string_view name);
};

/* console functions */
Expand Down
8 changes: 4 additions & 4 deletions src/core/tinystring_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class TinyString {
other.storage = nullptr;
}

inline TinyString(const std::string &other) noexcept
inline TinyString(std::string_view other) noexcept
{
if (!other.empty()) this->storage = stredup(other.c_str());
if (!other.empty()) this->storage = stredup(other.data(), other.data() + other.size());
}

inline TinyString(const char *str) noexcept
Expand Down Expand Up @@ -81,10 +81,10 @@ class TinyString {
return *this;
}

inline TinyString &operator=(const std::string &other)
inline TinyString &operator=(std::string_view other)
{
this->clear();
if (!other.empty()) this->storage = stredup(other.c_str());
if (!other.empty()) this->storage = stredup(other.data(), other.data() + other.size());
return *this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/fios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ void ScanScenarios()
* Constructs FiosNumberedSaveName. Initial number is the most recent save, or -1 if not found.
* @param prefix The prefix to use to generate a filename.
*/
FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(prefix), number(-1)
FiosNumberedSaveName::FiosNumberedSaveName(std::string_view prefix) : prefix(prefix), number(-1)
{
static std::optional<std::string> _autosave_path;
if (!_autosave_path) _autosave_path = FioFindDirectory(AUTOSAVE_DIR);
Expand Down
2 changes: 1 addition & 1 deletion src/fios.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const char *FindScenario(const ContentInfo *ci, bool md5sum);
* A savegame name automatically numbered.
*/
struct FiosNumberedSaveName {
FiosNumberedSaveName(const std::string &prefix);
FiosNumberedSaveName(std::string_view prefix);
std::string Filename();
std::string FilenameUsingMaxSaves(int max_saves);
void FilenameUsingNumber(struct format_target &buffer, int num, const char *suffix) const;
Expand Down
2 changes: 1 addition & 1 deletion src/news_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ struct NewsItem {
/** Container for a single string to be passed as NewsAllocatedData. */
struct NewsStringData : NewsAllocatedData {
std::string string; ///< The string to retain.
NewsStringData(const std::string &str) : string(str) {}
NewsStringData(std::string str) : string(std::move(str)) {}
};

/**
Expand Down
12 changes: 6 additions & 6 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3157,12 +3157,12 @@ void IntSettingDesc::ChangeValue(const void *object, int32_t newval, SaveToConfi
* @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(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, sd->save.ext_feature_test)) 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. */
Expand All @@ -3171,7 +3171,7 @@ static const SettingDesc *GetSettingFromName(const char *name, const SettingTabl
const char *short_name = strchr(sd->name, '.');
if (short_name != nullptr) {
short_name++;
if (strcmp(short_name, name) == 0) return sd.get();
if (short_name == name) return sd.get();
}
}

Expand All @@ -3184,9 +3184,9 @@ static const SettingDesc *GetSettingFromName(const char *name, const SettingTabl
* @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;
if (name.starts_with("company.")) name.remove_prefix(8);
return GetSettingFromName(name, _company_settings);
}

Expand All @@ -3196,7 +3196,7 @@ static const SettingDesc *GetCompanySettingFromName(const char *name)
* @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(std::string_view name)
{
for (auto &table : _generic_setting_tables) {
auto sd = GetSettingFromName(name, table);
Expand Down
6 changes: 1 addition & 5 deletions src/settings_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,7 @@ struct NullSettingDesc : SettingDesc {

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

const SettingDesc *GetSettingFromName(const char *name);
inline const SettingDesc *GetSettingFromName(const std::string &name)
{
return GetSettingFromName(name.c_str());
}
const SettingDesc *GetSettingFromName(std::string_view name);

bool SetSettingValue(const IntSettingDesc *sd, int32_t value, bool force_newgame = false);
bool SetSettingValue(const StringSettingDesc *sd, const std::string value, bool force_newgame = false);
Expand Down

0 comments on commit cfbacdf

Please sign in to comment.