| 1 |
#!perl |
| 2 |
# gen-zh : auxiliary script for Chinese |
| 3 |
# |
| 4 |
# output files |
| 5 |
# |
| 6 |
# zh.txt (equal to data/zh.txt) |
| 7 |
# loc_zh.t (equal to t/loc_zh.t) |
| 8 |
# |
| 9 |
use strict; |
| 10 |
use warnings; |
| 11 |
require './gen-head'; # for testcount() and testhead() |
| 12 |
|
| 13 |
use Unicode::Normalize; |
| 14 |
require './dumpstr'; # for element() and string() |
| 15 |
|
| 16 |
my @low = qw( 61 65 EA 69 6D 6E 6F 75 FC ); # lower base letters |
| 17 |
my @upp = qw( 41 45 CA 49 4D 4E 4F 55 DC ); # upper base letters |
| 18 |
my @tone = qw( 304 301 30C 300 0 ); # combining characters for tones |
| 19 |
my @misc = qw( 303 309 323 ); # some combining characters other than tones |
| 20 |
|
| 21 |
my $textf = 'zh.txt'; |
| 22 |
my $text0 = ''; |
| 23 |
my $testf = 'loc_zh.t'; |
| 24 |
my $test0 = ''; |
| 25 |
my $test1 = ''; |
| 26 |
my $test2 = ''; |
| 27 |
my $test3 = ''; |
| 28 |
|
| 29 |
for my $i (0..@low-1) { |
| 30 |
my $lb = pack('U', hex $low[$i]); |
| 31 |
my $ub = pack('U', hex $upp[$i]); |
| 32 |
for my $j (0..@tone-1) { |
| 33 |
my $tc = $tone[$j] ? pack('U', hex $tone[$j]) : ''; |
| 34 |
|
| 35 |
my $ldec = NFD($lb).$tc; |
| 36 |
my $udec = NFD($ub).$tc; |
| 37 |
|
| 38 |
my $sldec = string($ldec); |
| 39 |
my $sudec = string($udec); |
| 40 |
|
| 41 |
if ($tc) { # has a tone |
| 42 |
my $nc = $tone[$j+1] ? pack('U', hex $tone[$j+1]) : ''; |
| 43 |
my $snext = string(NFD($lb).$nc); |
| 44 |
$test0 .= qq|ok(\$objZh->eq($sldec, $snext));\n|; |
| 45 |
} |
| 46 |
if ($lb ne $ldec) { # has a tone, or the base is a composite |
| 47 |
$test1 .= qq|ok(\$objZh->eq($sldec, $sudec));\n|; |
| 48 |
} |
| 49 |
|
| 50 |
my $te1 = ''; |
| 51 |
my $te2 = ''; |
| 52 |
my $tx1 = ''; |
| 53 |
my $tx2 = ''; |
| 54 |
for my $bc ($lb, $ub) { # base letter |
| 55 |
my $dec = NFD($bc).$tc; |
| 56 |
my $com = NFC($bc.$tc); |
| 57 |
my $cat = $bc.$tc; |
| 58 |
|
| 59 |
my $sdec = string($dec); |
| 60 |
my $scom = string($com); |
| 61 |
my $scat = string($cat); |
| 62 |
{ |
| 63 |
my $sacc = $sdec; |
| 64 |
$te1 .= qq|ok(\$objZh->eq($sdec, $scom));\n| if $dec ne $com; |
| 65 |
$te1 .= qq|ok(\$objZh->eq($sdec, $sacc));\n| |
| 66 |
if $sacc =~ s/(x\{?3)0([01])/${1}4${2}/; |
| 67 |
} |
| 68 |
if ($dec ne $cat && $cat ne $com) { |
| 69 |
$te2 .= qq|ok(\$objZh->eq($sdec, $scat));\n|; |
| 70 |
$te2 .= qq|ok(\$objZh->eq($sdec, $scat));\n| |
| 71 |
if $scat =~ s/(x\{?3)0([01])/${1}4${2}/; |
| 72 |
} |
| 73 |
|
| 74 |
# .txt |
| 75 |
my $c = element($com); |
| 76 |
my @d = split //, $dec; |
| 77 |
my $dif = $bc eq $d[0] ? "--".(4-$j) : "++".($j+1); |
| 78 |
|
| 79 |
if ($dif ne '--0') { # $com is not a simple base |
| 80 |
my $e = $c; |
| 81 |
$tx1 .= "$e;$d[0]$dif\n"; |
| 82 |
$tx1 .= "$e;$d[0]$dif\n" if $e =~ s/(\b03)0([01])/${1}4${2}/; |
| 83 |
} |
| 84 |
if ($dec ne $cat && $tc ne '') { # $cat is composite + tone |
| 85 |
my $e = element($cat eq $com ? $dec : $cat); |
| 86 |
$tx2 .= "$e;<$c>\n"; |
| 87 |
$tx2 .= "$e;<$c>\n" if $e =~ s/(\b03)0([01])/${1}4${2}/; |
| 88 |
} |
| 89 |
} |
| 90 |
$test2 .= "$te1$te2"; |
| 91 |
$text0 .= "$tx1$tx2"; |
| 92 |
} |
| 93 |
|
| 94 |
for my $m (@misc) { # misc. combining character |
| 95 |
my $mc = pack('U', hex $m); |
| 96 |
for my $bc ($lb, $ub) { # base letter |
| 97 |
next if $bc eq NFD($bc); # $bc should be a composite |
| 98 |
my $dec = NFD($bc).$mc; |
| 99 |
my $com = NFC($bc.$mc); |
| 100 |
next if length $com > 1; # $com should be a composite |
| 101 |
my $sdec = string($dec); |
| 102 |
my $scom = string($com); |
| 103 |
$test3 .= qq|ok(\$objZh->eq($sdec, $scom));\n|; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
### WRITE DATA ### |
| 109 |
open my $dh, ">$textf" or die $textf; |
| 110 |
binmode $dh; |
| 111 |
print $dh $text0; |
| 112 |
close $dh or die $textf; |
| 113 |
|
| 114 |
### WRITE TEST ### |
| 115 |
my $test = ''; |
| 116 |
my $ok = 2; |
| 117 |
|
| 118 |
$test .= "\$objZh->change(level => 1);\n\n"; |
| 119 |
$test .= testcount(\$ok, $test0); |
| 120 |
|
| 121 |
$test0 =~ s/->eq/->lt/g; |
| 122 |
$test .= "\$objZh->change(level => 2);\n\n"; |
| 123 |
$test .= testcount(\$ok, $test0); |
| 124 |
$test .= testcount(\$ok, $test1); |
| 125 |
|
| 126 |
$test1 =~ s/->eq/->lt/g; |
| 127 |
$test .= "\$objZh->change(level => 3);\n\n"; |
| 128 |
$test .= testcount(\$ok, $test1); |
| 129 |
$test .= testcount(\$ok, $test2); |
| 130 |
$test .= testcount(\$ok, $test3); |
| 131 |
|
| 132 |
chomp $test; |
| 133 |
open my $th, ">$testf" or die $testf; |
| 134 |
binmode $th; |
| 135 |
print $th testhead('zh', $ok), $test; |
| 136 |
close $th or die $testf; |