[go: nahoru, domu]

blob: ef8100cf8c39f403b7599d0dd5c390d859250be3 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
mark@chromium.org65d00aff2008-10-03 18:21:472// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettw@chromium.org57999812013-02-24 05:40:525#include "base/files/file_path.h"
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:096
jhawkins@chromium.org2edc2862011-04-04 18:04:377#include <string.h>
Peter Kastingfea6bb72022-09-29 16:41:248
jhawkins@chromium.org2edc2862011-04-04 18:04:379#include <algorithm>
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:0910
Hans Wennborgc3cffa62020-04-27 10:09:1211#include "base/check_op.h"
Oksana Zhuravlova36356be2021-09-17 00:02:3012#include "base/files/safe_base_name.h"
mczeng334344c2022-07-19 12:11:1413#include "base/numerics/safe_conversions.h"
finnur@chromium.org44ec9b3b2010-02-03 01:17:2614#include "base/pickle.h"
Jeremy Roman9ec5db92022-02-14 19:46:3415#include "base/ranges/algorithm.h"
Jan Wilken Dörriefb1d7b662020-02-04 11:10:2316#include "base/strings/strcat.h"
tfarina@chromium.orgeb62f7262013-03-30 14:29:0017#include "base/strings/string_piece.h"
avi@chromium.org251cd6e52013-06-11 13:36:3718#include "base/strings/string_util.h"
brettw@chromium.org9fe1a5b2013-02-07 19:18:0319#include "base/strings/sys_string_conversions.h"
Lei Zhangf817d8b2023-10-18 09:09:4120#include "base/strings/utf_ostream_operators.h"
avi@chromium.orga4ea1f12013-06-07 18:37:0721#include "base/strings/utf_string_conversions.h"
Alexander Timin7372d5362021-03-09 00:55:3122#include "base/trace_event/base_tracing.h"
evanm@google.com640517f2008-10-30 23:54:0423
Xiaohan Wangb705a64a2022-01-15 18:31:1324#if BUILDFLAG(IS_APPLE)
Avi Drissmana09d7dd2023-08-17 16:26:5825#include "base/apple/scoped_cftyperef.h"
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:0926#include "base/third_party/icu/icu_utf.h"
27#endif
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:0928
Xiaohan Wangb705a64a2022-01-15 18:31:1329#if BUILDFLAG(IS_WIN)
jhawkins@chromium.org2edc2862011-04-04 18:04:3730#include <windows.h>
Cliff Smolinskyc5c52102019-05-03 20:51:5431#include "base/win/win_util.h"
Xiaohan Wangb705a64a2022-01-15 18:31:1332#elif BUILDFLAG(IS_APPLE)
jhawkins@chromium.org2edc2862011-04-04 18:04:3733#include <CoreFoundation/CoreFoundation.h>
34#endif
35
brettw@chromium.org04af979a2013-02-16 04:12:2636namespace base {
37
brettw89365dc2015-06-16 05:52:4738using StringType = FilePath::StringType;
39using StringPieceType = FilePath::StringPieceType;
erikkay@google.com235c17a2009-01-09 18:26:1940
mark@chromium.org1906c032008-12-08 16:49:0841namespace {
42
François Degrosd68b2132022-05-21 03:25:2143const char* const kCommonDoubleExtensionSuffixes[] = {
44 "bz", "bz2", "gz", "lz", "lzma", "lzo", "xz", "z", "zst"};
45const char* const kCommonDoubleExtensions[] = {"user.js"};
estade@chromium.org34322912010-07-27 21:24:3946
aedla@chromium.orgaeae59f2013-01-28 13:47:5547const FilePath::CharType kStringTerminator = FILE_PATH_LITERAL('\0');
48
mark@chromium.org1906c032008-12-08 16:49:0849// 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.
brettw89365dc2015-06-16 05:52:4754StringPieceType::size_type FindDriveLetter(StringPieceType path) {
mark@chromium.org1906c032008-12-08 16:49:0855#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.org1906c032008-12-08 16:49:0863#endif // FILE_PATH_USES_DRIVE_LETTERS
estade@chromium.org34322912010-07-27 21:24:3964 return StringType::npos;
mark@chromium.org1906c032008-12-08 16:49:0865}
66
rafaelw@chromium.org62e78f52009-07-01 01:03:3467#if defined(FILE_PATH_USES_DRIVE_LETTERS)
brettw89365dc2015-06-16 05:52:4768bool EqualDriveLetterCaseInsensitive(StringPieceType a, StringPieceType b) {
rafaelw@chromium.org62e78f52009-07-01 01:03:3469 size_t a_letter_pos = FindDriveLetter(a);
70 size_t b_letter_pos = FindDriveLetter(b);
71
estade@chromium.org34322912010-07-27 21:24:3972 if (a_letter_pos == StringType::npos || b_letter_pos == StringType::npos)
rafaelw@chromium.org62e78f52009-07-01 01:03:3473 return a == b;
74
brettw89365dc2015-06-16 05:52:4775 StringPieceType a_letter(a.substr(0, a_letter_pos + 1));
76 StringPieceType b_letter(b.substr(0, b_letter_pos + 1));
Jan Wilken Dörrieaf27f302020-05-17 16:35:2877 if (!StartsWith(a_letter, b_letter, CompareCase::INSENSITIVE_ASCII))
rafaelw@chromium.org62e78f52009-07-01 01:03:3478 return false;
79
brettw89365dc2015-06-16 05:52:4780 StringPieceType a_rest(a.substr(a_letter_pos + 1));
81 StringPieceType b_rest(b.substr(b_letter_pos + 1));
rafaelw@chromium.org62e78f52009-07-01 01:03:3482 return a_rest == b_rest;
83}
84#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
85
brettw89365dc2015-06-16 05:52:4786bool IsPathAbsolute(StringPieceType path) {
mark@chromium.org1906c032008-12-08 16:49:0887#if defined(FILE_PATH_USES_DRIVE_LETTERS)
estade@chromium.org34322912010-07-27 21:24:3988 StringType::size_type letter = FindDriveLetter(path);
89 if (letter != StringType::npos) {
mark@chromium.org1906c032008-12-08 16:49:0890 // Look for a separator right after the drive specification.
91 return path.length() > letter + 1 &&
92 FilePath::IsSeparator(path[letter + 1]);
93 }
94 // Look for a pair of leading separators.
95 return path.length() > 1 &&
96 FilePath::IsSeparator(path[0]) && FilePath::IsSeparator(path[1]);
97#else // FILE_PATH_USES_DRIVE_LETTERS
98 // Look for a separator in the first position.
99 return path.length() > 0 && FilePath::IsSeparator(path[0]);
100#endif // FILE_PATH_USES_DRIVE_LETTERS
101}
102
estade@chromium.org34322912010-07-27 21:24:39103bool AreAllSeparators(const StringType& input) {
jdoerrie6c6229352018-10-22 15:55:43104 for (auto it : input) {
105 if (!FilePath::IsSeparator(it))
rafaelw@chromium.org0e4f68db2009-06-24 21:28:30106 return false;
107 }
108
109 return true;
110}
111
estade@chromium.org34322912010-07-27 21:24:39112// Find the position of the '.' that separates the extension from the rest
113// of the file name. The position is relative to BaseName(), not value().
davidben@chromium.org18cbba0a2013-12-09 17:06:52114// Returns npos if it can't find an extension.
115StringType::size_type FinalExtensionSeparatorPosition(const StringType& path) {
estade@chromium.org34322912010-07-27 21:24:39116 // Special case "." and ".."
117 if (path == FilePath::kCurrentDirectory || path == FilePath::kParentDirectory)
118 return StringType::npos;
119
davidben@chromium.org18cbba0a2013-12-09 17:06:52120 return path.rfind(FilePath::kExtensionSeparator);
121}
122
123// Same as above, but allow a second extension component of up to 4
124// characters when the rightmost extension component is a common double
125// extension (gz, bz2, Z). For example, foo.tar.gz or foo.tar.Z would have
126// extension components of '.tar.gz' and '.tar.Z' respectively.
127StringType::size_type ExtensionSeparatorPosition(const StringType& path) {
128 const StringType::size_type last_dot = FinalExtensionSeparatorPosition(path);
estade@chromium.org34322912010-07-27 21:24:39129
130 // No extension, or the extension is the whole filename.
131 if (last_dot == StringType::npos || last_dot == 0U)
132 return last_dot;
133
estade@chromium.org34322912010-07-27 21:24:39134 const StringType::size_type penultimate_dot =
135 path.rfind(FilePath::kExtensionSeparator, last_dot - 1);
136 const StringType::size_type last_separator =
137 path.find_last_of(FilePath::kSeparators, last_dot - 1,
scottmg@chromium.orgf39e03812013-05-21 09:44:02138 FilePath::kSeparatorsLength - 1);
aa@chromium.org278f9ac612012-08-26 12:11:10139
140 if (penultimate_dot == StringType::npos ||
141 (last_separator != StringType::npos &&
142 penultimate_dot < last_separator)) {
143 return last_dot;
144 }
145
jdoerrie6c6229352018-10-22 15:55:43146 for (auto* i : kCommonDoubleExtensions) {
aa@chromium.org278f9ac612012-08-26 12:11:10147 StringType extension(path, penultimate_dot + 1);
Dan McArdlea1d60b32022-05-30 15:18:24148 if (EqualsCaseInsensitiveASCII(extension, i))
aa@chromium.org278f9ac612012-08-26 12:11:10149 return penultimate_dot;
150 }
151
152 StringType extension(path, last_dot + 1);
jdoerrie6c6229352018-10-22 15:55:43153 for (auto* i : kCommonDoubleExtensionSuffixes) {
Dan McArdlea1d60b32022-05-30 15:18:24154 if (EqualsCaseInsensitiveASCII(extension, i)) {
aa@chromium.org278f9ac612012-08-26 12:11:10155 if ((last_dot - penultimate_dot) <= 5U &&
156 (last_dot - penultimate_dot) > 1U) {
157 return penultimate_dot;
158 }
159 }
estade@chromium.org34322912010-07-27 21:24:39160 }
161
162 return last_dot;
163}
164
rdevlin.cronin@chromium.org922828b32012-04-25 01:53:44165// Returns true if path is "", ".", or "..".
166bool IsEmptyOrSpecialCase(const StringType& path) {
167 // Special cases "", ".", and ".."
168 if (path.empty() || path == FilePath::kCurrentDirectory ||
169 path == FilePath::kParentDirectory) {
170 return true;
171 }
172
173 return false;
174}
175
mark@chromium.org1906c032008-12-08 16:49:08176} // namespace
177
Chris Watkinsbb7211c2017-11-29 07:16:38178FilePath::FilePath() = default;
erg@chromium.org201366472010-07-16 17:22:49179
Chris Watkinsbb7211c2017-11-29 07:16:38180FilePath::FilePath(const FilePath& that) = default;
brettw4e53ee162017-03-29 18:41:18181FilePath::FilePath(FilePath&& that) noexcept = default;
erg@chromium.org201366472010-07-16 17:22:49182
Jan Wilken Dörriec15b4bc2020-01-30 07:04:12183FilePath::FilePath(StringPieceType path) : path_(path) {
aedla@chromium.orgaeae59f2013-01-28 13:47:55184 StringType::size_type nul_pos = path_.find(kStringTerminator);
185 if (nul_pos != StringType::npos)
186 path_.erase(nul_pos, StringType::npos);
erg@chromium.org201366472010-07-16 17:22:49187}
188
Chris Watkinsbb7211c2017-11-29 07:16:38189FilePath::~FilePath() = default;
erg@chromium.org201366472010-07-16 17:22:49190
Chris Watkinsbb7211c2017-11-29 07:16:38191FilePath& FilePath::operator=(const FilePath& that) = default;
erg@chromium.org201366472010-07-16 17:22:49192
Stephan Hartmannc65b4d02020-05-27 18:46:40193FilePath& FilePath::operator=(FilePath&& that) noexcept = default;
dmurph590f8e62017-01-12 02:54:12194
erg@google.com9989c9bb2011-01-07 20:23:43195bool FilePath::operator==(const FilePath& that) const {
196#if defined(FILE_PATH_USES_DRIVE_LETTERS)
197 return EqualDriveLetterCaseInsensitive(this->path_, that.path_);
198#else // defined(FILE_PATH_USES_DRIVE_LETTERS)
199 return path_ == that.path_;
200#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
201}
202
203bool FilePath::operator!=(const FilePath& that) const {
204#if defined(FILE_PATH_USES_DRIVE_LETTERS)
205 return !EqualDriveLetterCaseInsensitive(this->path_, that.path_);
206#else // defined(FILE_PATH_USES_DRIVE_LETTERS)
207 return path_ != that.path_;
208#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
209}
210
Mike Bjorge2cd6ba02017-05-08 22:45:20211std::ostream& operator<<(std::ostream& out, const FilePath& file_path) {
212 return out << file_path.value();
213}
214
erg@google.com9989c9bb2011-01-07 20:23:43215// static
estade@chromium.orgb9e04f02008-11-27 04:03:57216bool FilePath::IsSeparator(CharType character) {
scottmg@chromium.orgf39e03812013-05-21 09:44:02217 for (size_t i = 0; i < kSeparatorsLength - 1; ++i) {
estade@chromium.orgb9e04f02008-11-27 04:03:57218 if (character == kSeparators[i]) {
mark@chromium.org65d00aff2008-10-03 18:21:47219 return true;
220 }
221 }
222
223 return false;
224}
225
Jeremy Roman9ec5db92022-02-14 19:46:34226std::vector<FilePath::StringType> FilePath::GetComponents() const {
estade@chromium.org34322912010-07-27 21:24:39227 std::vector<StringType> ret_val;
Jeremy Roman9ec5db92022-02-14 19:46:34228 if (value().empty())
229 return ret_val;
230
rafaelw@chromium.org0e4f68db2009-06-24 21:28:30231 FilePath current = *this;
232 FilePath base;
233
234 // Capture path components.
235 while (current != current.DirName()) {
236 base = current.BaseName();
237 if (!AreAllSeparators(base.value()))
238 ret_val.push_back(base.value());
239 current = current.DirName();
240 }
241
242 // Capture root, if any.
243 base = current.BaseName();
244 if (!base.value().empty() && base.value() != kCurrentDirectory)
245 ret_val.push_back(current.BaseName().value());
246
247 // Capture drive letter, if any.
248 FilePath dir = current.DirName();
249 StringType::size_type letter = FindDriveLetter(dir.value());
Jeremy Roman9ec5db92022-02-14 19:46:34250 if (letter != StringType::npos)
251 ret_val.emplace_back(dir.value(), 0, letter + 1);
rafaelw@chromium.org0e4f68db2009-06-24 21:28:30252
Jeremy Roman9ec5db92022-02-14 19:46:34253 ranges::reverse(ret_val);
254 return ret_val;
rafaelw@chromium.org0e4f68db2009-06-24 21:28:30255}
256
257bool FilePath::IsParent(const FilePath& child) const {
Ivan Kotenkova16212a52017-11-08 12:37:33258 return AppendRelativePath(child, nullptr);
mark@chromium.org405a64b2009-09-16 21:03:44259}
260
261bool FilePath::AppendRelativePath(const FilePath& child,
262 FilePath* path) const {
Jeremy Roman9ec5db92022-02-14 19:46:34263 std::vector<StringType> parent_components = GetComponents();
264 std::vector<StringType> child_components = child.GetComponents();
rafaelw@chromium.org0e4f68db2009-06-24 21:28:30265
erg@google.com6775e40a2011-03-04 21:03:47266 if (parent_components.empty() ||
267 parent_components.size() >= child_components.size())
rafaelw@chromium.org0e4f68db2009-06-24 21:28:30268 return false;
269
estade@chromium.org34322912010-07-27 21:24:39270 std::vector<StringType>::const_iterator parent_comp =
rafaelw@chromium.org0e4f68db2009-06-24 21:28:30271 parent_components.begin();
estade@chromium.org34322912010-07-27 21:24:39272 std::vector<StringType>::const_iterator child_comp =
rafaelw@chromium.org0e4f68db2009-06-24 21:28:30273 child_components.begin();
rafaelw@chromium.org62e78f52009-07-01 01:03:34274
275#if defined(FILE_PATH_USES_DRIVE_LETTERS)
276 // Windows can access case sensitive filesystems, so component
277 // comparisions must be case sensitive, but drive letters are
278 // never case sensitive.
estade@chromium.org34322912010-07-27 21:24:39279 if ((FindDriveLetter(*parent_comp) != StringType::npos) &&
280 (FindDriveLetter(*child_comp) != StringType::npos)) {
Jan Wilken Dörrieaf27f302020-05-17 16:35:28281 if (!StartsWith(*parent_comp, *child_comp, CompareCase::INSENSITIVE_ASCII))
rafaelw@chromium.org62e78f52009-07-01 01:03:34282 return false;
283 ++parent_comp;
284 ++child_comp;
285 }
286#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
287
Joel Hockeybc3a2c32021-03-24 08:42:46288 // The first 2 components for network paths are [<2-Separators>, <hostname>].
289 // Use case-insensitive comparison for the hostname.
290 // https://tools.ietf.org/html/rfc3986#section-3.2.2
291 if (IsNetwork() && parent_components.size() > 1) {
292 if (*parent_comp++ != *child_comp++ ||
293 !base::EqualsCaseInsensitiveASCII(*parent_comp++, *child_comp++)) {
294 return false;
295 }
296 }
297
rafaelw@chromium.org0e4f68db2009-06-24 21:28:30298 while (parent_comp != parent_components.end()) {
299 if (*parent_comp != *child_comp)
300 return false;
301 ++parent_comp;
302 ++child_comp;
303 }
304
Ivan Kotenkova16212a52017-11-08 12:37:33305 if (path != nullptr) {
mark@chromium.org405a64b2009-09-16 21:03:44306 for (; child_comp != child_components.end(); ++child_comp) {
307 *path = path->Append(*child_comp);
308 }
309 }
rafaelw@chromium.org0e4f68db2009-06-24 21:28:30310 return true;
311}
312
mark@chromium.org65d00aff2008-10-03 18:21:47313// libgen's dirname and basename aren't guaranteed to be thread-safe and aren't
avi@google.com690a99c2009-01-06 16:48:45314// guaranteed to not modify their input strings, and in fact are implemented
mark@chromium.org65d00aff2008-10-03 18:21:47315// differently in this regard on different platforms. Don't use them, but
316// adhere to their behavior.
317FilePath FilePath::DirName() const {
318 FilePath new_path(path_);
avi@google.com0b733222008-12-11 14:55:12319 new_path.StripTrailingSeparatorsInternal();
mark@chromium.org65d00aff2008-10-03 18:21:47320
321 // The drive letter, if any, always needs to remain in the output. If there
322 // is no drive letter, as will always be the case on platforms which do not
323 // support drive letters, letter will be npos, or -1, so the comparisons and
324 // resizes below using letter will still be valid.
mark@chromium.org1906c032008-12-08 16:49:08325 StringType::size_type letter = FindDriveLetter(new_path.path_);
mark@chromium.org65d00aff2008-10-03 18:21:47326
327 StringType::size_type last_separator =
328 new_path.path_.find_last_of(kSeparators, StringType::npos,
scottmg@chromium.orgf39e03812013-05-21 09:44:02329 kSeparatorsLength - 1);
mark@chromium.org65d00aff2008-10-03 18:21:47330 if (last_separator == StringType::npos) {
331 // path_ is in the current directory.
332 new_path.path_.resize(letter + 1);
333 } else if (last_separator == letter + 1) {
334 // path_ is in the root directory.
335 new_path.path_.resize(letter + 2);
336 } else if (last_separator == letter + 2 &&
337 IsSeparator(new_path.path_[letter + 1])) {
338 // path_ is in "//" (possibly with a drive letter); leave the double
339 // separator intact indicating alternate root.
340 new_path.path_.resize(letter + 3);
341 } else if (last_separator != 0) {
Matthew Denton0cd91172023-03-07 21:42:48342 bool trim_to_basename = true;
343#if BUILDFLAG(IS_POSIX)
344 // On Posix, more than two leading separators are always collapsed to one.
345 // See
346 // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
347 // So, do not strip any of the separators, let
348 // StripTrailingSeparatorsInternal() take care of the extra.
349 if (AreAllSeparators(new_path.path_.substr(0, last_separator + 1))) {
350 new_path.path_.resize(last_separator + 1);
351 trim_to_basename = false;
352 }
353#endif // BUILDFLAG(IS_POSIX)
354 if (trim_to_basename) {
355 // path_ is somewhere else, trim the basename.
356 new_path.path_.resize(last_separator);
357 }
mark@chromium.org65d00aff2008-10-03 18:21:47358 }
359
avi@google.com0b733222008-12-11 14:55:12360 new_path.StripTrailingSeparatorsInternal();
mark@chromium.org65d00aff2008-10-03 18:21:47361 if (!new_path.path_.length())
362 new_path.path_ = kCurrentDirectory;
363
364 return new_path;
365}
366
estade@chromium.org5e207882009-01-05 23:59:36367FilePath FilePath::BaseName() const {
mark@chromium.org65d00aff2008-10-03 18:21:47368 FilePath new_path(path_);
avi@google.com0b733222008-12-11 14:55:12369 new_path.StripTrailingSeparatorsInternal();
mark@chromium.org65d00aff2008-10-03 18:21:47370
371 // The drive letter, if any, is always stripped.
mark@chromium.org1906c032008-12-08 16:49:08372 StringType::size_type letter = FindDriveLetter(new_path.path_);
mark@chromium.org65d00aff2008-10-03 18:21:47373 if (letter != StringType::npos) {
374 new_path.path_.erase(0, letter + 1);
375 }
376
377 // Keep everything after the final separator, but if the pathname is only
378 // one character and it's a separator, leave it alone.
379 StringType::size_type last_separator =
380 new_path.path_.find_last_of(kSeparators, StringType::npos,
scottmg@chromium.orgf39e03812013-05-21 09:44:02381 kSeparatorsLength - 1);
mark@chromium.org65d00aff2008-10-03 18:21:47382 if (last_separator != StringType::npos &&
383 last_separator < new_path.path_.length() - 1) {
384 new_path.path_.erase(0, last_separator + 1);
385 }
386
estade@chromium.org5e207882009-01-05 23:59:36387 return new_path;
mark@chromium.org65d00aff2008-10-03 18:21:47388}
389
estade@chromium.org34322912010-07-27 21:24:39390StringType FilePath::Extension() const {
391 FilePath base(BaseName());
392 const StringType::size_type dot = ExtensionSeparatorPosition(base.path_);
393 if (dot == StringType::npos)
erikkay@google.com235c17a2009-01-09 18:26:19394 return StringType();
395
estade@chromium.org34322912010-07-27 21:24:39396 return base.path_.substr(dot, StringType::npos);
erikkay@google.com235c17a2009-01-09 18:26:19397}
398
davidben@chromium.org18cbba0a2013-12-09 17:06:52399StringType FilePath::FinalExtension() const {
400 FilePath base(BaseName());
401 const StringType::size_type dot = FinalExtensionSeparatorPosition(base.path_);
402 if (dot == StringType::npos)
403 return StringType();
404
405 return base.path_.substr(dot, StringType::npos);
406}
407
erikkay@google.com235c17a2009-01-09 18:26:19408FilePath FilePath::RemoveExtension() const {
estade@chromium.org34322912010-07-27 21:24:39409 if (Extension().empty())
410 return *this;
411
412 const StringType::size_type dot = ExtensionSeparatorPosition(path_);
413 if (dot == StringType::npos)
414 return *this;
415
416 return FilePath(path_.substr(0, dot));
erikkay@google.com235c17a2009-01-09 18:26:19417}
418
davidben@chromium.org18cbba0a2013-12-09 17:06:52419FilePath FilePath::RemoveFinalExtension() const {
420 if (FinalExtension().empty())
421 return *this;
422
423 const StringType::size_type dot = FinalExtensionSeparatorPosition(path_);
424 if (dot == StringType::npos)
425 return *this;
426
427 return FilePath(path_.substr(0, dot));
428}
429
brettw89365dc2015-06-16 05:52:47430FilePath FilePath::InsertBeforeExtension(StringPieceType suffix) const {
erikkay@google.com235c17a2009-01-09 18:26:19431 if (suffix.empty())
432 return FilePath(path_);
433
rdevlin.cronin@chromium.org922828b32012-04-25 01:53:44434 if (IsEmptyOrSpecialCase(BaseName().value()))
erikkay@google.com235c17a2009-01-09 18:26:19435 return FilePath();
436
Jan Wilken Dörriefb1d7b662020-02-04 11:10:23437 return FilePath(
438 base::StrCat({RemoveExtension().value(), suffix, Extension()}));
erikkay@google.com235c17a2009-01-09 18:26:19439}
440
brettw89365dc2015-06-16 05:52:47441FilePath FilePath::InsertBeforeExtensionASCII(StringPiece suffix)
tony@chromium.org8a16266e2009-09-10 21:08:39442 const {
estade@chromium.org73e23482009-08-13 00:26:58443 DCHECK(IsStringASCII(suffix));
Xiaohan Wangb705a64a2022-01-15 18:31:13444#if BUILDFLAG(IS_WIN)
Jan Wilken Dörrie9bb441d2019-11-01 17:48:40445 return InsertBeforeExtension(UTF8ToWide(suffix));
Xiaohan Wangb705a64a2022-01-15 18:31:13446#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
brettw89365dc2015-06-16 05:52:47447 return InsertBeforeExtension(suffix);
estade@chromium.org73e23482009-08-13 00:26:58448#endif
449}
450
brettw89365dc2015-06-16 05:52:47451FilePath FilePath::AddExtension(StringPieceType extension) const {
rdevlin.cronin@chromium.org922828b32012-04-25 01:53:44452 if (IsEmptyOrSpecialCase(BaseName().value()))
erikkay@google.com235c17a2009-01-09 18:26:19453 return FilePath();
454
rdevlin.cronin@chromium.org922828b32012-04-25 01:53:44455 // If the new extension is "" or ".", then just return the current FilePath.
brettw89365dc2015-06-16 05:52:47456 if (extension.empty() ||
457 (extension.size() == 1 && extension[0] == kExtensionSeparator))
rdevlin.cronin@chromium.org922828b32012-04-25 01:53:44458 return *this;
459
460 StringType str = path_;
461 if (extension[0] != kExtensionSeparator &&
462 *(str.end() - 1) != kExtensionSeparator) {
463 str.append(1, kExtensionSeparator);
erikkay@google.com235c17a2009-01-09 18:26:19464 }
David Benjamin2eb24c22023-05-31 15:29:50465 str.append(extension);
rdevlin.cronin@chromium.org922828b32012-04-25 01:53:44466 return FilePath(str);
467}
468
Erik Jensenb54fb9c2018-11-27 23:58:24469FilePath FilePath::AddExtensionASCII(StringPiece extension) const {
470 DCHECK(IsStringASCII(extension));
Xiaohan Wangb705a64a2022-01-15 18:31:13471#if BUILDFLAG(IS_WIN)
Jan Wilken Dörrie9bb441d2019-11-01 17:48:40472 return AddExtension(UTF8ToWide(extension));
Xiaohan Wangb705a64a2022-01-15 18:31:13473#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Erik Jensenb54fb9c2018-11-27 23:58:24474 return AddExtension(extension);
475#endif
476}
477
brettw89365dc2015-06-16 05:52:47478FilePath FilePath::ReplaceExtension(StringPieceType extension) const {
rdevlin.cronin@chromium.org922828b32012-04-25 01:53:44479 if (IsEmptyOrSpecialCase(BaseName().value()))
480 return FilePath();
erikkay@google.com235c17a2009-01-09 18:26:19481
482 FilePath no_ext = RemoveExtension();
483 // If the new extension is "" or ".", then just remove the current extension.
brettw89365dc2015-06-16 05:52:47484 if (extension.empty() ||
485 (extension.size() == 1 && extension[0] == kExtensionSeparator))
erikkay@google.com235c17a2009-01-09 18:26:19486 return no_ext;
487
488 StringType str = no_ext.value();
489 if (extension[0] != kExtensionSeparator)
490 str.append(1, kExtensionSeparator);
David Benjamin2eb24c22023-05-31 15:29:50491 str.append(extension);
erikkay@google.com235c17a2009-01-09 18:26:19492 return FilePath(str);
493}
494
brettw89365dc2015-06-16 05:52:47495bool FilePath::MatchesExtension(StringPieceType extension) const {
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:09496 DCHECK(extension.empty() || extension[0] == kExtensionSeparator);
497
estade@chromium.org34322912010-07-27 21:24:39498 StringType current_extension = Extension();
avi@chromium.orgf1ce6e62009-06-29 20:31:29499
500 if (current_extension.length() != extension.length())
501 return false;
502
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:09503 return FilePath::CompareEqualIgnoreCase(extension, current_extension);
avi@chromium.orgf1ce6e62009-06-29 20:31:29504}
505
Marcello Salomaoc0bd43c2022-02-14 04:49:49506bool FilePath::MatchesFinalExtension(StringPieceType extension) const {
507 DCHECK(extension.empty() || extension[0] == kExtensionSeparator);
508
509 StringType current_final_extension = FinalExtension();
510
511 if (current_final_extension.length() != extension.length())
512 return false;
513
514 return FilePath::CompareEqualIgnoreCase(extension, current_final_extension);
515}
516
brettw89365dc2015-06-16 05:52:47517FilePath FilePath::Append(StringPieceType component) const {
518 StringPieceType appended = component;
aedla@chromium.orgaeae59f2013-01-28 13:47:55519 StringType without_nuls;
520
521 StringType::size_type nul_pos = component.find(kStringTerminator);
brettw89365dc2015-06-16 05:52:47522 if (nul_pos != StringPieceType::npos) {
Jan Wilken Dörriec15b4bc2020-01-30 07:04:12523 without_nuls = StringType(component.substr(0, nul_pos));
brettw89365dc2015-06-16 05:52:47524 appended = StringPieceType(without_nuls);
aedla@chromium.orgaeae59f2013-01-28 13:47:55525 }
526
brettw89365dc2015-06-16 05:52:47527 DCHECK(!IsPathAbsolute(appended));
aedla@chromium.orgaeae59f2013-01-28 13:47:55528
Sami Kyostilaf161e9c52017-06-05 15:12:53529 if (path_.compare(kCurrentDirectory) == 0 && !appended.empty()) {
mark@chromium.org65d00aff2008-10-03 18:21:47530 // Append normally doesn't do any normalization, but as a special case,
531 // when appending to kCurrentDirectory, just return a new path for the
532 // component argument. Appending component to kCurrentDirectory would
533 // serve no purpose other than needlessly lengthening the path, and
534 // it's likely in practice to wind up with FilePath objects containing
535 // only kCurrentDirectory when calling DirName on a single relative path
536 // component.
brettw89365dc2015-06-16 05:52:47537 return FilePath(appended);
mark@chromium.org65d00aff2008-10-03 18:21:47538 }
539
540 FilePath new_path(path_);
avi@google.com0b733222008-12-11 14:55:12541 new_path.StripTrailingSeparatorsInternal();
mark@chromium.org65d00aff2008-10-03 18:21:47542
543 // Don't append a separator if the path is empty (indicating the current
544 // directory) or if the path component is empty (indicating nothing to
545 // append).
pkasting9022cb42016-02-05 00:08:56546 if (!appended.empty() && !new_path.path_.empty()) {
mark@chromium.org65d00aff2008-10-03 18:21:47547 // Don't append a separator if the path still ends with a trailing
548 // separator after stripping (indicating the root directory).
pkasting9022cb42016-02-05 00:08:56549 if (!IsSeparator(new_path.path_.back())) {
mark@chromium.org65d00aff2008-10-03 18:21:47550 // Don't append a separator if the path is just a drive letter.
mark@chromium.org1906c032008-12-08 16:49:08551 if (FindDriveLetter(new_path.path_) + 1 != new_path.path_.length()) {
mark@chromium.org65d00aff2008-10-03 18:21:47552 new_path.path_.append(1, kSeparators[0]);
553 }
554 }
555 }
556
David Benjamin2eb24c22023-05-31 15:29:50557 new_path.path_.append(appended);
mark@chromium.org65d00aff2008-10-03 18:21:47558 return new_path;
559}
560
mark@chromium.org1906c032008-12-08 16:49:08561FilePath FilePath::Append(const FilePath& component) const {
562 return Append(component.value());
mark@chromium.org65d00aff2008-10-03 18:21:47563}
564
Oksana Zhuravlova36356be2021-09-17 00:02:30565FilePath FilePath::Append(const SafeBaseName& component) const {
566 return Append(component.path().value());
567}
568
brettw89365dc2015-06-16 05:52:47569FilePath FilePath::AppendASCII(StringPiece component) const {
brettw@chromium.org527965412014-05-07 14:38:26570 DCHECK(base::IsStringASCII(component));
Xiaohan Wangb705a64a2022-01-15 18:31:13571#if BUILDFLAG(IS_WIN)
Jan Wilken Dörrie9bb441d2019-11-01 17:48:40572 return Append(UTF8ToWide(component));
Xiaohan Wangb705a64a2022-01-15 18:31:13573#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
brettw89365dc2015-06-16 05:52:47574 return Append(component);
erikkay@google.com835c74a32009-01-21 22:15:57575#endif
576}
577
mark@chromium.org65d00aff2008-10-03 18:21:47578bool FilePath::IsAbsolute() const {
mark@chromium.org1906c032008-12-08 16:49:08579 return IsPathAbsolute(path_);
mark@chromium.org65d00aff2008-10-03 18:21:47580}
581
Joel Hockeybc3a2c32021-03-24 08:42:46582bool FilePath::IsNetwork() const {
583 return path_.length() > 1 && FilePath::IsSeparator(path_[0]) &&
584 FilePath::IsSeparator(path_[1]);
585}
586
brettw@chromium.org15476932013-04-12 05:17:15587bool FilePath::EndsWithSeparator() const {
588 if (empty())
589 return false;
thestig84abdbb72016-07-08 01:41:06590 return IsSeparator(path_.back());
brettw@chromium.org15476932013-04-12 05:17:15591}
592
593FilePath FilePath::AsEndingWithSeparator() const {
594 if (EndsWithSeparator() || path_.empty())
595 return *this;
596
597 StringType path_str;
598 path_str.reserve(path_.length() + 1); // Only allocate string once.
599
600 path_str = path_;
601 path_str.append(&kSeparators[0], 1);
602 return FilePath(path_str);
603}
604
erg@google.com9989c9bb2011-01-07 20:23:43605FilePath FilePath::StripTrailingSeparators() const {
606 FilePath new_path(path_);
607 new_path.StripTrailingSeparatorsInternal();
608
609 return new_path;
610}
611
612bool FilePath::ReferencesParent() const {
jkarlinf95b1d92016-11-11 20:44:33613 if (path_.find(kParentDirectory) == StringType::npos) {
614 // GetComponents is quite expensive, so avoid calling it in the majority
615 // of cases where there isn't a kParentDirectory anywhere in the path.
616 return false;
617 }
618
Jeremy Roman9ec5db92022-02-14 19:46:34619 std::vector<StringType> components = GetComponents();
davidben19011ca2015-12-01 06:46:00620 std::vector<StringType>::const_iterator it = components.begin();
621 for (; it != components.end(); ++it) {
622 const StringType& component = *it;
jschuh@chromium.orga00f5452013-05-17 02:27:46623 // Windows has odd, undocumented behavior with path components containing
624 // only whitespace and . characters. So, if all we see is . and
625 // whitespace, then we treat any .. sequence as referencing parent.
davidben19011ca2015-12-01 06:46:00626 // For simplicity we enforce this on all platforms.
jschuh@chromium.orga00f5452013-05-17 02:27:46627 if (component.find_first_not_of(FILE_PATH_LITERAL(". \n\r\t")) ==
628 std::string::npos &&
629 component.find(kParentDirectory) != std::string::npos) {
erg@google.com9989c9bb2011-01-07 20:23:43630 return true;
jschuh@chromium.orga00f5452013-05-17 02:27:46631 }
erg@google.com9989c9bb2011-01-07 20:23:43632 }
633 return false;
634}
635
Xiaohan Wangb705a64a2022-01-15 18:31:13636#if BUILDFLAG(IS_WIN)
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39637
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57638std::u16string FilePath::LossyDisplayName() const {
Jan Wilken Dörrieaf27f302020-05-17 16:35:28639 return AsString16(path_);
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39640}
641
642std::string FilePath::MaybeAsASCII() const {
Jan Wilken Dörrieaf27f302020-05-17 16:35:28643 return base::IsStringASCII(path_) ? WideToASCII(path_) : std::string();
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39644}
645
646std::string FilePath::AsUTF8Unsafe() const {
Jan Wilken Dörrieaf27f302020-05-17 16:35:28647 return WideToUTF8(value());
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39648}
649
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57650std::u16string FilePath::AsUTF16Unsafe() const {
Jan Wilken Dörrieaf27f302020-05-17 16:35:28651 return WideToUTF16(value());
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39652}
653
654// static
Maksim Ivanov716f1f22021-09-29 09:31:31655FilePath FilePath::FromASCII(StringPiece ascii) {
656 DCHECK(base::IsStringASCII(ascii));
657 return FilePath(ASCIIToWide(ascii));
658}
659
660// static
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39661FilePath FilePath::FromUTF8Unsafe(StringPiece utf8) {
Jan Wilken Dörrie9bb441d2019-11-01 17:48:40662 return FilePath(UTF8ToWide(utf8));
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39663}
664
665// static
666FilePath FilePath::FromUTF16Unsafe(StringPiece16 utf16) {
Lei Zhang692788372023-11-08 19:37:28667 return FilePath(AsWStringView(utf16));
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39668}
669
Xiaohan Wangb705a64a2022-01-15 18:31:13670#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39671
erg@google.com9989c9bb2011-01-07 20:23:43672// See file_path.h for a discussion of the encoding of paths on POSIX
evan@chromium.org8a205c02011-02-04 20:41:33673// platforms. These encoding conversion functions are not quite correct.
674
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57675std::u16string FilePath::LossyDisplayName() const {
brettw@chromium.org04af979a2013-02-16 04:12:26676 return WideToUTF16(SysNativeMBToWide(path_));
evan@chromium.org8a205c02011-02-04 20:41:33677}
678
evan@chromium.org1f34afe42011-03-01 21:38:51679std::string FilePath::MaybeAsASCII() const {
brettw@chromium.org527965412014-05-07 14:38:26680 if (base::IsStringASCII(path_))
evan@chromium.org1f34afe42011-03-01 21:38:51681 return path_;
dcheng@chromium.org007b3f82013-04-09 08:46:45682 return std::string();
evan@chromium.org1f34afe42011-03-01 21:38:51683}
684
satorux@chromium.org45440512011-11-02 04:55:23685std::string FilePath::AsUTF8Unsafe() const {
halliwellbd52a8a12014-12-12 17:21:04686#if defined(SYSTEM_NATIVE_UTF8)
satorux@chromium.org45440512011-11-02 04:55:23687 return value();
688#else
brettw@chromium.org04af979a2013-02-16 04:12:26689 return WideToUTF8(SysNativeMBToWide(value()));
satorux@chromium.org45440512011-11-02 04:55:23690#endif
691}
692
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57693std::u16string FilePath::AsUTF16Unsafe() const {
halliwellbd52a8a12014-12-12 17:21:04694#if defined(SYSTEM_NATIVE_UTF8)
darin@chromium.org65e269f2013-06-20 21:29:54695 return UTF8ToUTF16(value());
696#else
697 return WideToUTF16(SysNativeMBToWide(value()));
698#endif
699}
700
satorux@chromium.org45440512011-11-02 04:55:23701// static
Maksim Ivanov716f1f22021-09-29 09:31:31702FilePath FilePath::FromASCII(StringPiece ascii) {
703 DCHECK(base::IsStringASCII(ascii));
704 return FilePath(ascii);
705}
706
707// static
kinukob473f002016-02-22 05:23:19708FilePath FilePath::FromUTF8Unsafe(StringPiece utf8) {
halliwellbd52a8a12014-12-12 17:21:04709#if defined(SYSTEM_NATIVE_UTF8)
satorux@chromium.org45440512011-11-02 04:55:23710 return FilePath(utf8);
711#else
brettw@chromium.org04af979a2013-02-16 04:12:26712 return FilePath(SysWideToNativeMB(UTF8ToWide(utf8)));
satorux@chromium.org45440512011-11-02 04:55:23713#endif
714}
715
darin@chromium.org65e269f2013-06-20 21:29:54716// static
kinukob473f002016-02-22 05:23:19717FilePath FilePath::FromUTF16Unsafe(StringPiece16 utf16) {
halliwellbd52a8a12014-12-12 17:21:04718#if defined(SYSTEM_NATIVE_UTF8)
darin@chromium.org65e269f2013-06-20 21:29:54719 return FilePath(UTF16ToUTF8(utf16));
720#else
Jan Wilken Dörrie4aa966b2020-08-24 18:20:29721 return FilePath(SysWideToNativeMB(UTF16ToWide(utf16)));
darin@chromium.org65e269f2013-06-20 21:29:54722#endif
723}
724
Xiaohan Wangb705a64a2022-01-15 18:31:13725#endif // BUILDFLAG(IS_WIN)
erg@google.com9989c9bb2011-01-07 20:23:43726
aedla@chromium.orgaeae59f2013-01-28 13:47:55727void FilePath::WriteToPickle(Pickle* pickle) const {
Xiaohan Wangb705a64a2022-01-15 18:31:13728#if BUILDFLAG(IS_WIN)
Jan Wilken Dörrieaf27f302020-05-17 16:35:28729 pickle->WriteString16(AsStringPiece16(path_));
Xiaohan Wangb705a64a2022-01-15 18:31:13730#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
evan@chromium.org8ce9e492011-05-31 18:04:33731 pickle->WriteString(path_);
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39732#else
733#error Unsupported platform
evan@chromium.org8ce9e492011-05-31 18:04:33734#endif
erg@google.com9989c9bb2011-01-07 20:23:43735}
736
jbates@chromium.orgce208f872012-03-07 20:42:56737bool FilePath::ReadFromPickle(PickleIterator* iter) {
Xiaohan Wangb705a64a2022-01-15 18:31:13738#if BUILDFLAG(IS_WIN)
Jan Wilken Dörrie85285b02021-03-11 23:38:47739 std::u16string path;
Jan Wilken Dörrie9bb441d2019-11-01 17:48:40740 if (!iter->ReadString16(&path))
evan@chromium.org8ce9e492011-05-31 18:04:33741 return false;
Jan Wilken Dörrie9bb441d2019-11-01 17:48:40742 path_ = UTF16ToWide(path);
Xiaohan Wangb705a64a2022-01-15 18:31:13743#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
jbates@chromium.orgce208f872012-03-07 20:42:56744 if (!iter->ReadString(&path_))
evan@chromium.org8ce9e492011-05-31 18:04:33745 return false;
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39746#else
747#error Unsupported platform
evan@chromium.org8ce9e492011-05-31 18:04:33748#endif
749
aedla@chromium.orgaeae59f2013-01-28 13:47:55750 if (path_.find(kStringTerminator) != StringType::npos)
751 return false;
752
evan@chromium.org8ce9e492011-05-31 18:04:33753 return true;
erg@google.com9989c9bb2011-01-07 20:23:43754}
755
Xiaohan Wangb705a64a2022-01-15 18:31:13756#if BUILDFLAG(IS_WIN)
brettw89365dc2015-06-16 05:52:47757// Windows specific implementation of file string comparisons.
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:09758
brettw89365dc2015-06-16 05:52:47759int FilePath::CompareIgnoreCase(StringPieceType string1,
760 StringPieceType string2) {
Cliff Smolinskyc5c52102019-05-03 20:51:54761 // CharUpperW within user32 is used here because it will provide unicode
762 // conversions regardless of locale. The STL alternative, towupper, has a
763 // locale consideration that prevents it from converting all characters by
764 // default.
765 CHECK(win::IsUser32AndGdi32Available());
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:09766 // Perform character-wise upper case comparison rather than using the
767 // fully Unicode-aware CompareString(). For details see:
768 // http://blogs.msdn.com/michkap/archive/2005/10/17/481600.aspx
brettw89365dc2015-06-16 05:52:47769 StringPieceType::const_iterator i1 = string1.begin();
770 StringPieceType::const_iterator i2 = string2.begin();
771 StringPieceType::const_iterator string1end = string1.end();
772 StringPieceType::const_iterator string2end = string2.end();
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:09773 for ( ; i1 != string1end && i2 != string2end; ++i1, ++i2) {
thakis@chromium.orgff5f0892014-08-20 20:12:12774 wchar_t c1 =
Cliff Smolinskyc5c52102019-05-03 20:51:54775 (wchar_t)LOWORD(::CharUpperW((LPWSTR)(DWORD_PTR)MAKELONG(*i1, 0)));
thakis@chromium.orgff5f0892014-08-20 20:12:12776 wchar_t c2 =
Cliff Smolinskyc5c52102019-05-03 20:51:54777 (wchar_t)LOWORD(::CharUpperW((LPWSTR)(DWORD_PTR)MAKELONG(*i2, 0)));
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:09778 if (c1 < c2)
779 return -1;
780 if (c1 > c2)
781 return 1;
782 }
783 if (i1 != string1end)
784 return 1;
785 if (i2 != string2end)
786 return -1;
787 return 0;
788}
789
Xiaohan Wangb705a64a2022-01-15 18:31:13790#elif BUILDFLAG(IS_APPLE)
brettw89365dc2015-06-16 05:52:47791// Mac OS X specific implementation of file string comparisons.
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:09792
Arthur Sonzogni48785f62023-05-02 09:40:37793// cf. https://developer.apple.com/library/archive/technotes/tn/tn1150.html#UnicodeSubtleties
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:09794//
795// "When using CreateTextEncoding to create a text encoding, you should set
796// the TextEncodingBase to kTextEncodingUnicodeV2_0, set the
797// TextEncodingVariant to kUnicodeCanonicalDecompVariant, and set the
798// TextEncodingFormat to kUnicode16BitFormat. Using these values ensures that
799// the Unicode will be in the same form as on an HFS Plus volume, even as the
800// Unicode standard evolves."
801//
802// Another technical article for X 10.4 updates this: one should use
803// the new (unambiguous) kUnicodeHFSPlusDecompVariant.
804// cf. http://developer.apple.com/mac/library/releasenotes/TextFonts/RN-TEC/index.html
805//
806// This implementation uses CFStringGetFileSystemRepresentation() to get the
807// decomposed form, and an adapted version of the FastUnicodeCompare as
808// described in the tech note to compare the strings.
809
810// Character conversion table for FastUnicodeCompare()
811//
812// The lower case table consists of a 256-entry high-byte table followed by
813// some number of 256-entry subtables. The high-byte table contains either an
814// offset to the subtable for characters with that high byte or zero, which
815// means that there are no case mappings or ignored characters in that block.
816// Ignored characters are mapped to zero.
817//
818// cf. downloadable file linked in
Arthur Sonzogni48785f62023-05-02 09:40:37819// https://developer.apple.com/library/archive/technotes/tn/tn1150.html#Downloads
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:09820
821namespace {
822
Arthur Sonzogni48785f62023-05-02 09:40:37823// clang-format off
824const UInt16 lower_case_table[11 * 256] = {
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:09825 // High-byte indices ( == 0 iff no case mapping and no ignorables )
826
827 /* 0 */ 0x0100, 0x0200, 0x0000, 0x0300, 0x0400, 0x0500, 0x0000, 0x0000,
828 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
829 /* 1 */ 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
830 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
831 /* 2 */ 0x0700, 0x0800, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
832 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
833 /* 3 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
834 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
835 /* 4 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
836 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
837 /* 5 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
838 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
839 /* 6 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
840 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
841 /* 7 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
842 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
843 /* 8 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
844 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
845 /* 9 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
846 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
847 /* A */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
848 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
849 /* B */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
850 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
851 /* C */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
852 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
853 /* D */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
854 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
855 /* E */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
856 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
857 /* F */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
858 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0900, 0x0A00,
859
860 // Table 1 (for high byte 0x00)
861
862 /* 0 */ 0xFFFF, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
863 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
864 /* 1 */ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
865 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
866 /* 2 */ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
867 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
868 /* 3 */ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
869 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
870 /* 4 */ 0x0040, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
871 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
872 /* 5 */ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
873 0x0078, 0x0079, 0x007A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
874 /* 6 */ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
875 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
876 /* 7 */ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
877 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
878 /* 8 */ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
879 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
880 /* 9 */ 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
881 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
882 /* A */ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
883 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
884 /* B */ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
885 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
886 /* C */ 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00E6, 0x00C7,
887 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
888 /* D */ 0x00F0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
889 0x00F8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00FE, 0x00DF,
890 /* E */ 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
891 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
892 /* F */ 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
893 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF,
894
895 // Table 2 (for high byte 0x01)
896
897 /* 0 */ 0x0100, 0x0101, 0x0102, 0x0103, 0x0104, 0x0105, 0x0106, 0x0107,
898 0x0108, 0x0109, 0x010A, 0x010B, 0x010C, 0x010D, 0x010E, 0x010F,
899 /* 1 */ 0x0111, 0x0111, 0x0112, 0x0113, 0x0114, 0x0115, 0x0116, 0x0117,
900 0x0118, 0x0119, 0x011A, 0x011B, 0x011C, 0x011D, 0x011E, 0x011F,
901 /* 2 */ 0x0120, 0x0121, 0x0122, 0x0123, 0x0124, 0x0125, 0x0127, 0x0127,
902 0x0128, 0x0129, 0x012A, 0x012B, 0x012C, 0x012D, 0x012E, 0x012F,
903 /* 3 */ 0x0130, 0x0131, 0x0133, 0x0133, 0x0134, 0x0135, 0x0136, 0x0137,
904 0x0138, 0x0139, 0x013A, 0x013B, 0x013C, 0x013D, 0x013E, 0x0140,
905 /* 4 */ 0x0140, 0x0142, 0x0142, 0x0143, 0x0144, 0x0145, 0x0146, 0x0147,
906 0x0148, 0x0149, 0x014B, 0x014B, 0x014C, 0x014D, 0x014E, 0x014F,
907 /* 5 */ 0x0150, 0x0151, 0x0153, 0x0153, 0x0154, 0x0155, 0x0156, 0x0157,
908 0x0158, 0x0159, 0x015A, 0x015B, 0x015C, 0x015D, 0x015E, 0x015F,
909 /* 6 */ 0x0160, 0x0161, 0x0162, 0x0163, 0x0164, 0x0165, 0x0167, 0x0167,
910 0x0168, 0x0169, 0x016A, 0x016B, 0x016C, 0x016D, 0x016E, 0x016F,
911 /* 7 */ 0x0170, 0x0171, 0x0172, 0x0173, 0x0174, 0x0175, 0x0176, 0x0177,
912 0x0178, 0x0179, 0x017A, 0x017B, 0x017C, 0x017D, 0x017E, 0x017F,
913 /* 8 */ 0x0180, 0x0253, 0x0183, 0x0183, 0x0185, 0x0185, 0x0254, 0x0188,
914 0x0188, 0x0256, 0x0257, 0x018C, 0x018C, 0x018D, 0x01DD, 0x0259,
915 /* 9 */ 0x025B, 0x0192, 0x0192, 0x0260, 0x0263, 0x0195, 0x0269, 0x0268,
916 0x0199, 0x0199, 0x019A, 0x019B, 0x026F, 0x0272, 0x019E, 0x0275,
917 /* A */ 0x01A0, 0x01A1, 0x01A3, 0x01A3, 0x01A5, 0x01A5, 0x01A6, 0x01A8,
918 0x01A8, 0x0283, 0x01AA, 0x01AB, 0x01AD, 0x01AD, 0x0288, 0x01AF,
919 /* B */ 0x01B0, 0x028A, 0x028B, 0x01B4, 0x01B4, 0x01B6, 0x01B6, 0x0292,
920 0x01B9, 0x01B9, 0x01BA, 0x01BB, 0x01BD, 0x01BD, 0x01BE, 0x01BF,
921 /* C */ 0x01C0, 0x01C1, 0x01C2, 0x01C3, 0x01C6, 0x01C6, 0x01C6, 0x01C9,
922 0x01C9, 0x01C9, 0x01CC, 0x01CC, 0x01CC, 0x01CD, 0x01CE, 0x01CF,
923 /* D */ 0x01D0, 0x01D1, 0x01D2, 0x01D3, 0x01D4, 0x01D5, 0x01D6, 0x01D7,
924 0x01D8, 0x01D9, 0x01DA, 0x01DB, 0x01DC, 0x01DD, 0x01DE, 0x01DF,
925 /* E */ 0x01E0, 0x01E1, 0x01E2, 0x01E3, 0x01E5, 0x01E5, 0x01E6, 0x01E7,
926 0x01E8, 0x01E9, 0x01EA, 0x01EB, 0x01EC, 0x01ED, 0x01EE, 0x01EF,
927 /* F */ 0x01F0, 0x01F3, 0x01F3, 0x01F3, 0x01F4, 0x01F5, 0x01F6, 0x01F7,
928 0x01F8, 0x01F9, 0x01FA, 0x01FB, 0x01FC, 0x01FD, 0x01FE, 0x01FF,
929
930 // Table 3 (for high byte 0x03)
931
932 /* 0 */ 0x0300, 0x0301, 0x0302, 0x0303, 0x0304, 0x0305, 0x0306, 0x0307,
933 0x0308, 0x0309, 0x030A, 0x030B, 0x030C, 0x030D, 0x030E, 0x030F,
934 /* 1 */ 0x0310, 0x0311, 0x0312, 0x0313, 0x0314, 0x0315, 0x0316, 0x0317,
935 0x0318, 0x0319, 0x031A, 0x031B, 0x031C, 0x031D, 0x031E, 0x031F,
936 /* 2 */ 0x0320, 0x0321, 0x0322, 0x0323, 0x0324, 0x0325, 0x0326, 0x0327,
937 0x0328, 0x0329, 0x032A, 0x032B, 0x032C, 0x032D, 0x032E, 0x032F,
938 /* 3 */ 0x0330, 0x0331, 0x0332, 0x0333, 0x0334, 0x0335, 0x0336, 0x0337,
939 0x0338, 0x0339, 0x033A, 0x033B, 0x033C, 0x033D, 0x033E, 0x033F,
940 /* 4 */ 0x0340, 0x0341, 0x0342, 0x0343, 0x0344, 0x0345, 0x0346, 0x0347,
941 0x0348, 0x0349, 0x034A, 0x034B, 0x034C, 0x034D, 0x034E, 0x034F,
942 /* 5 */ 0x0350, 0x0351, 0x0352, 0x0353, 0x0354, 0x0355, 0x0356, 0x0357,
943 0x0358, 0x0359, 0x035A, 0x035B, 0x035C, 0x035D, 0x035E, 0x035F,
944 /* 6 */ 0x0360, 0x0361, 0x0362, 0x0363, 0x0364, 0x0365, 0x0366, 0x0367,
945 0x0368, 0x0369, 0x036A, 0x036B, 0x036C, 0x036D, 0x036E, 0x036F,
946 /* 7 */ 0x0370, 0x0371, 0x0372, 0x0373, 0x0374, 0x0375, 0x0376, 0x0377,
947 0x0378, 0x0379, 0x037A, 0x037B, 0x037C, 0x037D, 0x037E, 0x037F,
948 /* 8 */ 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0386, 0x0387,
949 0x0388, 0x0389, 0x038A, 0x038B, 0x038C, 0x038D, 0x038E, 0x038F,
950 /* 9 */ 0x0390, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7,
951 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
952 /* A */ 0x03C0, 0x03C1, 0x03A2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,
953 0x03C8, 0x03C9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF,
954 /* B */ 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7,
955 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
956 /* C */ 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,
957 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, 0x03CF,
958 /* D */ 0x03D0, 0x03D1, 0x03D2, 0x03D3, 0x03D4, 0x03D5, 0x03D6, 0x03D7,
959 0x03D8, 0x03D9, 0x03DA, 0x03DB, 0x03DC, 0x03DD, 0x03DE, 0x03DF,
960 /* E */ 0x03E0, 0x03E1, 0x03E3, 0x03E3, 0x03E5, 0x03E5, 0x03E7, 0x03E7,
961 0x03E9, 0x03E9, 0x03EB, 0x03EB, 0x03ED, 0x03ED, 0x03EF, 0x03EF,
962 /* F */ 0x03F0, 0x03F1, 0x03F2, 0x03F3, 0x03F4, 0x03F5, 0x03F6, 0x03F7,
963 0x03F8, 0x03F9, 0x03FA, 0x03FB, 0x03FC, 0x03FD, 0x03FE, 0x03FF,
964
965 // Table 4 (for high byte 0x04)
966
967 /* 0 */ 0x0400, 0x0401, 0x0452, 0x0403, 0x0454, 0x0455, 0x0456, 0x0407,
968 0x0458, 0x0459, 0x045A, 0x045B, 0x040C, 0x040D, 0x040E, 0x045F,
969 /* 1 */ 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
970 0x0438, 0x0419, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
971 /* 2 */ 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
972 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
973 /* 3 */ 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
974 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
975 /* 4 */ 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
976 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
977 /* 5 */ 0x0450, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457,
978 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x045D, 0x045E, 0x045F,
979 /* 6 */ 0x0461, 0x0461, 0x0463, 0x0463, 0x0465, 0x0465, 0x0467, 0x0467,
980 0x0469, 0x0469, 0x046B, 0x046B, 0x046D, 0x046D, 0x046F, 0x046F,
981 /* 7 */ 0x0471, 0x0471, 0x0473, 0x0473, 0x0475, 0x0475, 0x0476, 0x0477,
982 0x0479, 0x0479, 0x047B, 0x047B, 0x047D, 0x047D, 0x047F, 0x047F,
983 /* 8 */ 0x0481, 0x0481, 0x0482, 0x0483, 0x0484, 0x0485, 0x0486, 0x0487,
984 0x0488, 0x0489, 0x048A, 0x048B, 0x048C, 0x048D, 0x048E, 0x048F,
985 /* 9 */ 0x0491, 0x0491, 0x0493, 0x0493, 0x0495, 0x0495, 0x0497, 0x0497,
986 0x0499, 0x0499, 0x049B, 0x049B, 0x049D, 0x049D, 0x049F, 0x049F,
987 /* A */ 0x04A1, 0x04A1, 0x04A3, 0x04A3, 0x04A5, 0x04A5, 0x04A7, 0x04A7,
988 0x04A9, 0x04A9, 0x04AB, 0x04AB, 0x04AD, 0x04AD, 0x04AF, 0x04AF,
989 /* B */ 0x04B1, 0x04B1, 0x04B3, 0x04B3, 0x04B5, 0x04B5, 0x04B7, 0x04B7,
990 0x04B9, 0x04B9, 0x04BB, 0x04BB, 0x04BD, 0x04BD, 0x04BF, 0x04BF,
991 /* C */ 0x04C0, 0x04C1, 0x04C2, 0x04C4, 0x04C4, 0x04C5, 0x04C6, 0x04C8,
992 0x04C8, 0x04C9, 0x04CA, 0x04CC, 0x04CC, 0x04CD, 0x04CE, 0x04CF,
993 /* D */ 0x04D0, 0x04D1, 0x04D2, 0x04D3, 0x04D4, 0x04D5, 0x04D6, 0x04D7,
994 0x04D8, 0x04D9, 0x04DA, 0x04DB, 0x04DC, 0x04DD, 0x04DE, 0x04DF,
995 /* E */ 0x04E0, 0x04E1, 0x04E2, 0x04E3, 0x04E4, 0x04E5, 0x04E6, 0x04E7,
996 0x04E8, 0x04E9, 0x04EA, 0x04EB, 0x04EC, 0x04ED, 0x04EE, 0x04EF,
997 /* F */ 0x04F0, 0x04F1, 0x04F2, 0x04F3, 0x04F4, 0x04F5, 0x04F6, 0x04F7,
998 0x04F8, 0x04F9, 0x04FA, 0x04FB, 0x04FC, 0x04FD, 0x04FE, 0x04FF,
999
1000 // Table 5 (for high byte 0x05)
1001
1002 /* 0 */ 0x0500, 0x0501, 0x0502, 0x0503, 0x0504, 0x0505, 0x0506, 0x0507,
1003 0x0508, 0x0509, 0x050A, 0x050B, 0x050C, 0x050D, 0x050E, 0x050F,
1004 /* 1 */ 0x0510, 0x0511, 0x0512, 0x0513, 0x0514, 0x0515, 0x0516, 0x0517,
1005 0x0518, 0x0519, 0x051A, 0x051B, 0x051C, 0x051D, 0x051E, 0x051F,
1006 /* 2 */ 0x0520, 0x0521, 0x0522, 0x0523, 0x0524, 0x0525, 0x0526, 0x0527,
1007 0x0528, 0x0529, 0x052A, 0x052B, 0x052C, 0x052D, 0x052E, 0x052F,
1008 /* 3 */ 0x0530, 0x0561, 0x0562, 0x0563, 0x0564, 0x0565, 0x0566, 0x0567,
1009 0x0568, 0x0569, 0x056A, 0x056B, 0x056C, 0x056D, 0x056E, 0x056F,
1010 /* 4 */ 0x0570, 0x0571, 0x0572, 0x0573, 0x0574, 0x0575, 0x0576, 0x0577,
1011 0x0578, 0x0579, 0x057A, 0x057B, 0x057C, 0x057D, 0x057E, 0x057F,
1012 /* 5 */ 0x0580, 0x0581, 0x0582, 0x0583, 0x0584, 0x0585, 0x0586, 0x0557,
1013 0x0558, 0x0559, 0x055A, 0x055B, 0x055C, 0x055D, 0x055E, 0x055F,
1014 /* 6 */ 0x0560, 0x0561, 0x0562, 0x0563, 0x0564, 0x0565, 0x0566, 0x0567,
1015 0x0568, 0x0569, 0x056A, 0x056B, 0x056C, 0x056D, 0x056E, 0x056F,
1016 /* 7 */ 0x0570, 0x0571, 0x0572, 0x0573, 0x0574, 0x0575, 0x0576, 0x0577,
1017 0x0578, 0x0579, 0x057A, 0x057B, 0x057C, 0x057D, 0x057E, 0x057F,
1018 /* 8 */ 0x0580, 0x0581, 0x0582, 0x0583, 0x0584, 0x0585, 0x0586, 0x0587,
1019 0x0588, 0x0589, 0x058A, 0x058B, 0x058C, 0x058D, 0x058E, 0x058F,
1020 /* 9 */ 0x0590, 0x0591, 0x0592, 0x0593, 0x0594, 0x0595, 0x0596, 0x0597,
1021 0x0598, 0x0599, 0x059A, 0x059B, 0x059C, 0x059D, 0x059E, 0x059F,
1022 /* A */ 0x05A0, 0x05A1, 0x05A2, 0x05A3, 0x05A4, 0x05A5, 0x05A6, 0x05A7,
1023 0x05A8, 0x05A9, 0x05AA, 0x05AB, 0x05AC, 0x05AD, 0x05AE, 0x05AF,
1024 /* B */ 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7,
1025 0x05B8, 0x05B9, 0x05BA, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF,
1026 /* C */ 0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05C4, 0x05C5, 0x05C6, 0x05C7,
1027 0x05C8, 0x05C9, 0x05CA, 0x05CB, 0x05CC, 0x05CD, 0x05CE, 0x05CF,
1028 /* D */ 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7,
1029 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
1030 /* E */ 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,
1031 0x05E8, 0x05E9, 0x05EA, 0x05EB, 0x05EC, 0x05ED, 0x05EE, 0x05EF,
1032 /* F */ 0x05F0, 0x05F1, 0x05F2, 0x05F3, 0x05F4, 0x05F5, 0x05F6, 0x05F7,
1033 0x05F8, 0x05F9, 0x05FA, 0x05FB, 0x05FC, 0x05FD, 0x05FE, 0x05FF,
1034
1035 // Table 6 (for high byte 0x10)
1036
1037 /* 0 */ 0x1000, 0x1001, 0x1002, 0x1003, 0x1004, 0x1005, 0x1006, 0x1007,
1038 0x1008, 0x1009, 0x100A, 0x100B, 0x100C, 0x100D, 0x100E, 0x100F,
1039 /* 1 */ 0x1010, 0x1011, 0x1012, 0x1013, 0x1014, 0x1015, 0x1016, 0x1017,
1040 0x1018, 0x1019, 0x101A, 0x101B, 0x101C, 0x101D, 0x101E, 0x101F,
1041 /* 2 */ 0x1020, 0x1021, 0x1022, 0x1023, 0x1024, 0x1025, 0x1026, 0x1027,
1042 0x1028, 0x1029, 0x102A, 0x102B, 0x102C, 0x102D, 0x102E, 0x102F,
1043 /* 3 */ 0x1030, 0x1031, 0x1032, 0x1033, 0x1034, 0x1035, 0x1036, 0x1037,
1044 0x1038, 0x1039, 0x103A, 0x103B, 0x103C, 0x103D, 0x103E, 0x103F,
1045 /* 4 */ 0x1040, 0x1041, 0x1042, 0x1043, 0x1044, 0x1045, 0x1046, 0x1047,
1046 0x1048, 0x1049, 0x104A, 0x104B, 0x104C, 0x104D, 0x104E, 0x104F,
1047 /* 5 */ 0x1050, 0x1051, 0x1052, 0x1053, 0x1054, 0x1055, 0x1056, 0x1057,
1048 0x1058, 0x1059, 0x105A, 0x105B, 0x105C, 0x105D, 0x105E, 0x105F,
1049 /* 6 */ 0x1060, 0x1061, 0x1062, 0x1063, 0x1064, 0x1065, 0x1066, 0x1067,
1050 0x1068, 0x1069, 0x106A, 0x106B, 0x106C, 0x106D, 0x106E, 0x106F,
1051 /* 7 */ 0x1070, 0x1071, 0x1072, 0x1073, 0x1074, 0x1075, 0x1076, 0x1077,
1052 0x1078, 0x1079, 0x107A, 0x107B, 0x107C, 0x107D, 0x107E, 0x107F,
1053 /* 8 */ 0x1080, 0x1081, 0x1082, 0x1083, 0x1084, 0x1085, 0x1086, 0x1087,
1054 0x1088, 0x1089, 0x108A, 0x108B, 0x108C, 0x108D, 0x108E, 0x108F,
1055 /* 9 */ 0x1090, 0x1091, 0x1092, 0x1093, 0x1094, 0x1095, 0x1096, 0x1097,
1056 0x1098, 0x1099, 0x109A, 0x109B, 0x109C, 0x109D, 0x109E, 0x109F,
1057 /* A */ 0x10D0, 0x10D1, 0x10D2, 0x10D3, 0x10D4, 0x10D5, 0x10D6, 0x10D7,
1058 0x10D8, 0x10D9, 0x10DA, 0x10DB, 0x10DC, 0x10DD, 0x10DE, 0x10DF,
1059 /* B */ 0x10E0, 0x10E1, 0x10E2, 0x10E3, 0x10E4, 0x10E5, 0x10E6, 0x10E7,
1060 0x10E8, 0x10E9, 0x10EA, 0x10EB, 0x10EC, 0x10ED, 0x10EE, 0x10EF,
1061 /* C */ 0x10F0, 0x10F1, 0x10F2, 0x10F3, 0x10F4, 0x10F5, 0x10C6, 0x10C7,
1062 0x10C8, 0x10C9, 0x10CA, 0x10CB, 0x10CC, 0x10CD, 0x10CE, 0x10CF,
1063 /* D */ 0x10D0, 0x10D1, 0x10D2, 0x10D3, 0x10D4, 0x10D5, 0x10D6, 0x10D7,
1064 0x10D8, 0x10D9, 0x10DA, 0x10DB, 0x10DC, 0x10DD, 0x10DE, 0x10DF,
1065 /* E */ 0x10E0, 0x10E1, 0x10E2, 0x10E3, 0x10E4, 0x10E5, 0x10E6, 0x10E7,
1066 0x10E8, 0x10E9, 0x10EA, 0x10EB, 0x10EC, 0x10ED, 0x10EE, 0x10EF,
1067 /* F */ 0x10F0, 0x10F1, 0x10F2, 0x10F3, 0x10F4, 0x10F5, 0x10F6, 0x10F7,
1068 0x10F8, 0x10F9, 0x10FA, 0x10FB, 0x10FC, 0x10FD, 0x10FE, 0x10FF,
1069
1070 // Table 7 (for high byte 0x20)
1071
1072 /* 0 */ 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007,
1073 0x2008, 0x2009, 0x200A, 0x200B, 0x0000, 0x0000, 0x0000, 0x0000,
1074 /* 1 */ 0x2010, 0x2011, 0x2012, 0x2013, 0x2014, 0x2015, 0x2016, 0x2017,
1075 0x2018, 0x2019, 0x201A, 0x201B, 0x201C, 0x201D, 0x201E, 0x201F,
1076 /* 2 */ 0x2020, 0x2021, 0x2022, 0x2023, 0x2024, 0x2025, 0x2026, 0x2027,
1077 0x2028, 0x2029, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x202F,
1078 /* 3 */ 0x2030, 0x2031, 0x2032, 0x2033, 0x2034, 0x2035, 0x2036, 0x2037,
1079 0x2038, 0x2039, 0x203A, 0x203B, 0x203C, 0x203D, 0x203E, 0x203F,
1080 /* 4 */ 0x2040, 0x2041, 0x2042, 0x2043, 0x2044, 0x2045, 0x2046, 0x2047,
1081 0x2048, 0x2049, 0x204A, 0x204B, 0x204C, 0x204D, 0x204E, 0x204F,
1082 /* 5 */ 0x2050, 0x2051, 0x2052, 0x2053, 0x2054, 0x2055, 0x2056, 0x2057,
1083 0x2058, 0x2059, 0x205A, 0x205B, 0x205C, 0x205D, 0x205E, 0x205F,
1084 /* 6 */ 0x2060, 0x2061, 0x2062, 0x2063, 0x2064, 0x2065, 0x2066, 0x2067,
1085 0x2068, 0x2069, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
1086 /* 7 */ 0x2070, 0x2071, 0x2072, 0x2073, 0x2074, 0x2075, 0x2076, 0x2077,
1087 0x2078, 0x2079, 0x207A, 0x207B, 0x207C, 0x207D, 0x207E, 0x207F,
1088 /* 8 */ 0x2080, 0x2081, 0x2082, 0x2083, 0x2084, 0x2085, 0x2086, 0x2087,
1089 0x2088, 0x2089, 0x208A, 0x208B, 0x208C, 0x208D, 0x208E, 0x208F,
1090 /* 9 */ 0x2090, 0x2091, 0x2092, 0x2093, 0x2094, 0x2095, 0x2096, 0x2097,
1091 0x2098, 0x2099, 0x209A, 0x209B, 0x209C, 0x209D, 0x209E, 0x209F,
1092 /* A */ 0x20A0, 0x20A1, 0x20A2, 0x20A3, 0x20A4, 0x20A5, 0x20A6, 0x20A7,
1093 0x20A8, 0x20A9, 0x20AA, 0x20AB, 0x20AC, 0x20AD, 0x20AE, 0x20AF,
1094 /* B */ 0x20B0, 0x20B1, 0x20B2, 0x20B3, 0x20B4, 0x20B5, 0x20B6, 0x20B7,
1095 0x20B8, 0x20B9, 0x20BA, 0x20BB, 0x20BC, 0x20BD, 0x20BE, 0x20BF,
1096 /* C */ 0x20C0, 0x20C1, 0x20C2, 0x20C3, 0x20C4, 0x20C5, 0x20C6, 0x20C7,
1097 0x20C8, 0x20C9, 0x20CA, 0x20CB, 0x20CC, 0x20CD, 0x20CE, 0x20CF,
1098 /* D */ 0x20D0, 0x20D1, 0x20D2, 0x20D3, 0x20D4, 0x20D5, 0x20D6, 0x20D7,
1099 0x20D8, 0x20D9, 0x20DA, 0x20DB, 0x20DC, 0x20DD, 0x20DE, 0x20DF,
1100 /* E */ 0x20E0, 0x20E1, 0x20E2, 0x20E3, 0x20E4, 0x20E5, 0x20E6, 0x20E7,
1101 0x20E8, 0x20E9, 0x20EA, 0x20EB, 0x20EC, 0x20ED, 0x20EE, 0x20EF,
1102 /* F */ 0x20F0, 0x20F1, 0x20F2, 0x20F3, 0x20F4, 0x20F5, 0x20F6, 0x20F7,
1103 0x20F8, 0x20F9, 0x20FA, 0x20FB, 0x20FC, 0x20FD, 0x20FE, 0x20FF,
1104
1105 // Table 8 (for high byte 0x21)
1106
1107 /* 0 */ 0x2100, 0x2101, 0x2102, 0x2103, 0x2104, 0x2105, 0x2106, 0x2107,
1108 0x2108, 0x2109, 0x210A, 0x210B, 0x210C, 0x210D, 0x210E, 0x210F,
1109 /* 1 */ 0x2110, 0x2111, 0x2112, 0x2113, 0x2114, 0x2115, 0x2116, 0x2117,
1110 0x2118, 0x2119, 0x211A, 0x211B, 0x211C, 0x211D, 0x211E, 0x211F,
1111 /* 2 */ 0x2120, 0x2121, 0x2122, 0x2123, 0x2124, 0x2125, 0x2126, 0x2127,
1112 0x2128, 0x2129, 0x212A, 0x212B, 0x212C, 0x212D, 0x212E, 0x212F,
1113 /* 3 */ 0x2130, 0x2131, 0x2132, 0x2133, 0x2134, 0x2135, 0x2136, 0x2137,
1114 0x2138, 0x2139, 0x213A, 0x213B, 0x213C, 0x213D, 0x213E, 0x213F,
1115 /* 4 */ 0x2140, 0x2141, 0x2142, 0x2143, 0x2144, 0x2145, 0x2146, 0x2147,
1116 0x2148, 0x2149, 0x214A, 0x214B, 0x214C, 0x214D, 0x214E, 0x214F,
1117 /* 5 */ 0x2150, 0x2151, 0x2152, 0x2153, 0x2154, 0x2155, 0x2156, 0x2157,
1118 0x2158, 0x2159, 0x215A, 0x215B, 0x215C, 0x215D, 0x215E, 0x215F,
1119 /* 6 */ 0x2170, 0x2171, 0x2172, 0x2173, 0x2174, 0x2175, 0x2176, 0x2177,
1120 0x2178, 0x2179, 0x217A, 0x217B, 0x217C, 0x217D, 0x217E, 0x217F,
1121 /* 7 */ 0x2170, 0x2171, 0x2172, 0x2173, 0x2174, 0x2175, 0x2176, 0x2177,
1122 0x2178, 0x2179, 0x217A, 0x217B, 0x217C, 0x217D, 0x217E, 0x217F,
1123 /* 8 */ 0x2180, 0x2181, 0x2182, 0x2183, 0x2184, 0x2185, 0x2186, 0x2187,
1124 0x2188, 0x2189, 0x218A, 0x218B, 0x218C, 0x218D, 0x218E, 0x218F,
1125 /* 9 */ 0x2190, 0x2191, 0x2192, 0x2193, 0x2194, 0x2195, 0x2196, 0x2197,
1126 0x2198, 0x2199, 0x219A, 0x219B, 0x219C, 0x219D, 0x219E, 0x219F,
1127 /* A */ 0x21A0, 0x21A1, 0x21A2, 0x21A3, 0x21A4, 0x21A5, 0x21A6, 0x21A7,
1128 0x21A8, 0x21A9, 0x21AA, 0x21AB, 0x21AC, 0x21AD, 0x21AE, 0x21AF,
1129 /* B */ 0x21B0, 0x21B1, 0x21B2, 0x21B3, 0x21B4, 0x21B5, 0x21B6, 0x21B7,
1130 0x21B8, 0x21B9, 0x21BA, 0x21BB, 0x21BC, 0x21BD, 0x21BE, 0x21BF,
1131 /* C */ 0x21C0, 0x21C1, 0x21C2, 0x21C3, 0x21C4, 0x21C5, 0x21C6, 0x21C7,
1132 0x21C8, 0x21C9, 0x21CA, 0x21CB, 0x21CC, 0x21CD, 0x21CE, 0x21CF,
1133 /* D */ 0x21D0, 0x21D1, 0x21D2, 0x21D3, 0x21D4, 0x21D5, 0x21D6, 0x21D7,
1134 0x21D8, 0x21D9, 0x21DA, 0x21DB, 0x21DC, 0x21DD, 0x21DE, 0x21DF,
1135 /* E */ 0x21E0, 0x21E1, 0x21E2, 0x21E3, 0x21E4, 0x21E5, 0x21E6, 0x21E7,
1136 0x21E8, 0x21E9, 0x21EA, 0x21EB, 0x21EC, 0x21ED, 0x21EE, 0x21EF,
1137 /* F */ 0x21F0, 0x21F1, 0x21F2, 0x21F3, 0x21F4, 0x21F5, 0x21F6, 0x21F7,
1138 0x21F8, 0x21F9, 0x21FA, 0x21FB, 0x21FC, 0x21FD, 0x21FE, 0x21FF,
1139
1140 // Table 9 (for high byte 0xFE)
1141
1142 /* 0 */ 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05, 0xFE06, 0xFE07,
1143 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E, 0xFE0F,
1144 /* 1 */ 0xFE10, 0xFE11, 0xFE12, 0xFE13, 0xFE14, 0xFE15, 0xFE16, 0xFE17,
1145 0xFE18, 0xFE19, 0xFE1A, 0xFE1B, 0xFE1C, 0xFE1D, 0xFE1E, 0xFE1F,
1146 /* 2 */ 0xFE20, 0xFE21, 0xFE22, 0xFE23, 0xFE24, 0xFE25, 0xFE26, 0xFE27,
1147 0xFE28, 0xFE29, 0xFE2A, 0xFE2B, 0xFE2C, 0xFE2D, 0xFE2E, 0xFE2F,
1148 /* 3 */ 0xFE30, 0xFE31, 0xFE32, 0xFE33, 0xFE34, 0xFE35, 0xFE36, 0xFE37,
1149 0xFE38, 0xFE39, 0xFE3A, 0xFE3B, 0xFE3C, 0xFE3D, 0xFE3E, 0xFE3F,
1150 /* 4 */ 0xFE40, 0xFE41, 0xFE42, 0xFE43, 0xFE44, 0xFE45, 0xFE46, 0xFE47,
1151 0xFE48, 0xFE49, 0xFE4A, 0xFE4B, 0xFE4C, 0xFE4D, 0xFE4E, 0xFE4F,
1152 /* 5 */ 0xFE50, 0xFE51, 0xFE52, 0xFE53, 0xFE54, 0xFE55, 0xFE56, 0xFE57,
1153 0xFE58, 0xFE59, 0xFE5A, 0xFE5B, 0xFE5C, 0xFE5D, 0xFE5E, 0xFE5F,
1154 /* 6 */ 0xFE60, 0xFE61, 0xFE62, 0xFE63, 0xFE64, 0xFE65, 0xFE66, 0xFE67,
1155 0xFE68, 0xFE69, 0xFE6A, 0xFE6B, 0xFE6C, 0xFE6D, 0xFE6E, 0xFE6F,
1156 /* 7 */ 0xFE70, 0xFE71, 0xFE72, 0xFE73, 0xFE74, 0xFE75, 0xFE76, 0xFE77,
1157 0xFE78, 0xFE79, 0xFE7A, 0xFE7B, 0xFE7C, 0xFE7D, 0xFE7E, 0xFE7F,
1158 /* 8 */ 0xFE80, 0xFE81, 0xFE82, 0xFE83, 0xFE84, 0xFE85, 0xFE86, 0xFE87,
1159 0xFE88, 0xFE89, 0xFE8A, 0xFE8B, 0xFE8C, 0xFE8D, 0xFE8E, 0xFE8F,
1160 /* 9 */ 0xFE90, 0xFE91, 0xFE92, 0xFE93, 0xFE94, 0xFE95, 0xFE96, 0xFE97,
1161 0xFE98, 0xFE99, 0xFE9A, 0xFE9B, 0xFE9C, 0xFE9D, 0xFE9E, 0xFE9F,
1162 /* A */ 0xFEA0, 0xFEA1, 0xFEA2, 0xFEA3, 0xFEA4, 0xFEA5, 0xFEA6, 0xFEA7,
1163 0xFEA8, 0xFEA9, 0xFEAA, 0xFEAB, 0xFEAC, 0xFEAD, 0xFEAE, 0xFEAF,
1164 /* B */ 0xFEB0, 0xFEB1, 0xFEB2, 0xFEB3, 0xFEB4, 0xFEB5, 0xFEB6, 0xFEB7,
1165 0xFEB8, 0xFEB9, 0xFEBA, 0xFEBB, 0xFEBC, 0xFEBD, 0xFEBE, 0xFEBF,
1166 /* C */ 0xFEC0, 0xFEC1, 0xFEC2, 0xFEC3, 0xFEC4, 0xFEC5, 0xFEC6, 0xFEC7,
1167 0xFEC8, 0xFEC9, 0xFECA, 0xFECB, 0xFECC, 0xFECD, 0xFECE, 0xFECF,
1168 /* D */ 0xFED0, 0xFED1, 0xFED2, 0xFED3, 0xFED4, 0xFED5, 0xFED6, 0xFED7,
1169 0xFED8, 0xFED9, 0xFEDA, 0xFEDB, 0xFEDC, 0xFEDD, 0xFEDE, 0xFEDF,
1170 /* E */ 0xFEE0, 0xFEE1, 0xFEE2, 0xFEE3, 0xFEE4, 0xFEE5, 0xFEE6, 0xFEE7,
1171 0xFEE8, 0xFEE9, 0xFEEA, 0xFEEB, 0xFEEC, 0xFEED, 0xFEEE, 0xFEEF,
1172 /* F */ 0xFEF0, 0xFEF1, 0xFEF2, 0xFEF3, 0xFEF4, 0xFEF5, 0xFEF6, 0xFEF7,
1173 0xFEF8, 0xFEF9, 0xFEFA, 0xFEFB, 0xFEFC, 0xFEFD, 0xFEFE, 0x0000,
1174
1175 // Table 10 (for high byte 0xFF)
1176
1177 /* 0 */ 0xFF00, 0xFF01, 0xFF02, 0xFF03, 0xFF04, 0xFF05, 0xFF06, 0xFF07,
1178 0xFF08, 0xFF09, 0xFF0A, 0xFF0B, 0xFF0C, 0xFF0D, 0xFF0E, 0xFF0F,
1179 /* 1 */ 0xFF10, 0xFF11, 0xFF12, 0xFF13, 0xFF14, 0xFF15, 0xFF16, 0xFF17,
1180 0xFF18, 0xFF19, 0xFF1A, 0xFF1B, 0xFF1C, 0xFF1D, 0xFF1E, 0xFF1F,
1181 /* 2 */ 0xFF20, 0xFF41, 0xFF42, 0xFF43, 0xFF44, 0xFF45, 0xFF46, 0xFF47,
1182 0xFF48, 0xFF49, 0xFF4A, 0xFF4B, 0xFF4C, 0xFF4D, 0xFF4E, 0xFF4F,
1183 /* 3 */ 0xFF50, 0xFF51, 0xFF52, 0xFF53, 0xFF54, 0xFF55, 0xFF56, 0xFF57,
1184 0xFF58, 0xFF59, 0xFF5A, 0xFF3B, 0xFF3C, 0xFF3D, 0xFF3E, 0xFF3F,
1185 /* 4 */ 0xFF40, 0xFF41, 0xFF42, 0xFF43, 0xFF44, 0xFF45, 0xFF46, 0xFF47,
1186 0xFF48, 0xFF49, 0xFF4A, 0xFF4B, 0xFF4C, 0xFF4D, 0xFF4E, 0xFF4F,
1187 /* 5 */ 0xFF50, 0xFF51, 0xFF52, 0xFF53, 0xFF54, 0xFF55, 0xFF56, 0xFF57,
1188 0xFF58, 0xFF59, 0xFF5A, 0xFF5B, 0xFF5C, 0xFF5D, 0xFF5E, 0xFF5F,
1189 /* 6 */ 0xFF60, 0xFF61, 0xFF62, 0xFF63, 0xFF64, 0xFF65, 0xFF66, 0xFF67,
1190 0xFF68, 0xFF69, 0xFF6A, 0xFF6B, 0xFF6C, 0xFF6D, 0xFF6E, 0xFF6F,
1191 /* 7 */ 0xFF70, 0xFF71, 0xFF72, 0xFF73, 0xFF74, 0xFF75, 0xFF76, 0xFF77,
1192 0xFF78, 0xFF79, 0xFF7A, 0xFF7B, 0xFF7C, 0xFF7D, 0xFF7E, 0xFF7F,
1193 /* 8 */ 0xFF80, 0xFF81, 0xFF82, 0xFF83, 0xFF84, 0xFF85, 0xFF86, 0xFF87,
1194 0xFF88, 0xFF89, 0xFF8A, 0xFF8B, 0xFF8C, 0xFF8D, 0xFF8E, 0xFF8F,
1195 /* 9 */ 0xFF90, 0xFF91, 0xFF92, 0xFF93, 0xFF94, 0xFF95, 0xFF96, 0xFF97,
1196 0xFF98, 0xFF99, 0xFF9A, 0xFF9B, 0xFF9C, 0xFF9D, 0xFF9E, 0xFF9F,
1197 /* A */ 0xFFA0, 0xFFA1, 0xFFA2, 0xFFA3, 0xFFA4, 0xFFA5, 0xFFA6, 0xFFA7,
1198 0xFFA8, 0xFFA9, 0xFFAA, 0xFFAB, 0xFFAC, 0xFFAD, 0xFFAE, 0xFFAF,
1199 /* B */ 0xFFB0, 0xFFB1, 0xFFB2, 0xFFB3, 0xFFB4, 0xFFB5, 0xFFB6, 0xFFB7,
1200 0xFFB8, 0xFFB9, 0xFFBA, 0xFFBB, 0xFFBC, 0xFFBD, 0xFFBE, 0xFFBF,
1201 /* C */ 0xFFC0, 0xFFC1, 0xFFC2, 0xFFC3, 0xFFC4, 0xFFC5, 0xFFC6, 0xFFC7,
1202 0xFFC8, 0xFFC9, 0xFFCA, 0xFFCB, 0xFFCC, 0xFFCD, 0xFFCE, 0xFFCF,
1203 /* D */ 0xFFD0, 0xFFD1, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD5, 0xFFD6, 0xFFD7,
1204 0xFFD8, 0xFFD9, 0xFFDA, 0xFFDB, 0xFFDC, 0xFFDD, 0xFFDE, 0xFFDF,
1205 /* E */ 0xFFE0, 0xFFE1, 0xFFE2, 0xFFE3, 0xFFE4, 0xFFE5, 0xFFE6, 0xFFE7,
1206 0xFFE8, 0xFFE9, 0xFFEA, 0xFFEB, 0xFFEC, 0xFFED, 0xFFEE, 0xFFEF,
1207 /* F */ 0xFFF0, 0xFFF1, 0xFFF2, 0xFFF3, 0xFFF4, 0xFFF5, 0xFFF6, 0xFFF7,
1208 0xFFF8, 0xFFF9, 0xFFFA, 0xFFFB, 0xFFFC, 0xFFFD, 0xFFFE, 0xFFFF,
1209};
Arthur Sonzogni48785f62023-05-02 09:40:371210// clang-format on
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091211
Arthur Sonzogni48785f62023-05-02 09:40:371212// Returns the next non-ignorable codepoint within `string` starting from the
1213// position indicated by `index`, or zero if there are no more.
1214// The passed-in `index` is automatically advanced as the characters in the
1215// input HFS-decomposed UTF-8 strings are read.
Peter Kastingb74454862022-07-13 00:40:121216inline base_icu::UChar32 HFSReadNextNonIgnorableCodepoint(const char* string,
1217 size_t length,
1218 size_t* index) {
1219 base_icu::UChar32 codepoint = 0;
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091220 while (*index < length && codepoint == 0) {
1221 // CBU8_NEXT returns a value < 0 in error cases. For purposes of string
1222 // comparison, we just use that value and flag it with DCHECK.
Peter Kastingb74454862022-07-13 00:40:121223 CBU8_NEXT(reinterpret_cast<const uint8_t*>(string), *index, length,
1224 codepoint);
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091225 DCHECK_GT(codepoint, 0);
Arthur Sonzogni48785f62023-05-02 09:40:371226
1227 // Note: Here, there are no lower case conversion implemented in the
1228 // Supplementary Multilingual Plane (codepoint > 0xFFFF).
1229
1230 if (codepoint > 0 && codepoint <= 0xFFFF) {
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091231 // Check if there is a subtable for this upper byte.
1232 int lookup_offset = lower_case_table[codepoint >> 8];
1233 if (lookup_offset != 0)
1234 codepoint = lower_case_table[lookup_offset + (codepoint & 0x00FF)];
Arthur Sonzogni48785f62023-05-02 09:40:371235 // Note: `codepoint` may be again 0 at this point if the character was
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091236 // an ignorable.
1237 }
1238 }
1239 return codepoint;
1240}
1241
brettw89365dc2015-06-16 05:52:471242} // namespace
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091243
1244// Special UTF-8 version of FastUnicodeCompare. Cf:
1245// http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringComparisonAlgorithm
1246// The input strings must be in the special HFS decomposed form.
brettw89365dc2015-06-16 05:52:471247int FilePath::HFSFastUnicodeCompare(StringPieceType string1,
1248 StringPieceType string2) {
Peter Kastingb74454862022-07-13 00:40:121249 size_t length1 = string1.length();
1250 size_t length2 = string2.length();
1251 size_t index1 = 0;
1252 size_t index2 = 0;
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091253
1254 for (;;) {
Peter Kastingb74454862022-07-13 00:40:121255 base_icu::UChar32 codepoint1 =
1256 HFSReadNextNonIgnorableCodepoint(string1.data(), length1, &index1);
1257 base_icu::UChar32 codepoint2 =
1258 HFSReadNextNonIgnorableCodepoint(string2.data(), length2, &index2);
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091259 if (codepoint1 != codepoint2)
1260 return (codepoint1 < codepoint2) ? -1 : 1;
1261 if (codepoint1 == 0) {
1262 DCHECK_EQ(index1, length1);
1263 DCHECK_EQ(index2, length2);
1264 return 0;
1265 }
1266 }
1267}
1268
brettw89365dc2015-06-16 05:52:471269StringType FilePath::GetHFSDecomposedForm(StringPieceType string) {
jialiul03478a112016-03-31 18:38:431270 StringType result;
Avi Drissman28154a62023-08-22 04:06:451271 apple::ScopedCFTypeRef<CFStringRef> cfstring(CFStringCreateWithBytesNoCopy(
Peter Kastingb74454862022-07-13 00:40:121272 NULL, reinterpret_cast<const UInt8*>(string.data()),
1273 checked_cast<CFIndex>(string.length()), kCFStringEncodingUTF8, false,
1274 kCFAllocatorNull));
jialiul03478a112016-03-31 18:38:431275 if (cfstring) {
1276 // Query the maximum length needed to store the result. In most cases this
1277 // will overestimate the required space. The return value also already
1278 // includes the space needed for a terminating 0.
Avi Drissman887a4da2023-10-27 04:29:121279 CFIndex length =
1280 CFStringGetMaximumSizeOfFileSystemRepresentation(cfstring.get());
jialiul03478a112016-03-31 18:38:431281 DCHECK_GT(length, 0); // should be at least 1 for the 0-terminator.
1282 // Reserve enough space for CFStringGetFileSystemRepresentation to write
1283 // into. Also set the length to the maximum so that we can shrink it later.
1284 // (Increasing rather than decreasing it would clobber the string contents!)
Peter Kastingb74454862022-07-13 00:40:121285 result.reserve(static_cast<size_t>(length));
1286 result.resize(static_cast<size_t>(length) - 1);
Avi Drissman887a4da2023-10-27 04:29:121287 Boolean success =
1288 CFStringGetFileSystemRepresentation(cfstring.get(), &result[0], length);
jialiul03478a112016-03-31 18:38:431289 if (success) {
1290 // Reduce result.length() to actual string length.
1291 result.resize(strlen(result.c_str()));
1292 } else {
1293 // An error occurred -> clear result.
1294 result.clear();
1295 }
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091296 }
1297 return result;
1298}
1299
brettw89365dc2015-06-16 05:52:471300int FilePath::CompareIgnoreCase(StringPieceType string1,
1301 StringPieceType string2) {
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091302 // Quick checks for empty strings - these speed things up a bit and make the
1303 // following code cleaner.
1304 if (string1.empty())
1305 return string2.empty() ? 0 : -1;
1306 if (string2.empty())
1307 return 1;
1308
1309 StringType hfs1 = GetHFSDecomposedForm(string1);
1310 StringType hfs2 = GetHFSDecomposedForm(string2);
1311
1312 // GetHFSDecomposedForm() returns an empty string in an error case.
1313 if (hfs1.empty() || hfs2.empty()) {
Avi Drissman28154a62023-08-22 04:06:451314 apple::ScopedCFTypeRef<CFStringRef> cfstring1(CFStringCreateWithBytesNoCopy(
Peter Kastingb74454862022-07-13 00:40:121315 NULL, reinterpret_cast<const UInt8*>(string1.data()),
1316 checked_cast<CFIndex>(string1.length()), kCFStringEncodingUTF8, false,
1317 kCFAllocatorNull));
Avi Drissman28154a62023-08-22 04:06:451318 apple::ScopedCFTypeRef<CFStringRef> cfstring2(CFStringCreateWithBytesNoCopy(
Peter Kastingb74454862022-07-13 00:40:121319 NULL, reinterpret_cast<const UInt8*>(string2.data()),
1320 checked_cast<CFIndex>(string2.length()), kCFStringEncodingUTF8, false,
1321 kCFAllocatorNull));
Daniel Ruberyab28fb42019-06-05 00:02:451322 // If neither GetHFSDecomposedForm nor CFStringCreateWithBytesNoCopy
1323 // succeed, fall back to strcmp. This can occur when the input string is
1324 // invalid UTF-8.
1325 if (!cfstring1 || !cfstring2) {
Jan Wilken Dörrie5db50ac2021-02-15 11:43:161326 int comparison = memcmp(string1.data(), string2.data(),
1327 std::min(string1.length(), string2.length()));
Daniel Ruberyab28fb42019-06-05 00:02:451328 if (comparison < 0)
1329 return -1;
1330 if (comparison > 0)
1331 return 1;
1332 return 0;
1333 }
1334
Avi Drissman887a4da2023-10-27 04:29:121335 return static_cast<int>(CFStringCompare(cfstring1.get(), cfstring2.get(),
1336 kCFCompareCaseInsensitive));
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091337 }
1338
1339 return HFSFastUnicodeCompare(hfs1, hfs2);
1340}
1341
Xiaohan Wangb705a64a2022-01-15 18:31:131342#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091343
brettw8a800902015-07-10 18:28:331344// Generic Posix system comparisons.
brettw89365dc2015-06-16 05:52:471345int FilePath::CompareIgnoreCase(StringPieceType string1,
1346 StringPieceType string2) {
Jan Wilken Dörrie5db50ac2021-02-15 11:43:161347 size_t rlen = std::min(string1.size(), string2.size());
1348 int comparison = strncasecmp(string1.data(), string2.data(), rlen);
1349 if (comparison < 0 || (comparison == 0 && string1.size() < string2.size()))
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091350 return -1;
Jan Wilken Dörrie5db50ac2021-02-15 11:43:161351 if (comparison > 0 || (comparison == 0 && string1.size() > string2.size()))
rolandsteiner@chromium.orgeccb9d12009-10-28 05:40:091352 return 1;
1353 return 0;
1354}
1355
1356#endif // OS versions of CompareIgnoreCase()
1357
finnur@chromium.org44ec9b3b2010-02-03 01:17:261358
avi@google.com0b733222008-12-11 14:55:121359void FilePath::StripTrailingSeparatorsInternal() {
mark@chromium.org65d00aff2008-10-03 18:21:471360 // If there is no drive letter, start will be 1, which will prevent stripping
1361 // the leading separator if there is only one separator. If there is a drive
1362 // letter, start will be set appropriately to prevent stripping the first
1363 // separator following the drive letter, if a separator immediately follows
1364 // the drive letter.
mark@chromium.org1906c032008-12-08 16:49:081365 StringType::size_type start = FindDriveLetter(path_) + 2;
mark@chromium.org65d00aff2008-10-03 18:21:471366
1367 StringType::size_type last_stripped = StringType::npos;
1368 for (StringType::size_type pos = path_.length();
1369 pos > start && IsSeparator(path_[pos - 1]);
1370 --pos) {
1371 // If the string only has two separators and they're at the beginning,
1372 // don't strip them, unless the string began with more than two separators.
1373 if (pos != start + 1 || last_stripped == start + 2 ||
1374 !IsSeparator(path_[start - 1])) {
1375 path_.resize(pos - 1);
1376 last_stripped = pos;
1377 }
1378 }
1379}
cevans@chromium.org06708612009-08-15 21:44:311380
kinuko@chromium.orgd9034ed22012-02-10 02:04:401381FilePath FilePath::NormalizePathSeparators() const {
scottmg@chromium.orgee55eace2014-04-03 07:45:421382 return NormalizePathSeparatorsTo(kSeparators[0]);
1383}
1384
Alexander Timinbebb2002021-04-20 15:42:241385void FilePath::WriteIntoTrace(perfetto::TracedValue context) const {
Alexander Timin7372d5362021-03-09 00:55:311386 perfetto::WriteIntoTracedValue(std::move(context), value());
1387}
1388
scottmg@chromium.orgee55eace2014-04-03 07:45:421389FilePath FilePath::NormalizePathSeparatorsTo(CharType separator) const {
tony@chromium.org7c373772010-06-25 05:33:171390#if defined(FILE_PATH_USES_WIN_SEPARATORS)
scottmg@chromium.orgee55eace2014-04-03 07:45:421391 DCHECK_NE(kSeparators + kSeparatorsLength,
1392 std::find(kSeparators, kSeparators + kSeparatorsLength, separator));
tony@chromium.org7c373772010-06-25 05:33:171393 StringType copy = path_;
scottmg@chromium.orgee55eace2014-04-03 07:45:421394 for (size_t i = 0; i < kSeparatorsLength; ++i) {
1395 std::replace(copy.begin(), copy.end(), kSeparators[i], separator);
tony@chromium.org7c373772010-06-25 05:33:171396 }
1397 return FilePath(copy);
kinuko@chromium.orgd9034ed22012-02-10 02:04:401398#else
1399 return *this;
tony@chromium.org7c373772010-06-25 05:33:171400#endif
kinuko@chromium.orgd9034ed22012-02-10 02:04:401401}
gab@chromium.org1a0af402012-09-26 21:21:131402
Xiaohan Wangb705a64a2022-01-15 18:31:131403#if BUILDFLAG(IS_ANDROID)
qinmin@chromium.orgf12d1e12013-11-20 07:04:551404bool FilePath::IsContentUri() const {
brettw95509312015-07-16 23:57:331405 return StartsWith(path_, "content://", base::CompareCase::INSENSITIVE_ASCII);
qinmin@chromium.orgf12d1e12013-11-20 07:04:551406}
1407#endif
1408
brettw@chromium.org04af979a2013-02-16 04:12:261409} // namespace base