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

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: Retry with Green Tree and passing tests 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 // Special cases "", ".", and ".."
165 if (path.empty() || path == FilePath::kCurrentDirectory ||
166 path == FilePath::kParentDirectory) {
167 return true;
168 }
169
170 return false;
171 }
172
162 } // namespace 173 } // namespace
163 174
164 FilePath::FilePath() { 175 FilePath::FilePath() {
165 } 176 }
166 177
167 FilePath::FilePath(const FilePath& that) : path_(that.path_) { 178 FilePath::FilePath(const FilePath& that) : path_(that.path_) {
168 } 179 }
169 180
170 FilePath::FilePath(const StringType& path) : path_(path) { 181 FilePath::FilePath(const StringType& path) : path_(path) {
171 } 182 }
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 if (dot == StringType::npos) 379 if (dot == StringType::npos)
369 return *this; 380 return *this;
370 381
371 return FilePath(path_.substr(0, dot)); 382 return FilePath(path_.substr(0, dot));
372 } 383 }
373 384
374 FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const { 385 FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const {
375 if (suffix.empty()) 386 if (suffix.empty())
376 return FilePath(path_); 387 return FilePath(path_);
377 388
378 if (path_.empty()) 389 if (IsEmptyOrSpecialCase(BaseName().value()))
379 return FilePath(); 390 return FilePath();
380 391
381 StringType base = BaseName().value();
382 if (base.empty())
383 return FilePath();
384 if (*(base.end() - 1) == kExtensionSeparator) {
385 // Special case "." and ".."
386 if (base == kCurrentDirectory || base == kParentDirectory) {
387 return FilePath();
388 }
389 }
390
391 StringType ext = Extension(); 392 StringType ext = Extension();
392 StringType ret = RemoveExtension().value(); 393 StringType ret = RemoveExtension().value();
393 ret.append(suffix); 394 ret.append(suffix);
394 ret.append(ext); 395 ret.append(ext);
395 return FilePath(ret); 396 return FilePath(ret);
396 } 397 }
397 398
398 FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix) 399 FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix)
399 const { 400 const {
400 DCHECK(IsStringASCII(suffix)); 401 DCHECK(IsStringASCII(suffix));
401 #if defined(OS_WIN) 402 #if defined(OS_WIN)
402 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string())); 403 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string()));
403 #elif defined(OS_POSIX) 404 #elif defined(OS_POSIX)
404 return InsertBeforeExtension(suffix.as_string()); 405 return InsertBeforeExtension(suffix.as_string());
405 #endif 406 #endif
406 } 407 }
407 408
408 FilePath FilePath::ReplaceExtension(const StringType& extension) const { 409 FilePath FilePath::AddExtension(const StringType& extension) const {
409 if (path_.empty()) 410 if (IsEmptyOrSpecialCase(BaseName().value()))
410 return FilePath(); 411 return FilePath();
411 412
412 StringType base = BaseName().value(); 413 // If the new extension is "" or ".", then just return the current FilePath.
413 if (base.empty()) 414 if (extension.empty() || extension == StringType(1, kExtensionSeparator))
415 return *this;
416
417 StringType str = path_;
418 if (extension[0] != kExtensionSeparator &&
419 *(str.end() - 1) != kExtensionSeparator) {
420 str.append(1, kExtensionSeparator);
421 }
422 str.append(extension);
423 return FilePath(str);
424 }
425
426 FilePath FilePath::ReplaceExtension(const StringType& extension) const {
427 if (IsEmptyOrSpecialCase(BaseName().value()))
414 return FilePath(); 428 return FilePath();
415 if (*(base.end() - 1) == kExtensionSeparator) {
416 // Special case "." and ".."
417 if (base == kCurrentDirectory || base == kParentDirectory) {
418 return FilePath();
419 }
420 }
421 429
422 FilePath no_ext = RemoveExtension(); 430 FilePath no_ext = RemoveExtension();
423 // If the new extension is "" or ".", then just remove the current extension. 431 // If the new extension is "" or ".", then just remove the current extension.
424 if (extension.empty() || extension == StringType(1, kExtensionSeparator)) 432 if (extension.empty() || extension == StringType(1, kExtensionSeparator))
425 return no_ext; 433 return no_ext;
426 434
427 StringType str = no_ext.value(); 435 StringType str = no_ext.value();
428 if (extension[0] != kExtensionSeparator) 436 if (extension[0] != kExtensionSeparator)
429 str.append(1, kExtensionSeparator); 437 str.append(1, kExtensionSeparator);
430 str.append(extension); 438 str.append(extension);
(...skipping 783 matching lines...) Expand 10 before | Expand all | Expand 10 after
1214 #if defined(FILE_PATH_USES_WIN_SEPARATORS) 1222 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
1215 StringType copy = path_; 1223 StringType copy = path_;
1216 for (size_t i = 1; i < arraysize(kSeparators); ++i) { 1224 for (size_t i = 1; i < arraysize(kSeparators); ++i) {
1217 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); 1225 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]);
1218 } 1226 }
1219 return FilePath(copy); 1227 return FilePath(copy);
1220 #else 1228 #else
1221 return *this; 1229 return *this;
1222 #endif 1230 #endif
1223 } 1231 }
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