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

Unified Diff: base/files/file_path.cc

Issue 4883003: Add FilePath::FinalExtension() to avoid double extensions (.tar.gz) for file selector (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment (also a rebase) Created 7 years 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
« no previous file with comments | « base/files/file_path.h ('k') | base/files/file_path_unittest.cc » ('j') | 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 4cfa2e650a9214e01aed42e55ecc6f067f8251fe..3ea5856a0d58c35c0abacb4b10fc87fd92379512 100644
--- a/base/files/file_path.cc
+++ b/base/files/file_path.cc
@@ -107,18 +107,21 @@ bool AreAllSeparators(const StringType& input) {
// Find the position of the '.' that separates the extension from the rest
// of the file name. The position is relative to BaseName(), not value().
-// This allows a second extension component of up to 4 characters when the
-// rightmost extension component is a common double extension (gz, bz2, Z).
-// For example, foo.tar.gz or foo.tar.Z would have extension components of
-// '.tar.gz' and '.tar.Z' respectively. Returns npos if it can't find an
-// extension.
-StringType::size_type ExtensionSeparatorPosition(const StringType& path) {
+// 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;
- const StringType::size_type last_dot =
- path.rfind(FilePath::kExtensionSeparator);
+ return path.rfind(FilePath::kExtensionSeparator);
+}
+
+// Same as above, but allow a second extension component of up to 4
+// characters when the rightmost extension component is a common double
+// extension (gz, bz2, Z). For example, foo.tar.gz or foo.tar.Z would have
+// extension components of '.tar.gz' and '.tar.Z' respectively.
+StringType::size_type ExtensionSeparatorPosition(const StringType& path) {
+ const StringType::size_type last_dot = FinalExtensionSeparatorPosition(path);
// No extension, or the extension is the whole filename.
if (last_dot == StringType::npos || last_dot == 0U)
@@ -370,6 +373,15 @@ StringType FilePath::Extension() const {
return base.path_.substr(dot, StringType::npos);
}
+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::RemoveExtension() const {
if (Extension().empty())
return *this;
@@ -381,6 +393,17 @@ FilePath FilePath::RemoveExtension() const {
return FilePath(path_.substr(0, dot));
}
+FilePath FilePath::RemoveFinalExtension() const {
+ if (FinalExtension().empty())
+ return *this;
+
+ const StringType::size_type dot = FinalExtensionSeparatorPosition(path_);
+ if (dot == StringType::npos)
+ return *this;
+
+ return FilePath(path_.substr(0, dot));
+}
+
FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const {
if (suffix.empty())
return FilePath(path_);
« no previous file with comments | « base/files/file_path.h ('k') | base/files/file_path_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698