Chromium Code Reviews| Index: base/files/file_path.cc |
| diff --git a/base/files/file_path.cc b/base/files/file_path.cc |
| index 90cf40f4da8453396bae2e473c9865aaa3f27ab2..15fa40fa1647b877e9ebfc6676ad279df1012728 100644 |
| --- a/base/files/file_path.cc |
| +++ b/base/files/file_path.cc |
| @@ -18,6 +18,8 @@ const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); |
| #endif // FILE_PATH_USES_WIN_SEPARATORS |
| const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); |
| +const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); |
| +const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.'); |
| typedef FilePath::StringType StringType; |
| @@ -78,6 +80,17 @@ bool IsPathAbsolute(const StringType& path) { |
| #endif // FILE_PATH_USES_DRIVE_LETTERS |
| } |
| +// Find the position of the '.' that separates the extension from the rest |
| +// of the file name. The position is relative to BaseName(), not value(). |
|
Mark Mentovai
2015/01/20 17:11:53
The second sentence should be harsher: this functi
scottmg
2015/01/20 18:01:27
After simplifying below, just deleted this functio
|
| +// Returns npos if it can't find an extension. |
| +StringType::size_type FinalExtensionSeparatorPosition(const StringType& path) { |
| + // Special case "." and ".." |
| + if (path == FilePath::kCurrentDirectory || path == FilePath::kParentDirectory) |
| + return StringType::npos; |
| + |
| + return path.rfind(FilePath::kExtensionSeparator); |
| +} |
| + |
| } // namespace |
| FilePath::FilePath() { |
| @@ -190,6 +203,26 @@ FilePath FilePath::BaseName() const { |
| return new_path; |
| } |
| +StringType FilePath::FinalExtension() const { |
| + FilePath base(BaseName()); |
| + const StringType::size_type dot = FinalExtensionSeparatorPosition(base.path_); |
| + if (dot == StringType::npos) |
| + return StringType(); |
| + |
| + return base.path_.substr(dot, StringType::npos); |
| +} |
| + |
| +FilePath FilePath::RemoveFinalExtension() const { |
| + if (FinalExtension().empty()) |
| + return *this; |
| + |
| + const StringType::size_type dot = FinalExtensionSeparatorPosition(path_); |
|
Mark Mentovai
2015/01/20 17:11:53
This logic winds up calling FinalExtension() twice
scottmg
2015/01/20 18:01:27
Done.
|
| + if (dot == StringType::npos) |
| + return *this; |
| + |
| + return FilePath(path_.substr(0, dot)); |
| +} |
| + |
| FilePath FilePath::Append(const StringType& component) const { |
| const StringType* appended = &component; |
| StringType without_nuls; |