From: Joel Rosdahl Date: Sun, 12 Apr 2026 14:33:37 +0000 (+0200) Subject: refactor: Let util::strip_whitespace return std::string_view X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=95f0c55641c2374a2bc9cd96f009b24da43b3f95;p=thirdparty%2Fccache.git refactor: Let util::strip_whitespace return std::string_view --- diff --git a/src/ccache/ccache.cpp b/src/ccache/ccache.cpp index 1c03f9e5..39cdd53a 100644 --- a/src/ccache/ccache.cpp +++ b/src/ccache/ccache.cpp @@ -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()); diff --git a/src/ccache/config.cpp b/src/ccache/config.cpp index 55fbbd27..d748604f 100644 --- a/src/ccache/config.cpp +++ b/src/ccache/config.cpp @@ -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; } diff --git a/src/ccache/util/configreader.cpp b/src/ccache/util/configreader.cpp index c0174d73..97c6e5ea 100644 --- a/src/ccache/util/configreader.cpp +++ b/src/ccache/util/configreader.cpp @@ -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 += ' '; diff --git a/src/ccache/util/string.cpp b/src/ccache/util/string.cpp index 1fde6f07..1fdb42e1 100644 --- a/src/ccache/util/string.cpp +++ b/src/ccache/util/string.cpp @@ -364,7 +364,7 @@ parse_signed(std::string_view value, const std::optional 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 diff --git a/src/ccache/util/string.hpp b/src/ccache/util/string.hpp index ce62f407..b7819463 100644 --- a/src/ccache/util/string.hpp +++ b/src/ccache/util/string.hpp @@ -224,7 +224,7 @@ split_option_with_concat_path(std::string_view string); std::vector 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);