return path.substr(0, path.length() - get_extension(path).length());
}
+std::string
+change_extension(nonstd::string_view path, nonstd::string_view new_ext)
+{
+ string_view without_ext = Util::remove_extension(path);
+ return std::string(without_ext).append(new_ext.data(), new_ext.length());
+}
+
nonstd::string_view
get_truncated_base_name(nonstd::string_view path, size_t max_length)
{
// filename extension as determined by `get_extension()`.
nonstd::string_view remove_extension(nonstd::string_view path);
+// Remove the extension via `remove_extension()`, then add `new_ext`.
+// `new_ext` should start with a dot, no extra dot is inserted.
+std::string change_extension(nonstd::string_view path,
+ nonstd::string_view new_ext);
+
// 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.
CHECK(Util::remove_extension("/foo/bar/f.abc.txt") == "/foo/bar/f.abc");
}
+TEST_CASE("Util::change_extension")
+{
+ CHECK(Util::change_extension("", "") == "");
+ CHECK(Util::change_extension("x", "") == "x");
+ CHECK(Util::change_extension("", "x") == "x");
+ CHECK(Util::change_extension("", ".") == ".");
+ CHECK(Util::change_extension(".", "") == "");
+ CHECK(Util::change_extension("...", "x") == "..x");
+ CHECK(Util::change_extension("abc", "def") == "abcdef");
+ CHECK(Util::change_extension("foo.ext", "e2") == "fooe2");
+ CHECK(Util::change_extension("bar.txt", ".o") == "bar.o");
+ CHECK(Util::change_extension("foo.bar.txt", ".o") == "foo.bar.o");
+}
+
TEST_CASE("Util:get_truncated_base_name")
{
CHECK(Util::get_truncated_base_name("", 5) == "");