Skip the RV printing test under threads until fixed.
[p5sagit/p5-mst-13.2.git] / lib / utf8_heavy.pl
1 package utf8;
2
3 sub DEBUG () { 0 }
4
5 sub DESTROY {}
6
7 sub croak { require Carp; Carp::croak(@_) }
8
9 sub SWASHNEW {
10     my ($class, $type, $list, $minbits, $none) = @_;
11     local $^D = 0 if $^D;
12
13     print STDERR "SWASHNEW @_\n" if DEBUG;
14
15     my $file;
16
17     if ($type and ref ${"${class}::{$type}"} eq $class) {
18         warn qq/Found \${"${class}::{$type}"}\n/ if DEBUG;
19         return ${"${class}::{$type}"};  # Already there...
20     }
21
22     if ($type) {
23
24         defined %utf8::In || do "unicore/In.pl";
25
26         $type =~ s/^In(?:[-_]|\s+)?//i;
27         $type =~ s/\s+$//;
28
29         my $inprefix = substr(lc($type), 0, 3);
30         if (exists $utf8::InPat{$inprefix}) {
31             my $In = $type;
32             for my $k (keys %{$utf8::InPat{$inprefix}}) {
33                 if ($In =~ /^$k$/i) {
34                     $In = $utf8::InPat{$inprefix}->{$k};
35                     if (exists $utf8::In{$In}) {
36                         $file = "unicore/In/$utf8::In{$In}";
37                         print "inprefix = $inprefix, In = $In, k = $k, file = $file\n" if DEBUG;
38                         last;
39                     }
40                 }
41             }
42         }
43
44         # This is separate from 'To' in preparation of Is.pl (a la In.pl).
45         if ((not defined $file) && $type =~ /^Is([A-Z][A-Za-z]*)$/) {
46             $file = "unicore/Is/$1";
47         }
48
49         if ((not defined $file) && $type =~ /^To([A-Z][A-Za-z]*)$/) {
50             $file = "unicore/To/$1";
51         }
52     }
53
54     {
55         $list ||= do "$file.pl"
56               ||  do "unicore/Is/$type.pl"
57               ||  croak("Can't find Unicode character property \"$type\"");
58     }
59
60     my $extras;
61     my $bits;
62  
63     if ($list) {
64         my @tmp = split(/^/m, $list);
65         my %seen;
66         no warnings;
67         $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
68         $list = join '',
69             sort { hex $a <=> hex $b }
70             grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right
71     }
72
73     if ($none) {
74         my $hextra = sprintf "%04x", $none + 1;
75         $list =~ s/\tXXXX$/\t$hextra/mg;
76     }
77
78     if ($minbits < 32) {
79         my $top = 0;
80         while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
81             my $min = hex $1;
82             my $max = hex(defined $2 ? $2 : $1);
83             my $val = hex(defined $3 ? $3 : "");
84             $val += $max - $min if defined $3;
85             $top = $val if $val > $top;
86         }
87         $bits =
88             $top > 0xffff ? 32 :
89             $top > 0xff ? 16 :
90             $top > 1 ? 8 : 1
91     }
92     $bits = $minbits if $bits < $minbits;
93
94     my @extras;
95     for my $x ($extras) {
96         pos $x = 0;
97         while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
98             my $char = $1;
99             my $name = $2;
100 #           print STDERR "$1 => $2\n" if DEBUG;
101             if ($char =~ /[-+!]/) {
102                 my ($c,$t) = split(/::/, $name, 2);     # bogus use of ::, really
103                 my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
104                 push @extras, $name => $subobj;
105                 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
106             }
107         }
108     }
109
110     print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
111
112     ${"${class}::{$type}"} = bless {
113         TYPE => $type,
114         BITS => $bits,
115         EXTRAS => $extras,
116         LIST => $list,
117         NONE => $none,
118         @extras,
119     } => $class;
120 }
121
122 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
123
124 sub SWASHGET {
125     my ($self, $start, $len) = @_;
126     local $^D = 0 if $^D;
127     my $type = $self->{TYPE};
128     my $bits = $self->{BITS};
129     my $none = $self->{NONE};
130     print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
131     my $end = $start + $len;
132     my $swatch = "";
133     my $key;
134     vec($swatch, $len - 1, $bits) = 0;  # Extend to correct length.
135     if ($none) {
136         for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
137     }
138
139     for ($self->{LIST}) {
140         pos $_ = 0;
141         if ($bits > 1) {
142           LINE:
143             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
144                 my $min = hex $1;
145                 my $max = (defined $2 ? hex $2 : $min);
146                 my $val = hex $3;
147                 next if $max < $start;
148 #               print "$min $max $val\n";
149                 if ($none) {
150                     if ($min < $start) {
151                         $val += $start - $min if $val < $none;
152                         $min = $start;
153                     }
154                     for ($key = $min; $key <= $max; $key++) {
155                         last LINE if $key >= $end;
156 #                       print STDERR "$key => $val\n" if DEBUG;
157                         vec($swatch, $key - $start, $bits) = $val;
158                         ++$val if $val < $none;
159                     }
160                 }
161                 else {
162                     if ($min < $start) {
163                         $val += $start - $min;
164                         $min = $start;
165                     }
166                     for ($key = $min; $key <= $max; $key++, $val++) {
167                         last LINE if $key >= $end;
168 #                       print STDERR "$key => $val\n" if DEBUG;
169                         vec($swatch, $key - $start, $bits) = $val;
170                     }
171                 }
172             }
173         }
174         else {
175           LINE:
176             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
177                 my $min = hex $1;
178                 my $max = (defined $2 ? hex $2 : $min);
179                 next if $max < $start;
180                 if ($min < $start) {
181                     $min = $start;
182                 }
183                 for ($key = $min; $key <= $max; $key++) {
184                     last LINE if $key >= $end;
185 #                   print STDERR "$key => 1\n" if DEBUG;
186                     vec($swatch, $key - $start, 1) = 1;
187                 }
188             }
189         }
190     }
191     for my $x ($self->{EXTRAS}) {
192         pos $x = 0;
193         while ($x =~ /^([-+!])(.*)/mg) {
194             my $char = $1;
195             my $name = $2;
196             print STDERR "INDIRECT $1 $2\n" if DEBUG;
197             my $otherbits = $self->{$name}->{BITS};
198             croak("SWASHGET size mismatch") if $bits < $otherbits;
199             my $other = $self->{$name}->SWASHGET($start, $len);
200             if ($char eq '+') {
201                 if ($bits == 1 and $otherbits == 1) {
202                     $swatch |= $other;
203                 }
204                 else {
205                     for ($key = 0; $key < $len; $key++) {
206                         vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
207                     }
208                 }
209             }
210             elsif ($char eq '!') {
211                 if ($bits == 1 and $otherbits == 1) {
212                     $swatch |= ~$other;
213                 }
214                 else {
215                     for ($key = 0; $key < $len; $key++) {
216                         if (!vec($other, $key, $otherbits)) {
217                             vec($swatch, $key, $bits) = 1;
218                         }
219                     }
220                 }
221             }
222             elsif ($char eq '-') {
223                 if ($bits == 1 and $otherbits == 1) {
224                     $swatch &= ~$other;
225                 }
226                 else {
227                     for ($key = 0; $key < $len; $key++) {
228                         if (vec($other, $key, $otherbits)) {
229                             vec($swatch, $key, $bits) = 0;
230                         }
231                     }
232                 }
233             }
234         }
235     }
236     if (DEBUG) {
237         print STDERR "CELLS ";
238         for ($key = 0; $key < $len; $key++) {
239             print STDERR vec($swatch, $key, $bits), " ";
240         }
241         print STDERR "\n";
242     }
243     $swatch;
244 }
245
246 1;