Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Unified Diff: base/files/file_path.cc

Issue 863583002: Add FilePath::FinalExtension and RemoveFinalExtension (Closed) Base URL: https://chromium.googlesource.com/chromium/mini_chromium@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/files/file_path.h ('K') | « base/files/file_path.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
« base/files/file_path.h ('K') | « base/files/file_path.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698