]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::split_into_* to util
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 16 Jul 2023 07:18:57 +0000 (09:18 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 19:35:59 +0000 (21:35 +0200)
17 files changed:
src/Args.cpp
src/Context.cpp
src/Depfile.cpp
src/Util.cpp
src/Util.hpp
src/argprocessing.cpp
src/ccache.cpp
src/core/MsvcShowIncludesOutput.cpp
src/hashutil.cpp
src/storage/Storage.cpp
src/util/LockFile.cpp
src/util/path.cpp
src/util/string.cpp
src/util/string.hpp
unittest/test_Util.cpp
unittest/test_util_Tokenizer.cpp
unittest/test_util_string.cpp

index 2b05673ba0b8671e15888393e582e4015ac99826..caddb46c97d0b02dfa3db2d253db32a4846362c7 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -18,8 +18,6 @@
 
 #include "Args.hpp"
 
-#include "Util.hpp"
-
 #include <Logging.hpp>
 #include <core/exceptions.hpp>
 #include <util/file.hpp>
@@ -41,7 +39,7 @@ Args
 Args::from_string(std::string_view command)
 {
   Args args;
-  for (const std::string& word : Util::split_into_strings(command, " \t\r\n")) {
+  for (const std::string& word : util::split_into_strings(command, " \t\r\n")) {
     args.push_back(word);
   }
   return args;
index cdf79ea1f4016b7f663e2464c0f46227cfd56152..98d175af8db0e426a86027e1bb3e951ec6fd7394 100644 (file)
@@ -28,6 +28,7 @@
 #include <util/TimePoint.hpp>
 #include <util/path.hpp>
 #include <util/process.hpp>
+#include <util/string.hpp>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
@@ -58,7 +59,7 @@ Context::initialize(Args&& compiler_and_args,
   Logging::init(config);
   ignore_header_paths =
     util::split_path_list(config.ignore_headers_in_manifest());
-  set_ignore_options(Util::split_into_strings(config.ignore_options(), " "));
+  set_ignore_options(util::split_into_strings(config.ignore_options(), " "));
 
   // Set default umask for all files created by ccache from now on (if
   // configured to). This is intentionally done after calling Logging::init so
index c85164f18674f0d29f5c371e5f278063b800387a..064e2124ff331bdacaafb0b64432ffb50162b9da 100644 (file)
@@ -28,6 +28,7 @@
 #include <util/Tokenizer.hpp>
 #include <util/file.hpp>
 #include <util/path.hpp>
+#include <util/string.hpp>
 
 #include <algorithm>
 
@@ -83,7 +84,7 @@ rewrite_source_paths(const Context& ctx, std::string_view file_content)
                                    "\n",
                                    Tokenizer::Mode::include_empty,
                                    Tokenizer::IncludeDelimiter::yes)) {
-    const auto tokens = Util::split_into_views(line, " \t");
+    const auto tokens = util::split_into_views(line, " \t");
     for (size_t i = 0; i < tokens.size(); ++i) {
       DEBUG_ASSERT(!line.empty()); // line.empty() -> no tokens
       DEBUG_ASSERT(!tokens[i].empty());
index 580de30dab8e9fa7849b64a9409f9c4063c2c44b..af397ea01f71106f09a476702e5223d80bc39d7f 100644 (file)
@@ -31,7 +31,6 @@
 #include <core/exceptions.hpp>
 #include <core/wincompat.hpp>
 #include <fmtmacros.hpp>
-#include <util/TimePoint.hpp>
 #include <util/file.hpp>
 #include <util/filesystem.hpp>
 #include <util/path.hpp>
@@ -91,22 +90,6 @@ find_first_ansi_csi_seq(std::string_view string)
   }
 }
 
-template<typename T>
-std::vector<T>
-split_into(std::string_view string,
-           const char* separators,
-           util::Tokenizer::Mode mode,
-           IncludeDelimiter include_delimiter)
-
-{
-  std::vector<T> result;
-  for (const auto token :
-       util::Tokenizer(string, separators, mode, include_delimiter)) {
-    result.emplace_back(token);
-  }
-  return result;
-}
-
 std::string
 rewrite_stderr_to_absolute_paths(std::string_view text)
 {
@@ -683,25 +666,6 @@ send_to_fd(const Context& ctx, std::string_view text, int fd)
   }
 }
 
-std::vector<std::string_view>
-split_into_views(std::string_view string,
-                 const char* separators,
-                 util::Tokenizer::Mode mode,
-                 IncludeDelimiter include_delimiter)
-{
-  return split_into<std::string_view>(
-    string, separators, mode, include_delimiter);
-}
-
-std::vector<std::string>
-split_into_strings(std::string_view string,
-                   const char* separators,
-                   util::Tokenizer::Mode mode,
-                   IncludeDelimiter include_delimiter)
-{
-  return split_into<std::string>(string, separators, mode, include_delimiter);
-}
-
 std::string
 strip_ansi_csi_seqs(std::string_view string)
 {
index 5e8fc8ac7b989cdf989d1a1a5ec5cb6e13f84b0b..5c47bf24956a9628543e6eaee0d78c699f09f128 100644 (file)
@@ -19,7 +19,6 @@
 #pragma once
 
 #include <util/TimePoint.hpp>
-#include <util/Tokenizer.hpp>
 
 #include <cstdint>
 #include <filesystem>
@@ -172,24 +171,6 @@ std::string_view remove_extension(std::string_view path);
 // `core::Error` on error.
 void send_to_fd(const Context& ctx, std::string_view text, int fd);
 
-// Split `string` into tokens at any of the characters in `separators`. These
-// tokens are views into `string`. `separators` must neither be the empty string
-// nor a nullptr.
-std::vector<std::string_view>
-split_into_views(std::string_view string,
-                 const char* separators,
-                 util::Tokenizer::Mode mode = util::Tokenizer::Mode::skip_empty,
-                 util::Tokenizer::IncludeDelimiter include_delimiter =
-                   util::Tokenizer::IncludeDelimiter::no);
-
-// Same as `split_into_views` but the tokens are copied from `string`.
-std::vector<std::string> split_into_strings(
-  std::string_view string,
-  const char* separators,
-  util::Tokenizer::Mode mode = util::Tokenizer::Mode::skip_empty,
-  util::Tokenizer::IncludeDelimiter include_delimiter =
-    util::Tokenizer::IncludeDelimiter::no);
-
 // Returns a copy of string with the specified ANSI CSI sequences removed.
 [[nodiscard]] std::string strip_ansi_csi_seqs(std::string_view string);
 
index 4651ed95fd74cca51d3781856249b24376628e8a..99209fd69de471c840e03942f9b74e25ec6a96b9 100644 (file)
@@ -372,7 +372,7 @@ process_option_arg(const Context& ctx,
     ++i;
 
     // Argument is a comma-separated list of files.
-    auto paths = Util::split_into_strings(args[i], ",");
+    auto paths = util::split_into_strings(args[i], ",");
     for (auto it = paths.rbegin(); it != paths.rend(); ++it) {
       auto file_args = Args::from_atfile(*it);
       if (!file_args) {
index 45b7b1af4777a09baf9aabbabeb51e66b3b5534c..8002f7ccc2be46ad5d64a75e7610e613c6d75710 100644 (file)
@@ -165,7 +165,7 @@ add_prefix(const Context& ctx, Args& args, const std::string& prefix_command)
   }
 
   Args prefix;
-  for (const auto& word : Util::split_into_strings(prefix_command, " ")) {
+  for (const auto& word : util::split_into_strings(prefix_command, " ")) {
     std::string path = find_executable(ctx, word, ctx.orig_args[0]);
     if (path.empty()) {
       throw core::Fatal(FMT("{}: {}", word, strerror(errno)));
index 311a1bcbb811f28a46bfd17628cc67d8745b8249..dcc15398595ddd25eea15588c452c4cc19fd6b57 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Joel Rosdahl and other contributors
+// Copyright (C) 2022-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -19,7 +19,6 @@
 #include "MsvcShowIncludesOutput.hpp"
 
 #include <Context.hpp>
-#include <Util.hpp>
 #include <util/string.hpp>
 
 namespace core::MsvcShowIncludesOutput {
@@ -34,7 +33,7 @@ get_includes(std::string_view file_content, std::string_view prefix)
   std::vector<std::string_view> result;
   // This will split at each \r or \n, but that simply means there will be empty
   // "lines".
-  for (std::string_view line : Util::split_into_views(file_content, "\r\n")) {
+  for (std::string_view line : util::split_into_views(file_content, "\r\n")) {
     if (util::starts_with(line, prefix)) {
       size_t pos = prefix.size();
       while (pos < line.size() && isspace(line[pos])) {
index a06c787f9c429b2cc403c12a4278462f65f07a75..9a42db9cc2936d98f7d3cb5f747de7514fb60db2 100644 (file)
@@ -491,7 +491,7 @@ hash_multicommand_output(Hash& hash,
                          const std::string& command,
                          const std::string& compiler)
 {
-  for (const std::string& cmd : Util::split_into_strings(command, ";")) {
+  for (const std::string& cmd : util::split_into_strings(command, ";")) {
     if (!hash_command_output(hash, cmd, compiler)) {
       return false;
     }
index 7f15f9fe04cc484310bcc9684d02772f7bd17d33..f1521fa2db369e47489951be06f29bc1413d143a 100644 (file)
@@ -118,7 +118,7 @@ static RemoteStorageConfig
 parse_storage_config(const std::string_view entry)
 {
   const auto parts =
-    Util::split_into_views(entry, "|", util::Tokenizer::Mode::include_empty);
+    util::split_into_views(entry, "|", util::Tokenizer::Mode::include_empty);
 
   if (parts.empty() || parts.front().empty()) {
     throw core::Error(
index d9184ef2eb7c3101e110e2a3493db22d390c8548..9fb998f45d7afd9427f5d71eddf2082ad52733ca 100644 (file)
@@ -24,6 +24,7 @@
 #include "fmtmacros.hpp"
 
 #include <Stat.hpp>
+#include <assertions.hpp>
 #include <core/exceptions.hpp>
 #include <core/wincompat.hpp>
 #include <util/file.hpp>
index 6d9e55bd43fdc5a62a12876548ec62036dc18504..3e0b8bdb2622e43ce350d6e22f82f5970b317df8 100644 (file)
@@ -22,6 +22,7 @@
 #include <Util.hpp>
 #include <fmtmacros.hpp>
 #include <util/filesystem.hpp>
+#include <util/string.hpp>
 
 #ifdef _WIN32
 const char k_dev_null_path[] = "nul:";
@@ -99,7 +100,7 @@ real_path(std::string_view path)
 std::vector<std::string>
 split_path_list(std::string_view path_list)
 {
-  return Util::split_into_strings(path_list, k_path_delimiter);
+  return util::split_into_strings(path_list, k_path_delimiter);
 }
 
 std::string
index 7f17826d78c1b8226da48868295f5100d28d3545..b3a84e38a121f1ab80ebde0f777296e468d2fa55 100644 (file)
 #include <cctype>
 #include <iostream>
 
+namespace {
+
+template<typename T>
+std::vector<T>
+split_into(std::string_view string,
+           const char* separators,
+           util::Tokenizer::Mode mode,
+           util::Tokenizer::IncludeDelimiter include_delimiter)
+
+{
+  std::vector<T> result;
+  for (const auto token :
+       util::Tokenizer(string, separators, mode, include_delimiter)) {
+    result.emplace_back(token);
+  }
+  return result;
+}
+
+} // namespace
+
 namespace util {
 
 std::string
@@ -345,6 +365,25 @@ replace_first(const std::string_view string,
   return result;
 }
 
+std::vector<std::string>
+split_into_strings(std::string_view string,
+                   const char* separators,
+                   util::Tokenizer::Mode mode,
+                   util::Tokenizer::IncludeDelimiter include_delimiter)
+{
+  return split_into<std::string>(string, separators, mode, include_delimiter);
+}
+
+std::vector<std::string_view>
+split_into_views(std::string_view string,
+                 const char* separators,
+                 util::Tokenizer::Mode mode,
+                 util::Tokenizer::IncludeDelimiter include_delimiter)
+{
+  return split_into<std::string_view>(
+    string, separators, mode, include_delimiter);
+}
+
 std::pair<std::string_view, std::optional<std::string_view>>
 split_once(const char* string, const char split_char)
 {
index 1739e9a6cd61745b3a0379c49fab89c25cb91bdd..d292700c0bd273a76157c65c14639324f3c1d528 100644 (file)
@@ -19,6 +19,7 @@
 #pragma once
 
 #include <util/Bytes.hpp>
+#include <util/Tokenizer.hpp>
 #include <util/conversion.hpp>
 
 #include <third_party/nonstd/expected.hpp>
@@ -32,6 +33,7 @@
 #include <string>
 #include <string_view>
 #include <utility>
+#include <vector>
 
 namespace util {
 
@@ -138,6 +140,25 @@ std::string replace_first(std::string_view string,
                           std::string_view from,
                           std::string_view to);
 
+// Split `string` into tokens at any of the characters in `separators`.
+// `separators` must neither be the empty string nor a nullptr.
+std::vector<std::string> split_into_strings(
+  std::string_view string,
+  const char* separators,
+  util::Tokenizer::Mode mode = util::Tokenizer::Mode::skip_empty,
+  util::Tokenizer::IncludeDelimiter include_delimiter =
+    util::Tokenizer::IncludeDelimiter::no);
+
+// Split `string` into tokens at any of the characters in `separators`. These
+// tokens are views into `string`. `separators` must neither be the empty string
+// nor a nullptr.
+std::vector<std::string_view>
+split_into_views(std::string_view string,
+                 const char* separators,
+                 util::Tokenizer::Mode mode = util::Tokenizer::Mode::skip_empty,
+                 util::Tokenizer::IncludeDelimiter include_delimiter =
+                   util::Tokenizer::IncludeDelimiter::no);
+
 // Split `string` into two parts using `split_char` as the delimiter. The second
 // part will be `nullopt` if there is no `split_char` in `string.`
 std::pair<std::string_view, std::optional<std::string_view>>
index 6b9168a84865e3c1497b3f320685a1f1d51e4e03..331482aa987585b564774ffa7d545c64d2800e1a 100644 (file)
@@ -416,9 +416,6 @@ TEST_CASE("Util::remove_extension")
   CHECK(Util::remove_extension("/foo/bar/f.abc.txt") == "/foo/bar/f.abc");
 }
 
-// Util::split_into_strings and Util::split_into_views are tested implicitly in
-// test_util_Tokenizer.cpp.
-
 TEST_CASE("Util::to_lowercase")
 {
   CHECK(Util::to_lowercase("") == "");
index d6eeabed9bbe3051182579d2e6d1b318618948f5..f52fc4c6d7c7a939f86a77bf28291b3ae5f88e31 100644 (file)
@@ -18,6 +18,8 @@
 
 #include "../src/Util.hpp"
 
+#include <util/string.hpp>
+
 #include "third_party/doctest.h"
 
 #include <ostream> // https://github.com/doctest/doctest/issues/618
@@ -43,7 +45,7 @@ TEST_CASE("util::Tokenizer")
                const std::vector<std::string>& expected) const
     {
       const auto res =
-        Util::split_into_views(input, separators, m_mode, m_include_delimiter);
+        util::split_into_views(input, separators, m_mode, m_include_delimiter);
       REQUIRE(res.size() == expected.size());
       for (int i = 0, total = expected.size(); i < total; ++i) {
         CHECK(res[i] == expected[i]);
index 70a291c933b48e9b75450161dec5d2962c5230e1..4a02bed54ad72b8502421836d3b454be6026937b 100644 (file)
@@ -444,6 +444,20 @@ TEST_CASE("util::replace_first")
   CHECK(util::replace_first("xabcyabcz", "abc", "defdef") == "xdefdefyabcz");
 }
 
+TEST_CASE("util::split_into_strings")
+{
+  // Edge cases are tested in test_util_Tokenizer.cpp.
+  CHECK(util::split_into_strings("foo bar", " ")
+        == std::vector<std::string>{"foo", "bar"});
+}
+
+TEST_CASE("util::split_into_views")
+{
+  // Edge cases are tested in test_util_Tokenizer.cpp.
+  CHECK(util::split_into_views("foo bar", " ")
+        == std::vector<std::string_view>{"foo", "bar"});
+}
+
 TEST_CASE("util::split_once")
 {
   using std::make_pair;