#!/usr/bin/perl
use strict;
use warnings;
use utf8;

my $in_file = "UnicodeData.txt";
my $out_file = "unicode_combine.tbl";
my $IN;
my $OUT;
open($IN, $in_file) || die "Cannot open $in_file.";
open($OUT, ">:crlf:encoding(utf8)", $out_file) || die "Cannot open $out_file.";
print $OUT "// this file was generated by get_combine_table.pl\n";
my $ostart = 0;
my $oend = 0;
my $combine = 0;
my $comment;
while($a = <$IN>) {
	if ($a =~ /^([0-9A-F]+);([^;]*);([^;]*);/) {
		my $code_point = hex $1;
		my $name = $2;
		my $general_category = $3;

		#printf("%06x| %s| \"%s\"\n", $code_point, $general_category, $name);

		$combine = 0;
		if ($code_point < 0x300) {
			# U+0300未満は処理しない, combineしないと思われるため
		}
		elsif ($general_category eq "Mn" ||
			   $general_category eq "Mc" ||
			   $general_category eq "Me" ||
			   $general_category eq "Sk") {  # Sk = Modifier_Symbol
			$combine = 1;
		}

		if ($combine == 1) {
			if ($ostart == 0) {
				$ostart = $code_point;
				$oend = $code_point;
				$comment = $name;
			} else {
				if (0) {
					if ($oend + 1 != $code_point) {
						# 文字コードが連続していない
						printf "warn %04x\n", $code_point;
					}
				}
				$oend = $code_point;
			}
		} else {
			if ($ostart != 0) {
				printf $OUT "{ 0x%06x, 0x%06x },		// '%s'\n", $ostart, $oend, $comment;
				$ostart = 0;
			}
		}
	}
}
if ($ostart != 0) {
	printf $OUT "{ 0x%06x, 0x%06x },\n", $ostart, $oend;
}
