Skip to content

Commit

Permalink
Case-insensitive server console commands/settings (#759)
Browse files Browse the repository at this point in the history
* feat: Case-insensitive server console commands/settings

* use std::transform
  • Loading branch information
ToeKneeRED authored Jan 10, 2025
1 parent 7d17c1e commit 0cc4691
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions Code/components/console/ConsoleRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ std::unique_ptr<TiltedPhoques::String[]> SplitLineTokens(const TiltedPhoques::St
std::optional<bool> BoolifyString(std::string_view view)
{
// let the compiler optimize this.
if (view == "true" || view == "TRUE" || view == "1")
if (view == "true" || view == "True" || view == "TRUE" || view == "1")
return true;

if (view == "false" || view == "FALSE" || view == "0")
if (view == "false" || view == "False" || view == "FALSE" || view == "0")
return false;

return std::nullopt;
Expand Down Expand Up @@ -183,7 +183,18 @@ void ConsoleRegistry::AddSetting(TiltedPhoques::UniquePtr<SettingBase> apSetting
CommandBase* ConsoleRegistry::FindCommand(const char* acName)
{
// TODO: Maybe lookup by some hash...
auto it = std::find_if(m_commands.begin(), m_commands.end(), [&](CommandBase* apCommand) { return std::strcmp(apCommand->m_name, acName) == 0; });
auto it = std::find_if(
m_commands.begin(), m_commands.end(),
[&](CommandBase* apCommand)
{
TiltedPhoques::String findName(acName);
TiltedPhoques::String settingName(apCommand->m_name);

std::transform(findName.begin(), findName.end(), findName.begin(), [](unsigned char c) { return std::tolower(c); });
std::transform(settingName.begin(), settingName.end(), settingName.begin(), [](unsigned char c) { return std::tolower(c); });

return std::strcmp(settingName.c_str(), findName.c_str()) == 0;
});
if (it == m_commands.end())
return nullptr;
return *it;
Expand All @@ -192,7 +203,18 @@ CommandBase* ConsoleRegistry::FindCommand(const char* acName)
SettingBase* ConsoleRegistry::FindSetting(const char* acName)
{
// TODO: Maybe lookup by some hash...
auto it = std::find_if(m_settings.begin(), m_settings.end(), [&](SettingBase* apSetting) { return std::strcmp(apSetting->name, acName) == 0; });
auto it = std::find_if(
m_settings.begin(), m_settings.end(),
[&](SettingBase* apSetting)
{
TiltedPhoques::String findName(acName);
TiltedPhoques::String settingName(apSetting->name);

std::transform(findName.begin(), findName.end(), findName.begin(), [](unsigned char c) { return std::tolower(c); });
std::transform(settingName.begin(), settingName.end(), settingName.begin(), [](unsigned char c) { return std::tolower(c); });

return std::strcmp(settingName.c_str(), findName.c_str()) == 0;
});
if (it == m_settings.end())
return nullptr;
return *it;
Expand Down

0 comments on commit 0cc4691

Please sign in to comment.