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

Unified Diff: base/file_util.cc

Issue 2990: Fixes bug in file_util::ReplaceExtension that could chop off path... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 3 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 | « no previous file | base/file_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/file_util.cc
===================================================================
--- base/file_util.cc (revision 2409)
+++ base/file_util.cc (working copy)
@@ -194,14 +194,36 @@
#endif
}
+// Appends the extension to file adding a '.' if extension doesn't contain one.
+// This does nothing if extension is empty or '.'. This is used internally by
+// ReplaceExtension.
+static void AppendExtension(const std::wstring& extension,
+ std::wstring* file) {
+ if (!extension.empty() && extension != L".") {
+ if (extension[0] != L'.')
+ file->append(L".");
+ file->append(extension);
+ }
+}
+
void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) {
const std::wstring::size_type last_dot = file_name->rfind(L'.');
+ if (last_dot == std::wstring::npos) {
+ // No extension, just append the supplied extension.
+ AppendExtension(extension, file_name);
+ return;
+ }
+ const std::wstring::size_type last_separator =
+ file_name->rfind(kPathSeparator);
+ if (last_separator != std::wstring::npos && last_dot < last_separator) {
+ // File name doesn't have extension, but one of the directories does; don't
+ // replace it, just append the supplied extension. For example
+ // 'c:\tmp.bar\foo'.
+ AppendExtension(extension, file_name);
+ return;
+ }
std::wstring result = file_name->substr(0, last_dot);
- if (!extension.empty() && extension != L".") {
- if (extension.at(0) != L'.')
- result.append(L".");
- result.append(extension);
- }
+ AppendExtension(extension, &result);
file_name->swap(result);
}
« no previous file with comments | « no previous file | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698