aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2021-12-02 14:16:05 +0100
committerEelco Dolstra <edolstra@gmail.com>2022-01-07 13:23:00 +0100
commite1a0359b599d0beeb93f11d9948ec2cb1a1008c4 (patch)
tree6cfcf22e544fe56ead79bc564b0530f01e6f670f
parentbe64fb9b5160f50d5b127598c84ffb77b980799b (diff)
isInDir() / isDirOrInDir(): Use std::string_view
-rw-r--r--src/libutil/util.cc8
-rw-r--r--src/libutil/util.hh10
2 files changed, 10 insertions, 8 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index e18648557..9edd69c64 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -196,16 +196,16 @@ std::string_view baseNameOf(std::string_view path)
}
-bool isInDir(const Path & path, const Path & dir)
+bool isInDir(std::string_view path, std::string_view dir)
{
- return path[0] == '/'
- && string(path, 0, dir.size()) == dir
+ return path.substr(0, 1) == "/"
+ && path.substr(0, dir.size()) == dir
&& path.size() >= dir.size() + 2
&& path[dir.size()] == '/';
}
-bool isDirOrInDir(const Path & path, const Path & dir)
+bool isDirOrInDir(std::string_view path, std::string_view dir)
{
return path == dir || isInDir(path, dir);
}
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index d08f42826..dfad33ed2 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -66,11 +66,13 @@ Path dirOf(const Path & path);
following the final `/' (trailing slashes are removed). */
std::string_view baseNameOf(std::string_view path);
-/* Check whether 'path' is a descendant of 'dir'. */
-bool isInDir(const Path & path, const Path & dir);
+/* Check whether 'path' is a descendant of 'dir'. Both paths must be
+ canonicalized. */
+bool isInDir(std::string_view path, std::string_view dir);
-/* Check whether 'path' is equal to 'dir' or a descendant of 'dir'. */
-bool isDirOrInDir(const Path & path, const Path & dir);
+/* Check whether 'path' is equal to 'dir' or a descendant of
+ 'dir'. Both paths must be canonicalized. */
+bool isDirOrInDir(std::string_view path, std::string_view dir);
/* Get status of `path'. */
struct stat lstat(const Path & path);