2016-01-04 00:22:34 +00:00
|
|
|
#! /usr/bin/perl
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
my $USAGE = <<__EOF__;
|
|
|
|
usage: mkutable [-n] [-f#] type... [--] [<] UnicodeData.txt
|
|
|
|
-n = take non-matching types
|
2019-06-29 04:39:01 +00:00
|
|
|
-f = zero-based type field (default 2)
|
2016-01-04 00:22:34 +00:00
|
|
|
__EOF__
|
|
|
|
|
|
|
|
use Getopt::Std;
|
2020-10-24 05:25:54 +00:00
|
|
|
use vars qw( $opt_f $opt_n );
|
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
my $type_field = 2;
|
|
|
|
|
2017-11-20 06:53:49 +00:00
|
|
|
# Override Unicode tables for certain control chars
|
|
|
|
# that are expected to be found in normal text files.
|
|
|
|
my %force_space = (
|
|
|
|
0x08 => 1, # backspace
|
|
|
|
0x09 => 1, # tab
|
|
|
|
0x0a => 1, # newline
|
|
|
|
0x0c => 1, # form feed
|
|
|
|
0x0d => 1, # carriage return
|
|
|
|
);
|
|
|
|
|
2020-10-24 05:25:54 +00:00
|
|
|
# Hangul Jamo medial vowels and final consonants should be zero width.
|
|
|
|
my @force_compose = (
|
|
|
|
[0x1160, 0x11ff],
|
|
|
|
[0xd7b0, 0xd7c6],
|
|
|
|
[0xd7cb, 0xd7fb]
|
|
|
|
);
|
|
|
|
|
2017-04-25 03:42:16 +00:00
|
|
|
exit (main() ? 0 : 1);
|
2016-01-04 00:22:34 +00:00
|
|
|
|
|
|
|
sub main {
|
|
|
|
my $args = join ' ', @ARGV;
|
|
|
|
die $USAGE if not getopts('f:n');
|
|
|
|
$type_field = $opt_f if $opt_f;
|
2020-10-24 05:25:54 +00:00
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
my %types;
|
|
|
|
my $arg;
|
|
|
|
while ($arg = shift @ARGV) {
|
|
|
|
last if $arg eq '--';
|
|
|
|
$types{$arg} = 1;
|
|
|
|
}
|
|
|
|
my %out = ( 'types' => \%types );
|
|
|
|
|
2020-10-24 05:25:54 +00:00
|
|
|
my %force_compose;
|
|
|
|
foreach my $comp (@force_compose) {
|
|
|
|
my ($lo,$hi) = @$comp;
|
|
|
|
for (my $ch = $lo; $ch <= $hi; ++$ch) {
|
|
|
|
$force_compose{$ch} = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $date = `date`;
|
|
|
|
chomp $date;
|
|
|
|
print "/* Generated by \"$0 $args\" on $date */\n";
|
|
|
|
|
2017-04-25 03:42:16 +00:00
|
|
|
my $last_code = 0;
|
2016-01-04 00:22:34 +00:00
|
|
|
while (<>) {
|
|
|
|
chomp;
|
|
|
|
s/#.*//;
|
|
|
|
my @fields = split /;/;
|
|
|
|
next if not @fields;
|
2017-04-25 03:42:16 +00:00
|
|
|
my ($lo_code, $hi_code);
|
|
|
|
my $codes = $fields[0];
|
|
|
|
if ($codes =~ /(\w+)\.\.(\w+)/) {
|
|
|
|
$lo_code = hex $1;
|
|
|
|
$hi_code = hex $2;
|
|
|
|
} else {
|
2020-10-24 05:25:54 +00:00
|
|
|
$lo_code = $hi_code = hex $codes;
|
2017-04-25 03:42:16 +00:00
|
|
|
}
|
2016-01-04 00:22:34 +00:00
|
|
|
my $type = $fields[$type_field];
|
|
|
|
$type =~ s/\s//g;
|
2017-04-25 03:42:16 +00:00
|
|
|
for ($last_code = $lo_code; $last_code <= $hi_code; ++$last_code) {
|
2020-10-24 05:25:54 +00:00
|
|
|
output(\%out, $last_code,
|
|
|
|
$force_space{$last_code} ? 'Zs' : $force_compose{$last_code} ? 'Mn' : $type);
|
2016-01-04 00:22:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 03:42:16 +00:00
|
|
|
output(\%out, $last_code);
|
|
|
|
return 1;
|
2016-01-04 00:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub output {
|
|
|
|
my ($out, $code, $type) = @_;
|
2017-04-25 03:42:16 +00:00
|
|
|
my $type_ok = ($type and ${${$out}{types}}{$type});
|
|
|
|
$type_ok = not $type_ok if $opt_n;
|
|
|
|
my $prev_code = $$out{prev_code};
|
|
|
|
|
|
|
|
if (not $type_ok) {
|
|
|
|
end_run($out, $prev_code);
|
|
|
|
} elsif (not $$out{in_run} or $type ne $$out{run_type} or $code != $prev_code+1) {
|
|
|
|
end_run($out, $prev_code);
|
2016-01-04 00:22:34 +00:00
|
|
|
start_run($out, $code, $type);
|
|
|
|
}
|
2017-04-25 03:42:16 +00:00
|
|
|
$$out{prev_code} = $code;
|
2016-01-04 00:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub start_run {
|
|
|
|
my ($out, $code, $type) = @_;
|
|
|
|
$$out{start_code} = $code;
|
2017-04-25 03:42:16 +00:00
|
|
|
$$out{prev_code} = $code;
|
|
|
|
$$out{run_type} = $type;
|
2016-01-04 00:22:34 +00:00
|
|
|
$$out{in_run} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub end_run {
|
|
|
|
my ($out, $code) = @_;
|
|
|
|
return if not $$out{in_run};
|
2017-04-25 03:42:16 +00:00
|
|
|
printf "\t{ 0x%04x, 0x%04x }, /* %s */\n", $$out{start_code}, $code, $$out{run_type};
|
2016-01-04 00:22:34 +00:00
|
|
|
$$out{in_run} = 0;
|
|
|
|
}
|