mirror of https://github.com/OpenTTD/OpenTTD
Codefix: implement StrTrimInPlace without assigning a borrowed view of itself
parent
4109b6848b
commit
e2db8277b8
|
@ -934,8 +934,7 @@ static bool ConClientNickChange(std::span<std::string_view> argv)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string client_name(argv[2]);
|
std::string client_name{StrTrimView(argv[2])};
|
||||||
StrTrimInPlace(client_name);
|
|
||||||
if (!NetworkIsValidClientName(client_name)) {
|
if (!NetworkIsValidClientName(client_name)) {
|
||||||
IConsolePrint(CC_ERROR, "Cannot give a client an empty name.");
|
IConsolePrint(CC_ERROR, "Cannot give a client an empty name.");
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -225,7 +225,15 @@ bool StrValid(std::span<const char> str)
|
||||||
*/
|
*/
|
||||||
void StrTrimInPlace(std::string &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)
|
std::string_view StrTrimView(std::string_view str)
|
||||||
|
|
Loading…
Reference in New Issue