This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
unicode_constants.h: Add #defines for Byte Order Mark
[perl5.git] / regen / unicode_constants.pl
index 0bb5321..78e0110 100644 (file)
@@ -32,7 +32,7 @@ END
 # though empty comments are converted to blank lines.  Otherwise, each line
 # represents one #define, and begins with either a Unicode character name with
 # the blanks and dashes in it squeezed out or replaced by underscores; or it
 # though empty comments are converted to blank lines.  Otherwise, each line
 # represents one #define, and begins with either a Unicode character name with
 # the blanks and dashes in it squeezed out or replaced by underscores; or it
-# may be a hexadecimal Unicode code point.  In the latter
+# may be a hexadecimal Unicode code point of the form U+xxxx.  In the latter
 # case, the name will be looked-up to use as the name of the macro.  In either
 # case, the macro name will have suffixes as listed above, and all blanks and
 # dashes will be replaced by underscores.
 # case, the name will be looked-up to use as the name of the macro.  In either
 # case, the macro name will have suffixes as listed above, and all blanks and
 # dashes will be replaced by underscores.
@@ -86,31 +86,30 @@ while ( <DATA> ) {
 
     my $name;
     my $cp;
 
     my $name;
     my $cp;
+    my $U_cp;   # code point in Unicode (not-native) terms
     my $undef_ok = $desired_name || $flag =~ /skip_if_undef/;
 
     my $undef_ok = $desired_name || $flag =~ /skip_if_undef/;
 
-    if ($name_or_cp =~ /[^[:xdigit:]]/) {
-
-        # Anything that isn't a hex value must be a name.
-        $name = $name_or_cp;
-        $cp = charnames::vianame($name =~ s/_/ /gr);
-        die "Unknown name '$name' at line $.: $_\n" unless defined $name;
-    }
-    else {
-        $cp = $name_or_cp;
-        $name = charnames::viacode("0$cp"); # viacode requires a leading zero
-                                            # to be sure that the argument is
-                                            # hex
+    if ($name_or_cp =~ /^U\+(.*)/) {
+        $U_cp = hex $1;
+        $name = charnames::viacode($name_or_cp);
         if (! defined $name) {
         if (! defined $name) {
-            die "Unknown code point '$cp' at line $.: $_\n" unless $undef_ok;
+            die "Unknown code point '$name_or_cp' at line $.: $_\n" unless $undef_ok;
             $name = "";
         }
             $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", $_ }
     }
 
     $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", hex $cp));
+                       unpack("U0C*", pack("U", $cp));
 
     my $suffix = '_UTF8';
     if (! defined $flag  || $flag =~ /^ string (_skip_if_undef)? $/x) {
 
     my $suffix = '_UTF8';
     if (! defined $flag  || $flag =~ /^ string (_skip_if_undef)? $/x) {
@@ -126,15 +125,14 @@ while ( <DATA> ) {
         $str = "0x$str";        # Is a numeric constant
     }
     elsif ($flag eq 'native') {
         $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 hex $cp > 0xff;
+        die "Are you sure you want to run this on an above-Latin1 code point?" if $cp > 0xff;
         $suffix = '_NATIVE';
         $suffix = '_NATIVE';
-        $str = utf8::unicode_to_native(hex $cp);
-        $str = "0x$cp";        # Is a numeric constant
+        $str = sprintf "0x%02X", $cp;        # Is a numeric constant
     }
     else {
         die "Unknown flag at line $.: $_\n";
     }
     }
     else {
         die "Unknown flag at line $.: $_\n";
     }
-    print $out_fh "#define ${name}$suffix $str    /* U+$cp */\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";
 }
 
 print $out_fh "\n#endif /* H_UNICODE_CONSTANTS */\n";
@@ -142,21 +140,29 @@ print $out_fh "\n#endif /* H_UNICODE_CONSTANTS */\n";
 read_only_bottom_close_and_rename($out_fh);
 
 __DATA__
 read_only_bottom_close_and_rename($out_fh);
 
 __DATA__
-0300 string
-0301 string
-0308 string
+U+017F string
+
+U+0300 string
+U+0301 string
+U+0308 string
+
+U+0399 string
+U+03BC string
+
+U+03B9 string
 
 
-03B9 string
+U+03C5 string
 
 
-03C5 string
+U+1E9E string
 
 
-2010 string
-D800 first FIRST_SURROGATE
+U+2010 string
+U+D800 first FIRST_SURROGATE
+BOM first
+BOM tail
 
 
-007F native
-00DF native
-00E5 native
-00C5 native
-00FF native
-00B5 native
-0085 native
+DEL native
+U+00DF native
+U+00E5 native
+U+00C5 native
+U+00FF native
+U+00B5 native