Re: XS-assisted SWASHGET (esp. for t/uni/class.t speedup)
[p5sagit/p5-mst-13.2.git] / lib / utf8_heavy.pl
CommitLineData
a0ed51b3 1package utf8;
cf25bb62 2use strict;
3use warnings;
a0ed51b3 4
15732964 5sub DEBUG () { 0 }
a0ed51b3 6
7sub DESTROY {}
8
5beb625e 9my %Cache;
10
12ac2576 11our (%PropertyAlias, %PA_reverse, %PropValueAlias, %PVA_reverse, %PVA_abbr_map);
12
86916d66 13sub croak { require Carp; Carp::croak(@_) }
14
5beb625e 15##
3a2263fe 16## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape.
17## It's a data structure that encodes a set of Unicode characters.
5beb625e 18##
19
a0ed51b3 20sub SWASHNEW {
21 my ($class, $type, $list, $minbits, $none) = @_;
22 local $^D = 0 if $^D;
15732964 23
24 print STDERR "SWASHNEW @_\n" if DEBUG;
25
cf25bb62 26 ##
27 ## Get the list of codepoints for the type.
28 ## Called from utf8.c
29 ##
30 ## Given a $type, our goal is to fill $list with the set of codepoint
5beb625e 31 ## ranges.
cf25bb62 32 ##
33 ## To make the parsing of $type clear, this code takes the a rather
2d92f8a0 34 ## unorthodox approach of last'ing out of the block once we have the
cf25bb62 35 ## info we need. Were this to be a subroutine, the 'last' would just
36 ## be a 'return'.
37 ##
5beb625e 38 my $file; ## file to load data from, and also part of the %Cache key.
39 my $ListSorted = 0;
40
cf25bb62 41 if ($type)
42 {
43 $type =~ s/^\s+//;
44 $type =~ s/\s+$//;
45
46 print "type = $type\n" if DEBUG;
47
cf25bb62 48 GETFILE:
49 {
12ac2576 50 ##
51 ## It could be a user-defined property.
52 ##
53
54 my $caller1 = $type =~ s/(.+)::// ? $1 : caller(1);
55
56 if (defined $caller1 && $type =~ /^(?:\w+)$/) {
57 my $prop = "${caller1}::$type";
58 if (exists &{$prop}) {
59 no strict 'refs';
60
61 $list = &{$prop};
62 last GETFILE;
63 }
64 }
65
2d92f8a0 66 my $wasIs;
67
68 ($wasIs = $type =~ s/^Is(?:\s+|[-_])?//i)
5beb625e 69 or
12ac2576 70 $type =~ s/^(?:(?:General(?:\s+|_)?)?Category|gc)\s*[:=]\s*//i
5beb625e 71 or
12ac2576 72 $type =~ s/^(?:Script|sc)\s*[:=]\s*//i
5beb625e 73 or
12ac2576 74 $type =~ s/^Block\s*[:=]\s*/In/i;
75
76
77 ##
78 ## See if it's in some enumeration.
79 ##
7ebf06b3 80 require "unicore/PVA.pl";
12ac2576 81 if ($type =~ /^([\w\s]+)[:=]\s*(.*)/) {
12ac2576 82 my ($enum, $val) = (lc $1, lc $2);
83 $enum =~ tr/ _-//d;
84 $val =~ tr/ _-//d;
85
86 my $pa = $PropertyAlias{$enum} ? $enum : $PA_reverse{$enum};
87 my $f = $PropValueAlias{$pa}{$val} ? $val : $PVA_reverse{$pa}{lc $val};
88
89 if ($pa and $f) {
90 $pa = "gc_sc" if $pa eq "gc" or $pa eq "sc";
91 $file = "unicore/lib/$pa/$PVA_abbr_map{$pa}{lc $f}.pl";
92 last GETFILE;
93 }
94 }
95 else {
96 my $t = lc $type;
97 $t =~ tr/ _-//d;
98
99 if ($PropValueAlias{gc}{$t} or $PropValueAlias{sc}{$t}) {
100 $file = "unicore/lib/gc_sc/$PVA_abbr_map{gc_sc}{$t}.pl";
101 last GETFILE;
102 }
103 }
cf25bb62 104
5beb625e 105 ##
106 ## See if it's in the direct mapping table.
107 ##
108 require "unicore/Exact.pl";
109 if (my $base = $utf8::Exact{$type}) {
12ac2576 110 $file = "unicore/lib/gc_sc/$base.pl";
5beb625e 111 last GETFILE;
cf25bb62 112 }
113
114 ##
5beb625e 115 ## If not there exactly, try the canonical form. The canonical
116 ## form is lowercased, with any separators (\s+|[-_]) removed.
cf25bb62 117 ##
5beb625e 118 my $canonical = lc $type;
119 $canonical =~ s/(?<=[a-z\d])(?:\s+|[-_])(?=[a-z\d])//g;
120 print "canonical = $canonical\n" if DEBUG;
cf25bb62 121
5beb625e 122 require "unicore/Canonical.pl";
12ac2576 123 if (my $base = ($utf8::Canonical{$canonical} || $utf8::Canonical{ lc $utf8::PropertyAlias{$canonical} })) {
124 $file = "unicore/lib/gc_sc/$base.pl";
5beb625e 125 last GETFILE;
cf25bb62 126 }
127
491fd90a 128 ##
3a2263fe 129 ## See if it's a user-level "To".
130 ##
131
132 my $caller0 = caller(0);
133
134 if (defined $caller0 && $type =~ /^To(?:\w+)$/) {
135 my $map = $caller0 . "::" . $type;
09e0265a 136
3a2263fe 137 if (exists &{$map}) {
138 no strict 'refs';
139
140 $list = &{$map};
141 last GETFILE;
142 }
143 }
144
cf25bb62 145 ##
3a2263fe 146 ## Last attempt -- see if it's a standard "To" name
147 ## (e.g. "ToLower") ToTitle is used by ucfirst().
148 ## The user-level way to access ToDigit() and ToFold()
149 ## is to use Unicode::UCD.
cf25bb62 150 ##
3a2263fe 151 if ($type =~ /^To(Digit|Fold|Lower|Title|Upper)$/)
cf25bb62 152 {
153 $file = "unicore/To/$1.pl";
154 ## would like to test to see if $file actually exists....
155 last GETFILE;
156 }
157
158 ##
159 ## If we reach this line, it's because we couldn't figure
160 ## out what to do with $type. Ouch.
161 ##
bc45ce41 162
163 return $type;
cf25bb62 164 }
165
491fd90a 166 if (defined $file) {
167 print "found it (file='$file')\n" if DEBUG;
168
169 ##
170 ## If we reach here, it was due to a 'last GETFILE' above
3a2263fe 171 ## (exception: user-defined properties and mappings), so we
491fd90a 172 ## have a filename, so now we load it if we haven't already.
173 ## If we have, return the cached results. The cache key is the
174 ## file to load.
175 ##
176 if ($Cache{$file} and ref($Cache{$file}) eq $class)
177 {
178 print "Returning cached '$file' for \\p{$type}\n" if DEBUG;
179 return $Cache{$class, $file};
180 }
181
182 $list = do $file;
183 }
5beb625e 184
5beb625e 185 $ListSorted = 1; ## we know that these lists are sorted
886ba366 186 }
a0ed51b3 187
15732964 188 my $extras;
485aafd9 189 my $bits = 0;
cf25bb62 190
5beb625e 191 my $ORIG = $list;
a0ed51b3 192 if ($list) {
193 my @tmp = split(/^/m, $list);
194 my %seen;
db376a24 195 no warnings;
a0ed51b3 196 $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
197 $list = join '',
df68b396 198 map { $_->[1] }
199 sort { $a->[0] <=> $b->[0] }
200 map { /^([0-9a-fA-F]+)/; [ hex($1), $_ ] }
201 grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
a0ed51b3 202 }
203
204 if ($none) {
205 my $hextra = sprintf "%04x", $none + 1;
206 $list =~ s/\tXXXX$/\t$hextra/mg;
207 }
208
209 if ($minbits < 32) {
210 my $top = 0;
3a2263fe 211 while ($list =~ /^([0-9a-fA-F]+)(?:[\t]([0-9a-fA-F]+)?)(?:[ \t]([0-9a-fA-F]+))?/mg) {
a0ed51b3 212 my $min = hex $1;
3a2263fe 213 my $max = defined $2 ? hex $2 : $min;
214 my $val = defined $3 ? hex $3 : 0;
a0ed51b3 215 $val += $max - $min if defined $3;
216 $top = $val if $val > $top;
217 }
218 $bits =
219 $top > 0xffff ? 32 :
220 $top > 0xff ? 16 :
221 $top > 1 ? 8 : 1
222 }
223 $bits = $minbits if $bits < $minbits;
224
225 my @extras;
226 for my $x ($extras) {
227 pos $x = 0;
1fef36c7 228 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
a0ed51b3 229 my $char = $1;
230 my $name = $2;
6d6ae962 231 print STDERR "$1 => $2\n" if DEBUG;
09e0265a 232 if ($char =~ /[-+!&]/) {
a0ed51b3 233 my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
11ef8fdd 234 my $subobj;
235 if ($c eq 'utf8') {
09e0265a 236 $subobj = utf8->SWASHNEW($t, "", 0, 0, 0);
237 }
238 elsif (exists &$name) {
239 $subobj = utf8->SWASHNEW($name, "", 0, 0, 0);
11ef8fdd 240 }
241 elsif ($c =~ /^([0-9a-fA-F]+)/) {
242 $subobj = utf8->SWASHNEW("", $c, 0, 0, 0);
243 }
bc45ce41 244 return $subobj unless ref $subobj;
a0ed51b3 245 push @extras, $name => $subobj;
246 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
247 }
248 }
249 }
250
15732964 251 print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
a0ed51b3 252
5beb625e 253 my $SWASH = bless {
a0ed51b3 254 TYPE => $type,
255 BITS => $bits,
256 EXTRAS => $extras,
257 LIST => $list,
258 NONE => $none,
259 @extras,
260 } => $class;
5beb625e 261
262 if ($file) {
263 $Cache{$class, $file} = $SWASH;
264 }
265
266 return $SWASH;
a0ed51b3 267}
268
979f2922 269# Now SWASHGET is recasted into a C function S_swash_get (see utf8.c).
a0ed51b3 270
2711;