]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Let util::strip_whitespace return std::string_view
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 12 Apr 2026 14:33:37 +0000 (16:33 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 18 Apr 2026 14:02:03 +0000 (16:02 +0200)
src/ccache/ccache.cpp
src/ccache/config.cpp
src/ccache/util/configreader.cpp
src/ccache/util/string.cpp
src/ccache/util/string.hpp

index 1c03f9e55c46e6b7bd318b2f261838eafc1e6830..39cdd53aad242c00a23edf2e87f7d77e30851b47 100644 (file)
@@ -1121,10 +1121,12 @@ rewrite_stdout_from_compiler(const Context& ctx, util::Bytes&& stdout_data)
         std::string orig_line(line.data(), line.length());
         std::string abs_inc_path =
           util::replace_first(orig_line, ctx.config.msvc_dep_prefix(), "");
-        abs_inc_path = util::strip_whitespace(abs_inc_path);
-        fs::path rel_inc_path = core::make_relative_path(ctx, abs_inc_path);
+        std::string_view stripped_abs_inc_path =
+          util::strip_whitespace(abs_inc_path);
+        fs::path rel_inc_path =
+          core::make_relative_path(ctx, stripped_abs_inc_path);
         std::string line_with_rel_inc = util::replace_first(
-          orig_line, abs_inc_path, util::pstr(rel_inc_path).str());
+          orig_line, stripped_abs_inc_path, util::pstr(rel_inc_path).str());
         new_stdout_data.insert(new_stdout_data.end(),
                                line_with_rel_inc.data(),
                                line_with_rel_inc.size());
index 55fbbd27eb09d1afc813f3ad47e8c4fa9a73ffd0..d748604f3744983b33749b6fd59617e989a2a3c4 100644 (file)
@@ -478,7 +478,7 @@ parse_line(const std::string& line,
            std::string* value,
            std::string* error_message)
 {
-  std::string stripped_line = util::strip_whitespace(line);
+  std::string_view stripped_line = util::strip_whitespace(line);
   if (stripped_line.empty() || stripped_line[0] == '#') {
     return true;
   }
index c0174d738ff209f8916c08a73f176fc37b673097..97c6e5ead419cb811e1a69107295cea205871613 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2025 Joel Rosdahl and other contributors
+// Copyright (C) 2025-2026 Joel Rosdahl and other contributors
 //
 // See doc/authors.adoc for a complete list of contributors.
 //
@@ -29,7 +29,7 @@ namespace {
 bool
 is_comment_or_blank(std::string_view line)
 {
-  std::string stripped = util::strip_whitespace(line);
+  std::string_view stripped = util::strip_whitespace(line);
   return stripped.empty() || stripped[0] == '#';
 }
 
@@ -148,7 +148,7 @@ ConfigReader::read_next_item()
     split_into_views(raw_value, "\n", Tokenizer::Mode::include_empty);
 
   for (auto line : value_lines) {
-    std::string stripped = util::strip_whitespace(line);
+    std::string_view stripped = util::strip_whitespace(line);
     if (!stripped.empty() && stripped[0] != '#') {
       if (!normalized_value.empty()) {
         normalized_value += ' ';
index 1fde6f07f78439b2705f888838171d68af8d2efc..1fdb42e152601dd78fbcc5bfa5fba33cddfc6e0e 100644 (file)
@@ -364,7 +364,7 @@ parse_signed(std::string_view value,
              const std::optional<int64_t> max_value,
              const std::string_view description)
 {
-  const std::string stripped_value = strip_whitespace(value);
+  const std::string stripped_value{strip_whitespace(value)};
 
   size_t end = 0;
   long long result = 0;
@@ -449,7 +449,7 @@ parse_unsigned(std::string_view value,
                const std::string_view description,
                const int base)
 {
-  const std::string stripped_value = strip_whitespace(value);
+  const std::string stripped_value{strip_whitespace(value)};
 
   size_t end = 0;
   unsigned long long result = 0;
@@ -628,14 +628,15 @@ split_path_list(std::string_view path_list)
   return paths;
 }
 
-std::string
+std::string_view
 strip_whitespace(const std::string_view string)
 {
   const auto start =
     std::find_if_not(string.begin(), string.end(), util::is_space);
   const auto end =
     std::find_if_not(string.rbegin(), string.rend(), util::is_space).base();
-  return start < end ? std::string(start, end) : std::string();
+  return start < end ? string.substr(start - string.begin(), end - start)
+                     : std::string_view{};
 }
 
 std::string
index ce62f407c9d4d137dc4493d88507b6a68297240c..b78194635d609252ad9d46cf50a697a4fab10a0c 100644 (file)
@@ -224,7 +224,7 @@ split_option_with_concat_path(std::string_view string);
 std::vector<std::filesystem::path> split_path_list(std::string_view path_list);
 
 // Strip whitespace from left and right side of a string.
-[[nodiscard]] std::string strip_whitespace(std::string_view string);
+[[nodiscard]] std::string_view strip_whitespace(std::string_view string);
 
 // Return lowercase `ch`.
 char to_lower(char ch);