From: Joel Rosdahl Date: Tue, 13 Jul 2021 08:41:44 +0000 (+0200) Subject: Move strip_whitespace to util X-Git-Tag: v4.4~122 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d217f89fffd9c4529346ae99cf644301f6fc931e;p=thirdparty%2Fccache.git Move strip_whitespace to util --- diff --git a/src/Config.cpp b/src/Config.cpp index 937663e01..121f9ab9d 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -277,7 +277,7 @@ parse_sloppiness(const std::string& value) while (end != std::string::npos) { end = value.find_first_of(", ", start); std::string token = - Util::strip_whitespace(value.substr(start, end - start)); + util::strip_whitespace(value.substr(start, end - start)); if (token == "file_stat_matches") { result |= SLOPPY_FILE_STAT_MATCHES; } else if (token == "file_stat_matches_ctime") { @@ -374,7 +374,7 @@ parse_line(const std::string& line, std::string* value, std::string* error_message) { - std::string stripped_line = Util::strip_whitespace(line); + std::string stripped_line = util::strip_whitespace(line); if (stripped_line.empty() || stripped_line[0] == '#') { return true; } @@ -385,8 +385,8 @@ parse_line(const std::string& line, } *key = stripped_line.substr(0, equal_pos); *value = stripped_line.substr(equal_pos + 1); - *key = Util::strip_whitespace(*key); - *value = Util::strip_whitespace(*value); + *key = util::strip_whitespace(*key); + *value = util::strip_whitespace(*value); return true; } diff --git a/src/Util.cpp b/src/Util.cpp index 942465d17..0d546710c 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -29,6 +29,7 @@ #include #include +#include extern "C" { #include "third_party/base32hex.h" @@ -1035,7 +1036,7 @@ parse_signed(const std::string& value, optional max_value, string_view description) { - std::string stripped_value = strip_whitespace(value); + std::string stripped_value = util::strip_whitespace(value); size_t end = 0; long long result = 0; @@ -1106,7 +1107,7 @@ parse_unsigned(const std::string& value, string_view description, int base) { - std::string stripped_value = strip_whitespace(value); + std::string stripped_value = util::strip_whitespace(value); size_t end = 0; unsigned long long result = 0; @@ -1389,15 +1390,6 @@ strip_ansi_csi_seqs(string_view string) return result; } -std::string -strip_whitespace(string_view string) -{ - auto is_space = [](int ch) { return std::isspace(ch); }; - auto start = std::find_if_not(string.begin(), string.end(), is_space); - auto end = std::find_if_not(string.rbegin(), string.rend(), is_space).base(); - return start < end ? std::string(start, end) : std::string(); -} - std::string to_lowercase(string_view string) { diff --git a/src/Util.hpp b/src/Util.hpp index 61f697a75..a682f1daa 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -440,9 +440,6 @@ starts_with(nonstd::string_view string, nonstd::string_view prefix) // Returns a copy of string with the specified ANSI CSI sequences removed. [[nodiscard]] std::string strip_ansi_csi_seqs(nonstd::string_view string); -// Strip whitespace from left and right side of a string. -[[nodiscard]] std::string strip_whitespace(nonstd::string_view string); - // Convert a string to lowercase. [[nodiscard]] std::string to_lowercase(nonstd::string_view string); diff --git a/src/hashutil.cpp b/src/hashutil.cpp index 73350e33a..d25b1c240 100644 --- a/src/hashutil.cpp +++ b/src/hashutil.cpp @@ -25,12 +25,14 @@ #include "Logging.hpp" #include "Sloppiness.hpp" #include "Stat.hpp" +#include "Util.hpp" #include "Win32Util.hpp" #include "execute.hpp" #include "fmtmacros.hpp" #include "macroskip.hpp" #include +#include #ifdef INODE_CACHE_SUPPORTED # include "InodeCache.hpp" @@ -374,7 +376,7 @@ hash_command_output(Hash& hash, const std::string& compiler) { #ifdef _WIN32 - std::string adjusted_command = Util::strip_whitespace(command); + std::string adjusted_command = util::strip_whitespace(command); // Add "echo" command. bool using_cmd_exe; diff --git a/src/util/string_utils.cpp b/src/util/string_utils.cpp index 18c36f2a1..0e4397796 100644 --- a/src/util/string_utils.cpp +++ b/src/util/string_utils.cpp @@ -76,4 +76,14 @@ split_once(const nonstd::string_view string, const char split_char) } } +std::string +strip_whitespace(const nonstd::string_view string) +{ + const auto is_space = [](const int ch) { return std::isspace(ch); }; + const auto start = std::find_if_not(string.begin(), string.end(), is_space); + const auto end = + std::find_if_not(string.rbegin(), string.rend(), is_space).base(); + return start < end ? std::string(start, end) : std::string(); +} + } // namespace util diff --git a/src/util/string_utils.hpp b/src/util/string_utils.hpp index 7297b66ac..c55fc1239 100644 --- a/src/util/string_utils.hpp +++ b/src/util/string_utils.hpp @@ -43,4 +43,7 @@ percent_decode(nonstd::string_view string); std::pair> split_once(nonstd::string_view string, char split_char); +// Strip whitespace from left and right side of a string. +[[nodiscard]] std::string strip_whitespace(nonstd::string_view string); + } // namespace util diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index cd40e8d8f..cada79249 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -893,17 +893,6 @@ TEST_CASE("Util::starts_with") CHECK_FALSE(Util::starts_with(std::string("x"), "xy")); } -TEST_CASE("Util::strip_whitespace") -{ - CHECK(Util::strip_whitespace("") == ""); - CHECK(Util::strip_whitespace("x") == "x"); - CHECK(Util::strip_whitespace(" x") == "x"); - CHECK(Util::strip_whitespace("x ") == "x"); - CHECK(Util::strip_whitespace(" x ") == "x"); - CHECK(Util::strip_whitespace(" \n\tx \n\t") == "x"); - CHECK(Util::strip_whitespace(" x y ") == "x y"); -} - TEST_CASE("Util::to_lowercase") { CHECK(Util::to_lowercase("") == ""); diff --git a/unittest/test_util_string_utils.cpp b/unittest/test_util_string_utils.cpp index c70659867..17b8cd06b 100644 --- a/unittest/test_util_string_utils.cpp +++ b/unittest/test_util_string_utils.cpp @@ -28,6 +28,8 @@ operator==( return left.first == right.first && left.second == right.second; } +TEST_SUITE_BEGIN("util"); + TEST_CASE("util::parse_umask") { CHECK(util::parse_umask("1") == 01u); @@ -80,3 +82,16 @@ TEST_CASE("util::split_once") CHECK(split_once("a=b=c", '=') == make_pair("a", "b=c")); CHECK(split_once("x y", ' ') == make_pair("x", "y")); } + +TEST_CASE("util::strip_whitespace") +{ + CHECK(util::strip_whitespace("") == ""); + CHECK(util::strip_whitespace("x") == "x"); + CHECK(util::strip_whitespace(" x") == "x"); + CHECK(util::strip_whitespace("x ") == "x"); + CHECK(util::strip_whitespace(" x ") == "x"); + CHECK(util::strip_whitespace(" \n\tx \n\t") == "x"); + CHECK(util::strip_whitespace(" x y ") == "x y"); +} + +TEST_SUITE_END();