aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-09-02 22:25:57 +0100
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-09-02 22:25:57 +0100
commit84b5eba15ed723c382ed5560835e1eba43e1ef36 (patch)
treed4841ad0901ef6c6ab802aada18fe67554c938ed
parentd491d36b4c3fcb18818841a9ae08a8c99b48fc66 (diff)
downloadpaludis-84b5eba15ed723c382ed5560835e1eba43e1ef36.tar.gz
paludis-84b5eba15ed723c382ed5560835e1eba43e1ef36.tar.xz
FSPath::starts_with
-rw-r--r--paludis/util/fs_path.cc11
-rw-r--r--paludis/util/fs_path.hh6
-rw-r--r--paludis/util/fs_path_TEST.cc25
3 files changed, 42 insertions, 0 deletions
diff --git a/paludis/util/fs_path.cc b/paludis/util/fs_path.cc
index 3fd1eeb..ff7b303 100644
--- a/paludis/util/fs_path.cc
+++ b/paludis/util/fs_path.cc
@@ -196,6 +196,17 @@ FSPath::strip_leading(const FSPath & f) const
return FSPath(_imp->path.substr(root.length()));
}
+bool
+FSPath::starts_with(const FSPath & f) const
+{
+ std::string root(stringify(f));
+
+ if (root == "/")
+ root.clear();
+ root.append("/");
+ return 0 == (_imp->path + "/").compare(0, root.length(), root);
+}
+
FSPath
FSPath::dirname() const
{
diff --git a/paludis/util/fs_path.hh b/paludis/util/fs_path.hh
index f18fa61..bb9c65a 100644
--- a/paludis/util/fs_path.hh
+++ b/paludis/util/fs_path.hh
@@ -85,6 +85,12 @@ namespace paludis
FSPath strip_leading(const FSPath & prefix) const;
/**
+ * Do we start with a given path (eg '/foo/bar' starts with '/foo'
+ * but not '/fo')?
+ */
+ bool starts_with(const FSPath &) const;
+
+ /**
* Try to make a directory.
*
* \return True, if we succeeded, and false if the directory
diff --git a/paludis/util/fs_path_TEST.cc b/paludis/util/fs_path_TEST.cc
index 095db0f..3a87d13 100644
--- a/paludis/util/fs_path_TEST.cc
+++ b/paludis/util/fs_path_TEST.cc
@@ -107,6 +107,31 @@ TEST(FSPath, StripLeading)
EXPECT_TRUE(stringify(d.strip_leading(root3)) == "/my/directory");
}
+TEST(FSPath, StartsWith)
+{
+ FSPath slash("/");
+ FSPath usr("/usr");
+ FSPath lib("/lib");
+
+ EXPECT_TRUE(FSPath("/").starts_with(slash));
+ EXPECT_TRUE(FSPath("/usr").starts_with(slash));
+ EXPECT_TRUE(FSPath("/usr/bin/foo").starts_with(slash));
+ EXPECT_TRUE(FSPath("/lib/foo").starts_with(slash));
+ EXPECT_TRUE(FSPath("/lib64/foo").starts_with(slash));
+
+ EXPECT_FALSE(FSPath("/").starts_with(usr));
+ EXPECT_TRUE(FSPath("/usr").starts_with(usr));
+ EXPECT_TRUE(FSPath("/usr/bin/foo").starts_with(usr));
+ EXPECT_FALSE(FSPath("/lib/foo").starts_with(usr));
+ EXPECT_FALSE(FSPath("/lib64/foo").starts_with(usr));
+
+ EXPECT_FALSE(FSPath("/").starts_with(lib));
+ EXPECT_FALSE(FSPath("/usr").starts_with(lib));
+ EXPECT_FALSE(FSPath("/usr/bin/foo").starts_with(lib));
+ EXPECT_TRUE(FSPath("/lib/foo").starts_with(lib));
+ EXPECT_FALSE(FSPath("/lib64/foo").starts_with(lib));
+}
+
TEST(FSPath, OStream)
{
std::string n("fs_path_TEST_dir/no_perms");