1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
package mkcommon;
use 5.012002;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
# Common code used to create Unicode lookup tables.
#
# Generates C code that declares a bunch of arrays.
#
# The 'rangetab' array's structure is [firstchar, lastchar], and the
# 'classtab' array has the same size containing "class", giving the
# associated "value" for unicode character range firstchar-lastchar.
#
# The ranges are sorted in numerical order, but rangetab stores the least
# singificant byte of the 32-bit Unicode character (firstchar and lastchar).
# The leading bytes of both firstchar and lastchar are the same.
#
# In this manner, the Unicode data gets divided into 256 character blocks.
#
# The "starting_indextab" array enumerates which 256 character blocks have
# any data in the "rangetab" array. 256 characters that don't wind up
# with any data get skipped entirely.
#
# The "starting_pagetab" array is the starting index in the "rangetab"
# array for the corresponding 256 character block. "starting_indextab" and
# "starting_pagetab" arrays have the same size.
#
# "starting_indextab" is sorted, a binary search finds the start of the
# 256 character block containing the character, via "starting_pagetab".
#
# The end of the 256 character block in "rangetab" is given by the
# starting index of the next 256 character block, or the end of the "rangetab"
# array.
#
# A binary search is done to locate the range containing the given character,
# and the associated value from "classtab" gets returned.
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use mkcommon ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
);
our $VERSION = '0.01';
my $BLOCK_SIZE=256;
# Preloaded methods go here.
sub new {
my ($this, %params) = @_;
my $class = ref($this) || $this;
my $self = \%params;
bless $self, $class;
$self->{'char_array'}=[];
$self->{'char_class'}=[];
$self->{'char_start'}=[0];
$self->{'last_block'}=-1;
$self->{'last'}="";
$self->{'last_f'}=0;
$self->{'last_l'}=0;
$self->{"classtype"} //= "uint8_t";
$self->{"prefix"} //= "unicode";
return $self;
}
sub _doemit_block {
my $this=shift;
my $f=shift;
my $l=shift;
push @{$this->{'char_array'}}, [$f, $l];
push @{$this->{'char_class'}}, $this->{'last'};
}
sub _doemit_endblock {
my $this=shift;
push @{$this->{'char_start'}}, $#{$this->{'char_array'}}+1;
}
# _doemit invokes _doemit_block() for each unicode char range with a given
# linebreaking class. However, once a unicode char range starts in a different
# $BLOCK_SIZE character class, call _doemit_endblock() before calling _doemit_block().
#
# If a single unicode char range crosses a $BLOCK_SIZE character class boundary,
# split it at the boundary; call _doemit_endblock() to finish the current $BLOCK_SIZE
# char boundary, call _doemit_endblock(), then call _doemit_block() for the
# rest of the char range.
sub _doemit {
my $this=shift;
$this->_doemit_endblock()
if int($this->{'last_f'} / $BLOCK_SIZE)
!= $this->{'last_block'} && $this->{'last_block'} != -1;
if (int($this->{'last_f'} / $BLOCK_SIZE) != int($this->{'last_l'} / $BLOCK_SIZE))
{
while (int($this->{'last_f'} / $BLOCK_SIZE) != int($this->{'last_l'} / $BLOCK_SIZE))
{
my $n=int($this->{'last_f'} / $BLOCK_SIZE) * $BLOCK_SIZE + ($BLOCK_SIZE-1);
$this->_doemit_block($this->{'last_f'}, $n);
$this->_doemit_endblock();
$this->{'last_f'}=$n+1;
}
}
$this->_doemit_block($this->{'last_f'}, $this->{'last_l'});
$this->{'last_block'}=int($this->{'last_l'} / $BLOCK_SIZE);
}
#
# Coalesce adjacent unicode char blocks that have the same linebreaking
# property. Invoke _doemit() for the accumulate unicode char range once
# a range with a different linebreaking class is seen.
sub range {
my $this=shift;
my $f=shift;
my $l=shift;
my $t=shift // 'NONE';
if ($this->{'last_l'} + 1 == $f && $this->{'last'} eq $t)
{
$this->{'last_l'}=$l;
return;
}
$this->_doemit() if $this->{'last'}; # New linebreaking class
$this->{'last_f'}=$f;
$this->{'last_l'}=$l;
$this->{'last'}=$t;
}
sub output {
my $this=shift;
$this->_doemit(); # Emit last linebreaking unicode char range class
my $prefix = $this->{"prefix"};
print "static const uint8_t ${prefix}_rangetab[][2]={\n";
my $comma="\t";
my $modulo=sprintf("0x%X", $BLOCK_SIZE-1);
foreach ( @{$this->{'char_array'}} )
{
print "${comma}{0x" . sprintf("%04x", $$_[0]) . " & $modulo, 0x"
. sprintf("%04x", $$_[1]) . " & $modulo}";
$comma=",\n\t";
}
print "};\n\n";
unless ($this->{noclass})
{
print "static const " . $this->{classtype}
. " ${prefix}_classtab[]={\n";
$comma="\t";
foreach ( @{$this->{'char_class'}} )
{
print "${comma}$_";
$comma=",\n\t";
}
print "};\n\n";
}
my $prev_block=-1;
my @starting_indextab;
my @starting_pagetab;
foreach my $sp (@{$this->{'char_start'}})
{
my $block=int($this->{'char_array'}->[$sp]->[0] / $BLOCK_SIZE);
if ($block != $prev_block)
{
push @starting_indextab, $block;
push @starting_pagetab, $sp;
$prev_block=$block;
}
}
print "static const size_t ${prefix}_starting_indextab[]={\n";
$comma="\t";
foreach (@starting_indextab)
{
print "$comma$_";
$comma=",\n\t";
}
print "\n};\n\nstatic const char32_t ${prefix}_starting_pagetab[]={\n";
$comma="\t";
foreach (@starting_pagetab)
{
my $sp=$_;
print "$comma$sp";
$comma=",\n\t";
}
print "\n};\n\n";
}
1;
|