From 1f39d469ff01607a4ab2e7870c2a09cbdd5f8ff9 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 3 May 2025 08:05:54 +0200 Subject: [PATCH] Codechange: pass the characters to trim to StrTrimView --- src/console_cmds.cpp | 2 +- src/console_gui.cpp | 3 ++- src/hotkeys.cpp | 2 +- src/network/network.cpp | 1 + src/network/network_client.cpp | 1 + src/string.cpp | 10 +++++----- src/string_func.h | 2 +- src/tests/string_func.cpp | 6 ++++-- 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index a2141080a1..3797c3c2b1 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -934,7 +934,7 @@ static bool ConClientNickChange(std::span 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; diff --git a/src/console_gui.cpp b/src/console_gui.cpp index a3cf57c437..d9e3064b35 100644 --- a/src/console_gui.cpp +++ b/src/console_gui.cpp @@ -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 GetSuggestions(std::string_view prefix, std::string_view query) override { - prefix = StrTrimView(prefix); + prefix = StrTrimView(prefix, StringConsumer::WHITESPACE_NO_NEWLINE); std::vector suggestions; /* We only suggest commands or aliases, so we only do it for the first token or an argument to help command. */ diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index 34119cb1ba..59a623a968 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -96,7 +96,7 @@ static const std::initializer_list _keycode_to_name = { */ static std::optional 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; diff --git a/src/network/network.cpp b/src/network/network.cpp index 0054cd5a6e..c5c276deb1 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -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" diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index 6832618b57..2382cb6a70 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -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" diff --git a/src/string.cpp b/src/string.cpp index f3a90e692c..e28cc67f5f 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -225,24 +225,24 @@ bool StrValid(std::span 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); } diff --git a/src/string_func.h b/src/string_func.h index 3b0e54e4d1..455d5547b5 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -39,7 +39,7 @@ bool strtolower(std::string &str, std::string::size_type offs = 0); [[nodiscard]] bool StrValid(std::span 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); diff --git a/src/tests/string_func.cpp b/src/tests/string_func.cpp index 7fdb6b0434..69b1ef3bc8 100644 --- a/src/tests/string_func.cpp +++ b/src/tests/string_func.cpp @@ -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> _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); } }