From: Joel Rosdahl Date: Sat, 3 Sep 2022 18:11:47 +0000 (+0200) Subject: refactor: Use more std::string_view X-Git-Tag: v4.7~82 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8cd04998f47ad21822bf024e2b38e104b12133eb;p=thirdparty%2Fccache.git refactor: Use more std::string_view --- diff --git a/src/Args.cpp b/src/Args.cpp index 41a429e61..d4077a351 100644 --- a/src/Args.cpp +++ b/src/Args.cpp @@ -37,7 +37,7 @@ Args::from_argv(int argc, const char* const* argv) } Args -Args::from_string(const std::string& command) +Args::from_string(std::string_view command) { Args args; for (const std::string& word : Util::split_into_strings(command, " \t\r\n")) { diff --git a/src/Args.hpp b/src/Args.hpp index eeb66ad93..16bc7fe45 100644 --- a/src/Args.hpp +++ b/src/Args.hpp @@ -39,7 +39,7 @@ public: Args(Args&& other) noexcept; static Args from_argv(int argc, const char* const* argv); - static Args from_string(const std::string& command); + static Args from_string(std::string_view command); static std::optional from_atfile(const std::string& filename, diff --git a/src/AtomicFile.cpp b/src/AtomicFile.cpp index f171dd2fb..ec5fead6e 100644 --- a/src/AtomicFile.cpp +++ b/src/AtomicFile.cpp @@ -42,7 +42,7 @@ AtomicFile::~AtomicFile() } void -AtomicFile::write(const std::string& data) +AtomicFile::write(std::string_view data) { if (fwrite(data.data(), data.size(), 1, m_stream) != 1) { throw core::Error( diff --git a/src/AtomicFile.hpp b/src/AtomicFile.hpp index 3c9768a33..37ac37228 100644 --- a/src/AtomicFile.hpp +++ b/src/AtomicFile.hpp @@ -36,7 +36,7 @@ public: FILE* stream(); - void write(const std::string& data); + void write(std::string_view data); void write(nonstd::span data); // Close the temporary file and rename it to the destination file. Note: The diff --git a/src/Depfile.cpp b/src/Depfile.cpp index e85b6ccdb..ad63a7662 100644 --- a/src/Depfile.cpp +++ b/src/Depfile.cpp @@ -61,7 +61,7 @@ escape_filename(std::string_view filename) } std::optional -rewrite_source_paths(const Context& ctx, const std::string& file_content) +rewrite_source_paths(const Context& ctx, std::string_view file_content) { ASSERT(!ctx.config.base_dir().empty()); diff --git a/src/Depfile.hpp b/src/Depfile.hpp index 125746264..d45bf5397 100644 --- a/src/Depfile.hpp +++ b/src/Depfile.hpp @@ -32,8 +32,8 @@ namespace Depfile { std::string escape_filename(std::string_view filename); -std::optional -rewrite_source_paths(const Context& ctx, const std::string& file_content); +std::optional rewrite_source_paths(const Context& ctx, + std::string_view file_content); void make_paths_relative_in_output_dep(const Context& ctx); diff --git a/src/Util.cpp b/src/Util.cpp index 2208de0e4..cbe9ed5c0 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -966,7 +966,7 @@ normalize_concrete_absolute_path(const std::string& path) } uint64_t -parse_duration(const std::string& duration) +parse_duration(std::string_view duration) { uint64_t factor = 0; char last_ch = duration.empty() ? '\0' : duration[duration.length() - 1]; @@ -1118,9 +1118,9 @@ rename(const std::string& oldpath, const std::string& newpath) } void -send_to_fd(const Context& ctx, const std::string& text, int fd) +send_to_fd(const Context& ctx, std::string_view text, int fd) { - const std::string* text_to_send = &text; + std::string_view text_to_send = text; std::string modified_text; #ifdef _WIN32 @@ -1134,19 +1134,19 @@ send_to_fd(const Context& ctx, const std::string& text, int fd) if (ctx.args_info.strip_diagnostics_colors) { try { modified_text = strip_ansi_csi_seqs(text); - text_to_send = &modified_text; + text_to_send = modified_text; } catch (const core::Error&) { // Ignore. } } if (ctx.config.absolute_paths_in_stderr()) { - modified_text = rewrite_stderr_to_absolute_paths(*text_to_send); - text_to_send = &modified_text; + modified_text = rewrite_stderr_to_absolute_paths(text_to_send); + text_to_send = modified_text; } const auto result = - util::write_fd(fd, text_to_send->data(), text_to_send->length()); + util::write_fd(fd, text_to_send.data(), text_to_send.length()); if (!result) { throw core::Error(FMT("Failed to write to {}: {}", fd, result.error())); } diff --git a/src/Util.hpp b/src/Util.hpp index 6b2d21326..fbbd4079c 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -279,7 +279,7 @@ std::string normalize_concrete_absolute_path(const std::string& path); // Parse `duration`, an unsigned integer with d (days) or s (seconds) suffix, // into seconds. Throws `core::Error` on error. -uint64_t parse_duration(const std::string& duration); +uint64_t parse_duration(std::string_view duration); // Parse a "size value", i.e. a string that can end in k, M, G, T (10-based // suffixes) or Ki, Mi, Gi, Ti (2-based suffixes). For backward compatibility, K @@ -309,7 +309,7 @@ void rename(const std::string& oldpath, const std::string& newpath); // sequences if `ctx.args_info.strip_diagnostics_colors` is true and rewriting // paths to absolute if `ctx.config.absolute_paths_in_stderr` is true. Throws // `core::Error` on error. -void send_to_fd(const Context& ctx, const std::string& text, int fd); +void send_to_fd(const Context& ctx, std::string_view text, int fd); // Set the FD_CLOEXEC on file descriptor `fd`. This is a NOP on Windows. void set_cloexec_flag(int fd); diff --git a/src/util/string.cpp b/src/util/string.cpp index 560b8b9a8..7f1784159 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -48,7 +48,7 @@ parse_double(const std::string& value) } nonstd::expected -parse_signed(const std::string& value, +parse_signed(std::string_view value, const std::optional min_value, const std::optional max_value, const std::string_view description) @@ -80,13 +80,13 @@ parse_signed(const std::string& value, } nonstd::expected -parse_umask(const std::string& value) +parse_umask(std::string_view value) { return util::parse_unsigned(value, 0, 0777, "umask", 8); } nonstd::expected -parse_unsigned(const std::string& value, +parse_unsigned(std::string_view value, const std::optional min_value, const std::optional max_value, const std::string_view description, diff --git a/src/util/string.hpp b/src/util/string.hpp index 4e17eaebc..5ee9716d6 100644 --- a/src/util/string.hpp +++ b/src/util/string.hpp @@ -59,13 +59,13 @@ nonstd::expected parse_double(const std::string& value); // `max_value` default to min and max values of int64_t. `description` is // included in the error message for range violations. nonstd::expected -parse_signed(const std::string& value, +parse_signed(std::string_view value, std::optional min_value = std::nullopt, std::optional max_value = std::nullopt, std::string_view description = "integer"); // Parse `value` (an octal integer). -nonstd::expected parse_umask(const std::string& value); +nonstd::expected parse_umask(std::string_view value); // Parse a string into an unsigned integer. // @@ -74,7 +74,7 @@ nonstd::expected parse_umask(const std::string& value); // `min_value` and `max_value` default to min and max values of uint64_t. // `description` is included in the error message for range violations. nonstd::expected -parse_unsigned(const std::string& value, +parse_unsigned(std::string_view value, std::optional min_value = std::nullopt, std::optional max_value = std::nullopt, std::string_view description = "integer",