1
0
Fork 0

Codefix: implement StrTrimInPlace without assigning a borrowed view of itself

pull/14191/head
Rubidium 2025-05-03 07:49:14 +02:00 committed by rubidium42
parent 4109b6848b
commit e2db8277b8
2 changed files with 10 additions and 3 deletions

View File

@ -934,8 +934,7 @@ static bool ConClientNickChange(std::span<std::string_view> 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;

View File

@ -225,7 +225,15 @@ bool StrValid(std::span<const char> 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)