fix intolerance of SWASHes for blank lines
[p5sagit/p5-mst-13.2.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     $file =~ s#^(I[sn]|To)([A-Z].*)#$1/$2#;
30     $list ||= eval { $caller->$type(); }
31         || do "$file.pl"
32         || do "$encoding/$file.pl"
33         || do "$encoding/Is/${type}.pl"
34         || croak("Can't find $encoding character property definition via $caller->$type or $file.pl");
35
36     $| = 1;
37
38     if ($list) {
39         my @tmp = split(/^/m, $list);
40         my %seen;
41         local $^W = 0;
42         $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
43         $list = join '',
44             sort { hex $a <=> hex $b }
45             grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right
46     }
47
48     if ($none) {
49         my $hextra = sprintf "%04x", $none + 1;
50         $list =~ s/\tXXXX$/\t$hextra/mg;
51     }
52
53     if ($minbits < 32) {
54         my $top = 0;
55         while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
56             my $min = hex $1;
57             my $max = hex(defined $2 ? $2 : $1);
58             my $val = hex(defined $3 ? $3 : "");
59             $val += $max - $min if defined $3;
60             $top = $val if $val > $top;
61         }
62         $bits =
63             $top > 0xffff ? 32 :
64             $top > 0xff ? 16 :
65             $top > 1 ? 8 : 1
66     }
67     $bits = $minbits if $bits < $minbits;
68
69     my @extras;
70     for my $x ($extras) {
71         pos $x = 0;
72         while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
73             my $char = $1;
74             my $name = $2;
75             # print STDERR "$1 => $2\n" if $DEBUG;
76             if ($char =~ /[-+!]/) {
77                 my ($c,$t) = split(/::/, $name, 2);     # bogus use of ::, really
78                 my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
79                 push @extras, $name => $subobj;
80                 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
81             }
82         }
83     }
84
85     print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if $DEBUG;
86
87     ${"${class}::{$type}"} = bless {
88         TYPE => $type,
89         BITS => $bits,
90         EXTRAS => $extras,
91         LIST => $list,
92         NONE => $none,
93         @extras,
94     } => $class;
95 }
96
97 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
98
99 sub SWASHGET {
100     my ($self, $start, $len) = @_;
101     local $^D = 0 if $^D;
102     my $type = $self->{TYPE};
103     my $bits = $self->{BITS};
104     my $none = $self->{NONE};
105     print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if $DEBUG;
106     my $end = $start + $len;
107     my $swatch = "";
108     my $key;
109     vec($swatch, $len - 1, $bits) = 0;  # Extend to correct length.
110     if ($none) {
111         for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
112     }
113
114     for ($self->{LIST}) {
115         pos $_ = 0;
116         if ($bits > 1) {
117           LINE:
118             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
119                 my $min = hex $1;
120                 my $max = (defined $2 ? hex $2 : $min);
121                 my $val = hex $3;
122                 next if $max < $start;
123 #               print "$min $max $val\n";
124                 if ($none) {
125                     if ($min < $start) {
126                         $val += $start - $min;
127                         $val = $none if $val > $none;
128                         $min = $start;
129                     }
130                     for ($key = $min; $key <= $max; $key++) {
131                         last LINE if $key >= $end;
132 #                       print STDERR "$key => $val\n" if $DEBUG;
133                         vec($swatch, $key - $start, $bits) = $val;
134                         ++$val if $val < $none;
135                     }
136                 }
137                 else {
138                     if ($min < $start) {
139                         $val += $start - $min;
140                         $min = $start;
141                     }
142                     for ($key = $min; $key <= $max; $key++, $val++) {
143                         last LINE if $key >= $end;
144 #                       print STDERR "$key => $val\n" if $DEBUG;
145                         vec($swatch, $key - $start, $bits) = $val;
146                     }
147                 }
148             }
149         }
150         else {
151           LINE:
152             while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
153                 my $min = hex $1;
154                 my $max = (defined $2 ? hex $2 : $min);
155                 next if $max < $start;
156                 if ($min < $start) {
157                     $min = $start;
158                 }
159                 for ($key = $min; $key <= $max; $key++) {
160                     last LINE if $key >= $end;
161 #                   print STDERR "$key => 1\n" if $DEBUG;
162                     vec($swatch, $key - $start, 1) = 1;
163                 }
164             }
165         }
166     }
167     for my $x ($self->{EXTRAS}) {
168         pos $x = 0;
169         while ($x =~ /^([-+!])(.*)/mg) {
170             my $char = $1;
171             my $name = $2;
172             print STDERR "INDIRECT $1 $2\n" if $DEBUG;
173             my $otherbits = $self->{$name}->{BITS};
174             croak("SWASHGET size mismatch") if $bits < $otherbits;
175             my $other = $self->{$name}->SWASHGET($start, $len);
176             if ($char eq '+') {
177                 if ($bits == 1 and $otherbits == 1) {
178                     $swatch |= $other;
179                 }
180                 else {
181                     for ($key = 0; $key < $len; $key++) {
182                         vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
183                     }
184                 }
185             }
186             elsif ($char eq '!') {
187                 if ($bits == 1 and $otherbits == 1) {
188                     $swatch |= ~$other;
189                 }
190                 else {
191                     for ($key = 0; $key < $len; $key++) {
192                         if (!vec($other, $key, $otherbits)) {
193                             vec($swatch, $key, $bits) = 1;
194                         }
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) = 0;
206                         }
207                     }
208                 }
209             }
210         }
211     }
212     if ($DEBUG) {
213         print STDERR "CELLS ";
214         for ($key = 0; $key < $len; $key++) {
215             print STDERR vec($swatch, $key, $bits), " ";
216         }
217         print STDERR "\n";
218     }
219     $swatch;
220 }
221
222 1;