This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regen/unicode_constants.pl: Convert '-' in names to '_'
[perl5.git] / regen / unicode_constants.pl
CommitLineData
61dad979
KW
1use v5.16.0;
2use strict;
3use warnings;
4require 'regen/regen_lib.pl';
5use charnames qw(:loose);
6
1b0f46bf 7my $out_fh = open_new('unicode_constants.h', '>',
61dad979
KW
8 {style => '*', by => $0,
9 from => "Unicode data"});
10
11print $out_fh <<END;
d10c72f2 12
1b0f46bf
KW
13#ifndef H_UNICODE_CONSTANTS /* Guard against nested #includes */
14#define H_UNICODE_CONSTANTS 1
d10c72f2 15
61dad979 16/* This file contains #defines for various Unicode code points. The values
525b6419
KW
17 * the macros expand to are the native Unicode code point, or all or portions
18 * of the UTF-8 encoding for the code point. In the former case, the macro
19 * name has the suffix "_NATIVE"; otherwise, the suffix "_UTF8".
61dad979 20 *
525b6419
KW
21 * The macros that have the suffix "_UTF8" may have further suffixes, as
22 * follows:
23 * "_FIRST_BYTE" if the value is just the first byte of the UTF-8
24 * representation; the value will be a numeric constant.
25 * "_TAIL" if instead it represents all but the first byte. This, and
26 * with no additional suffix are both string constants */
61dad979
KW
27
28END
29
76837d21
KW
30# The data are at the end of this file. A blank line is output as-is.
31# Otherwise, each line represents one #define, and begins with either a
e9cddfae 32# Unicode character name with the blanks and dashes in it squeezed out or replaced by
76837d21
KW
33# underscores; or it may be a hexadecimal Unicode code point. In the latter
34# case, the name will be looked-up to use as the name of the macro. In either
e9cddfae
KW
35# case, the macro name will have suffixes as listed above, and all blanks and
36# dashes will be replaced by underscores.
61dad979
KW
37#
38# Each line may optionally have one of the following flags on it, separated by
39# white space from the initial token.
5f1720e9 40# string indicates that the output is to be of the string form
61dad979 41# described in the comments above that are placed in the file.
632c9f80
KW
42# string_skip_ifundef is the same as 'string', but instead of dying if the
43# code point doesn't exist, the line is just skipped: no output is
44# generated for it
5f1720e9 45# first indicates that the output is to be of the FIRST_BYTE form.
61dad979 46# tail indicates that the output is of the _TAIL form.
525b6419
KW
47# native indicates that the output is the code point, converted to the
48# platform's native character set if applicable
61dad979 49#
765ec46c
KW
50# If the code point has no official name, the desired name may be appended
51# after the flag, which will be ignored if there is an official name.
52#
61dad979
KW
53# This program is used to make it convenient to create compile time constants
54# of UTF-8, and to generate proper EBCDIC as well as ASCII without manually
55# having to figure things out.
56
57while ( <DATA> ) {
76837d21
KW
58 if ($_ !~ /\S/) {
59 print $out_fh "\n";
60 next;
61 }
62
61dad979
KW
63 chomp;
64 unless ($_ =~ m/ ^ ( [^\ ]* ) # Name or code point token
765ec46c
KW
65 (?: [\ ]+ ( [^ ]* ) )? # optional flag
66 (?: [\ ]+ ( .* ) )? # name if unnamed; flag is required
61dad979
KW
67 /x)
68 {
69 die "Unexpected syntax at line $.: $_\n";
70 }
71
72 my $name_or_cp = $1;
73 my $flag = $2;
765ec46c 74 my $desired_name = $3;
61dad979
KW
75
76 my $name;
77 my $cp;
632c9f80 78 my $undef_ok = $desired_name || $flag =~ /skip_if_undef/;
61dad979
KW
79
80 if ($name_or_cp =~ /[^[:xdigit:]]/) {
81
82 # Anything that isn't a hex value must be a name.
83 $name = $name_or_cp;
84 $cp = charnames::vianame($name =~ s/_/ /gr);
85 die "Unknown name '$name' at line $.: $_\n" unless defined $name;
86 }
87 else {
88 $cp = $name_or_cp;
632c9f80
KW
89 $name = charnames::viacode("0$cp"); # viacode requires a leading zero
90 # to be sure that the argument is
91 # hex
92 if (! defined $name) {
93 die "Unknown code point '$cp' at line $.: $_\n" unless $undef_ok;
94 $name = "";
95 }
61dad979
KW
96 }
97
632c9f80 98 $name = $desired_name if $name eq "" && $desired_name;
e9cddfae 99 $name =~ s/[- ]/_/g; # The macro name can have no blanks nor dashes
61dad979
KW
100
101 my $str = join "", map { sprintf "\\x%02X", $_ }
102 unpack("U0C*", pack("U", hex $cp));
103
104 my $suffix = '_UTF8';
632c9f80 105 if (! defined $flag || $flag =~ /^ string (_skip_if_undef)? $/x) {
61dad979
KW
106 $str = "\"$str\""; # Will be a string constant
107 } elsif ($flag eq 'tail') {
108 $str =~ s/\\x..//; # Remove the first byte
109 $suffix .= '_TAIL';
110 $str = "\"$str\""; # Will be a string constant
111 }
112 elsif ($flag eq 'first') {
113 $str =~ s/ \\x ( .. ) .* /$1/x; # Get the two nibbles of the 1st byte
114 $suffix .= '_FIRST_BYTE';
115 $str = "0x$str"; # Is a numeric constant
116 }
525b6419
KW
117 elsif ($flag eq 'native') {
118 die "Are you sure you want to run this on an above-Latin1 code point?" if hex $cp > 0xff;
119 $suffix = '_NATIVE';
120 $str = utf8::unicode_to_native(hex $cp);
121 $str = "0x$cp"; # Is a numeric constant
122 }
61dad979
KW
123 else {
124 die "Unknown flag at line $.: $_\n";
125 }
126 print $out_fh "#define ${name}$suffix $str /* U+$cp */\n";
127}
128
1b0f46bf 129print $out_fh "\n#endif /* H_UNICODE_CONSTANTS */\n";
d10c72f2 130
61dad979
KW
131read_only_bottom_close_and_rename($out_fh);
132
133__DATA__
5f1720e9
KW
1340300 string
1350301 string
1360308 string
76837d21 137
0a982f06 13803B9 string
76837d21 139
0a982f06 14003C5 string
76837d21 141
5f1720e9 1422010 string
765ec46c 143D800 first FIRST_SURROGATE
525b6419
KW
144
145007F native
14600DF native
14700E5 native
14800C5 native
14900FF native
15000B5 native
1510085 native