summaryrefslogtreecommitdiffstats
path: root/unicode/mkeastasianwidth.pl
blob: 862fa7a6900e42b6e6a5bb544d08e706cf769f7a (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
#! /usr/bin/perl

# USAGE: perl mkeastasianwidth.pl > charwidth.c

use IO::File;
use strict;
use warnings;

foreach my $pass (

    {
	header => "char32_t unicode_eastasia_tab[][2]",
	emit => sub {
	    my ($pb, $pe, $pv) = @_;

	    printf ("{0x%04x, 0x%04x},\n", $pb, $pe);
	},
    },
    {
	header => "unicode_eastasia_t unicode_eastasia_v[]",
	emit => sub {
	    my ($pb, $pe, $pv) = @_;

	    printf ("UNICODE_EASTASIA_%s,\n", $pv);
	},
    })
{
    my $emit = $pass->{emit};


    my $fh=new IO::File "<EastAsianWidth.txt";

    my $pb=-1;
    my $pe=-1;
    my $pv="";

    print "static const " . $pass->{header} . "={\n";

    my $full = sub {
	my $b=hex(shift);
	my $e=hex(shift);
	my $v=shift;

	if ($b == $pe+1 && $v eq $pv)
	{
	    $pe=$e;
	    return;
	}

	$emit->($pb, $pe, $pv) unless $pb < 0;

	$pb=$b;
	$pe=$e;
	$pv=$v;
    };


    while (defined($_=<$fh>))
    {
	chomp;
	s/#.*//;

	my @w=split(/;/);

	next unless @w;
	grep {s/^\s*//; s/\s*$//; } @w;

	if ($w[0] =~ /(.*)\.\.(.*)/)
	{
	    $full->($1, $2, $w[1]);
	}
	else
	{
	    $full->($w[0], $w[0], $w[1]);
	}
    }

    $emit->($pb, $pe, $pv);
    print "};\n\n";
}