diff options
-rw-r--r-- | base/file_path.cc | 9 | ||||
-rw-r--r-- | base/file_path.h | 8 | ||||
-rw-r--r-- | base/file_path_unittest.cc | 13 |
3 files changed, 29 insertions, 1 deletions
diff --git a/base/file_path.cc b/base/file_path.cc index 3375b66..406d018 100644 --- a/base/file_path.cc +++ b/base/file_path.cc @@ -252,6 +252,15 @@ FilePath FilePath::Append(const FilePath& component) const { return Append(component.value()); } +FilePath FilePath::AppendASCII(const std::string& component) const { + DCHECK(IsStringASCII(component)); +#if defined(OS_WIN) + return Append(ASCIIToWide(component)); +#elif defined(OS_POSIX) + return Append(component); +#endif +} + bool FilePath::IsAbsolute() const { return IsPathAbsolute(path_); } diff --git a/base/file_path.h b/base/file_path.h index d82f8f4..053b815b 100644 --- a/base/file_path.h +++ b/base/file_path.h @@ -189,6 +189,14 @@ class FilePath { FilePath Append(const StringType& component) const WARN_UNUSED_RESULT; FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; + // Although Windows StringType is std::wstring, since the encoding it uses for + // paths is well defined, it can handle ASCII path components as well. + // Mac uses UTF8, and since ASCII is a subset of that, it works there as well. + // On Linux, although it can use any 8-bit encoding for paths, we assume that + // ASCII is a valid subset, regardless of the encoding, since many operating + // system paths will always be ASCII. + FilePath AppendASCII(const std::string& component) const WARN_UNUSED_RESULT; + // Returns true if this FilePath contains an absolute path. On Windows, an // absolute path begins with either a drive letter specification followed by // a separator character, or with two separator characters. On POSIX diff --git a/base/file_path_unittest.cc b/base/file_path_unittest.cc index b237af6..3439c6c 100644 --- a/base/file_path_unittest.cc +++ b/base/file_path_unittest.cc @@ -5,7 +5,7 @@ #include "base/basictypes.h" #include "base/file_path.h" #include "base/file_util.h" -#include "base/path_service.h" +#include "base/string_util.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" @@ -293,6 +293,17 @@ TEST_F(FilePathTest, Append) { FilePath observed_path = root.Append(FilePath(leaf)); EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) << "i: " << i << ", root: " << root.value() << ", leaf: " << leaf; + + // TODO(erikkay): It would be nice to have a unicode test append value to + // handle the case when AppendASCII is passed UTF8 +#if defined(OS_WIN) + std::string ascii = WideToASCII(leaf); +#elif defined(OS_POSIX) + std::string ascii = leaf; +#endif + observed_str = root.AppendASCII(ascii); + EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) << + "i: " << i << ", root: " << root.value() << ", leaf: " << leaf; } } |