7 sub croak { require Carp; Carp::croak(@_) }
10 my ($class, $type, $list, $minbits, $none) = @_;
13 print STDERR "SWASHNEW @_\n" if DEBUG;
17 if ($type and ref ${"${class}::{$type}"} eq $class) {
18 warn qq/Found \${"${class}::{$type}"}\n/ if DEBUG;
19 return ${"${class}::{$type}"}; # Already there...
24 defined %utf8::In || do "unicore/In.pl";
26 $type =~ s/^In(?:[-_]|\s+)?//i;
29 my $inprefix = substr(lc($type), 0, 3);
30 if (exists $utf8::InPat{$inprefix}) {
32 for my $k (keys %{$utf8::InPat{$inprefix}}) {
34 $In = $utf8::InPat{$inprefix}->{$k};
35 if (exists $utf8::In{$In}) {
36 $file = "unicore/In/$utf8::In{$In}";
37 print "inprefix = $inprefix, In = $In, k = $k, file = $file\n" if DEBUG;
44 # This is separate from 'To' in preparation of Is.pl (a la In.pl).
45 if ((not defined $file) && $type =~ /^Is([A-Z][A-Za-z]*)$/) {
46 $file = "unicore/Is/$1";
49 if ((not defined $file) && $type =~ /^To([A-Z][A-Za-z]*)$/) {
50 $file = "unicore/To/$1";
55 $list ||= do "$file.pl"
56 || do "unicore/Is/$type.pl"
57 || croak("Can't find Unicode character property \"$type\"");
64 my @tmp = split(/^/m, $list);
67 $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
69 sort { hex $a <=> hex $b }
70 grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right
74 my $hextra = sprintf "%04x", $none + 1;
75 $list =~ s/\tXXXX$/\t$hextra/mg;
80 while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
82 my $max = hex(defined $2 ? $2 : $1);
83 my $val = hex(defined $3 ? $3 : "");
84 $val += $max - $min if defined $3;
85 $top = $val if $val > $top;
92 $bits = $minbits if $bits < $minbits;
97 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
100 # print STDERR "$1 => $2\n" if DEBUG;
101 if ($char =~ /[-+!]/) {
102 my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
103 my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
104 push @extras, $name => $subobj;
105 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
110 print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
112 ${"${class}::{$type}"} = bless {
122 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
125 my ($self, $start, $len) = @_;
126 local $^D = 0 if $^D;
127 my $type = $self->{TYPE};
128 my $bits = $self->{BITS};
129 my $none = $self->{NONE};
130 print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
131 my $end = $start + $len;
134 vec($swatch, $len - 1, $bits) = 0; # Extend to correct length.
136 for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
139 for ($self->{LIST}) {
143 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
145 my $max = (defined $2 ? hex $2 : $min);
147 next if $max < $start;
148 # print "$min $max $val\n";
151 $val += $start - $min if $val < $none;
154 for ($key = $min; $key <= $max; $key++) {
155 last LINE if $key >= $end;
156 # print STDERR "$key => $val\n" if DEBUG;
157 vec($swatch, $key - $start, $bits) = $val;
158 ++$val if $val < $none;
163 $val += $start - $min;
166 for ($key = $min; $key <= $max; $key++, $val++) {
167 last LINE if $key >= $end;
168 # print STDERR "$key => $val\n" if DEBUG;
169 vec($swatch, $key - $start, $bits) = $val;
176 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
178 my $max = (defined $2 ? hex $2 : $min);
179 next if $max < $start;
183 for ($key = $min; $key <= $max; $key++) {
184 last LINE if $key >= $end;
185 # print STDERR "$key => 1\n" if DEBUG;
186 vec($swatch, $key - $start, 1) = 1;
191 for my $x ($self->{EXTRAS}) {
193 while ($x =~ /^([-+!])(.*)/mg) {
196 print STDERR "INDIRECT $1 $2\n" if DEBUG;
197 my $otherbits = $self->{$name}->{BITS};
198 croak("SWASHGET size mismatch") if $bits < $otherbits;
199 my $other = $self->{$name}->SWASHGET($start, $len);
201 if ($bits == 1 and $otherbits == 1) {
205 for ($key = 0; $key < $len; $key++) {
206 vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
210 elsif ($char eq '!') {
211 if ($bits == 1 and $otherbits == 1) {
215 for ($key = 0; $key < $len; $key++) {
216 if (!vec($other, $key, $otherbits)) {
217 vec($swatch, $key, $bits) = 1;
222 elsif ($char eq '-') {
223 if ($bits == 1 and $otherbits == 1) {
227 for ($key = 0; $key < $len; $key++) {
228 if (vec($other, $key, $otherbits)) {
229 vec($swatch, $key, $bits) = 0;
237 print STDERR "CELLS ";
238 for ($key = 0; $key < $len; $key++) {
239 print STDERR vec($swatch, $key, $bits), " ";