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

Unified Diff: base/file_path.cc

Issue 10067002: Add an AddExtension() method in FilePath (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Moved function IsBaseEmptyOrSpecialCase Created 8 years, 8 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
« no previous file with comments | « base/file_path.h ('k') | base/file_path_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/file_path.cc
diff --git a/base/file_path.cc b/base/file_path.cc
index 3b1fca4b4ed6c7e8c0e52bf643211d66204a3c97..dfa3525db7462f663b7083646f1872bb3a214a77 100644
--- a/base/file_path.cc
+++ b/base/file_path.cc
@@ -159,6 +159,21 @@ StringType::size_type ExtensionSeparatorPosition(const StringType& path) {
return last_dot;
}
+// Returns true if path is "", ".", or "..".
+bool IsEmptyOrSpecialCase(const StringType& path) {
+ if (path.empty())
+ return true;
+ if (*(path.end() - 1) == FilePath::kExtensionSeparator) {
Mark Mentovai 2012/04/19 20:59:09 What’s this actually buy, other than making the co
Devlin 2012/04/21 01:37:36 I didn't write this portion of the code (just refa
+ // Special case "." and ".."
+ if (path == FilePath::kCurrentDirectory ||
+ path == FilePath::kParentDirectory) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
} // namespace
FilePath::FilePath() {
@@ -378,15 +393,8 @@ FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const {
if (path_.empty())
return FilePath();
- StringType base = BaseName().value();
- if (base.empty())
+ if (IsEmptyOrSpecialCase(BaseName().value()))
return FilePath();
- if (*(base.end() - 1) == kExtensionSeparator) {
- // Special case "." and ".."
- if (base == kCurrentDirectory || base == kParentDirectory) {
- return FilePath();
- }
- }
StringType ext = Extension();
StringType ret = RemoveExtension().value();
@@ -405,19 +413,32 @@ FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix)
#endif
}
-FilePath FilePath::ReplaceExtension(const StringType& extension) const {
+FilePath FilePath::AddExtension(const StringType& extension) const {
if (path_.empty())
return FilePath();
- StringType base = BaseName().value();
- if (base.empty())
+ if (IsEmptyOrSpecialCase(BaseName().value()))
return FilePath();
- if (*(base.end() - 1) == kExtensionSeparator) {
- // Special case "." and ".."
- if (base == kCurrentDirectory || base == kParentDirectory) {
- return FilePath();
- }
+
+ // If the new extension is "" or ".", then just return the current FilePath.
+ if (extension.empty() || extension == StringType(1, kExtensionSeparator))
+ return *this;
+
+ StringType str = path_;
+ if (extension[0] != kExtensionSeparator &&
+ *(str.end() - 1) != kExtensionSeparator) {
+ str.append(1, kExtensionSeparator);
}
+ str.append(extension);
+ return FilePath(str);
+}
+
+FilePath FilePath::ReplaceExtension(const StringType& extension) const {
+ if (path_.empty())
+ return FilePath();
+
+ if (IsEmptyOrSpecialCase(BaseName().value()))
+ return FilePath();
FilePath no_ext = RemoveExtension();
// If the new extension is "" or ".", then just remove the current extension.
« no previous file with comments | « base/file_path.h ('k') | base/file_path_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698