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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « base/file_path.h ('k') | base/file_path_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_path.h" 5 #include "base/file_path.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 #include <algorithm> 8 #include <algorithm>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 (last_separator == StringType::npos || 152 (last_separator == StringType::npos ||
153 penultimate_dot > last_separator) && 153 penultimate_dot > last_separator) &&
154 last_dot - penultimate_dot <= 5U && 154 last_dot - penultimate_dot <= 5U &&
155 last_dot - penultimate_dot > 1U) { 155 last_dot - penultimate_dot > 1U) {
156 return penultimate_dot; 156 return penultimate_dot;
157 } 157 }
158 158
159 return last_dot; 159 return last_dot;
160 } 160 }
161 161
162 // Returns true if path is "", ".", or "..".
163 bool IsEmptyOrSpecialCase(const StringType& path) {
164 if (path.empty())
165 return true;
166 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
167 // Special case "." and ".."
168 if (path == FilePath::kCurrentDirectory ||
169 path == FilePath::kParentDirectory) {
170 return true;
171 }
172 }
173
174 return false;
175 }
176
162 } // namespace 177 } // namespace
163 178
164 FilePath::FilePath() { 179 FilePath::FilePath() {
165 } 180 }
166 181
167 FilePath::FilePath(const FilePath& that) : path_(that.path_) { 182 FilePath::FilePath(const FilePath& that) : path_(that.path_) {
168 } 183 }
169 184
170 FilePath::FilePath(const StringType& path) : path_(path) { 185 FilePath::FilePath(const StringType& path) : path_(path) {
171 } 186 }
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 return FilePath(path_.substr(0, dot)); 386 return FilePath(path_.substr(0, dot));
372 } 387 }
373 388
374 FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const { 389 FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const {
375 if (suffix.empty()) 390 if (suffix.empty())
376 return FilePath(path_); 391 return FilePath(path_);
377 392
378 if (path_.empty()) 393 if (path_.empty())
379 return FilePath(); 394 return FilePath();
380 395
381 StringType base = BaseName().value(); 396 if (IsEmptyOrSpecialCase(BaseName().value()))
382 if (base.empty())
383 return FilePath(); 397 return FilePath();
384 if (*(base.end() - 1) == kExtensionSeparator) {
385 // Special case "." and ".."
386 if (base == kCurrentDirectory || base == kParentDirectory) {
387 return FilePath();
388 }
389 }
390 398
391 StringType ext = Extension(); 399 StringType ext = Extension();
392 StringType ret = RemoveExtension().value(); 400 StringType ret = RemoveExtension().value();
393 ret.append(suffix); 401 ret.append(suffix);
394 ret.append(ext); 402 ret.append(ext);
395 return FilePath(ret); 403 return FilePath(ret);
396 } 404 }
397 405
398 FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix) 406 FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix)
399 const { 407 const {
400 DCHECK(IsStringASCII(suffix)); 408 DCHECK(IsStringASCII(suffix));
401 #if defined(OS_WIN) 409 #if defined(OS_WIN)
402 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string())); 410 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string()));
403 #elif defined(OS_POSIX) 411 #elif defined(OS_POSIX)
404 return InsertBeforeExtension(suffix.as_string()); 412 return InsertBeforeExtension(suffix.as_string());
405 #endif 413 #endif
406 } 414 }
407 415
416 FilePath FilePath::AddExtension(const StringType& extension) const {
417 if (path_.empty())
418 return FilePath();
419
420 if (IsEmptyOrSpecialCase(BaseName().value()))
421 return FilePath();
422
423 // If the new extension is "" or ".", then just return the current FilePath.
424 if (extension.empty() || extension == StringType(1, kExtensionSeparator))
425 return *this;
426
427 StringType str = path_;
428 if (extension[0] != kExtensionSeparator &&
429 *(str.end() - 1) != kExtensionSeparator) {
430 str.append(1, kExtensionSeparator);
431 }
432 str.append(extension);
433 return FilePath(str);
434 }
435
408 FilePath FilePath::ReplaceExtension(const StringType& extension) const { 436 FilePath FilePath::ReplaceExtension(const StringType& extension) const {
409 if (path_.empty()) 437 if (path_.empty())
410 return FilePath(); 438 return FilePath();
411 439
412 StringType base = BaseName().value(); 440 if (IsEmptyOrSpecialCase(BaseName().value()))
413 if (base.empty())
414 return FilePath(); 441 return FilePath();
415 if (*(base.end() - 1) == kExtensionSeparator) {
416 // Special case "." and ".."
417 if (base == kCurrentDirectory || base == kParentDirectory) {
418 return FilePath();
419 }
420 }
421 442
422 FilePath no_ext = RemoveExtension(); 443 FilePath no_ext = RemoveExtension();
423 // If the new extension is "" or ".", then just remove the current extension. 444 // If the new extension is "" or ".", then just remove the current extension.
424 if (extension.empty() || extension == StringType(1, kExtensionSeparator)) 445 if (extension.empty() || extension == StringType(1, kExtensionSeparator))
425 return no_ext; 446 return no_ext;
426 447
427 StringType str = no_ext.value(); 448 StringType str = no_ext.value();
428 if (extension[0] != kExtensionSeparator) 449 if (extension[0] != kExtensionSeparator)
429 str.append(1, kExtensionSeparator); 450 str.append(1, kExtensionSeparator);
430 str.append(extension); 451 str.append(extension);
(...skipping 783 matching lines...) Expand 10 before | Expand all | Expand 10 after
1214 #if defined(FILE_PATH_USES_WIN_SEPARATORS) 1235 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
1215 StringType copy = path_; 1236 StringType copy = path_;
1216 for (size_t i = 1; i < arraysize(kSeparators); ++i) { 1237 for (size_t i = 1; i < arraysize(kSeparators); ++i) {
1217 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); 1238 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]);
1218 } 1239 }
1219 return FilePath(copy); 1240 return FilePath(copy);
1220 #else 1241 #else
1221 return *this; 1242 return *this;
1222 #endif 1243 #endif
1223 } 1244 }
OLDNEW
« 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