1*d713e089SXin LI#!/usr/bin/env perl 2a15691bfSXin LIuse strict; 3a15691bfSXin LI 4a15691bfSXin LImy $USAGE = <<__EOF__; 5a15691bfSXin LI usage: mkutable [-n] [-f#] type... [--] [<] UnicodeData.txt 6a15691bfSXin LI -n = take non-matching types 7a15691bfSXin LI -f = zero-based type field (default 2) 8a15691bfSXin LI__EOF__ 9a15691bfSXin LI 10a15691bfSXin LIuse Getopt::Std; 116f26c71dSXin LIuse vars qw( $opt_f $opt_n ); 126f26c71dSXin LI 13a15691bfSXin LImy $type_field = 2; 14a15691bfSXin LI 15b2ea2440SXin LI# Override Unicode tables for certain control chars 16b2ea2440SXin LI# that are expected to be found in normal text files. 17b2ea2440SXin LImy %force_space = ( 18b2ea2440SXin LI 0x08 => 1, # backspace 19b2ea2440SXin LI 0x09 => 1, # tab 20b2ea2440SXin LI 0x0a => 1, # newline 21b2ea2440SXin LI 0x0c => 1, # form feed 22b2ea2440SXin LI 0x0d => 1, # carriage return 23b2ea2440SXin LI); 24b2ea2440SXin LI 256f26c71dSXin LI# Hangul Jamo medial vowels and final consonants should be zero width. 266f26c71dSXin LImy @force_compose = ( 276f26c71dSXin LI [0x1160, 0x11ff], 286f26c71dSXin LI [0xd7b0, 0xd7c6], 296f26c71dSXin LI [0xd7cb, 0xd7fb] 306f26c71dSXin LI); 316f26c71dSXin LI 32f6b74a7dSXin LIexit (main() ? 0 : 1); 33a15691bfSXin LI 34a15691bfSXin LIsub main { 35a15691bfSXin LI my $args = join ' ', @ARGV; 36a15691bfSXin LI die $USAGE if not getopts('f:n'); 37a15691bfSXin LI $type_field = $opt_f if $opt_f; 386f26c71dSXin LI 39a15691bfSXin LI my %types; 40a15691bfSXin LI my $arg; 41a15691bfSXin LI while ($arg = shift @ARGV) { 42a15691bfSXin LI last if $arg eq '--'; 43a15691bfSXin LI $types{$arg} = 1; 44a15691bfSXin LI } 45a15691bfSXin LI my %out = ( 'types' => \%types ); 46a15691bfSXin LI 476f26c71dSXin LI my %force_compose; 486f26c71dSXin LI foreach my $comp (@force_compose) { 496f26c71dSXin LI my ($lo,$hi) = @$comp; 506f26c71dSXin LI for (my $ch = $lo; $ch <= $hi; ++$ch) { 516f26c71dSXin LI $force_compose{$ch} = 1; 526f26c71dSXin LI } 536f26c71dSXin LI } 546f26c71dSXin LI 556f26c71dSXin LI my $date = `date`; 566f26c71dSXin LI chomp $date; 576f26c71dSXin LI print "/* Generated by \"$0 $args\" on $date */\n"; 586f26c71dSXin LI 59f6b74a7dSXin LI my $last_code = 0; 60*d713e089SXin LI my $start_range = 0; 61a15691bfSXin LI while (<>) { 62a15691bfSXin LI chomp; 63a15691bfSXin LI s/#.*//; 64a15691bfSXin LI my @fields = split /;/; 65a15691bfSXin LI next if not @fields; 66f6b74a7dSXin LI my ($lo_code, $hi_code); 67f6b74a7dSXin LI my $codes = $fields[0]; 68f6b74a7dSXin LI if ($codes =~ /(\w+)\.\.(\w+)/) { 69f6b74a7dSXin LI $lo_code = hex $1; 70f6b74a7dSXin LI $hi_code = hex $2; 71f6b74a7dSXin LI } else { 726f26c71dSXin LI $lo_code = $hi_code = hex $codes; 73f6b74a7dSXin LI } 74*d713e089SXin LI if ($fields[1] =~ /, First>$/) { 75*d713e089SXin LI die "invalid Unicode data: First with range" if $hi_code != $lo_code; 76*d713e089SXin LI $start_range = $lo_code; 77*d713e089SXin LI next; 78*d713e089SXin LI } 79*d713e089SXin LI if ($fields[1] =~ /, Last>$/) { 80*d713e089SXin LI die "invalid Unicode data: Last without First" if not $start_range; 81*d713e089SXin LI $lo_code = $start_range; 82*d713e089SXin LI $start_range = 0; 83*d713e089SXin LI } elsif ($start_range) { 84*d713e089SXin LI die "invalid Unicode data: First without Last"; 85*d713e089SXin LI } 86a15691bfSXin LI my $type = $fields[$type_field]; 87a15691bfSXin LI $type =~ s/\s//g; 88f6b74a7dSXin LI for ($last_code = $lo_code; $last_code <= $hi_code; ++$last_code) { 896f26c71dSXin LI output(\%out, $last_code, 906f26c71dSXin LI $force_space{$last_code} ? 'Zs' : $force_compose{$last_code} ? 'Mn' : $type); 91a15691bfSXin LI } 92a15691bfSXin LI } 93f6b74a7dSXin LI output(\%out, $last_code); 94f6b74a7dSXin LI return 1; 95a15691bfSXin LI} 96a15691bfSXin LI 97a15691bfSXin LIsub output { 98a15691bfSXin LI my ($out, $code, $type) = @_; 99f6b74a7dSXin LI my $type_ok = ($type and ${${$out}{types}}{$type}); 100f6b74a7dSXin LI $type_ok = not $type_ok if $opt_n; 101f6b74a7dSXin LI my $prev_code = $$out{prev_code}; 102f6b74a7dSXin LI 103f6b74a7dSXin LI if (not $type_ok) { 104f6b74a7dSXin LI end_run($out, $prev_code); 105f6b74a7dSXin LI } elsif (not $$out{in_run} or $type ne $$out{run_type} or $code != $prev_code+1) { 106f6b74a7dSXin LI end_run($out, $prev_code); 107a15691bfSXin LI start_run($out, $code, $type); 108a15691bfSXin LI } 109f6b74a7dSXin LI $$out{prev_code} = $code; 110a15691bfSXin LI} 111a15691bfSXin LI 112a15691bfSXin LIsub start_run { 113a15691bfSXin LI my ($out, $code, $type) = @_; 114a15691bfSXin LI $$out{start_code} = $code; 115f6b74a7dSXin LI $$out{prev_code} = $code; 116f6b74a7dSXin LI $$out{run_type} = $type; 117a15691bfSXin LI $$out{in_run} = 1; 118a15691bfSXin LI} 119a15691bfSXin LI 120a15691bfSXin LIsub end_run { 121a15691bfSXin LI my ($out, $code) = @_; 122a15691bfSXin LI return if not $$out{in_run}; 123f6b74a7dSXin LI printf "\t{ 0x%04x, 0x%04x }, /* %s */\n", $$out{start_code}, $code, $$out{run_type}; 124a15691bfSXin LI $$out{in_run} = 0; 125a15691bfSXin LI} 126