Debian Codegraph @

libstdc++-v3/scripts/gen_text_encoding_data.py

raw
1 #!/usr/bin/env python3
2 #
3 # Script to generate tables for libstdc++ std::text_encoding.
4 #
5 # This file is part of GCC.
6 #
7 # GCC is free software; you can redistribute it and/or modify it under
8 # the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 3, or (at your option) any later
10 # version.
11 #
12 # GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 # for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with GCC; see the file COPYING3. If not see
19 # <http://www.gnu.org/licenses/>.
20
21 # To update the Libstdc++ static data in <bits/text_encoding-data.h> download
22 # the latest:
23 # https://www.iana.org/assignments/character-sets/character-sets-1.csv
24 # Then run this script and save the output to
25 # include/bits/text_encoding-data.h
26
27 import sys
28 import csv
29 import os
30
31 if len(sys.argv) != 2:
32 print("Usage: %s <character sets csv>" % sys.argv[0], file=sys.stderr)
33 sys.exit(1)
34
35 self = os.path.basename(__file__)
36 print("// Generated by scripts/{}, do not edit.".format(self))
37 print("""
38
39 // Copyright The GNU Toolchain Authors.
40 //
41 // This file is part of the GNU ISO C++ Library. This library is free
42 // software; you can redistribute it and/or modify it under the
43 // terms of the GNU General Public License as published by the
44 // Free Software Foundation; either version 3, or (at your option)
45 // any later version.
46
47 // This library is distributed in the hope that it will be useful,
48 // but WITHOUT ANY WARRANTY; without even the implied warranty of
49 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 // GNU General Public License for more details.
51
52 // Under Section 7 of GPL version 3, you are granted additional
53 // permissions described in the GCC Runtime Library Exception, version
54 // 3.1, as published by the Free Software Foundation.
55
56 // You should have received a copy of the GNU General Public License and
57 // a copy of the GCC Runtime Library Exception along with this program;
58 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
59 // <http://www.gnu.org/licenses/>.
60
61 /** @file bits/text_encoding-data.h
62 * This is an internal header file, included by other library headers.
63 * Do not attempt to use it directly. @headername{text_encoding}
64 */
65 """)
66 print("#ifndef _GLIBCXX_GET_ENCODING_DATA")
67 print('# error "This is not a public header, do not include it directly"')
68 print("#endif\n")
69
70 # We need to generate a list of initializers of the form { mib, alias }, e.g.,
71 # { 3, "US-ASCII" },
72 # { 3, "ISO646-US" },
73 # { 3, "csASCII" },
74 # { 4, "ISO_8859-1:1987" },
75 # { 4, "latin1" },
76 # The initializers must be sorted by the mib value. The first entry for
77 # a given mib must be the primary name for the encoding. Any aliases for
78 # the encoding come after the primary name.
79 # We also define a macro _GLIBCXX_TEXT_ENCODING_UTF8_OFFSET which is the
80 # offset into the list of the mib=106, alias="UTF-8" entry. This is used
81 # to optimize the common case, so we don't need to search for "UTF-8".
82
83 charsets = {}
84 with open(sys.argv[1], newline='') as f:
85 reader = csv.reader(f)
86 next(reader) # skip header row
87 for row in reader:
88 mib = int(row[2])
89 if mib in charsets:
90 raise ValueError("Multiple rows for mibEnum={}".format(mib))
91 name = row[1]
92 aliases = row[5].split()
93 # Ensure primary name comes first
94 if name in aliases:
95 aliases.remove(name)
96 charsets[mib] = [name] + aliases
97
98 # Remove "NATS-DANO" and "NATS-DANO-ADD" as specified by the C++ standard.
99 charsets.pop(33, None)
100 charsets.pop(34, None)
101
102 # This is not an official IANA alias, but we include it in the
103 # implementation-defined superset of aliases for US-ASCII.
104 # See also LWG 4043.
105 extra_aliases = {3: ["ASCII"]}
106
107 count = 0
108 for mib in sorted(charsets.keys()):
109 names = charsets[mib]
110 if names[0] == "UTF-8":
111 print("#define _GLIBCXX_TEXT_ENCODING_UTF8_OFFSET {}".format(count))
112 for name in names:
113 print(' {{ {:4}, "{}" }},'.format(mib, name))
114 count += len(names)
115 if mib in extra_aliases:
116 names = extra_aliases[mib]
117 for name in names:
118 print(' {{ {:4}, "{}" }}, // libstdc++ extension'.format(mib, name))
119 count += len(names)
120
121 # <text_encoding> gives an error if this macro is left defined.
122 # Do this last, so that the generated output is not usable unless we reach here.
123 print("\n#undef _GLIBCXX_GET_ENCODING_DATA")