release v4.2.3
[m6w6/ext-http] / scripts / gen_utf8.php
1 #!/usr/bin/env php
2 <?php
3
4 error_reporting(E_ALL);
5 set_error_handler(function($c, $e, $f, $l) {
6 throw new Exception("$e in $f on line $l");
7 });
8
9 $i18n = $argc >= 2 ? $argv[1] : "/usr/share/i18n/locales/i18n";
10
11 $f = fopen($i18n, "r");
12 $c = false;
13 $a = false;
14 $r = [];
15 $n = [];
16
17 ob_start(null, 0xffff);
18 while (!feof($f)) {
19 $line = fgets($f);
20 if (!$c && $line !== "LC_CTYPE\n") {
21 continue;
22 }
23 $c = true;
24 if ($line === "END LC_CTYPE\n") {
25 break;
26 }
27 switch($line{0}) {
28 case "%":
29 break;
30 case "\n":
31 if ($a) {
32 break 2;
33 }
34 break;
35 case " ":
36 if ($a) {
37 foreach (explode(";", trim($line, "\n/ ;")) as $ranges) {
38 $range = explode("..", $ranges);
39 $step = 0;
40 $end = 0;
41 switch (count($range)) {
42 case 3:
43 list($sstart, $sstep, $send) = $range;
44 sscanf($sstart, "<U%X>", $start);
45 sscanf($sstep, "(%d)", $step);
46 sscanf($send, "<U%X>", $end);
47
48 break;
49 case 2:
50 list($sstart, $send) = $range;
51 $step = 1;
52 sscanf($sstart, "<U%X>", $start);
53 sscanf($send, "<U%X>", $end);
54 break;
55 case 1:
56 list($sstart) = $range;
57 sscanf($sstart, "<U%X>", $start);
58 break;
59 }
60 if ($end) {
61 if ($step != 1) {
62 die("UNEXPECTED step=$step\n");
63 }
64 $r[] = [$start, $end];
65 } else {
66 $n[] = $start;
67 }
68 }
69 }
70 break;
71 default:
72 if ($a) {
73 break 2;
74 } elseif ($line === "alpha /\n") {
75 $a = true;
76 }
77 break;
78 }
79 }
80
81 $maxstep = 0;
82 printf("static const utf8_range_t utf8_ranges[] = {\n\t{");
83 foreach ($r as $i => list($start, $end)) {
84 if ($i) if ($i%3) {
85 printf(", {");
86 } else {
87 printf(",\n\t{");
88 }
89
90 printf("0x%08X, 0x%08X}", $start, $end);
91 }
92 printf("\n};\n\n");
93 printf("static const unsigned utf8_chars[] = {\n\t");
94 foreach ($n as $i => $u) {
95 if ($i) if (($i%6)) {
96 printf(", ");
97 } else {
98 printf(",\n\t");
99 }
100 printf("0x%08X", $u);
101 }
102 printf("\n};\n");
103
104 file_put_contents("php_http_utf8.h",
105 preg_replace('/(\/\* BEGIN::UTF8TABLE \*\/\n).*(\n\s*\/\* END::UTF8TABLE \*\/)/s', '$1'. ob_get_contents() .'$2',
106 file_get_contents("php_http_utf8.h")));