This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.c: Silence HP-UX compiler warning
[perl5.git] / regen / unicode_constants.pl
index bbd4497..baf25f1 100644 (file)
@@ -2,10 +2,12 @@ use v5.16.0;
 use strict;
 use warnings;
 require 'regen/regen_lib.pl';
+require 'regen/charset_translations.pl';
+use Unicode::UCD;
 use charnames qw(:loose);
 
 my $out_fh = open_new('unicode_constants.h', '>',
-                     {style => '*', by => $0,
+        {style => '*', by => $0,
                       from => "Unicode data"});
 
 print $out_fh <<END;
@@ -13,10 +15,11 @@ print $out_fh <<END;
 #ifndef H_UNICODE_CONSTANTS   /* Guard against nested #includes */
 #define H_UNICODE_CONSTANTS   1
 
-/* This file contains #defines for various Unicode code points.  The values
- * the macros expand to are the native Unicode code point, or all or portions
- * of the UTF-8 encoding for the code point.  In the former case, the macro
- * name has the suffix "_NATIVE"; otherwise, the suffix "_UTF8".
+/* This file contains #defines for the version of Unicode being used and
+ * various Unicode code points.  The values the code point macros expand to
+ * are the native Unicode code point, or all or portions of the UTF-8 encoding
+ * for the code point.  In the former case, the macro name has the suffix
+ * "_NATIVE"; otherwise, the suffix "_UTF8".
  *
  * The macros that have the suffix "_UTF8" may have further suffixes, as
  * follows:
@@ -27,7 +30,157 @@ print $out_fh <<END;
 
 END
 
-# The data are at the end of this file.  A blank line is output as-is.
+my $version = Unicode::UCD::UnicodeVersion();
+my ($major, $dot, $dotdot) = $version =~ / (.*?) \. (.*?) (?: \. (.*) )? $ /x;
+$dotdot = 0 unless defined $dotdot;
+
+print $out_fh <<END;
+#define UNICODE_MAJOR_VERSION   $major
+#define UNICODE_DOT_VERSION     $dot
+#define UNICODE_DOT_DOT_VERSION $dotdot
+
+END
+
+# The data are at __DATA__  in this file.
+
+my @data = <DATA>;
+
+foreach my $charset (get_supported_code_pages()) {
+    print $out_fh "\n" . get_conditional_compile_line_start($charset);
+
+    my @a2n = @{get_a2n($charset)};
+
+    for ( @data ) {
+        chomp;
+
+        # Convert any '#' comments to /* ... */; empty lines and comments are
+        # output as blank lines
+        if ($_ =~ m/ ^ \s* (?: \# ( .* ) )? $ /x) {
+            my $comment_body = $1 // "";
+            if ($comment_body ne "") {
+                print $out_fh "/* $comment_body */\n";
+            }
+            else {
+                print $out_fh "\n";
+            }
+            next;
+        }
+
+        unless ($_ =~ m/ ^ ( [^\ ]* )           # Name or code point token
+                        (?: [\ ]+ ( [^ ]* ) )?  # optional flag
+                        (?: [\ ]+ ( .* ) )?  # name if unnamed; flag is required
+                    /x)
+        {
+            die "Unexpected syntax at line $.: $_\n";
+        }
+
+        my $name_or_cp = $1;
+        my $flag = $2;
+        my $desired_name = $3;
+
+        my $name;
+        my $cp;
+        my $U_cp;   # code point in Unicode (not-native) terms
+
+        if ($name_or_cp =~ /^U\+(.*)/) {
+            $U_cp = hex $1;
+            $name = charnames::viacode($name_or_cp);
+            if (! defined $name) {
+                next if $flag =~ /skip_if_undef/;
+                die "Unknown code point '$name_or_cp' at line $.: $_\n" unless $desired_name;
+                $name = "";
+            }
+        }
+        else {
+            $name = $name_or_cp;
+            die "Unknown name '$name' at line $.: $_\n" unless defined $name;
+            $U_cp = charnames::vianame($name =~ s/_/ /gr);
+        }
+
+        $cp = ($U_cp < 256)
+            ? $a2n[$U_cp]
+            : $U_cp;
+
+        $name = $desired_name if $name eq "" && $desired_name;
+        $name =~ s/[- ]/_/g;   # The macro name can have no blanks nor dashes
+
+        my $str;
+        my $suffix;
+        if (defined $flag && $flag eq 'native') {
+            die "Are you sure you want to run this on an above-Latin1 code point?" if $cp > 0xff;
+            $suffix = '_NATIVE';
+            $str = sprintf "0x%02X", $cp;        # Is a numeric constant
+        }
+        else {
+            $str = join "", map { sprintf "\\x%02X", ord $_ } split //, cp_2_utfbytes($U_cp, $charset);
+
+            $suffix = '_UTF8';
+            if (! defined $flag || $flag =~ /^ string (_skip_if_undef)? $/x) {
+                $str = "\"$str\"";  # Will be a string constant
+            } elsif ($flag eq 'tail') {
+                    $str =~ s/\\x..//;  # Remove the first byte
+                    $suffix .= '_TAIL';
+                    $str = "\"$str\"";  # Will be a string constant
+            }
+            elsif ($flag eq 'first') {
+                $str =~ s/ \\x ( .. ) .* /$1/x; # Get the two nibbles of the 1st byte
+                $suffix .= '_FIRST_BYTE';
+                $str = "0x$str";        # Is a numeric constant
+            }
+            else {
+                die "Unknown flag at line $.: $_\n";
+            }
+        }
+        printf $out_fh "#   define %s%s  %s    /* U+%04X */\n", $name, $suffix, $str, $U_cp;
+    }
+
+    my $max_PRINT_A = 0;
+    for my $i (0x20 .. 0x7E) {
+        $max_PRINT_A = $a2n[$i] if $a2n[$i] > $max_PRINT_A;
+    }
+    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;
+
+    print $out_fh "\n" . get_conditional_compile_line_end();
+
+}
+
+use Unicode::UCD 'prop_invlist';
+
+my $count = 0;
+my @other_invlist = prop_invlist("Other");
+for (my $i = 0; $i < @other_invlist; $i += 2) {
+    $count += ((defined $other_invlist[$i+1])
+              ? $other_invlist[$i+1]
+              : 0x110000)
+              - $other_invlist[$i];
+}
+printf $out_fh "\n/* The number of code points not matching \\pC */\n"
+             . "#define NON_OTHER_COUNT_FOR_USE_ONLY_BY_REGCOMP_DOT_C  %d\n",
+            0x110000 - $count;
+
+# If this release has both the CWCM and CWCF properties, find the highest code
+# point which changes under any case change.  We can use this to short-circuit
+# code
+my @cwcm = prop_invlist('CWCM');
+if (@cwcm) {
+    my @cwcf = prop_invlist('CWCF');
+    if (@cwcf) {
+        my $max = ($cwcm[-1] < $cwcf[-1])
+                  ? $cwcf[-1]
+                  : $cwcm[-1];
+        printf $out_fh "\n/* The highest code point that has any type of case change */\n"
+             . "#define HIGHEST_CASE_CHANGING_CP_FOR_USE_ONLY_BY_UTF8_DOT_C  0x%X\n",
+            $max - 1;
+    }
+}
+
+print $out_fh "\n#endif /* H_UNICODE_CONSTANTS */\n";
+
+read_only_bottom_close_and_rename($out_fh);
+
+# DATA FORMAT
+#
+# A blank line is output as-is.
 # Comments (lines whose first non-blank is a '#') are converted to C-style,
 # though empty comments are converted to blank lines.  Otherwise, each line
 # represents one #define, and begins with either a Unicode character name with
@@ -56,116 +209,33 @@ END
 # of UTF-8, and to generate proper EBCDIC as well as ASCII without manually
 # having to figure things out.
 
-while ( <DATA> ) {
-    chomp;
-
-    # Convert any '#' comments to /* ... */; empty lines and comments are
-    # output as blank lines
-    if ($_ =~ m/ ^ \s* (?: \# ( .* ) )? $ /x) {
-        my $comment_body = $1 // "";
-        if ($comment_body ne "") {
-            print $out_fh "/* $comment_body */\n";
-        }
-        else {
-            print $out_fh "\n";
-        }
-        next;
-    }
-
-    unless ($_ =~ m/ ^ ( [^\ ]* )           # Name or code point token
-                       (?: [\ ]+ ( [^ ]* ) )?  # optional flag
-                       (?: [\ ]+ ( .* ) )?  # name if unnamed; flag is required
-                   /x)
-    {
-        die "Unexpected syntax at line $.: $_\n";
-    }
-
-    my $name_or_cp = $1;
-    my $flag = $2;
-    my $desired_name = $3;
-
-    my $name;
-    my $cp;
-    my $U_cp;   # code point in Unicode (not-native) terms
-    my $undef_ok = $desired_name || $flag =~ /skip_if_undef/;
-
-    if ($name_or_cp =~ /^U\+(.*)/) {
-        $U_cp = hex $1;
-        $name = charnames::viacode($name_or_cp);
-        if (! defined $name) {
-            die "Unknown code point '$name_or_cp' at line $.: $_\n" unless $undef_ok;
-            $name = "";
-        }
-        $cp = utf8::unicode_to_native($U_cp);
-    }
-    else {
-        $name = $name_or_cp;
-        $cp = charnames::vianame($name =~ s/_/ /gr);
-        $U_cp = utf8::native_to_unicode($cp);
-        die "Unknown name '$name' at line $.: $_\n" unless defined $name;
-    }
-
-    $name = $desired_name if $name eq "" && $desired_name;
-    $name =~ s/[- ]/_/g;   # The macro name can have no blanks nor dashes
-
-    my $str = join "", map { sprintf "\\x%02X", $_ }
-                       unpack("U0C*", pack("U", $cp));
-
-    my $suffix = '_UTF8';
-    if (! defined $flag  || $flag =~ /^ string (_skip_if_undef)? $/x) {
-        $str = "\"$str\"";  # Will be a string constant
-    } elsif ($flag eq 'tail') {
-            $str =~ s/\\x..//;  # Remove the first byte
-            $suffix .= '_TAIL';
-            $str = "\"$str\"";  # Will be a string constant
-    }
-    elsif ($flag eq 'first') {
-        $str =~ s/ \\x ( .. ) .* /$1/x; # Get the two nibbles of the 1st byte
-        $suffix .= '_FIRST_BYTE';
-        $str = "0x$str";        # Is a numeric constant
-    }
-    elsif ($flag eq 'native') {
-        die "Are you sure you want to run this on an above-Latin1 code point?" if $cp > 0xff;
-        $suffix = '_NATIVE';
-        $str = sprintf "0x%02X", $cp;        # Is a numeric constant
-    }
-    else {
-        die "Unknown flag at line $.: $_\n";
-    }
-    printf $out_fh "#define %s%s  %s    /* U+%04X */\n", $name, $suffix, $str, $U_cp;
-}
-
-print $out_fh "\n#endif /* H_UNICODE_CONSTANTS */\n";
-
-read_only_bottom_close_and_rename($out_fh);
-
 __DATA__
 U+017F string
 
 U+0300 string
-U+0301 string
-U+0308 string
 
 U+0399 string
 U+03BC string
 
-U+03B9 string
-
-U+03C5 string
-
-U+1E9E string
+U+1E9E string_skip_if_undef
 
 U+FB05 string
 U+FB06 string
+U+0130 string
+U+0131 string
 
 U+2010 string
-U+D800 first FIRST_SURROGATE
 BOM first
 BOM tail
 
+NBSP native
+NBSP string
+
 DEL native
 CR  native
 LF  native
+VT  native
+ESC native
 U+00DF native
 U+00E5 native
 U+00C5 native