This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Change docs display for PERL_UNUSED_foo
[perl5.git] / regen / unicode_constants.pl
CommitLineData
61dad979
KW
1use v5.16.0;
2use strict;
3use warnings;
3d7c117d
MB
4require './regen/regen_lib.pl';
5require './regen/charset_translations.pl';
4b4853d1 6use Unicode::UCD;
61dad979
KW
7use charnames qw(:loose);
8
1b0f46bf 9my $out_fh = open_new('unicode_constants.h', '>',
ad88cddb 10 {style => '*', by => $0,
61dad979
KW
11 from => "Unicode data"});
12
13print $out_fh <<END;
d10c72f2 14
6a5bc5ac
KW
15#ifndef PERL_UNICODE_CONSTANTS_H_ /* Guard against nested #includes */
16#define PERL_UNICODE_CONSTANTS_H_ 1
d10c72f2 17
4b4853d1
KW
18/* This file contains #defines for the version of Unicode being used and
19 * various Unicode code points. The values the code point macros expand to
20 * are the native Unicode code point, or all or portions of the UTF-8 encoding
21 * for the code point. In the former case, the macro name has the suffix
22 * "_NATIVE"; otherwise, the suffix "_UTF8".
61dad979 23 *
525b6419
KW
24 * The macros that have the suffix "_UTF8" may have further suffixes, as
25 * follows:
26 * "_FIRST_BYTE" if the value is just the first byte of the UTF-8
27 * representation; the value will be a numeric constant.
28 * "_TAIL" if instead it represents all but the first byte. This, and
29 * with no additional suffix are both string constants */
61dad979 30
69bc4c1f 31/*
70b05c7c 32=for apidoc_section Unicode Support
69bc4c1f 33
78342678 34=for apidoc AmnU|const char *|BOM_UTF8
69bc4c1f
KW
35
36This is a macro that evaluates to a string constant of the UTF-8 bytes that
37define the Unicode BYTE ORDER MARK (U+FEFF) for the platform that perl
38is compiled on. This allows code to use a mnemonic for this character that
39works on both ASCII and EBCDIC platforms.
40S<C<sizeof(BOM_UTF8) - 1>> can be used to get its length in
41bytes.
42
78342678 43=for apidoc AmnU|const char *|REPLACEMENT_CHARACTER_UTF8
69bc4c1f
KW
44
45This is a macro that evaluates to a string constant of the UTF-8 bytes that
46define the Unicode REPLACEMENT CHARACTER (U+FFFD) for the platform that perl
47is compiled on. This allows code to use a mnemonic for this character that
48works on both ASCII and EBCDIC platforms.
49S<C<sizeof(REPLACEMENT_CHARACTER_UTF8) - 1>> can be used to get its length in
50bytes.
51
52=cut
53*/
54
61dad979
KW
55END
56
4b4853d1
KW
57my $version = Unicode::UCD::UnicodeVersion();
58my ($major, $dot, $dotdot) = $version =~ / (.*?) \. (.*?) (?: \. (.*) )? $ /x;
59$dotdot = 0 unless defined $dotdot;
60
61print $out_fh <<END;
62#define UNICODE_MAJOR_VERSION $major
63#define UNICODE_DOT_VERSION $dot
64#define UNICODE_DOT_DOT_VERSION $dotdot
65
66END
67
9d8e3074 68# The data are at __DATA__ in this file.
61dad979 69
ad88cddb
KW
70my @data = <DATA>;
71
72foreach my $charset (get_supported_code_pages()) {
73 print $out_fh "\n" . get_conditional_compile_line_start($charset);
74
c30a0cf2 75 my @a2n = @{get_a2n($charset)};
ad88cddb 76
4a4b1311
KW
77 for ( @data ) {
78 chomp;
79
80 # Convert any '#' comments to /* ... */; empty lines and comments are
81 # output as blank lines
82 if ($_ =~ m/ ^ \s* (?: \# ( .* ) )? $ /x) {
83 my $comment_body = $1 // "";
84 if ($comment_body ne "") {
85 print $out_fh "/* $comment_body */\n";
86 }
87 else {
88 print $out_fh "\n";
89 }
90 next;
5a731a17 91 }
76837d21 92
4a4b1311
KW
93 unless ($_ =~ m/ ^ ( [^\ ]* ) # Name or code point token
94 (?: [\ ]+ ( [^ ]* ) )? # optional flag
95 (?: [\ ]+ ( .* ) )? # name if unnamed; flag is required
96 /x)
97 {
98 die "Unexpected syntax at line $.: $_\n";
99 }
61dad979 100
4a4b1311
KW
101 my $name_or_cp = $1;
102 my $flag = $2;
103 my $desired_name = $3;
104
105 my $name;
106 my $cp;
107 my $U_cp; # code point in Unicode (not-native) terms
4a4b1311
KW
108
109 if ($name_or_cp =~ /^U\+(.*)/) {
110 $U_cp = hex $1;
111 $name = charnames::viacode($name_or_cp);
112 if (! defined $name) {
280ac755
KW
113 next if $flag =~ /skip_if_undef/;
114 die "Unknown code point '$name_or_cp' at line $.: $_\n" unless $desired_name;
4a4b1311
KW
115 $name = "";
116 }
117 }
118 else {
119 $name = $name_or_cp;
120 die "Unknown name '$name' at line $.: $_\n" unless defined $name;
121 $U_cp = charnames::vianame($name =~ s/_/ /gr);
632c9f80 122 }
61dad979 123
4a4b1311
KW
124 $cp = ($U_cp < 256)
125 ? $a2n[$U_cp]
126 : $U_cp;
ad88cddb 127
4a4b1311
KW
128 $name = $desired_name if $name eq "" && $desired_name;
129 $name =~ s/[- ]/_/g; # The macro name can have no blanks nor dashes
61dad979 130
4a4b1311
KW
131 my $str;
132 my $suffix;
133 if (defined $flag && $flag eq 'native') {
134 die "Are you sure you want to run this on an above-Latin1 code point?" if $cp > 0xff;
135 $suffix = '_NATIVE';
136 $str = sprintf "0x%02X", $cp; # Is a numeric constant
81a2a11f
KW
137 }
138 else {
4a4b1311
KW
139 $str = join "", map { sprintf "\\x%02X", ord $_ } split //, cp_2_utfbytes($U_cp, $charset);
140
141 $suffix = '_UTF8';
142 if (! defined $flag || $flag =~ /^ string (_skip_if_undef)? $/x) {
143 $str = "\"$str\""; # Will be a string constant
144 } elsif ($flag eq 'tail') {
145 $str =~ s/\\x..//; # Remove the first byte
146 $suffix .= '_TAIL';
147 $str = "\"$str\""; # Will be a string constant
148 }
149 elsif ($flag eq 'first') {
150 $str =~ s/ \\x ( .. ) .* /$1/x; # Get the two nibbles of the 1st byte
151 $suffix .= '_FIRST_BYTE';
152 $str = "0x$str"; # Is a numeric constant
153 }
154 else {
155 die "Unknown flag at line $.: $_\n";
156 }
81a2a11f 157 }
4a4b1311 158 printf $out_fh "# define %s%s %s /* U+%04X */\n", $name, $suffix, $str, $U_cp;
a1beba5b 159 }
09cc440d
KW
160
161 my $max_PRINT_A = 0;
162 for my $i (0x20 .. 0x7E) {
163 $max_PRINT_A = $a2n[$i] if $a2n[$i] > $max_PRINT_A;
164 }
165 printf $out_fh "# define MAX_PRINT_A_FOR_USE_ONLY_BY_REGCOMP_DOT_C 0x%02X /* The max code point that isPRINT_A */\n", $max_PRINT_A;
166
ad88cddb 167 print $out_fh "\n" . get_conditional_compile_line_end();
b35552de
KW
168
169}
170
171use Unicode::UCD 'prop_invlist';
172
173my $count = 0;
174my @other_invlist = prop_invlist("Other");
175for (my $i = 0; $i < @other_invlist; $i += 2) {
176 $count += ((defined $other_invlist[$i+1])
177 ? $other_invlist[$i+1]
178 : 0x110000)
179 - $other_invlist[$i];
61dad979 180}
b35552de
KW
181printf $out_fh "\n/* The number of code points not matching \\pC */\n"
182 . "#define NON_OTHER_COUNT_FOR_USE_ONLY_BY_REGCOMP_DOT_C %d\n",
183 0x110000 - $count;
61dad979 184
3bfc1e70
KW
185# If this release has both the CWCM and CWCF properties, find the highest code
186# point which changes under any case change. We can use this to short-circuit
187# code
188my @cwcm = prop_invlist('CWCM');
189if (@cwcm) {
190 my @cwcf = prop_invlist('CWCF');
191 if (@cwcf) {
192 my $max = ($cwcm[-1] < $cwcf[-1])
193 ? $cwcf[-1]
194 : $cwcm[-1];
195 printf $out_fh "\n/* The highest code point that has any type of case change */\n"
196 . "#define HIGHEST_CASE_CHANGING_CP_FOR_USE_ONLY_BY_UTF8_DOT_C 0x%X\n",
197 $max - 1;
198 }
199}
200
6a5bc5ac 201print $out_fh "\n#endif /* PERL_UNICODE_CONSTANTS_H_ */\n";
d10c72f2 202
61dad979
KW
203read_only_bottom_close_and_rename($out_fh);
204
9d8e3074
KW
205# DATA FORMAT
206#
69bc4c1f
KW
207# Note that any apidoc comments you want in the file need to be added to one
208# of the prints above
209#
9d8e3074
KW
210# A blank line is output as-is.
211# Comments (lines whose first non-blank is a '#') are converted to C-style,
212# though empty comments are converted to blank lines. Otherwise, each line
213# represents one #define, and begins with either a Unicode character name with
214# the blanks and dashes in it squeezed out or replaced by underscores; or it
215# may be a hexadecimal Unicode code point of the form U+xxxx. In the latter
216# case, the name will be looked-up to use as the name of the macro. In either
217# case, the macro name will have suffixes as listed above, and all blanks and
218# dashes will be replaced by underscores.
219#
220# Each line may optionally have one of the following flags on it, separated by
221# white space from the initial token.
222# string indicates that the output is to be of the string form
223# described in the comments above that are placed in the file.
224# string_skip_ifundef is the same as 'string', but instead of dying if the
225# code point doesn't exist, the line is just skipped: no output is
226# generated for it
227# first indicates that the output is to be of the FIRST_BYTE form.
228# tail indicates that the output is of the _TAIL form.
229# native indicates that the output is the code point, converted to the
230# platform's native character set if applicable
231#
232# If the code point has no official name, the desired name may be appended
233# after the flag, which will be ignored if there is an official name.
234#
235# This program is used to make it convenient to create compile time constants
236# of UTF-8, and to generate proper EBCDIC as well as ASCII without manually
237# having to figure things out.
238
61dad979 239__DATA__
f2e06375 240U+017F string
76837d21 241
1dfa4f52 242U+0300 string
2a614cdc 243U+0307 string
a78bc3c6 244
8f57fa7d 245U+1E9E string_skip_if_undef
f2e06375 246
a9f50d33
KW
247U+FB05 string
248U+FB06 string
a0ffb25e
KW
249U+0130 string
250U+0131 string
a9f50d33 251
1dfa4f52 252U+2010 string
5f0aa340
KW
253BOM first
254BOM tail
525b6419 255
69bc4c1f
KW
256BOM string
257
258U+FFFD string
259
566efd88
KW
260U+10FFFF string MAX_UNICODE
261
df758df2
KW
262NBSP native
263NBSP string
264
05016631 265DEL native
c5eda08a
KW
266CR native
267LF native
d804860b
KW
268VT native
269ESC native
1dfa4f52
KW
270U+00DF native
271U+00E5 native
272U+00C5 native
273U+00FF native
274U+00B5 native