Commit | Line | Data |
---|---|---|
61dad979 KW |
1 | use v5.16.0; |
2 | use strict; | |
3 | use warnings; | |
4 | require 'regen/regen_lib.pl'; | |
5 | use charnames qw(:loose); | |
6 | ||
1b0f46bf | 7 | my $out_fh = open_new('unicode_constants.h', '>', |
61dad979 KW |
8 | {style => '*', by => $0, |
9 | from => "Unicode data"}); | |
10 | ||
11 | print $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 | |
28 | END | |
29 | ||
76837d21 | 30 | # The data are at the end of this file. A blank line is output as-is. |
5a731a17 KW |
31 | # Comments (lines whose first non-blank is a '#') are converted to C-style, |
32 | # though empty comments are converted to blank lines. Otherwise, each line | |
33 | # represents one #define, and begins with either a Unicode character name with | |
34 | # the blanks and dashes in it squeezed out or replaced by underscores; or it | |
1dfa4f52 | 35 | # may be a hexadecimal Unicode code point of the form U+xxxx. In the latter |
76837d21 | 36 | # case, the name will be looked-up to use as the name of the macro. In either |
e9cddfae KW |
37 | # case, the macro name will have suffixes as listed above, and all blanks and |
38 | # dashes will be replaced by underscores. | |
61dad979 KW |
39 | # |
40 | # Each line may optionally have one of the following flags on it, separated by | |
41 | # white space from the initial token. | |
5f1720e9 | 42 | # string indicates that the output is to be of the string form |
61dad979 | 43 | # described in the comments above that are placed in the file. |
632c9f80 KW |
44 | # string_skip_ifundef is the same as 'string', but instead of dying if the |
45 | # code point doesn't exist, the line is just skipped: no output is | |
46 | # generated for it | |
5f1720e9 | 47 | # first indicates that the output is to be of the FIRST_BYTE form. |
61dad979 | 48 | # tail indicates that the output is of the _TAIL form. |
525b6419 KW |
49 | # native indicates that the output is the code point, converted to the |
50 | # platform's native character set if applicable | |
61dad979 | 51 | # |
765ec46c KW |
52 | # If the code point has no official name, the desired name may be appended |
53 | # after the flag, which will be ignored if there is an official name. | |
54 | # | |
61dad979 KW |
55 | # This program is used to make it convenient to create compile time constants |
56 | # of UTF-8, and to generate proper EBCDIC as well as ASCII without manually | |
57 | # having to figure things out. | |
58 | ||
59 | while ( <DATA> ) { | |
5a731a17 KW |
60 | chomp; |
61 | ||
62 | # Convert any '#' comments to /* ... */; empty lines and comments are | |
63 | # output as blank lines | |
64 | if ($_ =~ m/ ^ \s* (?: \# ( .* ) )? $ /x) { | |
65 | my $comment_body = $1 // ""; | |
66 | if ($comment_body ne "") { | |
67 | print $out_fh "/* $comment_body */\n"; | |
68 | } | |
69 | else { | |
70 | print $out_fh "\n"; | |
71 | } | |
76837d21 KW |
72 | next; |
73 | } | |
74 | ||
61dad979 | 75 | unless ($_ =~ m/ ^ ( [^\ ]* ) # Name or code point token |
765ec46c KW |
76 | (?: [\ ]+ ( [^ ]* ) )? # optional flag |
77 | (?: [\ ]+ ( .* ) )? # name if unnamed; flag is required | |
61dad979 KW |
78 | /x) |
79 | { | |
80 | die "Unexpected syntax at line $.: $_\n"; | |
81 | } | |
82 | ||
83 | my $name_or_cp = $1; | |
84 | my $flag = $2; | |
765ec46c | 85 | my $desired_name = $3; |
61dad979 KW |
86 | |
87 | my $name; | |
88 | my $cp; | |
1dfa4f52 | 89 | my $U_cp; # code point in Unicode (not-native) terms |
632c9f80 | 90 | my $undef_ok = $desired_name || $flag =~ /skip_if_undef/; |
61dad979 | 91 | |
1dfa4f52 KW |
92 | if ($name_or_cp =~ /^U\+(.*)/) { |
93 | $U_cp = hex $1; | |
94 | $name = charnames::viacode($name_or_cp); | |
632c9f80 | 95 | if (! defined $name) { |
1dfa4f52 | 96 | die "Unknown code point '$name_or_cp' at line $.: $_\n" unless $undef_ok; |
632c9f80 KW |
97 | $name = ""; |
98 | } | |
1dfa4f52 KW |
99 | $cp = utf8::unicode_to_native($U_cp); |
100 | } | |
101 | else { | |
102 | $name = $name_or_cp; | |
103 | $cp = charnames::vianame($name =~ s/_/ /gr); | |
104 | $U_cp = utf8::native_to_unicode($cp); | |
105 | die "Unknown name '$name' at line $.: $_\n" unless defined $name; | |
61dad979 KW |
106 | } |
107 | ||
632c9f80 | 108 | $name = $desired_name if $name eq "" && $desired_name; |
e9cddfae | 109 | $name =~ s/[- ]/_/g; # The macro name can have no blanks nor dashes |
61dad979 KW |
110 | |
111 | my $str = join "", map { sprintf "\\x%02X", $_ } | |
1dfa4f52 | 112 | unpack("U0C*", pack("U", $cp)); |
61dad979 KW |
113 | |
114 | my $suffix = '_UTF8'; | |
632c9f80 | 115 | if (! defined $flag || $flag =~ /^ string (_skip_if_undef)? $/x) { |
61dad979 KW |
116 | $str = "\"$str\""; # Will be a string constant |
117 | } elsif ($flag eq 'tail') { | |
118 | $str =~ s/\\x..//; # Remove the first byte | |
119 | $suffix .= '_TAIL'; | |
120 | $str = "\"$str\""; # Will be a string constant | |
121 | } | |
122 | elsif ($flag eq 'first') { | |
123 | $str =~ s/ \\x ( .. ) .* /$1/x; # Get the two nibbles of the 1st byte | |
124 | $suffix .= '_FIRST_BYTE'; | |
125 | $str = "0x$str"; # Is a numeric constant | |
126 | } | |
525b6419 | 127 | elsif ($flag eq 'native') { |
1dfa4f52 | 128 | die "Are you sure you want to run this on an above-Latin1 code point?" if $cp > 0xff; |
525b6419 | 129 | $suffix = '_NATIVE'; |
1dfa4f52 | 130 | $str = sprintf "0x%02X", $cp; # Is a numeric constant |
525b6419 | 131 | } |
61dad979 KW |
132 | else { |
133 | die "Unknown flag at line $.: $_\n"; | |
134 | } | |
1dfa4f52 | 135 | printf $out_fh "#define %s%s %s /* U+%04X */\n", $name, $suffix, $str, $U_cp; |
61dad979 KW |
136 | } |
137 | ||
1b0f46bf | 138 | print $out_fh "\n#endif /* H_UNICODE_CONSTANTS */\n"; |
d10c72f2 | 139 | |
61dad979 KW |
140 | read_only_bottom_close_and_rename($out_fh); |
141 | ||
142 | __DATA__ | |
f2e06375 | 143 | U+017F string |
76837d21 | 144 | |
1dfa4f52 | 145 | U+0300 string |
1dfa4f52 | 146 | |
a78bc3c6 KW |
147 | U+0399 string |
148 | U+03BC string | |
149 | ||
f2e06375 KW |
150 | U+1E9E string |
151 | ||
a9f50d33 KW |
152 | U+FB05 string |
153 | U+FB06 string | |
154 | ||
1dfa4f52 KW |
155 | U+2010 string |
156 | U+D800 first FIRST_SURROGATE | |
5f0aa340 KW |
157 | BOM first |
158 | BOM tail | |
525b6419 | 159 | |
05016631 | 160 | DEL native |
c5eda08a KW |
161 | CR native |
162 | LF native | |
1dfa4f52 KW |
163 | U+00DF native |
164 | U+00E5 native | |
165 | U+00C5 native | |
166 | U+00FF native | |
167 | U+00B5 native |