]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Move strip_whitespace to util
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 13 Jul 2021 08:41:44 +0000 (10:41 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 19 Jul 2021 06:15:45 +0000 (08:15 +0200)
src/Config.cpp
src/Util.cpp
src/Util.hpp
src/hashutil.cpp
src/util/string_utils.cpp
src/util/string_utils.hpp
unittest/test_Util.cpp
unittest/test_util_string_utils.cpp

index 937663e01d83b41ccc6ace5c0d676ca1ad9accde..121f9ab9d2f1bc0e8e56fa74e06d85ff9ce95987 100644 (file)
@@ -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;
 }
 
index 942465d1770a6a92ebea21bff01d0bc2c51f01bb..0d546710ceeb67d2ed22742ab4bbb611d59dff88 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <core/wincompat.hpp>
 #include <util/path_utils.hpp>
+#include <util/string_utils.hpp>
 
 extern "C" {
 #include "third_party/base32hex.h"
@@ -1035,7 +1036,7 @@ parse_signed(const std::string& value,
              optional<int64_t> 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)
 {
index 61f697a75ff14885c2d28ce668739f5477cf708d..a682f1daa1df5cecaf3342c98815966b63d4ebfd 100644 (file)
@@ -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);
 
index 73350e33a0d6416bb3ab08cf9bb887d728c4ef8a..d25b1c240e7d5ee434dea0c4bc51ded12176f0c7 100644 (file)
 #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 <core/wincompat.hpp>
+#include <util/string_utils.hpp>
 
 #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;
index 18c36f2a1df40106dd98a68e3c86eb94fb1949e9..0e439779674adc7495110a9bb2aae6080239de11 100644 (file)
@@ -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
index 7297b66acf3cfb1e037ccfe46289f76a30c044b2..c55fc12390bfc79fcfa7ced1d1b13f8e3444d4f7 100644 (file)
@@ -43,4 +43,7 @@ percent_decode(nonstd::string_view string);
 std::pair<nonstd::string_view, nonstd::optional<nonstd::string_view>>
 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
index cd40e8d8fb946caf97351c7157f79eb4935cb9f4..cada792494d87a1e452ba07ae6bf5fe53f6a21b1 100644 (file)
@@ -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("") == "");
index c70659867a4070229f080493be47b521ed984609..17b8cd06bf9a79ab636dcf6c5a1d8854323f832f 100644 (file)
@@ -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();