This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
When Gconvert is a macro around sprintf with a .* format we need
[perl5.git] / lib / utf8_heavy.pl
CommitLineData
a0ed51b3 1package utf8;
cf25bb62
JH
2use strict;
3use warnings;
a0ed51b3 4
15732964 5sub DEBUG () { 0 }
a0ed51b3
LW
6
7sub DESTROY {}
8
5beb625e
JH
9my %Cache;
10
86916d66
JH
11sub croak { require Carp; Carp::croak(@_) }
12
5beb625e 13##
3a2263fe
RGS
14## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape.
15## It's a data structure that encodes a set of Unicode characters.
5beb625e
JH
16##
17
a0ed51b3
LW
18sub SWASHNEW {
19 my ($class, $type, $list, $minbits, $none) = @_;
20 local $^D = 0 if $^D;
15732964
JH
21
22 print STDERR "SWASHNEW @_\n" if DEBUG;
23
cf25bb62
JH
24 ##
25 ## Get the list of codepoints for the type.
26 ## Called from utf8.c
27 ##
28 ## Given a $type, our goal is to fill $list with the set of codepoint
5beb625e 29 ## ranges.
cf25bb62
JH
30 ##
31 ## To make the parsing of $type clear, this code takes the a rather
2d92f8a0 32 ## unorthodox approach of last'ing out of the block once we have the
cf25bb62
JH
33 ## info we need. Were this to be a subroutine, the 'last' would just
34 ## be a 'return'.
35 ##
5beb625e
JH
36 my $file; ## file to load data from, and also part of the %Cache key.
37 my $ListSorted = 0;
38
cf25bb62
JH
39 if ($type)
40 {
41 $type =~ s/^\s+//;
42 $type =~ s/\s+$//;
43
44 print "type = $type\n" if DEBUG;
45
cf25bb62
JH
46 GETFILE:
47 {
48 ##
5beb625e
JH
49 ## 'Is' is always optional, so if it's there, remove it.
50 ## Same with 'Category=' and 'Script='.
cf25bb62 51 ##
5beb625e 52 ## 'Block=' is replaced by 'In'.
cf25bb62 53 ##
2d92f8a0
JH
54 my $wasIs;
55
56 ($wasIs = $type =~ s/^Is(?:\s+|[-_])?//i)
5beb625e
JH
57 or
58 $type =~ s/^Category\s*=\s*//i
59 or
60 $type =~ s/^Script\s*=\s*//i
61 or
62 $type =~ s/^Block\s*=\s*/In/i;
cf25bb62 63
5beb625e
JH
64 ##
65 ## See if it's in the direct mapping table.
66 ##
67 require "unicore/Exact.pl";
68 if (my $base = $utf8::Exact{$type}) {
69 $file = "unicore/lib/$base.pl";
70 last GETFILE;
cf25bb62
JH
71 }
72
73 ##
5beb625e
JH
74 ## If not there exactly, try the canonical form. The canonical
75 ## form is lowercased, with any separators (\s+|[-_]) removed.
cf25bb62 76 ##
5beb625e
JH
77 my $canonical = lc $type;
78 $canonical =~ s/(?<=[a-z\d])(?:\s+|[-_])(?=[a-z\d])//g;
79 print "canonical = $canonical\n" if DEBUG;
cf25bb62 80
5beb625e
JH
81 require "unicore/Canonical.pl";
82 if (my $base = $utf8::Canonical{$canonical}) {
83 $file = "unicore/lib/$base.pl";
84 last GETFILE;
cf25bb62
JH
85 }
86
491fd90a
JH
87 ##
88 ## It could be a user-defined property.
89 ##
90
3a2263fe 91 my $caller1 = caller(1);
2d92f8a0 92
3a2263fe
RGS
93 if (defined $caller1 && $type =~ /^(?:\w+)$/) {
94 my $prop = $caller1 . "::" . ( $wasIs ? "Is" : "" ) . $type;
2d92f8a0
JH
95 if (exists &{$prop}) {
96 no strict 'refs';
97
98 $list = &{$prop};
99 last GETFILE;
100 }
101 }
491fd90a 102
3a2263fe
RGS
103 ##
104 ## See if it's a user-level "To".
105 ##
106
107 my $caller0 = caller(0);
108
109 if (defined $caller0 && $type =~ /^To(?:\w+)$/) {
110 my $map = $caller0 . "::" . $type;
111 if (exists &{$map}) {
112 no strict 'refs';
113
114 $list = &{$map};
115 last GETFILE;
116 }
117 }
118
cf25bb62 119 ##
3a2263fe
RGS
120 ## Last attempt -- see if it's a standard "To" name
121 ## (e.g. "ToLower") ToTitle is used by ucfirst().
122 ## The user-level way to access ToDigit() and ToFold()
123 ## is to use Unicode::UCD.
cf25bb62 124 ##
3a2263fe 125 if ($type =~ /^To(Digit|Fold|Lower|Title|Upper)$/)
cf25bb62
JH
126 {
127 $file = "unicore/To/$1.pl";
128 ## would like to test to see if $file actually exists....
129 last GETFILE;
130 }
131
132 ##
133 ## If we reach this line, it's because we couldn't figure
134 ## out what to do with $type. Ouch.
135 ##
bc45ce41
JH
136
137 return $type;
cf25bb62
JH
138 }
139
491fd90a
JH
140 if (defined $file) {
141 print "found it (file='$file')\n" if DEBUG;
142
143 ##
144 ## If we reach here, it was due to a 'last GETFILE' above
3a2263fe 145 ## (exception: user-defined properties and mappings), so we
491fd90a
JH
146 ## have a filename, so now we load it if we haven't already.
147 ## If we have, return the cached results. The cache key is the
148 ## file to load.
149 ##
150 if ($Cache{$file} and ref($Cache{$file}) eq $class)
151 {
152 print "Returning cached '$file' for \\p{$type}\n" if DEBUG;
153 return $Cache{$class, $file};
154 }
155
156 $list = do $file;
157 }
5beb625e 158
5beb625e 159 $ListSorted = 1; ## we know that these lists are sorted
886ba366 160 }
a0ed51b3 161
15732964 162 my $extras;
485aafd9 163 my $bits = 0;
cf25bb62 164
5beb625e 165 my $ORIG = $list;
a0ed51b3
LW
166 if ($list) {
167 my @tmp = split(/^/m, $list);
168 my %seen;
db376a24 169 no warnings;
a0ed51b3
LW
170 $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
171 $list = join '',
df68b396
JH
172 map { $_->[1] }
173 sort { $a->[0] <=> $b->[0] }
174 map { /^([0-9a-fA-F]+)/; [ hex($1), $_ ] }
175 grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
a0ed51b3
LW
176 }
177
178 if ($none) {
179 my $hextra = sprintf "%04x", $none + 1;
180 $list =~ s/\tXXXX$/\t$hextra/mg;
181 }
182
183 if ($minbits < 32) {
184 my $top = 0;
3a2263fe 185 while ($list =~ /^([0-9a-fA-F]+)(?:[\t]([0-9a-fA-F]+)?)(?:[ \t]([0-9a-fA-F]+))?/mg) {
a0ed51b3 186 my $min = hex $1;
3a2263fe
RGS
187 my $max = defined $2 ? hex $2 : $min;
188 my $val = defined $3 ? hex $3 : 0;
a0ed51b3
LW
189 $val += $max - $min if defined $3;
190 $top = $val if $val > $top;
191 }
192 $bits =
193 $top > 0xffff ? 32 :
194 $top > 0xff ? 16 :
195 $top > 1 ? 8 : 1
196 }
197 $bits = $minbits if $bits < $minbits;
198
199 my @extras;
200 for my $x ($extras) {
201 pos $x = 0;
1fef36c7 202 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
a0ed51b3
LW
203 my $char = $1;
204 my $name = $2;
6d6ae962 205 print STDERR "$1 => $2\n" if DEBUG;
a0ed51b3
LW
206 if ($char =~ /[-+!]/) {
207 my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
11ef8fdd
JH
208 my $subobj;
209 if ($c eq 'utf8') {
210 $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
211 }
212 elsif ($c =~ /^([0-9a-fA-F]+)/) {
213 $subobj = utf8->SWASHNEW("", $c, 0, 0, 0);
214 }
bc45ce41 215 return $subobj unless ref $subobj;
a0ed51b3
LW
216 push @extras, $name => $subobj;
217 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
218 }
219 }
220 }
221
15732964 222 print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
a0ed51b3 223
5beb625e 224 my $SWASH = bless {
a0ed51b3
LW
225 TYPE => $type,
226 BITS => $bits,
227 EXTRAS => $extras,
228 LIST => $list,
229 NONE => $none,
230 @extras,
231 } => $class;
5beb625e
JH
232
233 if ($file) {
234 $Cache{$class, $file} = $SWASH;
235 }
236
237 return $SWASH;
a0ed51b3
LW
238}
239
240# NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
241
242sub SWASHGET {
035d37be 243 # See utf8.c:Perl_swash_fetch for problems with this interface.
a0ed51b3
LW
244 my ($self, $start, $len) = @_;
245 local $^D = 0 if $^D;
a0ed51b3
LW
246 my $type = $self->{TYPE};
247 my $bits = $self->{BITS};
248 my $none = $self->{NONE};
15732964 249 print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
a0ed51b3
LW
250 my $end = $start + $len;
251 my $swatch = "";
252 my $key;
253 vec($swatch, $len - 1, $bits) = 0; # Extend to correct length.
254 if ($none) {
255 for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
256 }
257
258 for ($self->{LIST}) {
259 pos $_ = 0;
260 if ($bits > 1) {
261 LINE:
3a2263fe
RGS
262 while (/^([0-9a-fA-F]+)(?:[ \t]([0-9a-fA-F]+)?)?(?:[ \t]([0-9a-fA-F]+))?/mg) {
263 chomp;
264 my ($a, $b, $c) = ($1, $2, $3);
265 croak "$type: illegal mapping '$_'"
266 if $type =~ /^To/ &&
267 !(defined $a && defined $c);
268 my $min = hex $a;
269 my $max = defined $b ? hex $b : $min;
270 my $val = defined $c ? hex $c : 0;
a0ed51b3 271 next if $max < $start;
6d6ae962 272 print "$min $max $val\n" if DEBUG;
a0ed51b3
LW
273 if ($none) {
274 if ($min < $start) {
710250cb 275 $val += $start - $min if $val < $none;
a0ed51b3
LW
276 $min = $start;
277 }
278 for ($key = $min; $key <= $max; $key++) {
279 last LINE if $key >= $end;
6d6ae962 280 print STDERR "$key => $val\n" if DEBUG;
a0ed51b3
LW
281 vec($swatch, $key - $start, $bits) = $val;
282 ++$val if $val < $none;
283 }
284 }
285 else {
286 if ($min < $start) {
287 $val += $start - $min;
288 $min = $start;
289 }
290 for ($key = $min; $key <= $max; $key++, $val++) {
291 last LINE if $key >= $end;
6d6ae962 292 print STDERR "$key => $val\n" if DEBUG;
a0ed51b3
LW
293 vec($swatch, $key - $start, $bits) = $val;
294 }
295 }
296 }
297 }
298 else {
299 LINE:
99a6b1f0 300 while (/^([0-9a-fA-F]+)(?:[ \t]+([0-9a-fA-F]+))?/mg) {
3a2263fe 301 chomp;
a0ed51b3 302 my $min = hex $1;
3a2263fe 303 my $max = defined $2 ? hex $2 : $min;
a0ed51b3
LW
304 next if $max < $start;
305 if ($min < $start) {
306 $min = $start;
307 }
308 for ($key = $min; $key <= $max; $key++) {
309 last LINE if $key >= $end;
6d6ae962 310 print STDERR "$key => 1\n" if DEBUG;
a0ed51b3
LW
311 vec($swatch, $key - $start, 1) = 1;
312 }
313 }
314 }
315 }
316 for my $x ($self->{EXTRAS}) {
317 pos $x = 0;
1fef36c7 318 while ($x =~ /^([-+!])(.*)/mg) {
a0ed51b3
LW
319 my $char = $1;
320 my $name = $2;
15732964 321 print STDERR "INDIRECT $1 $2\n" if DEBUG;
1fef36c7
GA
322 my $otherbits = $self->{$name}->{BITS};
323 croak("SWASHGET size mismatch") if $bits < $otherbits;
324 my $other = $self->{$name}->SWASHGET($start, $len);
325 if ($char eq '+') {
326 if ($bits == 1 and $otherbits == 1) {
327 $swatch |= $other;
a0ed51b3 328 }
1fef36c7
GA
329 else {
330 for ($key = 0; $key < $len; $key++) {
331 vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
a0ed51b3 332 }
1fef36c7
GA
333 }
334 }
335 elsif ($char eq '!') {
336 if ($bits == 1 and $otherbits == 1) {
337 $swatch |= ~$other;
338 }
339 else {
340 for ($key = 0; $key < $len; $key++) {
341 if (!vec($other, $key, $otherbits)) {
342 vec($swatch, $key, $bits) = 1;
a0ed51b3
LW
343 }
344 }
345 }
1fef36c7
GA
346 }
347 elsif ($char eq '-') {
348 if ($bits == 1 and $otherbits == 1) {
349 $swatch &= ~$other;
350 }
351 else {
352 for ($key = 0; $key < $len; $key++) {
353 if (vec($other, $key, $otherbits)) {
354 vec($swatch, $key, $bits) = 0;
a0ed51b3
LW
355 }
356 }
357 }
358 }
359 }
360 }
15732964 361 if (DEBUG) {
a0ed51b3
LW
362 print STDERR "CELLS ";
363 for ($key = 0; $key < $len; $key++) {
364 print STDERR vec($swatch, $key, $bits), " ";
365 }
366 print STDERR "\n";
367 }
368 $swatch;
369}
370
3711;