diff options
author | rafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-01 01:03:34 +0000 |
---|---|---|
committer | rafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-01 01:03:34 +0000 |
commit | 62e78f50607bd7b5212435e332eca9e27c2a8a8a (patch) | |
tree | 8cf5e41ac6e13eaf25398dc602148317809ec9b2 /base | |
parent | e37d90b84954ca9d6a1fb059637b94755039e6b6 (diff) | |
download | chromium_src-62e78f50607bd7b5212435e332eca9e27c2a8a8a.zip chromium_src-62e78f50607bd7b5212435e332eca9e27c2a8a8a.tar.gz chromium_src-62e78f50607bd7b5212435e332eca9e27c2a8a8a.tar.bz2 |
make FilePath:IsParent use case-insensitive compare for drive letters on windows
R=erikkay
BUG=15659
Review URL: http://codereview.chromium.org/150109
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19694 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/file_path.cc | 52 | ||||
-rw-r--r-- | base/file_path.h | 8 | ||||
-rw-r--r-- | base/file_path_unittest.cc | 71 |
3 files changed, 124 insertions, 7 deletions
diff --git a/base/file_path.cc b/base/file_path.cc index 6712d70..f342054 100644 --- a/base/file_path.cc +++ b/base/file_path.cc @@ -44,6 +44,28 @@ FilePath::StringType::size_type FindDriveLetter( return FilePath::StringType::npos; } + +#if defined(FILE_PATH_USES_DRIVE_LETTERS) +bool EqualDriveLetterCaseInsensitive(const FilePath::StringType a, + const FilePath::StringType b) { + size_t a_letter_pos = FindDriveLetter(a); + size_t b_letter_pos = FindDriveLetter(b); + + if ((a_letter_pos == FilePath::StringType::npos) || + (b_letter_pos == FilePath::StringType::npos)) + return a == b; + + FilePath::StringType a_letter(a.substr(0, a_letter_pos + 1)); + FilePath::StringType b_letter(b.substr(0, b_letter_pos + 1)); + if (!StartsWith(a_letter, b_letter, false)) + return false; + + FilePath::StringType a_rest(a.substr(a_letter_pos + 1)); + FilePath::StringType b_rest(b.substr(b_letter_pos + 1)); + return a_rest == b_rest; +} +#endif // defined(FILE_PATH_USES_DRIVE_LETTERS) + bool IsPathAbsolute(const FilePath::StringType& path) { #if defined(FILE_PATH_USES_DRIVE_LETTERS) FilePath::StringType::size_type letter = FindDriveLetter(path); @@ -120,6 +142,22 @@ void FilePath::GetComponents(std::vector<FilePath::StringType>* components) ret_val.rend()); } +bool FilePath::operator==(const FilePath& that) const { +#if defined(FILE_PATH_USES_DRIVE_LETTERS) + return EqualDriveLetterCaseInsensitive(this->path_, that.path_); +#else // defined(FILE_PATH_USES_DRIVE_LETTERS) + return path_ == that.path_; +#endif // defined(FILE_PATH_USES_DRIVE_LETTERS) +} + +bool FilePath::operator!=(const FilePath& that) const { +#if defined(FILE_PATH_USES_DRIVE_LETTERS) + return !EqualDriveLetterCaseInsensitive(this->path_, that.path_); +#else // defined(FILE_PATH_USES_DRIVE_LETTERS) + return path_ != that.path_; +#endif // defined(FILE_PATH_USES_DRIVE_LETTERS) +} + bool FilePath::IsParent(const FilePath& child) const { std::vector<FilePath::StringType> parent_components; std::vector<FilePath::StringType> child_components; @@ -135,6 +173,20 @@ bool FilePath::IsParent(const FilePath& child) const { parent_components.begin(); std::vector<FilePath::StringType>::const_iterator child_comp = child_components.begin(); + +#if defined(FILE_PATH_USES_DRIVE_LETTERS) + // Windows can access case sensitive filesystems, so component + // comparisions must be case sensitive, but drive letters are + // never case sensitive. + if ((FindDriveLetter(*parent_comp) != FilePath::StringType::npos) && + (FindDriveLetter(*child_comp) != FilePath::StringType::npos)) { + if (!StartsWith(*parent_comp, *child_comp, false)) + return false; + ++parent_comp; + ++child_comp; + } +#endif // defined(FILE_PATH_USES_DRIVE_LETTERS) + while (parent_comp != parent_components.end()) { if (*parent_comp != *child_comp) return false; diff --git a/base/file_path.h b/base/file_path.h index 5fe8cda..17653d1 100644 --- a/base/file_path.h +++ b/base/file_path.h @@ -122,13 +122,9 @@ class FilePath { return *this; } - bool operator==(const FilePath& that) const { - return path_ == that.path_; - } + bool operator==(const FilePath& that) const; - bool operator!=(const FilePath& that) const { - return path_ != that.path_; - } + bool operator!=(const FilePath& that) const; // Required for some STL containers and operations bool operator<(const FilePath& that) const { diff --git a/base/file_path_unittest.cc b/base/file_path_unittest.cc index 55efeac..ca30baa 100644 --- a/base/file_path_unittest.cc +++ b/base/file_path_unittest.cc @@ -450,10 +450,18 @@ TEST_F(FilePathTest, IsParentTest) { { { FPL(""), FPL("foo") }, false}, #if defined(FILE_PATH_USES_DRIVE_LETTERS) { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true}, + { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true}, + { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true}, + { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false}, + { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false}, { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true}, { { FPL("c:"), FPL("c:/foo/bar/baz") }, true}, { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false}, + { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false}, + { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false}, { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false}, + { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false}, + { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false}, { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false}, #endif // FILE_PATH_USES_DRIVE_LETTERS #if defined(FILE_PATH_USES_WIN_SEPARATORS) @@ -464,7 +472,7 @@ TEST_F(FilePathTest, IsParentTest) { { { FPL(""), FPL("\\foo\\bar\\baz") }, false}, { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false}, { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false}, -#endif // FILE_PATH_USES_DRIVE_LETTERS +#endif // FILE_PATH_USES_WIN_SEPARATORS }; for (size_t i = 0; i < arraysize(cases); ++i) { @@ -477,6 +485,67 @@ TEST_F(FilePathTest, IsParentTest) { } } +TEST_F(FilePathTest, EqualityTest) { + const struct BinaryBooleanTestData cases[] = { + { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true}, + { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false}, + { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false}, + { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true}, + { { FPL("/foo/bar"), FPL("/foo2/bar") }, false}, + { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false}, + { { FPL("foo/bar"), FPL("foo/bar") }, true}, + { { FPL("foo/bar"), FPL("foo/bar/baz") }, false}, + { { FPL(""), FPL("foo") }, false}, +#if defined(FILE_PATH_USES_DRIVE_LETTERS) + { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true}, + { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true}, + { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true}, + { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false}, + { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false}, + { { FPL("c:/"), FPL("c:/") }, true}, + { { FPL("c:"), FPL("c:") }, true}, + { { FPL("c:/foo/bar"), FPL("d:/foo/bar") }, false}, + { { FPL("c:/foo/bar"), FPL("D:/foo/bar") }, false}, + { { FPL("C:/foo/bar"), FPL("d:/foo/bar") }, false}, + { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false}, +#endif // FILE_PATH_USES_DRIVE_LETTERS +#if defined(FILE_PATH_USES_WIN_SEPARATORS) + { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true}, + { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true}, + { { FPL("\\foo/bar"), FPL("\\foo\bar") }, false}, + { { FPL("\\"), FPL("\\") }, true}, + { { FPL("\\"), FPL("/") }, false}, + { { FPL(""), FPL("\\") }, false}, + { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false}, + { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false}, +#if defined(FILE_PATH_USES_DRIVE_LETTERS) + { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true}, + { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true}, + { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false}, +#endif // FILE_PATH_USES_DRIVE_LETTERS +#endif // FILE_PATH_USES_WIN_SEPARATORS + }; + + for (size_t i = 0; i < arraysize(cases); ++i) { + FilePath a(cases[i].inputs[0]); + FilePath b(cases[i].inputs[1]); + + EXPECT_EQ(a == b, cases[i].expected) << + "equality i: " << i << ", a: " << a.value() << ", b: " << + b.value(); + } + + + for (size_t i = 0; i < arraysize(cases); ++i) { + FilePath a(cases[i].inputs[0]); + FilePath b(cases[i].inputs[1]); + + EXPECT_EQ(a != b, !cases[i].expected) << + "inequality i: " << i << ", a: " << a.value() << ", b: " << + b.value(); + } +} + TEST_F(FilePathTest, Extension) { FilePath base_dir(FILE_PATH_LITERAL("base_dir")); |