diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-12 05:17:15 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-12 05:17:15 +0000 |
commit | 154769362a046967efd14bb0a0da5d6f3a301e32 (patch) | |
tree | 33e4550cd4f8895b639fabc55b45f5bb6e8d67bc /base/files | |
parent | e7a810943e48e9649db8063058fb77ddefaf9f96 (diff) | |
download | chromium_src-154769362a046967efd14bb0a0da5d6f3a301e32.zip chromium_src-154769362a046967efd14bb0a0da5d6f3a301e32.tar.gz chromium_src-154769362a046967efd14bb0a0da5d6f3a301e32.tar.bz2 |
Move path functions from file_util to FilePath object.
EnsureEndsWithSeparator used to check whether the file existed. This seems bad and unnecessary so I removed it.
I removed file_util::ContainsPath and used the existing file_util::IsParent instead. The functions descriptions are the same but the implementations do slightly different things, which is worrying. The only non-test use of this function to worry about is content/browser/storage_partition_impl_map.cc. As far as I see, the requirements for this seem OK, but I'm not very familiar with this.
After some discussion with akalin, I changed sync/internal_api/sync_manager_impl.cc to be a DCHECK that the path is absolute rather than make it absolute. The old code relied on the behavior of the old function that the argument would be unchanged if the file didn't exist, and this (possibly relative) path would be used later. This behavior doesn't make a lot of sense, and it looks like now that the path is always absolute, so I replaced this call with a DCHECK.
BUG=
Review URL: https://codereview.chromium.org/13196006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@193855 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/files')
-rw-r--r-- | base/files/file_path.cc | 18 | ||||
-rw-r--r-- | base/files/file_path.h | 7 | ||||
-rw-r--r-- | base/files/file_path_unittest.cc | 29 |
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 |