Chromium Code Reviews| Index: base/i18n/character_encoding.cc |
| diff --git a/base/i18n/character_encoding.cc b/base/i18n/character_encoding.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..573859a84a268637b727194e79e01b66809c4d4a |
| --- /dev/null |
| +++ b/base/i18n/character_encoding.cc |
| @@ -0,0 +1,42 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/i18n/character_encoding.h" |
| + |
| +#include "base/macros.h" |
| +#include "third_party/icu/source/common/unicode/ucnv.h" |
| + |
| +namespace base { |
| +namespace { |
| + |
| +// An array of all supported canonical encoding names. |
| +const char* const kCanonicalEncodingNames[] = { |
| + "Big5", "EUC-JP", "EUC-KR", "gb18030", |
| + "GBK", "IBM866", "ISO-2022-JP", "ISO-8859-10", |
| + "ISO-8859-13", "ISO-8859-14", "ISO-8859-15", "ISO-8859-16", |
| + "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", "ISO-8859-5", |
| + "ISO-8859-6", "ISO-8859-7", "ISO-8859-8", "ISO-8859-8-I", |
| + "KOI8-R", "KOI8-U", "macintosh", "Shift_JIS", |
| + "UTF-16LE", "UTF-8", "windows-1250", "windows-1251", |
| + "windows-1252", "windows-1253", "windows-1254", "windows-1255", |
| + "windows-1256", "windows-1257", "windows-1258", "windows-874"}; |
| + |
| +} // namespace |
| + |
| +const char* GetCanonicalEncodingNameByAliasName(const std::string& alias_name) { |
| + for (auto& encoding_name : kCanonicalEncodingNames) { |
| + if (alias_name == encoding_name) |
| + return alias_name.c_str(); |
|
dcheng
2017/01/03 21:52:54
return encoding_name; is safer (there's no guarant
Jinsuk Kim
2017/01/03 23:42:30
Done.
|
| + } |
| + const char* standards[3] = {"HTML", "MIME", "IANA"}; |
|
dcheng
2017/01/03 21:52:54
static, and name this kStandards
Jinsuk Kim
2017/01/03 23:42:30
Done.
|
| + for (auto& standard : standards) { |
| + UErrorCode error_code = U_ZERO_ERROR; |
| + const char* canonical_name = |
| + ucnv_getStandardName(alias_name.c_str(), standard, &error_code); |
| + if (U_SUCCESS(error_code) && canonical_name) |
| + return canonical_name; |
| + } |
| + return nullptr; |
| +} |
| +} // namespace base |