return n == 0 ? "/" : path.substr(0, n);
}
+nonstd::string_view
+get_truncated_base_name(nonstd::string_view path, size_t max_length)
+{
+ string_view input_base = Util::base_name(path);
+ size_t dot_pos = input_base.find('.');
+ size_t truncate_pos =
+ std::min(max_length, std::min(input_base.size(), dot_pos));
+ return input_base.substr(0, truncate_pos);
+}
+
bool
ends_with(nonstd::string_view string, nonstd::string_view suffix)
{
// Get base name of path.
nonstd::string_view base_name(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.
+nonstd::string_view get_truncated_base_name(nonstd::string_view path,
+ size_t max_length);
+
// Get an integer value from bytes in big endian order.
//
// Parameters:
// Limit the basename to 10 characters in order to cope with filesystem with
// small maximum filename length limits.
- string_view input_base = Util::base_name(input_file);
- size_t dot_pos = input_base.find('.');
- size_t truncate_pos =
- std::min(size_t(10), std::min(input_base.size(), dot_pos));
- input_base = input_base.substr(0, truncate_pos);
-
+ string_view input_base = Util::get_truncated_base_name(input_file, 10);
path_stdout =
x_strdup(fmt::format("{}/{}.stdout", temp_dir(), input_base).c_str());
-
int path_stdout_fd = create_tmp_fd(&path_stdout);
add_pending_tmp_file(path_stdout);
CHECK(Util::base_name("/foo/bar/f.txt") == "f.txt");
}
+TEST_CASE("Util:get_truncated_base_name")
+{
+ CHECK(Util::get_truncated_base_name("", 5) == "");
+ CHECK(Util::get_truncated_base_name("a", 5) == "a");
+ CHECK(Util::get_truncated_base_name("abcdefg", 5) == "abcde");
+ CHECK(Util::get_truncated_base_name("abc.foo", 5) == "abc");
+ CHECK(Util::get_truncated_base_name("/path/to/abc.foo", 5) == "abc");
+ CHECK(Util::get_truncated_base_name("/path/to/abcdefg.foo", 5) == "abcde");
+ CHECK(Util::get_truncated_base_name("/path/to/.hidden", 5) == "");
+ CHECK(Util::get_truncated_base_name("/path/to/", 5) == "");
+}
+
TEST_CASE("Util::big_endian_to_int")
{
uint8_t bytes[8] = {0x70, 0x9e, 0x9a, 0xbc, 0xd6, 0x54, 0x4b, 0xca};