]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add and test remove_extension()
authorThomas Otto <thomas.otto@pdv-fs.de>
Wed, 30 Oct 2019 10:26:23 +0000 (11:26 +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 e9f0b57d4c573ba0d7b679a82b76a8c957ddd90f..8ad1ebd89566a33da6e5047e13ee50ab921537f6 100644 (file)
@@ -161,6 +161,12 @@ get_extension(nonstd::string_view path)
   }
 }
 
+nonstd::string_view
+remove_extension(nonstd::string_view path)
+{
+  return path.substr(0, path.length() - get_extension(path).length());
+}
+
 nonstd::string_view
 get_truncated_base_name(nonstd::string_view path, size_t max_length)
 {
index f86c7c1de14a2e61b146f69ef22e232b5cb6957f..7c87295512568bcb68da9c838c0d6303bb62aa93 100644 (file)
@@ -45,6 +45,10 @@ nonstd::string_view base_name(nonstd::string_view path);
 // If `path` has no file extension, an empty string_view is returned.
 nonstd::string_view get_extension(nonstd::string_view path);
 
+// Return a view into `path` containing the given path without the
+// filename extension as determined by `get_extension()`.
+nonstd::string_view remove_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 5a500e1d06acce141d844c4ff87976243df86049..ef1d2655dc72f271a74a582a3dd60f2d77b11fa6 100644 (file)
@@ -47,6 +47,21 @@ TEST_CASE("Util::get_extension")
   CHECK(Util::get_extension("/foo/bar/f.abc.txt") == ".txt");
 }
 
+TEST_CASE("Util::remove_extension")
+{
+  CHECK(Util::remove_extension("") == "");
+  CHECK(Util::remove_extension(".") == "");
+  CHECK(Util::remove_extension("...") == "..");
+  CHECK(Util::remove_extension("foo") == "foo");
+  CHECK(Util::remove_extension("/") == "/");
+  CHECK(Util::remove_extension("/foo") == "/foo");
+  CHECK(Util::remove_extension("/foo/bar/f") == "/foo/bar/f");
+  CHECK(Util::remove_extension("f.txt") == "f");
+  CHECK(Util::remove_extension("f.abc.txt") == "f.abc");
+  CHECK(Util::remove_extension("/foo/bar/f.txt") == "/foo/bar/f");
+  CHECK(Util::remove_extension("/foo/bar/f.abc.txt") == "/foo/bar/f.abc");
+}
+
 TEST_CASE("Util:get_truncated_base_name")
 {
   CHECK(Util::get_truncated_base_name("", 5) == "");