summaryrefslogtreecommitdiffstats
path: root/base/files
diff options
context:
space:
mode:
Diffstat (limited to 'base/files')
-rw-r--r--base/files/file_path.cc18
-rw-r--r--base/files/file_path.h7
-rw-r--r--base/files/file_path_unittest.cc29
3 files changed, 53 insertions, 1 deletions
diff --git a/base/files/file_path.cc b/base/files/file_path.cc
index 01d9eab..e9495a1 100644
--- a/base/files/file_path.cc
+++ b/base/files/file_path.cc
@@ -521,6 +521,24 @@ bool FilePath::IsAbsolute() const {
return IsPathAbsolute(path_);
}
+bool FilePath::EndsWithSeparator() const {
+ if (empty())
+ return false;
+ return IsSeparator(path_[path_.size() - 1]);
+}
+
+FilePath FilePath::AsEndingWithSeparator() const {
+ if (EndsWithSeparator() || path_.empty())
+ return *this;
+
+ StringType path_str;
+ path_str.reserve(path_.length() + 1); // Only allocate string once.
+
+ path_str = path_;
+ path_str.append(&kSeparators[0], 1);
+ return FilePath(path_str);
+}
+
FilePath FilePath::StripTrailingSeparators() const {
FilePath new_path(path_);
new_path.StripTrailingSeparatorsInternal();
diff --git a/base/files/file_path.h b/base/files/file_path.h
index cf3dc62..d66d631 100644
--- a/base/files/file_path.h
+++ b/base/files/file_path.h
@@ -285,6 +285,13 @@ class BASE_EXPORT FilePath {
// platforms, an absolute path begins with a separator character.
bool IsAbsolute() const;
+ // Returns true if the patch ends with a path separator character.
+ bool EndsWithSeparator() const WARN_UNUSED_RESULT;
+
+ // Returns a copy of this FilePath that ends with a trailing separator. If
+ // the input path is empty, an empty FilePath will be returned.
+ FilePath AsEndingWithSeparator() const WARN_UNUSED_RESULT;
+
// Returns a copy of this FilePath that does not end with a trailing
// separator.
FilePath StripTrailingSeparators() const WARN_UNUSED_RESULT;
diff --git a/base/files/file_path_unittest.cc b/base/files/file_path_unittest.cc
index 9a690a9..28778d9 100644
--- a/base/files/file_path_unittest.cc
+++ b/base/files/file_path_unittest.cc
@@ -1196,7 +1196,34 @@ TEST_F(FilePathTest, NormalizePathSeparators) {
"i: " << i << ", input: " << input.value();
}
}
-
#endif
+TEST_F(FilePathTest, EndsWithSeparator) {
+ const UnaryBooleanTestData cases[] = {
+ { FPL(""), false },
+ { FPL("/"), true },
+ { FPL("foo/"), true },
+ { FPL("bar"), false },
+ { FPL("/foo/bar"), false },
+ };
+ for (size_t i = 0; i < arraysize(cases); ++i) {
+ FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
+ EXPECT_EQ(cases[i].expected, input.EndsWithSeparator());
+ }
+}
+
+TEST_F(FilePathTest, AsEndingWithSeparator) {
+ const UnaryTestData cases[] = {
+ { FPL(""), FPL("") },
+ { FPL("/"), FPL("/") },
+ { FPL("foo"), FPL("foo/") },
+ { FPL("foo/"), FPL("foo/") }
+ };
+ for (size_t i = 0; i < arraysize(cases); ++i) {
+ FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
+ FilePath expected = FilePath(cases[i].expected).NormalizePathSeparators();
+ EXPECT_EQ(expected.value(), input.AsEndingWithSeparator().value());
+ }
+}
+
} // namespace base