]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Use more std::string_view
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 3 Sep 2022 18:11:47 +0000 (20:11 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 6 Sep 2022 06:13:27 +0000 (08:13 +0200)
src/Args.cpp
src/Args.hpp
src/AtomicFile.cpp
src/AtomicFile.hpp
src/Depfile.cpp
src/Depfile.hpp
src/Util.cpp
src/Util.hpp
src/util/string.cpp
src/util/string.hpp

index 41a429e6102b7935ea80078718760af781aafc92..d4077a351c5a17577b97286efa55ef840dc1017c 100644 (file)
@@ -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")) {
index eeb66ad93c3fffaa3bdb0857a52913601439f5be..16bc7fe451294d41935965282facc322cc4f6b44 100644 (file)
@@ -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<Args>
   from_atfile(const std::string& filename,
index f171dd2fbc3636f5e22bc6274710d85bf85dad55..ec5fead6e76eb5fbe1ae34909e4b3068b6e65f1b 100644 (file)
@@ -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(
index 3c9768a3361a8c169665a9ae0cf1b8b25c7523d1..37ac37228557e548cad65d9e28fccede337318f5 100644 (file)
@@ -36,7 +36,7 @@ public:
 
   FILE* stream();
 
-  void write(const std::string& data);
+  void write(std::string_view data);
   void write(nonstd::span<const uint8_t> data);
 
   // Close the temporary file and rename it to the destination file. Note: The
index e85b6ccdb9a49d04350b94a831878e03f477e12d..ad63a7662756a1d51595c6344bd99cd38c811e8a 100644 (file)
@@ -61,7 +61,7 @@ escape_filename(std::string_view filename)
 }
 
 std::optional<std::string>
-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());
 
index 1257462646096a2f0b6c79721216f28aa3c23c69..d45bf539707fd1a5f4fe34158a8526d4769047e4 100644 (file)
@@ -32,8 +32,8 @@ namespace Depfile {
 
 std::string escape_filename(std::string_view filename);
 
-std::optional<std::string>
-rewrite_source_paths(const Context& ctx, const std::string& file_content);
+std::optional<std::string> rewrite_source_paths(const Context& ctx,
+                                                std::string_view file_content);
 
 void make_paths_relative_in_output_dep(const Context& ctx);
 
index 2208de0e49c0fdca5da2e21f89fb91d448c7c12d..cbe9ed5c0f8908e319513bf72744c81a70165654 100644 (file)
@@ -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()));
   }
index 6b2d21326aa1c0381f7cff0f407a6094c44251ca..fbbd4079c34f5767ab50529262c347a4a594920c 100644 (file)
@@ -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);
index 560b8b9a830e2726be5af91f9dda9c6135ba069e..7f17841590044eb2db12fd477ab6b9acec640b9a 100644 (file)
@@ -48,7 +48,7 @@ parse_double(const std::string& value)
 }
 
 nonstd::expected<int64_t, std::string>
-parse_signed(const std::string& value,
+parse_signed(std::string_view value,
              const std::optional<int64_t> min_value,
              const std::optional<int64_t> max_value,
              const std::string_view description)
@@ -80,13 +80,13 @@ parse_signed(const std::string& value,
 }
 
 nonstd::expected<mode_t, std::string>
-parse_umask(const std::string& value)
+parse_umask(std::string_view value)
 {
   return util::parse_unsigned(value, 0, 0777, "umask", 8);
 }
 
 nonstd::expected<uint64_t, std::string>
-parse_unsigned(const std::string& value,
+parse_unsigned(std::string_view value,
                const std::optional<uint64_t> min_value,
                const std::optional<uint64_t> max_value,
                const std::string_view description,
index 4e17eaebc9e793d71b69ff7e8a1baabc2351b623..5ee9716d6dee7364171437e7a4bf5057ff766d5c 100644 (file)
@@ -59,13 +59,13 @@ nonstd::expected<double, std::string> 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<int64_t, std::string>
-parse_signed(const std::string& value,
+parse_signed(std::string_view value,
              std::optional<int64_t> min_value = std::nullopt,
              std::optional<int64_t> max_value = std::nullopt,
              std::string_view description = "integer");
 
 // Parse `value` (an octal integer).
-nonstd::expected<mode_t, std::string> parse_umask(const std::string& value);
+nonstd::expected<mode_t, std::string> parse_umask(std::string_view value);
 
 // Parse a string into an unsigned integer.
 //
@@ -74,7 +74,7 @@ nonstd::expected<mode_t, std::string> 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<uint64_t, std::string>
-parse_unsigned(const std::string& value,
+parse_unsigned(std::string_view value,
                std::optional<uint64_t> min_value = std::nullopt,
                std::optional<uint64_t> max_value = std::nullopt,
                std::string_view description = "integer",