This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Preserve the case of t/lib/vmsfspec.t.
[perl5.git] / regen / mk_PL_charclass.pl
index 5a3dbbe..33b7c87 100644 (file)
@@ -1,5 +1,5 @@
 #!perl -w
-use 5.012;
+use v5.15.8;
 use strict;
 use warnings;
 require 'regen/regen_lib.pl';
@@ -22,82 +22,140 @@ require 'regen/regen_lib.pl';
 # new Unicode release, to make sure things haven't been changed by it.
 
 my @properties = qw(
-    ALNUMC_A
-    ALNUMC_L1
-    ALPHA_A
-    ALPHA_L1
-    BLANK_A
-    BLANK_L1
+    NONLATIN1_FOLD
+    ALNUMC
+    ALPHA
+    ASCII
+    BLANK
     CHARNAME_CONT
-    CNTRL_A
-    CNTRL_L1
-    DIGIT_A
-    GRAPH_A
-    GRAPH_L1
-    IDFIRST_A
-    IDFIRST_L1
-    LOWER_A
-    LOWER_L1
-    PRINT_A
-    PRINT_L1
-    PSXSPC_A
-    PSXSPC_L1
-    PUNCT_A
-    PUNCT_L1
-    SPACE_A
-    SPACE_L1
-    UPPER_A
-    UPPER_L1
-    WORDCHAR_A
-    WORDCHAR_L1
-    XDIGIT_A
+    CNTRL
+    DIGIT
+    GRAPH
+    IDFIRST
+    LOWER
+    NON_FINAL_FOLD
+    PRINT
+    PSXSPC
+    PUNCT
     QUOTEMETA
+    SPACE
+    UPPER
+    WORDCHAR
+    XDIGIT
+    VERTSPACE
+    IS_IN_SOME_FOLD
 );
 
 # Read in the case fold mappings.
 my %folded_closure;
-my $file="lib/unicore/CaseFolding.txt";
-open my $fh, "<", $file or die "Failed to read '$file': $!";
-while (<$fh>) {
-    chomp;
-
-    # Lines look like (without the initial '#'
-    #0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE
-    # Get rid of comments, ignore blank or comment-only lines
-    my $line = $_ =~ s/ (?: \s* \# .* )? $ //rx;
-    next unless length $line;
-    my ($hex_from, $fold_type, @folded) = split /[\s;]+/, $line;
-
-    my $from = hex $hex_from;
-
-    # Perl only deals with C and F folds
-    next if $fold_type ne 'C' and $fold_type ne 'F';
-
-    # Get each code point in the range that participates in this line's fold.
-    # The hash has keys of each code point in the range, and values of what it
-    # folds to and what folds to it
-    foreach my $hex_fold (@folded) {
-        my $fold = hex $hex_fold;
-        push @{$folded_closure{$fold}}, $from if $fold < 256;
-        push @{$folded_closure{$from}}, $fold if $from < 256;
+my @hex_non_final_folds;
+my @folds;
+use Unicode::UCD;
+
+BEGIN { # Have to do this at compile time because using user-defined \p{property}
+
+    # Use the Unicode data file if we are on an ASCII platform (which its data
+    # is for), and it is in the modern format (starting in Unicode 3.1.0) and
+    # it is available.  This avoids being affected by potential bugs
+    # introduced by other layers of Perl
+    my $file="lib/unicore/CaseFolding.txt";
+
+    if (ord('A') == 65
+        && pack("C*", split /\./, Unicode::UCD::UnicodeVersion()) ge v3.1.0
+        && open my $fh, "<", $file)
+    {
+        @folds = <$fh>;
     }
-}
+    else {
+        my ($invlist_ref, $invmap_ref, undef, $default)
+                                    = Unicode::UCD::prop_invmap('Case_Folding');
+        for my $i (0 .. @$invlist_ref - 1 - 1) {
+            next if $invmap_ref->[$i] == $default;
+            my $adjust = -1;
+            for my $j ($invlist_ref->[$i] .. $invlist_ref->[$i+1] -1) {
+                $adjust++;
 
-# Now having read all the lines, combine them into the full closure of each
-# code point in the range by adding lists together that share a common element
-foreach my $folded (keys %folded_closure) {
-    foreach my $from (grep { $_ < 256 } @{$folded_closure{$folded}}) {
-        push @{$folded_closure{$from}}, @{$folded_closure{$folded}};
+                # Single-code point maps go to a 'C' type
+                if (! ref $invmap_ref->[$i]) {
+                    push @folds, sprintf("%04X; C; %04X\n",
+                                        $j,
+                                        $invmap_ref->[$i] + $adjust);
+                }
+                else {  # Multi-code point maps go to 'F'.  prop_invmap()
+                        # guarantees that no adjustment is needed for these,
+                        # as the range will contain just one element
+                    push @folds, sprintf("%04X; F; %s\n",
+                                        $j,
+                                        join " ", map { sprintf "%04X", $_ }
+                                                        @{$invmap_ref->[$i]});
+                }
+            }
+        }
+    }
+
+    for (@folds) {
+        chomp;
+
+        # Lines look like (without the initial '#'
+        #0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE
+        # Get rid of comments, ignore blank or comment-only lines
+        my $line = $_ =~ s/ (?: \s* \# .* )? $ //rx;
+        next unless length $line;
+        my ($hex_from, $fold_type, @folded) = split /[\s;]+/, $line;
+
+        my $from = hex $hex_from;
+
+        # Perl only deals with C and F folds
+        next if $fold_type ne 'C' and $fold_type ne 'F';
+
+        # Get each code point in the range that participates in this line's fold.
+        # The hash has keys of each code point in the range, and values of what it
+        # folds to and what folds to it
+        for my $i (0 .. @folded - 1) {
+            my $hex_fold = $folded[$i];
+            my $fold = hex $hex_fold;
+            push @{$folded_closure{$fold}}, $from if $fold < 256;
+            push @{$folded_closure{$from}}, $fold if $from < 256;
+
+            if ($i < @folded-1
+                && $fold < 256
+                && ! grep { $_ eq $hex_fold } @hex_non_final_folds)
+            {
+                push @hex_non_final_folds, $hex_fold;
+
+                # Also add the upper case, which in the latin1 range folds to
+                # $fold
+                push @hex_non_final_folds, sprintf "%04X", ord uc chr $fold;
+            }
+        }
+    }
+
+    # Now having read all the lines, combine them into the full closure of each
+    # code point in the range by adding lists together that share a common
+    # element
+    foreach my $folded (keys %folded_closure) {
+        foreach my $from (grep { $_ < 256 } @{$folded_closure{$folded}}) {
+            push @{$folded_closure{$from}}, @{$folded_closure{$folded}};
+        }
     }
 }
 
-my @bits;   # Bit map for each code point
+sub Is_Non_Latin1_Fold {
+    my @return;
 
-foreach my $folded (keys %folded_closure) {
-    $bits[$folded] = "_CC_NONLATIN1_FOLD" if grep { $_ > 255 }
-                                                @{$folded_closure{$folded}};
+    foreach my $folded (keys %folded_closure) {
+        push @return, sprintf("%X", $folded), if grep { $_ > 255 }
+                                                     @{$folded_closure{$folded}};
+    }
+    return join("\n", @return) . "\n";
+}
+
+sub Is_Non_Final_Fold {
+    return join("\n", @hex_non_final_folds) . "\n";
 }
 
+my @bits;   # Bit map for each code point
+
 # For each character, calculate which properties it matches.
 for my $ord (0..255) {
     my $char = chr($ord);
@@ -113,8 +171,8 @@ for my $ord (0..255) {
         if (! ($name =~ s/_L1$//)) {
 
             # Here, isn't an _L1.  If its _A, it's automatically false for
-            # non-ascii.  The only one current one without a suffix is valid
-            # over the whole range.
+            # non-ascii.  The only current ones (besides ASCII) without a
+            # suffix are valid over the whole range.
             next if $name =~ s/_A$// && $ord >= 128;
 
         }
@@ -125,7 +183,7 @@ for my $ord (0..255) {
             # just \pP outside it.
             $re = qr/\p{Punct}|[^\P{Symbol}\P{ASCII}]/;
         } elsif ($name eq 'CHARNAME_CONT') {;
-            $re = qr/[-\p{XPosixWord} ():\xa0]/;
+            $re = qr/\p{_Perl_Charname_Continue}/,
         } elsif ($name eq 'SPACE') {;
             $re = qr/\p{XPerlSpace}/;
         } elsif ($name eq 'IDFIRST') {
@@ -137,10 +195,14 @@ for my $ord (0..255) {
         } elsif ($name eq 'ALNUMC') {
             # Like \w, but no underscore
             $re = qr/\p{Alnum}/;
-        } elsif ($name eq 'OCTAL') {
-            $re = qr/[0-7]/;
         } elsif ($name eq 'QUOTEMETA') {
             $re = qr/\p{_Perl_Quotemeta}/;
+        } elsif ($name eq 'NONLATIN1_FOLD') {
+            $re = qr/\p{Is_Non_Latin1_Fold}/;
+        } elsif ($name eq 'NON_FINAL_FOLD') {
+            $re = qr/\p{Is_Non_Final_Fold}/;
+        } elsif ($name eq 'IS_IN_SOME_FOLD') {
+            $re = qr/\p{_Perl_Any_Folds}/;
         } else {    # The remainder have the same name and values as Unicode
             $re = eval "qr/\\p{$name}/";
             use Carp;
@@ -149,7 +211,7 @@ for my $ord (0..255) {
         #print "$ord, $name $property, $re\n";
         if ($char =~ $re) {  # Add this property if matches
             $bits[$ord] .= '|' if $bits[$ord];
-            $bits[$ord] .= "_CC_$property";
+            $bits[$ord] .= "(1U<<_CC_$property)";
         }
     }
     #print __LINE__, " $ord $char $bits[$ord]\n";
@@ -230,7 +292,7 @@ my @C1 = qw(
 
 my $out_fh = open_new('l1_char_class_tab.h', '>',
                      {style => '*', by => $0,
-                      from => "property definitions and $file"});
+                      from => "property definitions"});
 
 # Output the table using fairly short names for each char.
 for my $ord (0..255) {