a small patch to perlintro.pod.
[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
a0ed51b3 15 if ($type and ref ${"${class}::{$type}"} eq $class) {
15732964 16 warn qq/Found \${"${class}::{$type}"}\n/ if DEBUG;
a0ed51b3 17 return ${"${class}::{$type}"}; # Already there...
18 }
19
15732964 20 if ($type) {
d73e5302 21 $type =~ s/^\s+//;
22 $type =~ s/\s+$//;
15732964 23
d73e5302 24 print "type = $type\n" if DEBUG;
15732964 25
d73e5302 26 my $file;
15732964 27
d73e5302 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 }
d9efae67 78 }
79 }
80 }
15732964 81
d73e5302 82 unless (defined $file) {
83 if ($type =~ /^To([A-Z][A-Za-z]+)$/) {
84 $file = "unicore/To/$1";
85 }
dee4fd60 86 }
15732964 87 }
886ba366 88
d73e5302 89 if (defined $file) {
90 $list = do "$file.pl";
91 }
92
93 croak("Can't find Unicode character property \"$type\"")
94 unless $list;
886ba366 95 }
a0ed51b3 96
15732964 97 my $extras;
98 my $bits;
99
a0ed51b3 100 if ($list) {
101 my @tmp = split(/^/m, $list);
102 my %seen;
db376a24 103 no warnings;
a0ed51b3 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;
1fef36c7 134 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
a0ed51b3 135 my $char = $1;
136 my $name = $2;
6d6ae962 137 print STDERR "$1 => $2\n" if DEBUG;
a0ed51b3 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
15732964 147 print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
a0ed51b3 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
161sub SWASHGET {
035d37be 162 # See utf8.c:Perl_swash_fetch for problems with this interface.
a0ed51b3 163 my ($self, $start, $len) = @_;
164 local $^D = 0 if $^D;
a0ed51b3 165 my $type = $self->{TYPE};
166 my $bits = $self->{BITS};
167 my $none = $self->{NONE};
15732964 168 print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
a0ed51b3 169 my $end = $start + $len;
170 my $swatch = "";
171 my $key;
172 vec($swatch, $len - 1, $bits) = 0; # Extend to correct length.
173 if ($none) {
174 for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
175 }
176
177 for ($self->{LIST}) {
178 pos $_ = 0;
179 if ($bits > 1) {
180 LINE:
181 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
182 my $min = hex $1;
183 my $max = (defined $2 ? hex $2 : $min);
184 my $val = hex $3;
185 next if $max < $start;
6d6ae962 186 print "$min $max $val\n" if DEBUG;
a0ed51b3 187 if ($none) {
188 if ($min < $start) {
710250cb 189 $val += $start - $min if $val < $none;
a0ed51b3 190 $min = $start;
191 }
192 for ($key = $min; $key <= $max; $key++) {
193 last LINE if $key >= $end;
6d6ae962 194 print STDERR "$key => $val\n" if DEBUG;
a0ed51b3 195 vec($swatch, $key - $start, $bits) = $val;
196 ++$val if $val < $none;
197 }
198 }
199 else {
200 if ($min < $start) {
201 $val += $start - $min;
202 $min = $start;
203 }
204 for ($key = $min; $key <= $max; $key++, $val++) {
205 last LINE if $key >= $end;
6d6ae962 206 print STDERR "$key => $val\n" if DEBUG;
a0ed51b3 207 vec($swatch, $key - $start, $bits) = $val;
208 }
209 }
210 }
211 }
212 else {
213 LINE:
214 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
215 my $min = hex $1;
216 my $max = (defined $2 ? hex $2 : $min);
217 next if $max < $start;
218 if ($min < $start) {
219 $min = $start;
220 }
221 for ($key = $min; $key <= $max; $key++) {
222 last LINE if $key >= $end;
6d6ae962 223 print STDERR "$key => 1\n" if DEBUG;
a0ed51b3 224 vec($swatch, $key - $start, 1) = 1;
225 }
226 }
227 }
228 }
229 for my $x ($self->{EXTRAS}) {
230 pos $x = 0;
1fef36c7 231 while ($x =~ /^([-+!])(.*)/mg) {
a0ed51b3 232 my $char = $1;
233 my $name = $2;
15732964 234 print STDERR "INDIRECT $1 $2\n" if DEBUG;
1fef36c7 235 my $otherbits = $self->{$name}->{BITS};
236 croak("SWASHGET size mismatch") if $bits < $otherbits;
237 my $other = $self->{$name}->SWASHGET($start, $len);
238 if ($char eq '+') {
239 if ($bits == 1 and $otherbits == 1) {
240 $swatch |= $other;
a0ed51b3 241 }
1fef36c7 242 else {
243 for ($key = 0; $key < $len; $key++) {
244 vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
a0ed51b3 245 }
1fef36c7 246 }
247 }
248 elsif ($char eq '!') {
249 if ($bits == 1 and $otherbits == 1) {
250 $swatch |= ~$other;
251 }
252 else {
253 for ($key = 0; $key < $len; $key++) {
254 if (!vec($other, $key, $otherbits)) {
255 vec($swatch, $key, $bits) = 1;
a0ed51b3 256 }
257 }
258 }
1fef36c7 259 }
260 elsif ($char eq '-') {
261 if ($bits == 1 and $otherbits == 1) {
262 $swatch &= ~$other;
263 }
264 else {
265 for ($key = 0; $key < $len; $key++) {
266 if (vec($other, $key, $otherbits)) {
267 vec($swatch, $key, $bits) = 0;
a0ed51b3 268 }
269 }
270 }
271 }
272 }
273 }
15732964 274 if (DEBUG) {
a0ed51b3 275 print STDERR "CELLS ";
276 for ($key = 0; $key < $len; $key++) {
277 print STDERR vec($swatch, $key, $bits), " ";
278 }
279 print STDERR "\n";
280 }
281 $swatch;
282}
283
2841;