]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Use std::string_view in compopt functions
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 7 Jan 2024 12:29:20 +0000 (13:29 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 7 Jan 2024 12:29:20 +0000 (13:29 +0100)
src/compopt.cpp
src/compopt.hpp

index 3b268f6d94fd5e9dc91853f379a7f58662b0b2c9..4a0148b465aad0a78a7c3968e84c897fb0e0aa6d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2010-2023 Joel Rosdahl and other contributors
+// Copyright (C) 2010-2024 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -45,7 +45,7 @@
 
 struct CompOpt
 {
-  const char* name;
+  std::string_view name;
   int type;
 };
 
@@ -175,7 +175,7 @@ compare_compopts(const void* key1, const void* key2)
 {
   const CompOpt* opt1 = static_cast<const CompOpt*>(key1);
   const CompOpt* opt2 = static_cast<const CompOpt*>(key2);
-  return strcmp(opt1->name, opt2->name);
+  return opt1->name.compare(opt2->name);
 }
 
 static int
@@ -183,24 +183,22 @@ compare_prefix_compopts(const void* key1, const void* key2)
 {
   const CompOpt* opt1 = static_cast<const CompOpt*>(key1);
   const CompOpt* opt2 = static_cast<const CompOpt*>(key2);
-  return strncmp(opt1->name, opt2->name, strlen(opt2->name));
+  return opt1->name.substr(0, opt2->name.length()).compare(opt2->name);
 }
 
 static const CompOpt*
-find(const std::string& option)
+find(std::string_view option)
 {
-  CompOpt key;
-  key.name = option.c_str();
+  CompOpt key{option, 0};
   void* result = bsearch(
     &key, compopts, std::size(compopts), sizeof(compopts[0]), compare_compopts);
   return static_cast<CompOpt*>(result);
 }
 
 static const CompOpt*
-find_prefix(const std::string& option)
+find_prefix(std::string_view option)
 {
-  CompOpt key;
-  key.name = option.c_str();
+  CompOpt key{option, 0};
   void* result = bsearch(&key,
                          compopts,
                          std::size(compopts),
@@ -228,7 +226,7 @@ compopt_verify_sortedness_and_flags()
       continue;
     }
 
-    if (strcmp(compopts[i - 1].name, compopts[i].name) >= 0) {
+    if (compopts[i - 1].name >= compopts[i].name) {
       PRINT(stderr,
             "compopt_verify_sortedness: {} >= {}\n",
             compopts[i - 1].name,
@@ -240,49 +238,49 @@ compopt_verify_sortedness_and_flags()
 }
 
 bool
-compopt_affects_cpp_output(const std::string& option)
+compopt_affects_cpp_output(std::string_view option)
 {
   const CompOpt* co = find(option);
   return co && (co->type & AFFECTS_CPP);
 }
 
 bool
-compopt_affects_compiler_output(const std::string& option)
+compopt_affects_compiler_output(std::string_view option)
 {
   const CompOpt* co = find(option);
   return co && (co->type & AFFECTS_COMP);
 }
 
 bool
-compopt_too_hard(const std::string& option)
+compopt_too_hard(std::string_view option)
 {
   const CompOpt* co = find(option);
   return co && (co->type & TOO_HARD);
 }
 
 bool
-compopt_too_hard_for_direct_mode(const std::string& option)
+compopt_too_hard_for_direct_mode(std::string_view option)
 {
   const CompOpt* co = find(option);
   return co && (co->type & TOO_HARD_DIRECT);
 }
 
 bool
-compopt_takes_path(const std::string& option)
+compopt_takes_path(std::string_view option)
 {
   const CompOpt* co = find(option);
   return co && (co->type & TAKES_PATH);
 }
 
 bool
-compopt_takes_arg(const std::string& option)
+compopt_takes_arg(std::string_view option)
 {
   const CompOpt* co = find(option);
   return co && (co->type & TAKES_ARG);
 }
 
 bool
-compopt_takes_concat_arg(const std::string& option)
+compopt_takes_concat_arg(std::string_view option)
 {
   const CompOpt* co = find(option);
   return co && (co->type & TAKES_CONCAT_ARG);
@@ -291,7 +289,7 @@ compopt_takes_concat_arg(const std::string& option)
 // Determines if the prefix of the option matches any option and affects the
 // preprocessor.
 bool
-compopt_prefix_affects_cpp_output(const std::string& option)
+compopt_prefix_affects_cpp_output(std::string_view option)
 {
   // Prefix options have to take concatenated args.
   const CompOpt* co = find_prefix(option);
@@ -301,7 +299,7 @@ compopt_prefix_affects_cpp_output(const std::string& option)
 // Determines if the prefix of the option matches any option and affects the
 // preprocessor.
 bool
-compopt_prefix_affects_compiler_output(const std::string& option)
+compopt_prefix_affects_compiler_output(std::string_view option)
 {
   // Prefix options have to take concatenated args.
   const CompOpt* co = find_prefix(option);
index 29a3354b0061123c84f7004f4a0650c8244387aa..3adfe46ddb6c680f36f2472cbbda91a857cb3030 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2010-2021 Joel Rosdahl and other contributors
+// Copyright (C) 2010-2024 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
 
 #pragma once
 
-#include <string>
+#include <string_view>
 
-bool compopt_short(bool (*fn)(const std::string& option),
-                   const std::string& option);
-bool compopt_affects_cpp_output(const std::string& option);
-bool compopt_affects_compiler_output(const std::string& option);
-bool compopt_too_hard(const std::string& option);
-bool compopt_too_hard_for_direct_mode(const std::string& option);
-bool compopt_takes_path(const std::string& option);
-bool compopt_takes_arg(const std::string& option);
-bool compopt_takes_concat_arg(const std::string& option);
-bool compopt_prefix_affects_cpp_output(const std::string& option);
-bool compopt_prefix_affects_compiler_output(const std::string& option);
+bool compopt_short(bool (*fn)(std::string_view option),
+                   std::string_view option);
+bool compopt_affects_cpp_output(std::string_view option);
+bool compopt_affects_compiler_output(std::string_view option);
+bool compopt_too_hard(std::string_view option);
+bool compopt_too_hard_for_direct_mode(std::string_view option);
+bool compopt_takes_path(std::string_view option);
+bool compopt_takes_arg(std::string_view option);
+bool compopt_takes_concat_arg(std::string_view option);
+bool compopt_prefix_affects_cpp_output(std::string_view option);
+bool compopt_prefix_affects_compiler_output(std::string_view option);