1
0
Fork 0

Codechange: pass the characters to trim to StrTrimView

pull/14191/head
Rubidium 2025-05-03 08:05:54 +02:00 committed by rubidium42
parent e2db8277b8
commit 1f39d469ff
8 changed files with 16 additions and 11 deletions

View File

@ -934,7 +934,7 @@ static bool ConClientNickChange(std::span<std::string_view> argv)
return true;
}
std::string client_name{StrTrimView(argv[2])};
std::string client_name{StrTrimView(argv[2], StringConsumer::WHITESPACE_NO_NEWLINE)};
if (!NetworkIsValidClientName(client_name)) {
IConsolePrint(CC_ERROR, "Cannot give a client an empty name.");
return true;

View File

@ -8,6 +8,7 @@
/** @file console_gui.cpp Handling the GUI of the in-game console. */
#include "stdafx.h"
#include "core/string_consumer.hpp"
#include "textbuf_type.h"
#include "window_gui.h"
#include "autocompletion.h"
@ -77,7 +78,7 @@ public:
private:
std::vector<std::string> GetSuggestions(std::string_view prefix, std::string_view query) override
{
prefix = StrTrimView(prefix);
prefix = StrTrimView(prefix, StringConsumer::WHITESPACE_NO_NEWLINE);
std::vector<std::string> suggestions;
/* We only suggest commands or aliases, so we only do it for the first token or an argument to help command. */

View File

@ -96,7 +96,7 @@ static const std::initializer_list<KeycodeNames> _keycode_to_name = {
*/
static std::optional<uint16_t> ParseCode(std::string_view keystr)
{
keystr = StrTrimView(keystr);
keystr = StrTrimView(keystr, StringConsumer::WHITESPACE_NO_NEWLINE);
for (const auto &kn : _keycode_to_name) {
if (StrEqualsIgnoreCase(keystr, kn.name)) {
return kn.keycode;

View File

@ -9,6 +9,7 @@
#include "../stdafx.h"
#include "../core/string_consumer.hpp"
#include "../strings_func.h"
#include "../command_func.h"
#include "../timer/timer_game_tick.h"

View File

@ -9,6 +9,7 @@
#include "../stdafx.h"
#include "network_gui.h"
#include "../core/string_consumer.hpp"
#include "../saveload/saveload.h"
#include "../saveload/saveload_filter.h"
#include "../command_func.h"

View File

@ -225,24 +225,24 @@ bool StrValid(std::span<const char> str)
*/
void StrTrimInPlace(std::string &str)
{
size_t first_pos = str.find_first_not_of(' ');
size_t first_pos = str.find_first_not_of(StringConsumer::WHITESPACE_NO_NEWLINE);
if (first_pos == std::string::npos) {
str.clear();
return;
}
str.erase(0, first_pos);
size_t last_pos = str.find_last_not_of(' ');
size_t last_pos = str.find_last_not_of(StringConsumer::WHITESPACE_NO_NEWLINE);
str.erase(last_pos + 1);
}
std::string_view StrTrimView(std::string_view str)
std::string_view StrTrimView(std::string_view str, std::string_view characters_to_trim)
{
size_t first_pos = str.find_first_not_of(' ');
size_t first_pos = str.find_first_not_of(characters_to_trim);
if (first_pos == std::string::npos) {
return std::string_view{};
}
size_t last_pos = str.find_last_not_of(' ');
size_t last_pos = str.find_last_not_of(characters_to_trim);
return str.substr(first_pos, last_pos - first_pos + 1);
}

View File

@ -39,7 +39,7 @@ bool strtolower(std::string &str, std::string::size_type offs = 0);
[[nodiscard]] bool StrValid(std::span<const char> str);
void StrTrimInPlace(std::string &str);
[[nodiscard]] std::string_view StrTrimView(std::string_view str);
[[nodiscard]] std::string_view StrTrimView(std::string_view str, std::string_view characters_to_trim);
[[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, std::string_view prefix);
[[nodiscard]] bool StrEndsWithIgnoreCase(std::string_view str, std::string_view suffix);

View File

@ -14,6 +14,7 @@
#include "../string_func.h"
#include "../strings_func.h"
#include "../core/string_builder.hpp"
#include "../core/string_consumer.hpp"
#include "../table/control_codes.h"
#include "table/strings.h"
@ -398,7 +399,8 @@ static const std::vector<std::pair<std::string, std::string>> _str_trim_testcase
{"a ", "a"},
{" a ", "a"},
{" a b c ", "a b c"},
{" ", ""}
{" ", ""},
{" \r\f\t ", ""},
};
TEST_CASE("StrTrimInPlace")
@ -411,7 +413,7 @@ TEST_CASE("StrTrimInPlace")
TEST_CASE("StrTrimView") {
for (const auto& [input, expected] : _str_trim_testcases) {
CHECK(StrTrimView(input) == expected);
CHECK(StrTrimView(input, StringConsumer::WHITESPACE_NO_NEWLINE) == expected);
}
}