]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add and test get_extension()
authorThomas Otto <thomas.otto@pdv-fs.de>
Wed, 30 Oct 2019 10:21:00 +0000 (11:21 +0100)
committerThomas Otto <thomas.otto@pdv-fs.de>
Tue, 26 Nov 2019 13:45:21 +0000 (14:45 +0100)
Works as a replacement for the legacy util function.

src/Util.cpp
src/Util.hpp
unittest/test_Util.cpp

index 42ba9d23cb39b8e73e70e7bc4e6676b94ab638e3..e9f0b57d4c573ba0d7b679a82b76a8c957ddd90f 100644 (file)
@@ -24,6 +24,8 @@
 #include <algorithm>
 #include <fstream>
 
+using nonstd::string_view;
+
 namespace {
 
 void
@@ -40,7 +42,7 @@ get_cache_files_internal(const std::string& dir,
   std::vector<std::string> directories;
   dirent* de;
   while ((de = readdir(d))) {
-    nonstd::string_view name(de->d_name);
+    string_view name(de->d_name);
     if (name == "" || name == "." || name == ".." || name == "CACHEDIR.TAG"
         || name == "stats" || name.starts_with(".nfs")) {
       continue;
@@ -139,6 +141,26 @@ dir_name(nonstd::string_view path)
   return n == 0 ? "/" : path.substr(0, n);
 }
 
+nonstd::string_view
+get_extension(nonstd::string_view path)
+{
+#ifndef _WIN32
+  const char stop_at_chars[] = "./";
+#else
+  const char stop_at_chars[] = "./\\";
+#endif
+  size_t pos = path.find_last_of(stop_at_chars);
+  if (pos == string_view::npos || path.at(pos) == '/') {
+    return string_view();
+#ifdef _WIN32
+  } else if (path.at(pos) == '\\') {
+    return string_view();
+#endif
+  } else {
+    return path.substr(pos);
+  }
+}
+
 nonstd::string_view
 get_truncated_base_name(nonstd::string_view path, size_t max_length)
 {
index f8876343a2206f9a683bf08bcaae4d7c12013147..f86c7c1de14a2e61b146f69ef22e232b5cb6957f 100644 (file)
@@ -41,6 +41,10 @@ typedef std::function<void(const std::string& /*dir_path*/,
 // Get base name of path.
 nonstd::string_view base_name(nonstd::string_view path);
 
+// Return the file extension (including the dot) as a view into `path`.
+// If `path` has no file extension, an empty string_view is returned.
+nonstd::string_view get_extension(nonstd::string_view path);
+
 // Return a shortened view into the base name of `path``. This view starts at
 // the beginning of the base name and ends at either the position the first dot,
 // or `max_length`, or the length of the base name, whichever is the shortest.
index eb031a639b47b4f37be336cb266f6b23708c95b9..5a500e1d06acce141d844c4ff87976243df86049 100644 (file)
@@ -32,6 +32,21 @@ TEST_CASE("Util::base_name")
   CHECK(Util::base_name("/foo/bar/f.txt") == "f.txt");
 }
 
+TEST_CASE("Util::get_extension")
+{
+  CHECK(Util::get_extension("") == "");
+  CHECK(Util::get_extension(".") == ".");
+  CHECK(Util::get_extension("...") == ".");
+  CHECK(Util::get_extension("foo") == "");
+  CHECK(Util::get_extension("/") == "");
+  CHECK(Util::get_extension("/foo") == "");
+  CHECK(Util::get_extension("/foo/bar/f") == "");
+  CHECK(Util::get_extension("f.txt") == ".txt");
+  CHECK(Util::get_extension("f.abc.txt") == ".txt");
+  CHECK(Util::get_extension("/foo/bar/f.txt") == ".txt");
+  CHECK(Util::get_extension("/foo/bar/f.abc.txt") == ".txt");
+}
+
 TEST_CASE("Util:get_truncated_base_name")
 {
   CHECK(Util::get_truncated_base_name("", 5) == "");