]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add and test and use get_truncated_base_name()
authorThomas Otto <thomas.otto@pdv-fs.de>
Tue, 19 Nov 2019 17:34:26 +0000 (18:34 +0100)
committerThomas Otto <thomas.otto@pdv-fs.de>
Tue, 26 Nov 2019 13:45:12 +0000 (14:45 +0100)
src/Util.cpp
src/Util.hpp
src/ccache.cpp
unittest/test_Util.cpp

index 591d9462268c94ff6641377284166f8bdde61912..42ba9d23cb39b8e73e70e7bc4e6676b94ab638e3 100644 (file)
@@ -139,6 +139,16 @@ dir_name(nonstd::string_view path)
   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)
 {
index 1f3f266e097a439a305060e37106b3955ea4294a..f8876343a2206f9a683bf08bcaae4d7c12013147 100644 (file)
@@ -41,6 +41,12 @@ typedef std::function<void(const std::string& /*dir_path*/,
 // 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:
index a978e82bc12a779a5ef39ad5ce810da7c8eb8b6e..8b5791d037e28e6d2e56230378f056b10c55ef54 100644 (file)
@@ -1559,15 +1559,9 @@ get_result_name_from_cpp(struct args* args, struct hash* hash)
 
     // 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);
 
index 42c7207037952758c79e7ddef19c8b15c191af1a..eb031a639b47b4f37be336cb266f6b23708c95b9 100644 (file)
@@ -32,6 +32,18 @@ TEST_CASE("Util::base_name")
   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};