summaryrefslogtreecommitdiffstats
path: root/unicode/mkcommon.pm
blob: dc2fa9966f50371e8c1ceb31c94d3001e4da2b7b (plain)
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
package mkcommon;

use 5.012002;
use strict;
use warnings;

require Exporter;

our @ISA = qw(Exporter);

# 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";

    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;

    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

    $this->_doemit_endblock(); # End of the most recent $BLOCK_SIZE char range class

    print "static const uint8_t unicode_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";

    print "static const " . $this->{classtype} . " unicode_classtab[]={\n";

    $comma="\t";
    foreach ( @{$this->{'char_class'}} )
    {
	print "${comma}$_";
	$comma=",\n\t";
    }

    print "};\n\n";

    print "static const size_t unicode_indextab[]={\n";

    $comma="\t";

    my $prev_block=-1;
    foreach (@{$this->{'char_start'}})
    {
	my $sp=$_;
	my $cnt=1;

	if ($sp <= $#{$this->{'char_array'}})
	{
	    my $block=int($this->{'char_array'}->[$sp]->[0] / $BLOCK_SIZE);

	    $cnt = $block - $prev_block;
	    $prev_block=$block;
	}

	foreach (1..$cnt)
	{
	    print "$comma$sp";
	    $comma=",\n\t";
	}
    }

    print "};\n\n";
}

1;