home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / lib / utf8_heavy.pl < prev    next >
Encoding:
Perl Script  |  2002-10-29  |  8.9 KB  |  346 lines

  1. package utf8;
  2. use strict;
  3. use warnings;
  4.  
  5. sub DEBUG () { 0 }
  6.  
  7. sub DESTROY {}
  8.  
  9. my %Cache;
  10.  
  11. sub croak { require Carp; Carp::croak(@_) }
  12.  
  13. ##
  14. ## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape
  15. ##
  16.  
  17. sub SWASHNEW {
  18.     my ($class, $type, $list, $minbits, $none) = @_;
  19.     local $^D = 0 if $^D;
  20.  
  21.     print STDERR "SWASHNEW @_\n" if DEBUG;
  22.  
  23.     ##
  24.     ## Get the list of codepoints for the type.
  25.     ## Called from utf8.c
  26.     ##
  27.     ## Given a $type, our goal is to fill $list with the set of codepoint
  28.     ## ranges.
  29.     ##
  30.     ## To make the parsing of $type clear, this code takes the a rather
  31.     ## unorthodox approach of last'ing out of the block once we have the
  32.     ## info we need. Were this to be a subroutine, the 'last' would just
  33.     ## be a 'return'.
  34.     ##
  35.     my $file; ## file to load data from, and also part of the %Cache key.
  36.     my $ListSorted = 0;
  37.  
  38.     if ($type)
  39.     {
  40.         $type =~ s/^\s+//;
  41.         $type =~ s/\s+$//;
  42.  
  43.         print "type = $type\n" if DEBUG;
  44.  
  45.       GETFILE:
  46.         {
  47.             ##
  48.             ## 'Is' is always optional, so if it's there, remove it.
  49.             ## Same with 'Category=' and 'Script='.
  50.             ##
  51.             ## 'Block=' is replaced by 'In'.
  52.             ##
  53.             my $wasIs;
  54.  
  55.             ($wasIs = $type =~ s/^Is(?:\s+|[-_])?//i)
  56.               or
  57.             $type =~ s/^Category\s*=\s*//i
  58.               or
  59.             $type =~ s/^Script\s*=\s*//i
  60.               or
  61.             $type =~ s/^Block\s*=\s*/In/i;
  62.  
  63.             ##
  64.             ## See if it's in the direct mapping table.
  65.             ##
  66.             require "unicore/Exact.pl";
  67.             if (my $base = $utf8::Exact{$type}) {
  68.                 $file = "unicore/lib/$base.pl";
  69.                 last GETFILE;
  70.             }
  71.  
  72.             ##
  73.             ## If not there exactly, try the canonical form. The canonical
  74.             ## form is lowercased, with any separators (\s+|[-_]) removed.
  75.             ##
  76.             my $canonical = lc $type;
  77.             $canonical =~ s/(?<=[a-z\d])(?:\s+|[-_])(?=[a-z\d])//g;
  78.             print "canonical = $canonical\n" if DEBUG;
  79.  
  80.             require "unicore/Canonical.pl";
  81.             if (my $base = $utf8::Canonical{$canonical}) {
  82.                 $file = "unicore/lib/$base.pl";
  83.                 last GETFILE;
  84.             }
  85.  
  86.         ##
  87.         ## It could be a user-defined property.
  88.         ##
  89.  
  90.         my $caller = caller(1);
  91.  
  92.         if (defined $caller && $type =~ /^(?:\w+)$/) {
  93.         my $prop = $caller . "::" . ( $wasIs ? "Is" : "" ) . $type;
  94.         if (exists &{$prop}) {
  95.             no strict 'refs';
  96.             
  97.             $list = &{$prop};
  98.             last GETFILE;
  99.         }
  100.         }
  101.  
  102.             ##
  103.             ## Last attempt -- see if it's a "To" name (e.g. "ToLower")
  104.             ##
  105.             if ($type =~ /^To([A-Z][A-Za-z]+)$/)
  106.             {
  107.                 $file = "unicore/To/$1.pl";
  108.                 ## would like to test to see if $file actually exists....
  109.                 last GETFILE;
  110.             }
  111.  
  112.             ##
  113.             ## If we reach this line, it's because we couldn't figure
  114.             ## out what to do with $type. Ouch.
  115.             ##
  116.  
  117.             return $type;
  118.         }
  119.  
  120.     if (defined $file) {
  121.         print "found it (file='$file')\n" if DEBUG;
  122.  
  123.         ##
  124.         ## If we reach here, it was due to a 'last GETFILE' above
  125.         ## (exception: user-defined properties), so we
  126.         ## have a filename, so now we load it if we haven't already.
  127.         ## If we have, return the cached results. The cache key is the
  128.         ## file to load.
  129.         ##
  130.         if ($Cache{$file} and ref($Cache{$file}) eq $class)
  131.         {
  132.         print "Returning cached '$file' for \\p{$type}\n" if DEBUG;
  133.         return $Cache{$class, $file};
  134.         }
  135.  
  136.         $list = do $file;
  137.     }
  138.  
  139.         $ListSorted = 1; ## we know that these lists are sorted
  140.     }
  141.  
  142.     my $extras;
  143.     my $bits;
  144.  
  145.     my $ORIG = $list;
  146.     if ($list) {
  147.     my @tmp = split(/^/m, $list);
  148.     my %seen;
  149.     no warnings;
  150.     $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
  151.     $list = join '',
  152.         map  { $_->[1] }
  153.         sort { $a->[0] <=> $b->[0] }
  154.         map  { /^([0-9a-fA-F]+)/; [ hex($1), $_ ] }
  155.         grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
  156.     }
  157.  
  158.     if ($none) {
  159.     my $hextra = sprintf "%04x", $none + 1;
  160.     $list =~ s/\tXXXX$/\t$hextra/mg;
  161.     }
  162.  
  163.     if ($minbits < 32) {
  164.     my $top = 0;
  165.     while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
  166.         my $min = hex $1;
  167.         my $max = hex(defined $2 ? $2 : $1);
  168.         my $val = hex(defined $3 ? $3 : "");
  169.         $val += $max - $min if defined $3;
  170.         $top = $val if $val > $top;
  171.     }
  172.     $bits =
  173.         $top > 0xffff ? 32 :
  174.         $top > 0xff ? 16 :
  175.         $top > 1 ? 8 : 1
  176.     }
  177.     $bits = $minbits if $bits < $minbits;
  178.  
  179.     my @extras;
  180.     for my $x ($extras) {
  181.     pos $x = 0;
  182.     while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
  183.         my $char = $1;
  184.         my $name = $2;
  185.         print STDERR "$1 => $2\n" if DEBUG;
  186.         if ($char =~ /[-+!]/) {
  187.         my ($c,$t) = split(/::/, $name, 2);    # bogus use of ::, really
  188.         my $subobj;
  189.         if ($c eq 'utf8') {
  190.             $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
  191.         }
  192.         elsif ($c =~ /^([0-9a-fA-F]+)/) {
  193.             $subobj = utf8->SWASHNEW("", $c, 0, 0, 0);
  194.         }
  195.         return $subobj unless ref $subobj;
  196.         push @extras, $name => $subobj;
  197.         $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
  198.         }
  199.     }
  200.     }
  201.  
  202.     print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
  203.  
  204.     my $SWASH = bless {
  205.     TYPE => $type,
  206.     BITS => $bits,
  207.     EXTRAS => $extras,
  208.     LIST => $list,
  209.     NONE => $none,
  210.     @extras,
  211.     } => $class;
  212.  
  213.     if ($file) {
  214.         $Cache{$class, $file} = $SWASH;
  215.     }
  216.  
  217.     return $SWASH;
  218. }
  219.  
  220. # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
  221.  
  222. sub SWASHGET {
  223.     # See utf8.c:Perl_swash_fetch for problems with this interface.
  224.     my ($self, $start, $len) = @_;
  225.     local $^D = 0 if $^D;
  226.     my $type = $self->{TYPE};
  227.     my $bits = $self->{BITS};
  228.     my $none = $self->{NONE};
  229.     print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
  230.     my $end = $start + $len;
  231.     my $swatch = "";
  232.     my $key;
  233.     vec($swatch, $len - 1, $bits) = 0;    # Extend to correct length.
  234.     if ($none) {
  235.     for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
  236.     }
  237.  
  238.     for ($self->{LIST}) {
  239.     pos $_ = 0;
  240.     if ($bits > 1) {
  241.       LINE:
  242.         while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
  243.         my $min = hex $1;
  244.         my $max = (defined $2 ? hex $2 : $min);
  245.         my $val = hex $3;
  246.         next if $max < $start;
  247.         print "$min $max $val\n" if DEBUG;
  248.         if ($none) {
  249.             if ($min < $start) {
  250.             $val += $start - $min if $val < $none;
  251.             $min = $start;
  252.             }
  253.             for ($key = $min; $key <= $max; $key++) {
  254.             last LINE if $key >= $end;
  255.             print STDERR "$key => $val\n" if DEBUG;
  256.             vec($swatch, $key - $start, $bits) = $val;
  257.             ++$val if $val < $none;
  258.             }
  259.         }
  260.         else {
  261.             if ($min < $start) {
  262.             $val += $start - $min;
  263.             $min = $start;
  264.             }
  265.             for ($key = $min; $key <= $max; $key++, $val++) {
  266.             last LINE if $key >= $end;
  267.             print STDERR "$key => $val\n" if DEBUG;
  268.             vec($swatch, $key - $start, $bits) = $val;
  269.             }
  270.         }
  271.         }
  272.     }
  273.     else {
  274.       LINE:
  275.         while (/^([0-9a-fA-F]+)(?:[ \t]+([0-9a-fA-F]+))?/mg) {
  276.         my $min = hex $1;
  277.         my $max = (defined $2 ? hex $2 : $min);
  278.         next if $max < $start;
  279.         if ($min < $start) {
  280.             $min = $start;
  281.         }
  282.         for ($key = $min; $key <= $max; $key++) {
  283.             last LINE if $key >= $end;
  284.             print STDERR "$key => 1\n" if DEBUG;
  285.             vec($swatch, $key - $start, 1) = 1;
  286.         }
  287.         }
  288.     }
  289.     }
  290.     for my $x ($self->{EXTRAS}) {
  291.     pos $x = 0;
  292.     while ($x =~ /^([-+!])(.*)/mg) {
  293.         my $char = $1;
  294.         my $name = $2;
  295.         print STDERR "INDIRECT $1 $2\n" if DEBUG;
  296.         my $otherbits = $self->{$name}->{BITS};
  297.         croak("SWASHGET size mismatch") if $bits < $otherbits;
  298.         my $other = $self->{$name}->SWASHGET($start, $len);
  299.         if ($char eq '+') {
  300.         if ($bits == 1 and $otherbits == 1) {
  301.             $swatch |= $other;
  302.         }
  303.         else {
  304.             for ($key = 0; $key < $len; $key++) {
  305.             vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
  306.             }
  307.         }
  308.         }
  309.         elsif ($char eq '!') {
  310.         if ($bits == 1 and $otherbits == 1) {
  311.             $swatch |= ~$other;
  312.         }
  313.         else {
  314.             for ($key = 0; $key < $len; $key++) {
  315.             if (!vec($other, $key, $otherbits)) {
  316.                 vec($swatch, $key, $bits) = 1;
  317.             }
  318.             }
  319.         }
  320.         }
  321.         elsif ($char eq '-') {
  322.         if ($bits == 1 and $otherbits == 1) {
  323.             $swatch &= ~$other;
  324.         }
  325.         else {
  326.             for ($key = 0; $key < $len; $key++) {
  327.             if (vec($other, $key, $otherbits)) {
  328.                 vec($swatch, $key, $bits) = 0;
  329.             }
  330.             }
  331.         }
  332.         }
  333.     }
  334.     }
  335.     if (DEBUG) {
  336.     print STDERR "CELLS ";
  337.     for ($key = 0; $key < $len; $key++) {
  338.         print STDERR vec($swatch, $key, $bits), " ";
  339.     }
  340.     print STDERR "\n";
  341.     }
  342.     $swatch;
  343. }
  344.  
  345. 1;
  346.