blob: b9308989a05ddbaf3c714e419e3253b3e8673d3c [file] [log] [blame]
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
mark@chromium.org6380e262008-10-04 03:21:47 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/file_path.h"
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +09006
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +09007#include <string.h>
8#include <algorithm>
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +09009
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090010#include "base/basictypes.h"
mark@chromium.orgfac7b262008-12-09 01:49:08 +090011#include "base/logging.h"
finnur@chromium.org2a48aca2010-02-03 10:17:26 +090012#include "base/pickle.h"
mark@chromium.org6380e262008-10-04 03:21:47 +090013
evanm@google.com874d1672008-10-31 08:54:04 +090014// These includes are just for the *Hack functions, and should be removed
15// when those functions are removed.
16#include "base/string_piece.h"
aa@chromium.org76e5c632008-12-18 17:38:16 +090017#include "base/string_util.h"
evanm@google.com874d1672008-10-31 08:54:04 +090018#include "base/sys_string_conversions.h"
brettw@chromium.orge0994892010-08-01 02:59:44 +090019#include "base/utf_string_conversions.h"
evanm@google.com874d1672008-10-31 08:54:04 +090020
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +090021#if defined(OS_MACOSX)
brettw@chromium.org3ce2c372010-10-17 13:09:06 +090022#include "base/mac/scoped_cftyperef.h"
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +090023#include "base/third_party/icu/icu_utf.h"
24#endif
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +090025
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090026#if defined(OS_WIN)
27#include <windows.h>
28#elif defined(OS_MACOSX)
29#include <CoreFoundation/CoreFoundation.h>
30#endif
31
mark@chromium.org6380e262008-10-04 03:21:47 +090032#if defined(FILE_PATH_USES_WIN_SEPARATORS)
33const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/");
34#else // FILE_PATH_USES_WIN_SEPARATORS
35const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/");
36#endif // FILE_PATH_USES_WIN_SEPARATORS
37
38const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL(".");
39const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL("..");
40
erikkay@google.com985d4202009-01-10 03:26:19 +090041const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.');
42
estade@chromium.org99a42e72010-07-28 06:24:39 +090043typedef FilePath::StringType StringType;
erikkay@google.com985d4202009-01-10 03:26:19 +090044
mark@chromium.orgfac7b262008-12-09 01:49:08 +090045namespace {
46
estade@chromium.org99a42e72010-07-28 06:24:39 +090047const char* kCommonDoubleExtensions[] = { "gz", "z", "bz2" };
48
mark@chromium.orgfac7b262008-12-09 01:49:08 +090049// If this FilePath contains a drive letter specification, returns the
50// position of the last character of the drive letter specification,
51// otherwise returns npos. This can only be true on Windows, when a pathname
52// begins with a letter followed by a colon. On other platforms, this always
53// returns npos.
estade@chromium.org99a42e72010-07-28 06:24:39 +090054StringType::size_type FindDriveLetter(const StringType& path) {
mark@chromium.orgfac7b262008-12-09 01:49:08 +090055#if defined(FILE_PATH_USES_DRIVE_LETTERS)
56 // This is dependent on an ASCII-based character set, but that's a
57 // reasonable assumption. iswalpha can be too inclusive here.
58 if (path.length() >= 2 && path[1] == L':' &&
59 ((path[0] >= L'A' && path[0] <= L'Z') ||
60 (path[0] >= L'a' && path[0] <= L'z'))) {
61 return 1;
62 }
mark@chromium.orgfac7b262008-12-09 01:49:08 +090063#endif // FILE_PATH_USES_DRIVE_LETTERS
estade@chromium.org99a42e72010-07-28 06:24:39 +090064 return StringType::npos;
mark@chromium.orgfac7b262008-12-09 01:49:08 +090065}
66
rafaelw@chromium.org724cf542009-07-01 10:03:34 +090067#if defined(FILE_PATH_USES_DRIVE_LETTERS)
finnur@chromium.orgdd436eb2011-03-28 19:45:25 +090068bool EqualDriveLetterCaseInsensitive(const StringType& a,
69 const StringType& b) {
rafaelw@chromium.org724cf542009-07-01 10:03:34 +090070 size_t a_letter_pos = FindDriveLetter(a);
71 size_t b_letter_pos = FindDriveLetter(b);
72
estade@chromium.org99a42e72010-07-28 06:24:39 +090073 if (a_letter_pos == StringType::npos || b_letter_pos == StringType::npos)
rafaelw@chromium.org724cf542009-07-01 10:03:34 +090074 return a == b;
75
estade@chromium.org99a42e72010-07-28 06:24:39 +090076 StringType a_letter(a.substr(0, a_letter_pos + 1));
77 StringType b_letter(b.substr(0, b_letter_pos + 1));
rafaelw@chromium.org724cf542009-07-01 10:03:34 +090078 if (!StartsWith(a_letter, b_letter, false))
79 return false;
80
estade@chromium.org99a42e72010-07-28 06:24:39 +090081 StringType a_rest(a.substr(a_letter_pos + 1));
82 StringType b_rest(b.substr(b_letter_pos + 1));
rafaelw@chromium.org724cf542009-07-01 10:03:34 +090083 return a_rest == b_rest;
84}
85#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
86
estade@chromium.org99a42e72010-07-28 06:24:39 +090087bool IsPathAbsolute(const StringType& path) {
mark@chromium.orgfac7b262008-12-09 01:49:08 +090088#if defined(FILE_PATH_USES_DRIVE_LETTERS)
estade@chromium.org99a42e72010-07-28 06:24:39 +090089 StringType::size_type letter = FindDriveLetter(path);
90 if (letter != StringType::npos) {
mark@chromium.orgfac7b262008-12-09 01:49:08 +090091 // Look for a separator right after the drive specification.
92 return path.length() > letter + 1 &&
93 FilePath::IsSeparator(path[letter + 1]);
94 }
95 // Look for a pair of leading separators.
96 return path.length() > 1 &&
97 FilePath::IsSeparator(path[0]) && FilePath::IsSeparator(path[1]);
98#else // FILE_PATH_USES_DRIVE_LETTERS
99 // Look for a separator in the first position.
100 return path.length() > 0 && FilePath::IsSeparator(path[0]);
101#endif // FILE_PATH_USES_DRIVE_LETTERS
102}
103
estade@chromium.org99a42e72010-07-28 06:24:39 +0900104bool AreAllSeparators(const StringType& input) {
105 for (StringType::const_iterator it = input.begin();
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900106 it != input.end(); ++it) {
107 if (!FilePath::IsSeparator(*it))
108 return false;
109 }
110
111 return true;
112}
113
estade@chromium.org99a42e72010-07-28 06:24:39 +0900114// Find the position of the '.' that separates the extension from the rest
115// of the file name. The position is relative to BaseName(), not value().
116// This allows a second extension component of up to 4 characters when the
117// rightmost extension component is a common double extension (gz, bz2, Z).
118// For example, foo.tar.gz or foo.tar.Z would have extension components of
119// '.tar.gz' and '.tar.Z' respectively. Returns npos if it can't find an
120// extension.
121StringType::size_type ExtensionSeparatorPosition(const StringType& path) {
122 // Special case "." and ".."
123 if (path == FilePath::kCurrentDirectory || path == FilePath::kParentDirectory)
124 return StringType::npos;
125
126 const StringType::size_type last_dot =
127 path.rfind(FilePath::kExtensionSeparator);
128
129 // No extension, or the extension is the whole filename.
130 if (last_dot == StringType::npos || last_dot == 0U)
131 return last_dot;
132
133 // Special case .<extension1>.<extension2>, but only if the final extension
134 // is one of a few common double extensions.
135 StringType extension(path, last_dot + 1);
136 bool is_common_double_extension = false;
137 for (size_t i = 0; i < arraysize(kCommonDoubleExtensions); ++i) {
138 if (LowerCaseEqualsASCII(extension, kCommonDoubleExtensions[i]))
139 is_common_double_extension = true;
140 }
141 if (!is_common_double_extension)
142 return last_dot;
143
144 // Check that <extension1> is 1-4 characters, otherwise fall back to
145 // <extension2>.
146 const StringType::size_type penultimate_dot =
147 path.rfind(FilePath::kExtensionSeparator, last_dot - 1);
148 const StringType::size_type last_separator =
149 path.find_last_of(FilePath::kSeparators, last_dot - 1,
150 arraysize(FilePath::kSeparators) - 1);
151 if (penultimate_dot != StringType::npos &&
152 (last_separator == StringType::npos ||
153 penultimate_dot > last_separator) &&
154 last_dot - penultimate_dot <= 5U &&
155 last_dot - penultimate_dot > 1U) {
156 return penultimate_dot;
157 }
158
159 return last_dot;
160}
161
rdevlin.cronin@chromium.org9a980f72012-04-25 10:53:44 +0900162// Returns true if path is "", ".", or "..".
163bool 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
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900173} // namespace
174
erg@chromium.orga3644722010-07-17 02:22:49 +0900175FilePath::FilePath() {
176}
177
178FilePath::FilePath(const FilePath& that) : path_(that.path_) {
179}
180
181FilePath::FilePath(const StringType& path) : path_(path) {
182}
183
184FilePath::~FilePath() {
185}
186
187FilePath& FilePath::operator=(const FilePath& that) {
188 path_ = that.path_;
189 return *this;
190}
191
erg@google.com67a25432011-01-08 05:23:43 +0900192bool FilePath::operator==(const FilePath& that) const {
193#if defined(FILE_PATH_USES_DRIVE_LETTERS)
194 return EqualDriveLetterCaseInsensitive(this->path_, that.path_);
195#else // defined(FILE_PATH_USES_DRIVE_LETTERS)
196 return path_ == that.path_;
197#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
198}
199
200bool FilePath::operator!=(const FilePath& that) const {
201#if defined(FILE_PATH_USES_DRIVE_LETTERS)
202 return !EqualDriveLetterCaseInsensitive(this->path_, that.path_);
203#else // defined(FILE_PATH_USES_DRIVE_LETTERS)
204 return path_ != that.path_;
205#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
206}
207
208// static
estade@chromium.org97e37822008-11-27 13:03:57 +0900209bool FilePath::IsSeparator(CharType character) {
210 for (size_t i = 0; i < arraysize(kSeparators) - 1; ++i) {
211 if (character == kSeparators[i]) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900212 return true;
213 }
214 }
215
216 return false;
217}
218
estade@chromium.org99a42e72010-07-28 06:24:39 +0900219void FilePath::GetComponents(std::vector<StringType>* components) const {
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900220 DCHECK(components);
221 if (!components)
222 return;
223 components->clear();
224 if (value().empty())
225 return;
226
estade@chromium.org99a42e72010-07-28 06:24:39 +0900227 std::vector<StringType> ret_val;
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900228 FilePath current = *this;
229 FilePath base;
230
231 // Capture path components.
232 while (current != current.DirName()) {
233 base = current.BaseName();
234 if (!AreAllSeparators(base.value()))
235 ret_val.push_back(base.value());
236 current = current.DirName();
237 }
238
239 // Capture root, if any.
240 base = current.BaseName();
241 if (!base.value().empty() && base.value() != kCurrentDirectory)
242 ret_val.push_back(current.BaseName().value());
243
244 // Capture drive letter, if any.
245 FilePath dir = current.DirName();
246 StringType::size_type letter = FindDriveLetter(dir.value());
estade@chromium.org99a42e72010-07-28 06:24:39 +0900247 if (letter != StringType::npos) {
248 ret_val.push_back(StringType(dir.value(), 0, letter + 1));
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900249 }
250
estade@chromium.org99a42e72010-07-28 06:24:39 +0900251 *components = std::vector<StringType>(ret_val.rbegin(), ret_val.rend());
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900252}
253
254bool FilePath::IsParent(const FilePath& child) const {
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900255 return AppendRelativePath(child, NULL);
256}
257
258bool FilePath::AppendRelativePath(const FilePath& child,
259 FilePath* path) const {
estade@chromium.org99a42e72010-07-28 06:24:39 +0900260 std::vector<StringType> parent_components;
261 std::vector<StringType> child_components;
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900262 GetComponents(&parent_components);
263 child.GetComponents(&child_components);
264
erg@google.com24e9fac2011-03-05 06:03:47 +0900265 if (parent_components.empty() ||
266 parent_components.size() >= child_components.size())
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900267 return false;
268
estade@chromium.org99a42e72010-07-28 06:24:39 +0900269 std::vector<StringType>::const_iterator parent_comp =
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900270 parent_components.begin();
estade@chromium.org99a42e72010-07-28 06:24:39 +0900271 std::vector<StringType>::const_iterator child_comp =
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900272 child_components.begin();
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900273
274#if defined(FILE_PATH_USES_DRIVE_LETTERS)
275 // Windows can access case sensitive filesystems, so component
276 // comparisions must be case sensitive, but drive letters are
277 // never case sensitive.
estade@chromium.org99a42e72010-07-28 06:24:39 +0900278 if ((FindDriveLetter(*parent_comp) != StringType::npos) &&
279 (FindDriveLetter(*child_comp) != StringType::npos)) {
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900280 if (!StartsWith(*parent_comp, *child_comp, false))
281 return false;
282 ++parent_comp;
283 ++child_comp;
284 }
285#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
286
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900287 while (parent_comp != parent_components.end()) {
288 if (*parent_comp != *child_comp)
289 return false;
290 ++parent_comp;
291 ++child_comp;
292 }
293
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900294 if (path != NULL) {
295 for (; child_comp != child_components.end(); ++child_comp) {
296 *path = path->Append(*child_comp);
297 }
298 }
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900299 return true;
300}
301
mark@chromium.org6380e262008-10-04 03:21:47 +0900302// libgen's dirname and basename aren't guaranteed to be thread-safe and aren't
avi@google.com84f04ea2009-01-07 01:48:45 +0900303// guaranteed to not modify their input strings, and in fact are implemented
mark@chromium.org6380e262008-10-04 03:21:47 +0900304// differently in this regard on different platforms. Don't use them, but
305// adhere to their behavior.
306FilePath FilePath::DirName() const {
307 FilePath new_path(path_);
avi@google.com5cb79352008-12-11 23:55:12 +0900308 new_path.StripTrailingSeparatorsInternal();
mark@chromium.org6380e262008-10-04 03:21:47 +0900309
310 // The drive letter, if any, always needs to remain in the output. If there
311 // is no drive letter, as will always be the case on platforms which do not
312 // support drive letters, letter will be npos, or -1, so the comparisons and
313 // resizes below using letter will still be valid.
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900314 StringType::size_type letter = FindDriveLetter(new_path.path_);
mark@chromium.org6380e262008-10-04 03:21:47 +0900315
316 StringType::size_type last_separator =
317 new_path.path_.find_last_of(kSeparators, StringType::npos,
318 arraysize(kSeparators) - 1);
319 if (last_separator == StringType::npos) {
320 // path_ is in the current directory.
321 new_path.path_.resize(letter + 1);
322 } else if (last_separator == letter + 1) {
323 // path_ is in the root directory.
324 new_path.path_.resize(letter + 2);
325 } else if (last_separator == letter + 2 &&
326 IsSeparator(new_path.path_[letter + 1])) {
327 // path_ is in "//" (possibly with a drive letter); leave the double
328 // separator intact indicating alternate root.
329 new_path.path_.resize(letter + 3);
330 } else if (last_separator != 0) {
331 // path_ is somewhere else, trim the basename.
332 new_path.path_.resize(last_separator);
333 }
334
avi@google.com5cb79352008-12-11 23:55:12 +0900335 new_path.StripTrailingSeparatorsInternal();
mark@chromium.org6380e262008-10-04 03:21:47 +0900336 if (!new_path.path_.length())
337 new_path.path_ = kCurrentDirectory;
338
339 return new_path;
340}
341
estade@chromium.orgd872d682009-01-06 08:59:36 +0900342FilePath FilePath::BaseName() const {
mark@chromium.org6380e262008-10-04 03:21:47 +0900343 FilePath new_path(path_);
avi@google.com5cb79352008-12-11 23:55:12 +0900344 new_path.StripTrailingSeparatorsInternal();
mark@chromium.org6380e262008-10-04 03:21:47 +0900345
346 // The drive letter, if any, is always stripped.
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900347 StringType::size_type letter = FindDriveLetter(new_path.path_);
mark@chromium.org6380e262008-10-04 03:21:47 +0900348 if (letter != StringType::npos) {
349 new_path.path_.erase(0, letter + 1);
350 }
351
352 // Keep everything after the final separator, but if the pathname is only
353 // one character and it's a separator, leave it alone.
354 StringType::size_type last_separator =
355 new_path.path_.find_last_of(kSeparators, StringType::npos,
356 arraysize(kSeparators) - 1);
357 if (last_separator != StringType::npos &&
358 last_separator < new_path.path_.length() - 1) {
359 new_path.path_.erase(0, last_separator + 1);
360 }
361
estade@chromium.orgd872d682009-01-06 08:59:36 +0900362 return new_path;
mark@chromium.org6380e262008-10-04 03:21:47 +0900363}
364
estade@chromium.org99a42e72010-07-28 06:24:39 +0900365StringType FilePath::Extension() const {
366 FilePath base(BaseName());
367 const StringType::size_type dot = ExtensionSeparatorPosition(base.path_);
368 if (dot == StringType::npos)
erikkay@google.com985d4202009-01-10 03:26:19 +0900369 return StringType();
370
estade@chromium.org99a42e72010-07-28 06:24:39 +0900371 return base.path_.substr(dot, StringType::npos);
erikkay@google.com985d4202009-01-10 03:26:19 +0900372}
373
374FilePath FilePath::RemoveExtension() const {
estade@chromium.org99a42e72010-07-28 06:24:39 +0900375 if (Extension().empty())
376 return *this;
377
378 const StringType::size_type dot = ExtensionSeparatorPosition(path_);
379 if (dot == StringType::npos)
380 return *this;
381
382 return FilePath(path_.substr(0, dot));
erikkay@google.com985d4202009-01-10 03:26:19 +0900383}
384
385FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const {
386 if (suffix.empty())
387 return FilePath(path_);
388
rdevlin.cronin@chromium.org9a980f72012-04-25 10:53:44 +0900389 if (IsEmptyOrSpecialCase(BaseName().value()))
erikkay@google.com985d4202009-01-10 03:26:19 +0900390 return FilePath();
391
erikkay@google.com985d4202009-01-10 03:26:19 +0900392 StringType ext = Extension();
393 StringType ret = RemoveExtension().value();
394 ret.append(suffix);
395 ret.append(ext);
396 return FilePath(ret);
397}
398
tony@chromium.orgb84e9bd2009-09-11 06:08:39 +0900399FilePath FilePath::InsertBeforeExtensionASCII(const base::StringPiece& suffix)
400 const {
estade@chromium.orgaca97492009-08-13 09:26:58 +0900401 DCHECK(IsStringASCII(suffix));
402#if defined(OS_WIN)
brettw@chromium.org8eeabf02010-08-01 03:07:53 +0900403 return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string()));
estade@chromium.orgaca97492009-08-13 09:26:58 +0900404#elif defined(OS_POSIX)
405 return InsertBeforeExtension(suffix.as_string());
406#endif
407}
408
rdevlin.cronin@chromium.org9a980f72012-04-25 10:53:44 +0900409FilePath FilePath::AddExtension(const StringType& extension) const {
410 if (IsEmptyOrSpecialCase(BaseName().value()))
erikkay@google.com985d4202009-01-10 03:26:19 +0900411 return FilePath();
412
rdevlin.cronin@chromium.org9a980f72012-04-25 10:53:44 +0900413 // If the new extension is "" or ".", then just return the current FilePath.
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);
erikkay@google.com985d4202009-01-10 03:26:19 +0900421 }
rdevlin.cronin@chromium.org9a980f72012-04-25 10:53:44 +0900422 str.append(extension);
423 return FilePath(str);
424}
425
426FilePath FilePath::ReplaceExtension(const StringType& extension) const {
427 if (IsEmptyOrSpecialCase(BaseName().value()))
428 return FilePath();
erikkay@google.com985d4202009-01-10 03:26:19 +0900429
430 FilePath no_ext = RemoveExtension();
431 // If the new extension is "" or ".", then just remove the current extension.
432 if (extension.empty() || extension == StringType(1, kExtensionSeparator))
433 return no_ext;
434
435 StringType str = no_ext.value();
436 if (extension[0] != kExtensionSeparator)
437 str.append(1, kExtensionSeparator);
438 str.append(extension);
439 return FilePath(str);
440}
441
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900442bool FilePath::MatchesExtension(const StringType& extension) const {
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900443 DCHECK(extension.empty() || extension[0] == kExtensionSeparator);
444
estade@chromium.org99a42e72010-07-28 06:24:39 +0900445 StringType current_extension = Extension();
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900446
447 if (current_extension.length() != extension.length())
448 return false;
449
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900450 return FilePath::CompareEqualIgnoreCase(extension, current_extension);
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900451}
452
erikkay@google.com985d4202009-01-10 03:26:19 +0900453FilePath FilePath::Append(const StringType& component) const {
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900454 DCHECK(!IsPathAbsolute(component));
mark@chromium.org6380e262008-10-04 03:21:47 +0900455 if (path_.compare(kCurrentDirectory) == 0) {
456 // Append normally doesn't do any normalization, but as a special case,
457 // when appending to kCurrentDirectory, just return a new path for the
458 // component argument. Appending component to kCurrentDirectory would
459 // serve no purpose other than needlessly lengthening the path, and
460 // it's likely in practice to wind up with FilePath objects containing
461 // only kCurrentDirectory when calling DirName on a single relative path
462 // component.
463 return FilePath(component);
464 }
465
466 FilePath new_path(path_);
avi@google.com5cb79352008-12-11 23:55:12 +0900467 new_path.StripTrailingSeparatorsInternal();
mark@chromium.org6380e262008-10-04 03:21:47 +0900468
469 // Don't append a separator if the path is empty (indicating the current
470 // directory) or if the path component is empty (indicating nothing to
471 // append).
472 if (component.length() > 0 && new_path.path_.length() > 0) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900473 // Don't append a separator if the path still ends with a trailing
474 // separator after stripping (indicating the root directory).
475 if (!IsSeparator(new_path.path_[new_path.path_.length() - 1])) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900476 // Don't append a separator if the path is just a drive letter.
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900477 if (FindDriveLetter(new_path.path_) + 1 != new_path.path_.length()) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900478 new_path.path_.append(1, kSeparators[0]);
479 }
480 }
481 }
482
483 new_path.path_.append(component);
484 return new_path;
485}
486
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900487FilePath FilePath::Append(const FilePath& component) const {
488 return Append(component.value());
mark@chromium.org6380e262008-10-04 03:21:47 +0900489}
490
tony@chromium.orgb84e9bd2009-09-11 06:08:39 +0900491FilePath FilePath::AppendASCII(const base::StringPiece& component) const {
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900492 DCHECK(IsStringASCII(component));
493#if defined(OS_WIN)
brettw@chromium.org8eeabf02010-08-01 03:07:53 +0900494 return Append(ASCIIToUTF16(component.as_string()));
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900495#elif defined(OS_POSIX)
deanm@chromium.org043b6462009-05-29 22:11:36 +0900496 return Append(component.as_string());
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900497#endif
498}
499
mark@chromium.org6380e262008-10-04 03:21:47 +0900500bool FilePath::IsAbsolute() const {
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900501 return IsPathAbsolute(path_);
mark@chromium.org6380e262008-10-04 03:21:47 +0900502}
503
erg@google.com67a25432011-01-08 05:23:43 +0900504FilePath FilePath::StripTrailingSeparators() const {
505 FilePath new_path(path_);
506 new_path.StripTrailingSeparatorsInternal();
507
508 return new_path;
509}
510
511bool FilePath::ReferencesParent() const {
512 std::vector<StringType> components;
513 GetComponents(&components);
514
515 std::vector<StringType>::const_iterator it = components.begin();
516 for (; it != components.end(); ++it) {
517 const StringType& component = *it;
518 if (component == kParentDirectory)
519 return true;
520 }
521 return false;
522}
523
524#if defined(OS_POSIX)
erg@google.com67a25432011-01-08 05:23:43 +0900525// See file_path.h for a discussion of the encoding of paths on POSIX
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900526// platforms. These encoding conversion functions are not quite correct.
527
528string16 FilePath::LossyDisplayName() const {
529 return WideToUTF16(base::SysNativeMBToWide(path_));
530}
531
evan@chromium.orgfc31f792011-03-02 06:38:51 +0900532std::string FilePath::MaybeAsASCII() const {
533 if (IsStringASCII(path_))
534 return path_;
535 return "";
536}
537
satorux@chromium.orgefba4172011-11-02 13:55:23 +0900538std::string FilePath::AsUTF8Unsafe() const {
539#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
540 return value();
541#else
542 return WideToUTF8(base::SysNativeMBToWide(value()));
543#endif
544}
545
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900546// The *Hack functions are temporary while we fix the remainder of the code.
erg@google.com67a25432011-01-08 05:23:43 +0900547// Remember to remove the #includes at the top when you remove these.
548
549// static
550FilePath FilePath::FromWStringHack(const std::wstring& wstring) {
551 return FilePath(base::SysWideToNativeMB(wstring));
552}
satorux@chromium.orgefba4172011-11-02 13:55:23 +0900553
554// static
555FilePath FilePath::FromUTF8Unsafe(const std::string& utf8) {
556#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
557 return FilePath(utf8);
558#else
559 return FilePath(base::SysWideToNativeMB(UTF8ToWide(utf8)));
560#endif
561}
562
erg@google.com67a25432011-01-08 05:23:43 +0900563#elif defined(OS_WIN)
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900564string16 FilePath::LossyDisplayName() const {
565 return path_;
566}
567
evan@chromium.orgfc31f792011-03-02 06:38:51 +0900568std::string FilePath::MaybeAsASCII() const {
569 if (IsStringASCII(path_))
570 return WideToASCII(path_);
571 return "";
572}
573
satorux@chromium.orgefba4172011-11-02 13:55:23 +0900574std::string FilePath::AsUTF8Unsafe() const {
575 return WideToUTF8(value());
576}
577
erg@google.com67a25432011-01-08 05:23:43 +0900578// static
579FilePath FilePath::FromWStringHack(const std::wstring& wstring) {
580 return FilePath(wstring);
581}
satorux@chromium.orgefba4172011-11-02 13:55:23 +0900582
583// static
584FilePath FilePath::FromUTF8Unsafe(const std::string& utf8) {
585 return FilePath(UTF8ToWide(utf8));
586}
erg@google.com67a25432011-01-08 05:23:43 +0900587#endif
588
erg@google.com67a25432011-01-08 05:23:43 +0900589void FilePath::WriteToPickle(Pickle* pickle) {
evan@chromium.org62ff6cc2011-06-01 03:04:33 +0900590#if defined(OS_WIN)
591 pickle->WriteString16(path_);
592#else
593 pickle->WriteString(path_);
594#endif
erg@google.com67a25432011-01-08 05:23:43 +0900595}
596
jbates@chromium.org0fc87362012-03-08 05:42:56 +0900597bool FilePath::ReadFromPickle(PickleIterator* iter) {
evan@chromium.org62ff6cc2011-06-01 03:04:33 +0900598#if defined(OS_WIN)
jbates@chromium.org0fc87362012-03-08 05:42:56 +0900599 if (!iter->ReadString16(&path_))
evan@chromium.org62ff6cc2011-06-01 03:04:33 +0900600 return false;
601#else
jbates@chromium.org0fc87362012-03-08 05:42:56 +0900602 if (!iter->ReadString(&path_))
evan@chromium.org62ff6cc2011-06-01 03:04:33 +0900603 return false;
604#endif
605
606 return true;
erg@google.com67a25432011-01-08 05:23:43 +0900607}
608
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900609#if defined(OS_WIN)
610// Windows specific implementation of file string comparisons
611
612int FilePath::CompareIgnoreCase(const StringType& string1,
613 const StringType& string2) {
614 // Perform character-wise upper case comparison rather than using the
615 // fully Unicode-aware CompareString(). For details see:
616 // http://blogs.msdn.com/michkap/archive/2005/10/17/481600.aspx
617 StringType::const_iterator i1 = string1.begin();
618 StringType::const_iterator i2 = string2.begin();
619 StringType::const_iterator string1end = string1.end();
620 StringType::const_iterator string2end = string2.end();
621 for ( ; i1 != string1end && i2 != string2end; ++i1, ++i2) {
622 wchar_t c1 = (wchar_t)LOWORD(::CharUpperW((LPWSTR)MAKELONG(*i1, 0)));
623 wchar_t c2 = (wchar_t)LOWORD(::CharUpperW((LPWSTR)MAKELONG(*i2, 0)));
624 if (c1 < c2)
625 return -1;
626 if (c1 > c2)
627 return 1;
628 }
629 if (i1 != string1end)
630 return 1;
631 if (i2 != string2end)
632 return -1;
633 return 0;
634}
635
636#elif defined(OS_MACOSX)
637// Mac OS X specific implementation of file string comparisons
638
639// cf. http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSubtleties
640//
641// "When using CreateTextEncoding to create a text encoding, you should set
642// the TextEncodingBase to kTextEncodingUnicodeV2_0, set the
643// TextEncodingVariant to kUnicodeCanonicalDecompVariant, and set the
644// TextEncodingFormat to kUnicode16BitFormat. Using these values ensures that
645// the Unicode will be in the same form as on an HFS Plus volume, even as the
646// Unicode standard evolves."
647//
648// Another technical article for X 10.4 updates this: one should use
649// the new (unambiguous) kUnicodeHFSPlusDecompVariant.
650// cf. http://developer.apple.com/mac/library/releasenotes/TextFonts/RN-TEC/index.html
651//
652// This implementation uses CFStringGetFileSystemRepresentation() to get the
653// decomposed form, and an adapted version of the FastUnicodeCompare as
654// described in the tech note to compare the strings.
655
656// Character conversion table for FastUnicodeCompare()
657//
658// The lower case table consists of a 256-entry high-byte table followed by
659// some number of 256-entry subtables. The high-byte table contains either an
660// offset to the subtable for characters with that high byte or zero, which
661// means that there are no case mappings or ignored characters in that block.
662// Ignored characters are mapped to zero.
663//
664// cf. downloadable file linked in
665// http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringComparisonAlgorithm
666
667namespace {
668
669const UInt16 lower_case_table[] = {
670 // High-byte indices ( == 0 iff no case mapping and no ignorables )
671
672 /* 0 */ 0x0100, 0x0200, 0x0000, 0x0300, 0x0400, 0x0500, 0x0000, 0x0000,
673 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
674 /* 1 */ 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
675 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
676 /* 2 */ 0x0700, 0x0800, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
677 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
678 /* 3 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
679 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
680 /* 4 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
681 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
682 /* 5 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
683 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
684 /* 6 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
685 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
686 /* 7 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
687 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
688 /* 8 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
689 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
690 /* 9 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
691 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
692 /* A */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
693 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
694 /* B */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
695 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
696 /* C */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
697 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
698 /* D */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
699 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
700 /* E */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
701 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
702 /* F */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
703 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0900, 0x0A00,
704
705 // Table 1 (for high byte 0x00)
706
707 /* 0 */ 0xFFFF, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
708 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
709 /* 1 */ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
710 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
711 /* 2 */ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
712 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
713 /* 3 */ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
714 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
715 /* 4 */ 0x0040, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
716 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
717 /* 5 */ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
718 0x0078, 0x0079, 0x007A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
719 /* 6 */ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
720 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
721 /* 7 */ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
722 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
723 /* 8 */ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
724 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
725 /* 9 */ 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
726 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
727 /* A */ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
728 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
729 /* B */ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
730 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
731 /* C */ 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00E6, 0x00C7,
732 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
733 /* D */ 0x00F0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
734 0x00F8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00FE, 0x00DF,
735 /* E */ 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
736 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
737 /* F */ 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
738 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF,
739
740 // Table 2 (for high byte 0x01)
741
742 /* 0 */ 0x0100, 0x0101, 0x0102, 0x0103, 0x0104, 0x0105, 0x0106, 0x0107,
743 0x0108, 0x0109, 0x010A, 0x010B, 0x010C, 0x010D, 0x010E, 0x010F,
744 /* 1 */ 0x0111, 0x0111, 0x0112, 0x0113, 0x0114, 0x0115, 0x0116, 0x0117,
745 0x0118, 0x0119, 0x011A, 0x011B, 0x011C, 0x011D, 0x011E, 0x011F,
746 /* 2 */ 0x0120, 0x0121, 0x0122, 0x0123, 0x0124, 0x0125, 0x0127, 0x0127,
747 0x0128, 0x0129, 0x012A, 0x012B, 0x012C, 0x012D, 0x012E, 0x012F,
748 /* 3 */ 0x0130, 0x0131, 0x0133, 0x0133, 0x0134, 0x0135, 0x0136, 0x0137,
749 0x0138, 0x0139, 0x013A, 0x013B, 0x013C, 0x013D, 0x013E, 0x0140,
750 /* 4 */ 0x0140, 0x0142, 0x0142, 0x0143, 0x0144, 0x0145, 0x0146, 0x0147,
751 0x0148, 0x0149, 0x014B, 0x014B, 0x014C, 0x014D, 0x014E, 0x014F,
752 /* 5 */ 0x0150, 0x0151, 0x0153, 0x0153, 0x0154, 0x0155, 0x0156, 0x0157,
753 0x0158, 0x0159, 0x015A, 0x015B, 0x015C, 0x015D, 0x015E, 0x015F,
754 /* 6 */ 0x0160, 0x0161, 0x0162, 0x0163, 0x0164, 0x0165, 0x0167, 0x0167,
755 0x0168, 0x0169, 0x016A, 0x016B, 0x016C, 0x016D, 0x016E, 0x016F,
756 /* 7 */ 0x0170, 0x0171, 0x0172, 0x0173, 0x0174, 0x0175, 0x0176, 0x0177,
757 0x0178, 0x0179, 0x017A, 0x017B, 0x017C, 0x017D, 0x017E, 0x017F,
758 /* 8 */ 0x0180, 0x0253, 0x0183, 0x0183, 0x0185, 0x0185, 0x0254, 0x0188,
759 0x0188, 0x0256, 0x0257, 0x018C, 0x018C, 0x018D, 0x01DD, 0x0259,
760 /* 9 */ 0x025B, 0x0192, 0x0192, 0x0260, 0x0263, 0x0195, 0x0269, 0x0268,
761 0x0199, 0x0199, 0x019A, 0x019B, 0x026F, 0x0272, 0x019E, 0x0275,
762 /* A */ 0x01A0, 0x01A1, 0x01A3, 0x01A3, 0x01A5, 0x01A5, 0x01A6, 0x01A8,
763 0x01A8, 0x0283, 0x01AA, 0x01AB, 0x01AD, 0x01AD, 0x0288, 0x01AF,
764 /* B */ 0x01B0, 0x028A, 0x028B, 0x01B4, 0x01B4, 0x01B6, 0x01B6, 0x0292,
765 0x01B9, 0x01B9, 0x01BA, 0x01BB, 0x01BD, 0x01BD, 0x01BE, 0x01BF,
766 /* C */ 0x01C0, 0x01C1, 0x01C2, 0x01C3, 0x01C6, 0x01C6, 0x01C6, 0x01C9,
767 0x01C9, 0x01C9, 0x01CC, 0x01CC, 0x01CC, 0x01CD, 0x01CE, 0x01CF,
768 /* D */ 0x01D0, 0x01D1, 0x01D2, 0x01D3, 0x01D4, 0x01D5, 0x01D6, 0x01D7,
769 0x01D8, 0x01D9, 0x01DA, 0x01DB, 0x01DC, 0x01DD, 0x01DE, 0x01DF,
770 /* E */ 0x01E0, 0x01E1, 0x01E2, 0x01E3, 0x01E5, 0x01E5, 0x01E6, 0x01E7,
771 0x01E8, 0x01E9, 0x01EA, 0x01EB, 0x01EC, 0x01ED, 0x01EE, 0x01EF,
772 /* F */ 0x01F0, 0x01F3, 0x01F3, 0x01F3, 0x01F4, 0x01F5, 0x01F6, 0x01F7,
773 0x01F8, 0x01F9, 0x01FA, 0x01FB, 0x01FC, 0x01FD, 0x01FE, 0x01FF,
774
775 // Table 3 (for high byte 0x03)
776
777 /* 0 */ 0x0300, 0x0301, 0x0302, 0x0303, 0x0304, 0x0305, 0x0306, 0x0307,
778 0x0308, 0x0309, 0x030A, 0x030B, 0x030C, 0x030D, 0x030E, 0x030F,
779 /* 1 */ 0x0310, 0x0311, 0x0312, 0x0313, 0x0314, 0x0315, 0x0316, 0x0317,
780 0x0318, 0x0319, 0x031A, 0x031B, 0x031C, 0x031D, 0x031E, 0x031F,
781 /* 2 */ 0x0320, 0x0321, 0x0322, 0x0323, 0x0324, 0x0325, 0x0326, 0x0327,
782 0x0328, 0x0329, 0x032A, 0x032B, 0x032C, 0x032D, 0x032E, 0x032F,
783 /* 3 */ 0x0330, 0x0331, 0x0332, 0x0333, 0x0334, 0x0335, 0x0336, 0x0337,
784 0x0338, 0x0339, 0x033A, 0x033B, 0x033C, 0x033D, 0x033E, 0x033F,
785 /* 4 */ 0x0340, 0x0341, 0x0342, 0x0343, 0x0344, 0x0345, 0x0346, 0x0347,
786 0x0348, 0x0349, 0x034A, 0x034B, 0x034C, 0x034D, 0x034E, 0x034F,
787 /* 5 */ 0x0350, 0x0351, 0x0352, 0x0353, 0x0354, 0x0355, 0x0356, 0x0357,
788 0x0358, 0x0359, 0x035A, 0x035B, 0x035C, 0x035D, 0x035E, 0x035F,
789 /* 6 */ 0x0360, 0x0361, 0x0362, 0x0363, 0x0364, 0x0365, 0x0366, 0x0367,
790 0x0368, 0x0369, 0x036A, 0x036B, 0x036C, 0x036D, 0x036E, 0x036F,
791 /* 7 */ 0x0370, 0x0371, 0x0372, 0x0373, 0x0374, 0x0375, 0x0376, 0x0377,
792 0x0378, 0x0379, 0x037A, 0x037B, 0x037C, 0x037D, 0x037E, 0x037F,
793 /* 8 */ 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0386, 0x0387,
794 0x0388, 0x0389, 0x038A, 0x038B, 0x038C, 0x038D, 0x038E, 0x038F,
795 /* 9 */ 0x0390, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7,
796 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
797 /* A */ 0x03C0, 0x03C1, 0x03A2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,
798 0x03C8, 0x03C9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF,
799 /* B */ 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7,
800 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
801 /* C */ 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,
802 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, 0x03CF,
803 /* D */ 0x03D0, 0x03D1, 0x03D2, 0x03D3, 0x03D4, 0x03D5, 0x03D6, 0x03D7,
804 0x03D8, 0x03D9, 0x03DA, 0x03DB, 0x03DC, 0x03DD, 0x03DE, 0x03DF,
805 /* E */ 0x03E0, 0x03E1, 0x03E3, 0x03E3, 0x03E5, 0x03E5, 0x03E7, 0x03E7,
806 0x03E9, 0x03E9, 0x03EB, 0x03EB, 0x03ED, 0x03ED, 0x03EF, 0x03EF,
807 /* F */ 0x03F0, 0x03F1, 0x03F2, 0x03F3, 0x03F4, 0x03F5, 0x03F6, 0x03F7,
808 0x03F8, 0x03F9, 0x03FA, 0x03FB, 0x03FC, 0x03FD, 0x03FE, 0x03FF,
809
810 // Table 4 (for high byte 0x04)
811
812 /* 0 */ 0x0400, 0x0401, 0x0452, 0x0403, 0x0454, 0x0455, 0x0456, 0x0407,
813 0x0458, 0x0459, 0x045A, 0x045B, 0x040C, 0x040D, 0x040E, 0x045F,
814 /* 1 */ 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
815 0x0438, 0x0419, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
816 /* 2 */ 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
817 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
818 /* 3 */ 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
819 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
820 /* 4 */ 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
821 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
822 /* 5 */ 0x0450, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457,
823 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x045D, 0x045E, 0x045F,
824 /* 6 */ 0x0461, 0x0461, 0x0463, 0x0463, 0x0465, 0x0465, 0x0467, 0x0467,
825 0x0469, 0x0469, 0x046B, 0x046B, 0x046D, 0x046D, 0x046F, 0x046F,
826 /* 7 */ 0x0471, 0x0471, 0x0473, 0x0473, 0x0475, 0x0475, 0x0476, 0x0477,
827 0x0479, 0x0479, 0x047B, 0x047B, 0x047D, 0x047D, 0x047F, 0x047F,
828 /* 8 */ 0x0481, 0x0481, 0x0482, 0x0483, 0x0484, 0x0485, 0x0486, 0x0487,
829 0x0488, 0x0489, 0x048A, 0x048B, 0x048C, 0x048D, 0x048E, 0x048F,
830 /* 9 */ 0x0491, 0x0491, 0x0493, 0x0493, 0x0495, 0x0495, 0x0497, 0x0497,
831 0x0499, 0x0499, 0x049B, 0x049B, 0x049D, 0x049D, 0x049F, 0x049F,
832 /* A */ 0x04A1, 0x04A1, 0x04A3, 0x04A3, 0x04A5, 0x04A5, 0x04A7, 0x04A7,
833 0x04A9, 0x04A9, 0x04AB, 0x04AB, 0x04AD, 0x04AD, 0x04AF, 0x04AF,
834 /* B */ 0x04B1, 0x04B1, 0x04B3, 0x04B3, 0x04B5, 0x04B5, 0x04B7, 0x04B7,
835 0x04B9, 0x04B9, 0x04BB, 0x04BB, 0x04BD, 0x04BD, 0x04BF, 0x04BF,
836 /* C */ 0x04C0, 0x04C1, 0x04C2, 0x04C4, 0x04C4, 0x04C5, 0x04C6, 0x04C8,
837 0x04C8, 0x04C9, 0x04CA, 0x04CC, 0x04CC, 0x04CD, 0x04CE, 0x04CF,
838 /* D */ 0x04D0, 0x04D1, 0x04D2, 0x04D3, 0x04D4, 0x04D5, 0x04D6, 0x04D7,
839 0x04D8, 0x04D9, 0x04DA, 0x04DB, 0x04DC, 0x04DD, 0x04DE, 0x04DF,
840 /* E */ 0x04E0, 0x04E1, 0x04E2, 0x04E3, 0x04E4, 0x04E5, 0x04E6, 0x04E7,
841 0x04E8, 0x04E9, 0x04EA, 0x04EB, 0x04EC, 0x04ED, 0x04EE, 0x04EF,
842 /* F */ 0x04F0, 0x04F1, 0x04F2, 0x04F3, 0x04F4, 0x04F5, 0x04F6, 0x04F7,
843 0x04F8, 0x04F9, 0x04FA, 0x04FB, 0x04FC, 0x04FD, 0x04FE, 0x04FF,
844
845 // Table 5 (for high byte 0x05)
846
847 /* 0 */ 0x0500, 0x0501, 0x0502, 0x0503, 0x0504, 0x0505, 0x0506, 0x0507,
848 0x0508, 0x0509, 0x050A, 0x050B, 0x050C, 0x050D, 0x050E, 0x050F,
849 /* 1 */ 0x0510, 0x0511, 0x0512, 0x0513, 0x0514, 0x0515, 0x0516, 0x0517,
850 0x0518, 0x0519, 0x051A, 0x051B, 0x051C, 0x051D, 0x051E, 0x051F,
851 /* 2 */ 0x0520, 0x0521, 0x0522, 0x0523, 0x0524, 0x0525, 0x0526, 0x0527,
852 0x0528, 0x0529, 0x052A, 0x052B, 0x052C, 0x052D, 0x052E, 0x052F,
853 /* 3 */ 0x0530, 0x0561, 0x0562, 0x0563, 0x0564, 0x0565, 0x0566, 0x0567,
854 0x0568, 0x0569, 0x056A, 0x056B, 0x056C, 0x056D, 0x056E, 0x056F,
855 /* 4 */ 0x0570, 0x0571, 0x0572, 0x0573, 0x0574, 0x0575, 0x0576, 0x0577,
856 0x0578, 0x0579, 0x057A, 0x057B, 0x057C, 0x057D, 0x057E, 0x057F,
857 /* 5 */ 0x0580, 0x0581, 0x0582, 0x0583, 0x0584, 0x0585, 0x0586, 0x0557,
858 0x0558, 0x0559, 0x055A, 0x055B, 0x055C, 0x055D, 0x055E, 0x055F,
859 /* 6 */ 0x0560, 0x0561, 0x0562, 0x0563, 0x0564, 0x0565, 0x0566, 0x0567,
860 0x0568, 0x0569, 0x056A, 0x056B, 0x056C, 0x056D, 0x056E, 0x056F,
861 /* 7 */ 0x0570, 0x0571, 0x0572, 0x0573, 0x0574, 0x0575, 0x0576, 0x0577,
862 0x0578, 0x0579, 0x057A, 0x057B, 0x057C, 0x057D, 0x057E, 0x057F,
863 /* 8 */ 0x0580, 0x0581, 0x0582, 0x0583, 0x0584, 0x0585, 0x0586, 0x0587,
864 0x0588, 0x0589, 0x058A, 0x058B, 0x058C, 0x058D, 0x058E, 0x058F,
865 /* 9 */ 0x0590, 0x0591, 0x0592, 0x0593, 0x0594, 0x0595, 0x0596, 0x0597,
866 0x0598, 0x0599, 0x059A, 0x059B, 0x059C, 0x059D, 0x059E, 0x059F,
867 /* A */ 0x05A0, 0x05A1, 0x05A2, 0x05A3, 0x05A4, 0x05A5, 0x05A6, 0x05A7,
868 0x05A8, 0x05A9, 0x05AA, 0x05AB, 0x05AC, 0x05AD, 0x05AE, 0x05AF,
869 /* B */ 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7,
870 0x05B8, 0x05B9, 0x05BA, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF,
871 /* C */ 0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05C4, 0x05C5, 0x05C6, 0x05C7,
872 0x05C8, 0x05C9, 0x05CA, 0x05CB, 0x05CC, 0x05CD, 0x05CE, 0x05CF,
873 /* D */ 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7,
874 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
875 /* E */ 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,
876 0x05E8, 0x05E9, 0x05EA, 0x05EB, 0x05EC, 0x05ED, 0x05EE, 0x05EF,
877 /* F */ 0x05F0, 0x05F1, 0x05F2, 0x05F3, 0x05F4, 0x05F5, 0x05F6, 0x05F7,
878 0x05F8, 0x05F9, 0x05FA, 0x05FB, 0x05FC, 0x05FD, 0x05FE, 0x05FF,
879
880 // Table 6 (for high byte 0x10)
881
882 /* 0 */ 0x1000, 0x1001, 0x1002, 0x1003, 0x1004, 0x1005, 0x1006, 0x1007,
883 0x1008, 0x1009, 0x100A, 0x100B, 0x100C, 0x100D, 0x100E, 0x100F,
884 /* 1 */ 0x1010, 0x1011, 0x1012, 0x1013, 0x1014, 0x1015, 0x1016, 0x1017,
885 0x1018, 0x1019, 0x101A, 0x101B, 0x101C, 0x101D, 0x101E, 0x101F,
886 /* 2 */ 0x1020, 0x1021, 0x1022, 0x1023, 0x1024, 0x1025, 0x1026, 0x1027,
887 0x1028, 0x1029, 0x102A, 0x102B, 0x102C, 0x102D, 0x102E, 0x102F,
888 /* 3 */ 0x1030, 0x1031, 0x1032, 0x1033, 0x1034, 0x1035, 0x1036, 0x1037,
889 0x1038, 0x1039, 0x103A, 0x103B, 0x103C, 0x103D, 0x103E, 0x103F,
890 /* 4 */ 0x1040, 0x1041, 0x1042, 0x1043, 0x1044, 0x1045, 0x1046, 0x1047,
891 0x1048, 0x1049, 0x104A, 0x104B, 0x104C, 0x104D, 0x104E, 0x104F,
892 /* 5 */ 0x1050, 0x1051, 0x1052, 0x1053, 0x1054, 0x1055, 0x1056, 0x1057,
893 0x1058, 0x1059, 0x105A, 0x105B, 0x105C, 0x105D, 0x105E, 0x105F,
894 /* 6 */ 0x1060, 0x1061, 0x1062, 0x1063, 0x1064, 0x1065, 0x1066, 0x1067,
895 0x1068, 0x1069, 0x106A, 0x106B, 0x106C, 0x106D, 0x106E, 0x106F,
896 /* 7 */ 0x1070, 0x1071, 0x1072, 0x1073, 0x1074, 0x1075, 0x1076, 0x1077,
897 0x1078, 0x1079, 0x107A, 0x107B, 0x107C, 0x107D, 0x107E, 0x107F,
898 /* 8 */ 0x1080, 0x1081, 0x1082, 0x1083, 0x1084, 0x1085, 0x1086, 0x1087,
899 0x1088, 0x1089, 0x108A, 0x108B, 0x108C, 0x108D, 0x108E, 0x108F,
900 /* 9 */ 0x1090, 0x1091, 0x1092, 0x1093, 0x1094, 0x1095, 0x1096, 0x1097,
901 0x1098, 0x1099, 0x109A, 0x109B, 0x109C, 0x109D, 0x109E, 0x109F,
902 /* A */ 0x10D0, 0x10D1, 0x10D2, 0x10D3, 0x10D4, 0x10D5, 0x10D6, 0x10D7,
903 0x10D8, 0x10D9, 0x10DA, 0x10DB, 0x10DC, 0x10DD, 0x10DE, 0x10DF,
904 /* B */ 0x10E0, 0x10E1, 0x10E2, 0x10E3, 0x10E4, 0x10E5, 0x10E6, 0x10E7,
905 0x10E8, 0x10E9, 0x10EA, 0x10EB, 0x10EC, 0x10ED, 0x10EE, 0x10EF,
906 /* C */ 0x10F0, 0x10F1, 0x10F2, 0x10F3, 0x10F4, 0x10F5, 0x10C6, 0x10C7,
907 0x10C8, 0x10C9, 0x10CA, 0x10CB, 0x10CC, 0x10CD, 0x10CE, 0x10CF,
908 /* D */ 0x10D0, 0x10D1, 0x10D2, 0x10D3, 0x10D4, 0x10D5, 0x10D6, 0x10D7,
909 0x10D8, 0x10D9, 0x10DA, 0x10DB, 0x10DC, 0x10DD, 0x10DE, 0x10DF,
910 /* E */ 0x10E0, 0x10E1, 0x10E2, 0x10E3, 0x10E4, 0x10E5, 0x10E6, 0x10E7,
911 0x10E8, 0x10E9, 0x10EA, 0x10EB, 0x10EC, 0x10ED, 0x10EE, 0x10EF,
912 /* F */ 0x10F0, 0x10F1, 0x10F2, 0x10F3, 0x10F4, 0x10F5, 0x10F6, 0x10F7,
913 0x10F8, 0x10F9, 0x10FA, 0x10FB, 0x10FC, 0x10FD, 0x10FE, 0x10FF,
914
915 // Table 7 (for high byte 0x20)
916
917 /* 0 */ 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007,
918 0x2008, 0x2009, 0x200A, 0x200B, 0x0000, 0x0000, 0x0000, 0x0000,
919 /* 1 */ 0x2010, 0x2011, 0x2012, 0x2013, 0x2014, 0x2015, 0x2016, 0x2017,
920 0x2018, 0x2019, 0x201A, 0x201B, 0x201C, 0x201D, 0x201E, 0x201F,
921 /* 2 */ 0x2020, 0x2021, 0x2022, 0x2023, 0x2024, 0x2025, 0x2026, 0x2027,
922 0x2028, 0x2029, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x202F,
923 /* 3 */ 0x2030, 0x2031, 0x2032, 0x2033, 0x2034, 0x2035, 0x2036, 0x2037,
924 0x2038, 0x2039, 0x203A, 0x203B, 0x203C, 0x203D, 0x203E, 0x203F,
925 /* 4 */ 0x2040, 0x2041, 0x2042, 0x2043, 0x2044, 0x2045, 0x2046, 0x2047,
926 0x2048, 0x2049, 0x204A, 0x204B, 0x204C, 0x204D, 0x204E, 0x204F,
927 /* 5 */ 0x2050, 0x2051, 0x2052, 0x2053, 0x2054, 0x2055, 0x2056, 0x2057,
928 0x2058, 0x2059, 0x205A, 0x205B, 0x205C, 0x205D, 0x205E, 0x205F,
929 /* 6 */ 0x2060, 0x2061, 0x2062, 0x2063, 0x2064, 0x2065, 0x2066, 0x2067,
930 0x2068, 0x2069, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
931 /* 7 */ 0x2070, 0x2071, 0x2072, 0x2073, 0x2074, 0x2075, 0x2076, 0x2077,
932 0x2078, 0x2079, 0x207A, 0x207B, 0x207C, 0x207D, 0x207E, 0x207F,
933 /* 8 */ 0x2080, 0x2081, 0x2082, 0x2083, 0x2084, 0x2085, 0x2086, 0x2087,
934 0x2088, 0x2089, 0x208A, 0x208B, 0x208C, 0x208D, 0x208E, 0x208F,
935 /* 9 */ 0x2090, 0x2091, 0x2092, 0x2093, 0x2094, 0x2095, 0x2096, 0x2097,
936 0x2098, 0x2099, 0x209A, 0x209B, 0x209C, 0x209D, 0x209E, 0x209F,
937 /* A */ 0x20A0, 0x20A1, 0x20A2, 0x20A3, 0x20A4, 0x20A5, 0x20A6, 0x20A7,
938 0x20A8, 0x20A9, 0x20AA, 0x20AB, 0x20AC, 0x20AD, 0x20AE, 0x20AF,
939 /* B */ 0x20B0, 0x20B1, 0x20B2, 0x20B3, 0x20B4, 0x20B5, 0x20B6, 0x20B7,
940 0x20B8, 0x20B9, 0x20BA, 0x20BB, 0x20BC, 0x20BD, 0x20BE, 0x20BF,
941 /* C */ 0x20C0, 0x20C1, 0x20C2, 0x20C3, 0x20C4, 0x20C5, 0x20C6, 0x20C7,
942 0x20C8, 0x20C9, 0x20CA, 0x20CB, 0x20CC, 0x20CD, 0x20CE, 0x20CF,
943 /* D */ 0x20D0, 0x20D1, 0x20D2, 0x20D3, 0x20D4, 0x20D5, 0x20D6, 0x20D7,
944 0x20D8, 0x20D9, 0x20DA, 0x20DB, 0x20DC, 0x20DD, 0x20DE, 0x20DF,
945 /* E */ 0x20E0, 0x20E1, 0x20E2, 0x20E3, 0x20E4, 0x20E5, 0x20E6, 0x20E7,
946 0x20E8, 0x20E9, 0x20EA, 0x20EB, 0x20EC, 0x20ED, 0x20EE, 0x20EF,
947 /* F */ 0x20F0, 0x20F1, 0x20F2, 0x20F3, 0x20F4, 0x20F5, 0x20F6, 0x20F7,
948 0x20F8, 0x20F9, 0x20FA, 0x20FB, 0x20FC, 0x20FD, 0x20FE, 0x20FF,
949
950 // Table 8 (for high byte 0x21)
951
952 /* 0 */ 0x2100, 0x2101, 0x2102, 0x2103, 0x2104, 0x2105, 0x2106, 0x2107,
953 0x2108, 0x2109, 0x210A, 0x210B, 0x210C, 0x210D, 0x210E, 0x210F,
954 /* 1 */ 0x2110, 0x2111, 0x2112, 0x2113, 0x2114, 0x2115, 0x2116, 0x2117,
955 0x2118, 0x2119, 0x211A, 0x211B, 0x211C, 0x211D, 0x211E, 0x211F,
956 /* 2 */ 0x2120, 0x2121, 0x2122, 0x2123, 0x2124, 0x2125, 0x2126, 0x2127,
957 0x2128, 0x2129, 0x212A, 0x212B, 0x212C, 0x212D, 0x212E, 0x212F,
958 /* 3 */ 0x2130, 0x2131, 0x2132, 0x2133, 0x2134, 0x2135, 0x2136, 0x2137,
959 0x2138, 0x2139, 0x213A, 0x213B, 0x213C, 0x213D, 0x213E, 0x213F,
960 /* 4 */ 0x2140, 0x2141, 0x2142, 0x2143, 0x2144, 0x2145, 0x2146, 0x2147,
961 0x2148, 0x2149, 0x214A, 0x214B, 0x214C, 0x214D, 0x214E, 0x214F,
962 /* 5 */ 0x2150, 0x2151, 0x2152, 0x2153, 0x2154, 0x2155, 0x2156, 0x2157,
963 0x2158, 0x2159, 0x215A, 0x215B, 0x215C, 0x215D, 0x215E, 0x215F,
964 /* 6 */ 0x2170, 0x2171, 0x2172, 0x2173, 0x2174, 0x2175, 0x2176, 0x2177,
965 0x2178, 0x2179, 0x217A, 0x217B, 0x217C, 0x217D, 0x217E, 0x217F,
966 /* 7 */ 0x2170, 0x2171, 0x2172, 0x2173, 0x2174, 0x2175, 0x2176, 0x2177,
967 0x2178, 0x2179, 0x217A, 0x217B, 0x217C, 0x217D, 0x217E, 0x217F,
968 /* 8 */ 0x2180, 0x2181, 0x2182, 0x2183, 0x2184, 0x2185, 0x2186, 0x2187,
969 0x2188, 0x2189, 0x218A, 0x218B, 0x218C, 0x218D, 0x218E, 0x218F,
970 /* 9 */ 0x2190, 0x2191, 0x2192, 0x2193, 0x2194, 0x2195, 0x2196, 0x2197,
971 0x2198, 0x2199, 0x219A, 0x219B, 0x219C, 0x219D, 0x219E, 0x219F,
972 /* A */ 0x21A0, 0x21A1, 0x21A2, 0x21A3, 0x21A4, 0x21A5, 0x21A6, 0x21A7,
973 0x21A8, 0x21A9, 0x21AA, 0x21AB, 0x21AC, 0x21AD, 0x21AE, 0x21AF,
974 /* B */ 0x21B0, 0x21B1, 0x21B2, 0x21B3, 0x21B4, 0x21B5, 0x21B6, 0x21B7,
975 0x21B8, 0x21B9, 0x21BA, 0x21BB, 0x21BC, 0x21BD, 0x21BE, 0x21BF,
976 /* C */ 0x21C0, 0x21C1, 0x21C2, 0x21C3, 0x21C4, 0x21C5, 0x21C6, 0x21C7,
977 0x21C8, 0x21C9, 0x21CA, 0x21CB, 0x21CC, 0x21CD, 0x21CE, 0x21CF,
978 /* D */ 0x21D0, 0x21D1, 0x21D2, 0x21D3, 0x21D4, 0x21D5, 0x21D6, 0x21D7,
979 0x21D8, 0x21D9, 0x21DA, 0x21DB, 0x21DC, 0x21DD, 0x21DE, 0x21DF,
980 /* E */ 0x21E0, 0x21E1, 0x21E2, 0x21E3, 0x21E4, 0x21E5, 0x21E6, 0x21E7,
981 0x21E8, 0x21E9, 0x21EA, 0x21EB, 0x21EC, 0x21ED, 0x21EE, 0x21EF,
982 /* F */ 0x21F0, 0x21F1, 0x21F2, 0x21F3, 0x21F4, 0x21F5, 0x21F6, 0x21F7,
983 0x21F8, 0x21F9, 0x21FA, 0x21FB, 0x21FC, 0x21FD, 0x21FE, 0x21FF,
984
985 // Table 9 (for high byte 0xFE)
986
987 /* 0 */ 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05, 0xFE06, 0xFE07,
988 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E, 0xFE0F,
989 /* 1 */ 0xFE10, 0xFE11, 0xFE12, 0xFE13, 0xFE14, 0xFE15, 0xFE16, 0xFE17,
990 0xFE18, 0xFE19, 0xFE1A, 0xFE1B, 0xFE1C, 0xFE1D, 0xFE1E, 0xFE1F,
991 /* 2 */ 0xFE20, 0xFE21, 0xFE22, 0xFE23, 0xFE24, 0xFE25, 0xFE26, 0xFE27,
992 0xFE28, 0xFE29, 0xFE2A, 0xFE2B, 0xFE2C, 0xFE2D, 0xFE2E, 0xFE2F,
993 /* 3 */ 0xFE30, 0xFE31, 0xFE32, 0xFE33, 0xFE34, 0xFE35, 0xFE36, 0xFE37,
994 0xFE38, 0xFE39, 0xFE3A, 0xFE3B, 0xFE3C, 0xFE3D, 0xFE3E, 0xFE3F,
995 /* 4 */ 0xFE40, 0xFE41, 0xFE42, 0xFE43, 0xFE44, 0xFE45, 0xFE46, 0xFE47,
996 0xFE48, 0xFE49, 0xFE4A, 0xFE4B, 0xFE4C, 0xFE4D, 0xFE4E, 0xFE4F,
997 /* 5 */ 0xFE50, 0xFE51, 0xFE52, 0xFE53, 0xFE54, 0xFE55, 0xFE56, 0xFE57,
998 0xFE58, 0xFE59, 0xFE5A, 0xFE5B, 0xFE5C, 0xFE5D, 0xFE5E, 0xFE5F,
999 /* 6 */ 0xFE60, 0xFE61, 0xFE62, 0xFE63, 0xFE64, 0xFE65, 0xFE66, 0xFE67,
1000 0xFE68, 0xFE69, 0xFE6A, 0xFE6B, 0xFE6C, 0xFE6D, 0xFE6E, 0xFE6F,
1001 /* 7 */ 0xFE70, 0xFE71, 0xFE72, 0xFE73, 0xFE74, 0xFE75, 0xFE76, 0xFE77,
1002 0xFE78, 0xFE79, 0xFE7A, 0xFE7B, 0xFE7C, 0xFE7D, 0xFE7E, 0xFE7F,
1003 /* 8 */ 0xFE80, 0xFE81, 0xFE82, 0xFE83, 0xFE84, 0xFE85, 0xFE86, 0xFE87,
1004 0xFE88, 0xFE89, 0xFE8A, 0xFE8B, 0xFE8C, 0xFE8D, 0xFE8E, 0xFE8F,
1005 /* 9 */ 0xFE90, 0xFE91, 0xFE92, 0xFE93, 0xFE94, 0xFE95, 0xFE96, 0xFE97,
1006 0xFE98, 0xFE99, 0xFE9A, 0xFE9B, 0xFE9C, 0xFE9D, 0xFE9E, 0xFE9F,
1007 /* A */ 0xFEA0, 0xFEA1, 0xFEA2, 0xFEA3, 0xFEA4, 0xFEA5, 0xFEA6, 0xFEA7,
1008 0xFEA8, 0xFEA9, 0xFEAA, 0xFEAB, 0xFEAC, 0xFEAD, 0xFEAE, 0xFEAF,
1009 /* B */ 0xFEB0, 0xFEB1, 0xFEB2, 0xFEB3, 0xFEB4, 0xFEB5, 0xFEB6, 0xFEB7,
1010 0xFEB8, 0xFEB9, 0xFEBA, 0xFEBB, 0xFEBC, 0xFEBD, 0xFEBE, 0xFEBF,
1011 /* C */ 0xFEC0, 0xFEC1, 0xFEC2, 0xFEC3, 0xFEC4, 0xFEC5, 0xFEC6, 0xFEC7,
1012 0xFEC8, 0xFEC9, 0xFECA, 0xFECB, 0xFECC, 0xFECD, 0xFECE, 0xFECF,
1013 /* D */ 0xFED0, 0xFED1, 0xFED2, 0xFED3, 0xFED4, 0xFED5, 0xFED6, 0xFED7,
1014 0xFED8, 0xFED9, 0xFEDA, 0xFEDB, 0xFEDC, 0xFEDD, 0xFEDE, 0xFEDF,
1015 /* E */ 0xFEE0, 0xFEE1, 0xFEE2, 0xFEE3, 0xFEE4, 0xFEE5, 0xFEE6, 0xFEE7,
1016 0xFEE8, 0xFEE9, 0xFEEA, 0xFEEB, 0xFEEC, 0xFEED, 0xFEEE, 0xFEEF,
1017 /* F */ 0xFEF0, 0xFEF1, 0xFEF2, 0xFEF3, 0xFEF4, 0xFEF5, 0xFEF6, 0xFEF7,
1018 0xFEF8, 0xFEF9, 0xFEFA, 0xFEFB, 0xFEFC, 0xFEFD, 0xFEFE, 0x0000,
1019
1020 // Table 10 (for high byte 0xFF)
1021
1022 /* 0 */ 0xFF00, 0xFF01, 0xFF02, 0xFF03, 0xFF04, 0xFF05, 0xFF06, 0xFF07,
1023 0xFF08, 0xFF09, 0xFF0A, 0xFF0B, 0xFF0C, 0xFF0D, 0xFF0E, 0xFF0F,
1024 /* 1 */ 0xFF10, 0xFF11, 0xFF12, 0xFF13, 0xFF14, 0xFF15, 0xFF16, 0xFF17,
1025 0xFF18, 0xFF19, 0xFF1A, 0xFF1B, 0xFF1C, 0xFF1D, 0xFF1E, 0xFF1F,
1026 /* 2 */ 0xFF20, 0xFF41, 0xFF42, 0xFF43, 0xFF44, 0xFF45, 0xFF46, 0xFF47,
1027 0xFF48, 0xFF49, 0xFF4A, 0xFF4B, 0xFF4C, 0xFF4D, 0xFF4E, 0xFF4F,
1028 /* 3 */ 0xFF50, 0xFF51, 0xFF52, 0xFF53, 0xFF54, 0xFF55, 0xFF56, 0xFF57,
1029 0xFF58, 0xFF59, 0xFF5A, 0xFF3B, 0xFF3C, 0xFF3D, 0xFF3E, 0xFF3F,
1030 /* 4 */ 0xFF40, 0xFF41, 0xFF42, 0xFF43, 0xFF44, 0xFF45, 0xFF46, 0xFF47,
1031 0xFF48, 0xFF49, 0xFF4A, 0xFF4B, 0xFF4C, 0xFF4D, 0xFF4E, 0xFF4F,
1032 /* 5 */ 0xFF50, 0xFF51, 0xFF52, 0xFF53, 0xFF54, 0xFF55, 0xFF56, 0xFF57,
1033 0xFF58, 0xFF59, 0xFF5A, 0xFF5B, 0xFF5C, 0xFF5D, 0xFF5E, 0xFF5F,
1034 /* 6 */ 0xFF60, 0xFF61, 0xFF62, 0xFF63, 0xFF64, 0xFF65, 0xFF66, 0xFF67,
1035 0xFF68, 0xFF69, 0xFF6A, 0xFF6B, 0xFF6C, 0xFF6D, 0xFF6E, 0xFF6F,
1036 /* 7 */ 0xFF70, 0xFF71, 0xFF72, 0xFF73, 0xFF74, 0xFF75, 0xFF76, 0xFF77,
1037 0xFF78, 0xFF79, 0xFF7A, 0xFF7B, 0xFF7C, 0xFF7D, 0xFF7E, 0xFF7F,
1038 /* 8 */ 0xFF80, 0xFF81, 0xFF82, 0xFF83, 0xFF84, 0xFF85, 0xFF86, 0xFF87,
1039 0xFF88, 0xFF89, 0xFF8A, 0xFF8B, 0xFF8C, 0xFF8D, 0xFF8E, 0xFF8F,
1040 /* 9 */ 0xFF90, 0xFF91, 0xFF92, 0xFF93, 0xFF94, 0xFF95, 0xFF96, 0xFF97,
1041 0xFF98, 0xFF99, 0xFF9A, 0xFF9B, 0xFF9C, 0xFF9D, 0xFF9E, 0xFF9F,
1042 /* A */ 0xFFA0, 0xFFA1, 0xFFA2, 0xFFA3, 0xFFA4, 0xFFA5, 0xFFA6, 0xFFA7,
1043 0xFFA8, 0xFFA9, 0xFFAA, 0xFFAB, 0xFFAC, 0xFFAD, 0xFFAE, 0xFFAF,
1044 /* B */ 0xFFB0, 0xFFB1, 0xFFB2, 0xFFB3, 0xFFB4, 0xFFB5, 0xFFB6, 0xFFB7,
1045 0xFFB8, 0xFFB9, 0xFFBA, 0xFFBB, 0xFFBC, 0xFFBD, 0xFFBE, 0xFFBF,
1046 /* C */ 0xFFC0, 0xFFC1, 0xFFC2, 0xFFC3, 0xFFC4, 0xFFC5, 0xFFC6, 0xFFC7,
1047 0xFFC8, 0xFFC9, 0xFFCA, 0xFFCB, 0xFFCC, 0xFFCD, 0xFFCE, 0xFFCF,
1048 /* D */ 0xFFD0, 0xFFD1, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD5, 0xFFD6, 0xFFD7,
1049 0xFFD8, 0xFFD9, 0xFFDA, 0xFFDB, 0xFFDC, 0xFFDD, 0xFFDE, 0xFFDF,
1050 /* E */ 0xFFE0, 0xFFE1, 0xFFE2, 0xFFE3, 0xFFE4, 0xFFE5, 0xFFE6, 0xFFE7,
1051 0xFFE8, 0xFFE9, 0xFFEA, 0xFFEB, 0xFFEC, 0xFFED, 0xFFEE, 0xFFEF,
1052 /* F */ 0xFFF0, 0xFFF1, 0xFFF2, 0xFFF3, 0xFFF4, 0xFFF5, 0xFFF6, 0xFFF7,
1053 0xFFF8, 0xFFF9, 0xFFFA, 0xFFFB, 0xFFFC, 0xFFFD, 0xFFFE, 0xFFFF,
1054};
1055
1056// Returns the next non-ignorable codepoint within string starting from the
1057// position indicated by index, or zero if there are no more.
1058// The passed-in index is automatically advanced as the characters in the input
1059// HFS-decomposed UTF-8 strings are read.
1060inline int HFSReadNextNonIgnorableCodepoint(const char* string,
1061 int length,
1062 int* index) {
1063 int codepoint = 0;
1064 while (*index < length && codepoint == 0) {
1065 // CBU8_NEXT returns a value < 0 in error cases. For purposes of string
1066 // comparison, we just use that value and flag it with DCHECK.
1067 CBU8_NEXT(string, *index, length, codepoint);
1068 DCHECK_GT(codepoint, 0);
1069 if (codepoint > 0) {
1070 // Check if there is a subtable for this upper byte.
1071 int lookup_offset = lower_case_table[codepoint >> 8];
1072 if (lookup_offset != 0)
1073 codepoint = lower_case_table[lookup_offset + (codepoint & 0x00FF)];
1074 // Note: codepoint1 may be again 0 at this point if the character was
1075 // an ignorable.
1076 }
1077 }
1078 return codepoint;
1079}
1080
1081} // anonymous namespace
1082
1083// Special UTF-8 version of FastUnicodeCompare. Cf:
1084// http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringComparisonAlgorithm
1085// The input strings must be in the special HFS decomposed form.
1086int FilePath::HFSFastUnicodeCompare(const StringType& string1,
1087 const StringType& string2) {
1088 int length1 = string1.length();
1089 int length2 = string2.length();
1090 int index1 = 0;
1091 int index2 = 0;
1092
1093 for (;;) {
1094 int codepoint1 = HFSReadNextNonIgnorableCodepoint(string1.c_str(),
1095 length1,
1096 &index1);
1097 int codepoint2 = HFSReadNextNonIgnorableCodepoint(string2.c_str(),
1098 length2,
1099 &index2);
1100 if (codepoint1 != codepoint2)
1101 return (codepoint1 < codepoint2) ? -1 : 1;
1102 if (codepoint1 == 0) {
1103 DCHECK_EQ(index1, length1);
1104 DCHECK_EQ(index2, length2);
1105 return 0;
1106 }
1107 }
1108}
1109
estade@chromium.org99a42e72010-07-28 06:24:39 +09001110StringType FilePath::GetHFSDecomposedForm(const StringType& string) {
brettw@chromium.org3ce2c372010-10-17 13:09:06 +09001111 base::mac::ScopedCFTypeRef<CFStringRef> cfstring(
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +09001112 CFStringCreateWithBytesNoCopy(
1113 NULL,
1114 reinterpret_cast<const UInt8*>(string.c_str()),
1115 string.length(),
1116 kCFStringEncodingUTF8,
1117 false,
1118 kCFAllocatorNull));
1119 // Query the maximum length needed to store the result. In most cases this
1120 // will overestimate the required space. The return value also already
1121 // includes the space needed for a terminating 0.
1122 CFIndex length = CFStringGetMaximumSizeOfFileSystemRepresentation(cfstring);
1123 DCHECK_GT(length, 0); // should be at least 1 for the 0-terminator.
1124 // Reserve enough space for CFStringGetFileSystemRepresentation to write into.
1125 // Also set the length to the maximum so that we can shrink it later.
1126 // (Increasing rather than decreasing it would clobber the string contents!)
1127 StringType result;
1128 result.reserve(length);
1129 result.resize(length - 1);
1130 Boolean success = CFStringGetFileSystemRepresentation(cfstring,
1131 &result[0],
1132 length);
1133 if (success) {
1134 // Reduce result.length() to actual string length.
1135 result.resize(strlen(result.c_str()));
1136 } else {
1137 // An error occurred -> clear result.
1138 result.clear();
1139 }
1140 return result;
1141}
1142
1143int FilePath::CompareIgnoreCase(const StringType& string1,
1144 const StringType& string2) {
1145 // Quick checks for empty strings - these speed things up a bit and make the
1146 // following code cleaner.
1147 if (string1.empty())
1148 return string2.empty() ? 0 : -1;
1149 if (string2.empty())
1150 return 1;
1151
1152 StringType hfs1 = GetHFSDecomposedForm(string1);
1153 StringType hfs2 = GetHFSDecomposedForm(string2);
1154
1155 // GetHFSDecomposedForm() returns an empty string in an error case.
1156 if (hfs1.empty() || hfs2.empty()) {
1157 NOTREACHED();
brettw@chromium.org3ce2c372010-10-17 13:09:06 +09001158 base::mac::ScopedCFTypeRef<CFStringRef> cfstring1(
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +09001159 CFStringCreateWithBytesNoCopy(
1160 NULL,
1161 reinterpret_cast<const UInt8*>(string1.c_str()),
1162 string1.length(),
1163 kCFStringEncodingUTF8,
1164 false,
1165 kCFAllocatorNull));
brettw@chromium.org3ce2c372010-10-17 13:09:06 +09001166 base::mac::ScopedCFTypeRef<CFStringRef> cfstring2(
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +09001167 CFStringCreateWithBytesNoCopy(
1168 NULL,
1169 reinterpret_cast<const UInt8*>(string2.c_str()),
1170 string2.length(),
1171 kCFStringEncodingUTF8,
1172 false,
1173 kCFAllocatorNull));
1174 return CFStringCompare(cfstring1,
1175 cfstring2,
1176 kCFCompareCaseInsensitive);
1177 }
1178
1179 return HFSFastUnicodeCompare(hfs1, hfs2);
1180}
1181
1182#else // << WIN. MACOSX | other (POSIX) >>
1183
1184// Generic (POSIX) implementation of file string comparison.
1185// TODO(rolandsteiner) check if this is sufficient/correct.
1186int FilePath::CompareIgnoreCase(const StringType& string1,
1187 const StringType& string2) {
1188 int comparison = strcasecmp(string1.c_str(), string2.c_str());
1189 if (comparison < 0)
1190 return -1;
1191 if (comparison > 0)
1192 return 1;
1193 return 0;
1194}
1195
1196#endif // OS versions of CompareIgnoreCase()
1197
finnur@chromium.org2a48aca2010-02-03 10:17:26 +09001198
avi@google.com5cb79352008-12-11 23:55:12 +09001199void FilePath::StripTrailingSeparatorsInternal() {
mark@chromium.org6380e262008-10-04 03:21:47 +09001200 // If there is no drive letter, start will be 1, which will prevent stripping
1201 // the leading separator if there is only one separator. If there is a drive
1202 // letter, start will be set appropriately to prevent stripping the first
1203 // separator following the drive letter, if a separator immediately follows
1204 // the drive letter.
mark@chromium.orgfac7b262008-12-09 01:49:08 +09001205 StringType::size_type start = FindDriveLetter(path_) + 2;
mark@chromium.org6380e262008-10-04 03:21:47 +09001206
1207 StringType::size_type last_stripped = StringType::npos;
1208 for (StringType::size_type pos = path_.length();
1209 pos > start && IsSeparator(path_[pos - 1]);
1210 --pos) {
1211 // If the string only has two separators and they're at the beginning,
1212 // don't strip them, unless the string began with more than two separators.
1213 if (pos != start + 1 || last_stripped == start + 2 ||
1214 !IsSeparator(path_[start - 1])) {
1215 path_.resize(pos - 1);
1216 last_stripped = pos;
1217 }
1218 }
1219}
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001220
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001221FilePath FilePath::NormalizePathSeparators() const {
tony@chromium.org65049b12010-06-25 14:33:17 +09001222#if defined(FILE_PATH_USES_WIN_SEPARATORS)
tony@chromium.org65049b12010-06-25 14:33:17 +09001223 StringType copy = path_;
1224 for (size_t i = 1; i < arraysize(kSeparators); ++i) {
1225 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]);
1226 }
1227 return FilePath(copy);
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001228#else
1229 return *this;
tony@chromium.org65049b12010-06-25 14:33:17 +09001230#endif
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001231}