]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Restore sortedness of Util functions
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 8 Feb 2020 17:35:37 +0000 (18:35 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 8 Feb 2020 17:35:37 +0000 (18:35 +0100)
src/Util.cpp
src/Util.hpp

index ab0d7abb6382069f32c86a688d79abdd4c4d43bb..a4a8f50250b60e05ccbd9f976b4a9cc950fb8488 100644 (file)
@@ -87,6 +87,13 @@ base_name(string_view path)
   return n == std::string::npos ? path : path.substr(n + 1);
 }
 
+std::string
+change_extension(string_view path, string_view new_ext)
+{
+  string_view without_ext = Util::remove_extension(path);
+  return std::string(without_ext).append(new_ext.data(), new_ext.length());
+}
+
 bool
 create_dir(string_view dir)
 {
@@ -141,49 +148,6 @@ dir_name(string_view path)
   return n == 0 ? "/" : path.substr(0, n);
 }
 
-string_view
-get_extension(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 {};
-#ifdef _WIN32
-  } else if (path.at(pos) == '\\') {
-    return {};
-#endif
-  } else {
-    return path.substr(pos);
-  }
-}
-
-string_view
-remove_extension(string_view path)
-{
-  return path.substr(0, path.length() - get_extension(path).length());
-}
-
-std::string
-change_extension(string_view path, string_view new_ext)
-{
-  string_view without_ext = Util::remove_extension(path);
-  return std::string(without_ext).append(new_ext.data(), new_ext.length());
-}
-
-string_view
-get_truncated_base_name(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(string_view string, string_view suffix)
 {
@@ -206,6 +170,26 @@ for_each_level_1_subdir(const std::string& cache_dir,
   progress_receiver(1.0);
 }
 
+string_view
+get_extension(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 {};
+#ifdef _WIN32
+  } else if (path.at(pos) == '\\') {
+    return {};
+#endif
+  } else {
+    return path.substr(pos);
+  }
+}
+
 void
 get_level_1_files(const std::string& dir,
                   const ProgressReceiver& progress_receiver,
@@ -241,6 +225,16 @@ get_path_in_cache(string_view cache_dir,
   return path;
 }
 
+string_view
+get_truncated_base_name(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);
+}
+
 int
 parse_int(const std::string& value)
 {
@@ -269,6 +263,12 @@ read_file(const std::string& path)
                      std::istreambuf_iterator<char>());
 }
 
+string_view
+remove_extension(string_view path)
+{
+  return path.substr(0, path.length() - get_extension(path).length());
+}
+
 bool
 starts_with(string_view string, string_view prefix)
 {
index 1c1ebacc525eb28225e8fd8827778e894f4d972b..19237c797de62f80f151ea9968fdfdaf7ff65f01 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2020 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -42,25 +42,6 @@ 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 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);
-
-// 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.
-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:
@@ -91,6 +72,11 @@ big_endian_to_int(const uint8_t* buffer, uint8_t& value)
   value = buffer[0];
 }
 
+// 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);
+
 // Create a directory if needed, including its parents if needed.
 //
 // Returns true if the directory exists or could be created, otherwise false.
@@ -123,6 +109,10 @@ void for_each_level_1_subdir(const std::string& cache_dir,
                              const SubdirVisitor& visitor,
                              const ProgressReceiver& progress_receiver);
 
+// 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);
+
 // Get a list of files in a level 1 subdirectory of the cache.
 //
 // The function works under the assumption that directory entries with one
@@ -154,6 +144,11 @@ std::string get_path_in_cache(nonstd::string_view cache_dir,
                               uint32_t levels,
                               nonstd::string_view name,
                               nonstd::string_view suffix);
+// 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);
 
 // Write bytes in big endian order from an integer value.
 //
@@ -194,6 +189,10 @@ int parse_int(const std::string& value);
 // Throws Error on error.
 std::string read_file(const std::string& 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 true if prefix is a prefix of string.
 bool starts_with(nonstd::string_view string, nonstd::string_view prefix);