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