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

Side by Side Diff: base/files/file_path.cc

Issue 863583002: Add FilePath::FinalExtension and RemoveFinalExtension (Closed) Base URL: https://chromium.googlesource.com/chromium/mini_chromium@master
Patch Set: simplify Created 5 years, 11 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/files/file_path.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 The Chromium Authors. All rights reserved. 1 // Copyright 2008 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/files/file_path.h" 5 #include "base/files/file_path.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 11
12 namespace base { 12 namespace base {
13 13
14 #if defined(FILE_PATH_USES_WIN_SEPARATORS) 14 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
15 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); 15 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/");
16 #else // FILE_PATH_USES_WIN_SEPARATORS 16 #else // FILE_PATH_USES_WIN_SEPARATORS
17 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); 17 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/");
18 #endif // FILE_PATH_USES_WIN_SEPARATORS 18 #endif // FILE_PATH_USES_WIN_SEPARATORS
19 19
20 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); 20 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL(".");
21 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL("..");
22 const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.');
21 23
22 typedef FilePath::StringType StringType; 24 typedef FilePath::StringType StringType;
23 25
24 namespace { 26 namespace {
25 27
26 const FilePath::CharType kStringTerminator = FILE_PATH_LITERAL('\0'); 28 const FilePath::CharType kStringTerminator = FILE_PATH_LITERAL('\0');
27 29
28 // If this FilePath contains a drive letter specification, returns the 30 // If this FilePath contains a drive letter specification, returns the
29 // position of the last character of the drive letter specification, 31 // position of the last character of the drive letter specification,
30 // otherwise returns npos. This can only be true on Windows, when a pathname 32 // otherwise returns npos. This can only be true on Windows, when a pathname
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 new_path.path_.find_last_of(kSeparators, StringType::npos, 185 new_path.path_.find_last_of(kSeparators, StringType::npos,
184 arraysize(kSeparators) - 1); 186 arraysize(kSeparators) - 1);
185 if (last_separator != StringType::npos && 187 if (last_separator != StringType::npos &&
186 last_separator < new_path.path_.length() - 1) { 188 last_separator < new_path.path_.length() - 1) {
187 new_path.path_.erase(0, last_separator + 1); 189 new_path.path_.erase(0, last_separator + 1);
188 } 190 }
189 191
190 return new_path; 192 return new_path;
191 } 193 }
192 194
195 StringType FilePath::FinalExtension() const {
196 StringType base(BaseName().value());
197 // Special case "." and ".."
198 if (base == FilePath::kCurrentDirectory || base == FilePath::kParentDirectory)
199 return StringType();
200 const StringType::size_type dot = base.rfind(FilePath::kExtensionSeparator);
201 if (dot == StringType::npos)
202 return StringType();
203
204 return base.substr(dot, StringType::npos);
205 }
206
207 FilePath FilePath::RemoveFinalExtension() const {
208 StringType extension = FinalExtension();
209 if (FinalExtension().empty())
210 return *this;
211 return FilePath(path_.substr(0, path_.size() - extension.size()));
212 }
213
193 FilePath FilePath::Append(const StringType& component) const { 214 FilePath FilePath::Append(const StringType& component) const {
194 const StringType* appended = &component; 215 const StringType* appended = &component;
195 StringType without_nuls; 216 StringType without_nuls;
196 217
197 StringType::size_type nul_pos = component.find(kStringTerminator); 218 StringType::size_type nul_pos = component.find(kStringTerminator);
198 if (nul_pos != StringType::npos) { 219 if (nul_pos != StringType::npos) {
199 without_nuls = component.substr(0, nul_pos); 220 without_nuls = component.substr(0, nul_pos);
200 appended = &without_nuls; 221 appended = &without_nuls;
201 } 222 }
202 223
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 last_stripped = pos; 283 last_stripped = pos;
263 } 284 }
264 } 285 }
265 } 286 }
266 287
267 } // namespace base 288 } // namespace base
268 289
269 void PrintTo(const base::FilePath& path, std::ostream* out) { 290 void PrintTo(const base::FilePath& path, std::ostream* out) {
270 *out << path.value().c_str(); 291 *out << path.value().c_str();
271 } 292 }
OLDNEW
« no previous file with comments | « base/files/file_path.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698