File: character_encoding.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (41 lines) | stat: -rw-r--r-- 1,576 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2016 The Chromium Authors
// 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 "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

std::string GetCanonicalEncodingNameByAliasName(const std::string& alias_name) {
  for (auto* encoding_name : kCanonicalEncodingNames) {
    if (alias_name == encoding_name)
      return alias_name;
  }
  static const char* kStandards[3] = {"HTML", "MIME", "IANA"};
  for (auto* standard : kStandards) {
    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 std::string();
}
}  // namespace base