This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Here are the long-expected Unicode/UTF-8 modifications.
[perl5.git] / lib / utf8_heavy.pl
CommitLineData
a0ed51b3
LW
1package utf8;
2
3my $DEBUG = 0;
4my $seq = "AAA0000";
5
6sub DESTROY {}
7
8sub croak { require Carp; Carp::croak(@_) }
9
10sub 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 "unicode/$file.pl"
33 || do "unicode/Is/${type}.pl"
34 || croak("Can't find 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])(.*)/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
99sub SWASHGET {
100 my ($self, $start, $len) = @_;
101 local $^D = 0 if $^D;
102 print STDERR "SWASHGET @_\n" if $DEBUG;
103 my $type = $self->{TYPE};
104 my $bits = $self->{BITS};
105 my $none = $self->{NONE};
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 =~ /^([^0-9a-fA-F])(.*)/mg) {
170 my $char = $1;
171 my $name = $2;
172 print STDERR "INDIRECT $1 $2\n" if $DEBUG;
173 if ($char =~ /^[-+!]$/) {
174 my $otherbits = $self->{$name}->{BITS};
175 croak("SWASHGET size mismatch") if $bits < $otherbits;
176 my $other = $self->{$name}->SWASHGET($start, $len);
177 if ($char eq '+') {
178 if ($bits == 1 and $otherbits == 1) {
179 $swatch |= $other;
180 }
181 else {
182 for ($key = 0; $key < $len; $key++) {
183 vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
184 }
185 }
186 }
187 elsif ($char eq '!') {
188 if ($bits == 1 and $otherbits == 1) {
189 $swatch |= ~$other;
190 }
191 else {
192 for ($key = 0; $key < $len; $key++) {
193 if (!vec($other, $key, $otherbits)) {
194 vec($swatch, $key, $bits) = 1;
195 }
196 }
197 }
198 }
199 elsif ($char eq '-') {
200 if ($bits == 1 and $otherbits == 1) {
201 $swatch &= ~$other;
202 }
203 else {
204 for ($key = 0; $key < $len; $key++) {
205 if (vec($other, $key, $otherbits)) {
206 vec($swatch, $key, $bits) = 0;
207 }
208 }
209 }
210 }
211 }
212 }
213 }
214 if ($DEBUG) {
215 print STDERR "CELLS ";
216 for ($key = 0; $key < $len; $key++) {
217 print STDERR vec($swatch, $key, $bits), " ";
218 }
219 print STDERR "\n";
220 }
221 $swatch;
222}
223
2241;