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