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