]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Simplify Util::{base,dir}_name
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 12 Feb 2020 21:06:08 +0000 (22:06 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 13 Feb 2020 20:56:13 +0000 (21:56 +0100)
This also fixes a bug in the Windows versions.

src/Util.cpp

index a4a8f50250b60e05ccbd9f976b4a9cc950fb8488..591ef074deea06032a0af009af0327c74922d500 100644 (file)
@@ -77,13 +77,12 @@ namespace Util {
 string_view
 base_name(string_view path)
 {
-  size_t n = path.rfind('/');
 #ifdef _WIN32
-  size_t n2 = path.rfind('\\');
-  if (n2 != std::string::npos && n2 > n) {
-    n = n2;
-  }
+  const char delim[] = "/\\";
+#else
+  const char delim[] = "/";
 #endif
+  size_t n = path.find_last_of(delim);
   return n == std::string::npos ? path : path.substr(n + 1);
 }
 
@@ -135,17 +134,17 @@ create_temp_fd(string_view path_prefix)
 string_view
 dir_name(string_view path)
 {
-  size_t n = path.rfind('/');
 #ifdef _WIN32
-  size_t n2 = path.rfind('\\');
-  if (n2 != std::string::npos && n2 > n) {
-    n = n2;
-  }
+  const char delim[] = "/\\";
+#else
+  const char delim[] = "/";
 #endif
+  size_t n = path.find_last_of(delim);
   if (n == std::string::npos) {
     return ".";
+  } else {
+    return n == 0 ? "/" : path.substr(0, n);
   }
-  return n == 0 ? "/" : path.substr(0, n);
 }
 
 bool