diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 54b8e7aa8c..a2141080a1 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -934,8 +934,7 @@ static bool ConClientNickChange(std::span argv) return true; } - std::string client_name(argv[2]); - StrTrimInPlace(client_name); + std::string client_name{StrTrimView(argv[2])}; if (!NetworkIsValidClientName(client_name)) { IConsolePrint(CC_ERROR, "Cannot give a client an empty name."); return true; diff --git a/src/string.cpp b/src/string.cpp index 494541610f..f3a90e692c 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -225,7 +225,15 @@ bool StrValid(std::span str) */ void StrTrimInPlace(std::string &str) { - str = StrTrimView(str); + size_t first_pos = str.find_first_not_of(' '); + if (first_pos == std::string::npos) { + str.clear(); + return; + } + str.erase(0, first_pos); + + size_t last_pos = str.find_last_not_of(' '); + str.erase(last_pos + 1); } std::string_view StrTrimView(std::string_view str)