Refactor bug-strtod.c to better test new types.
[glibc.git] / locale / gen-translit.pl
blob30d3f2f195951a9408b8faeedeb1db5c73fe0f49
1 #!/usr/bin/perl -w
2 open F, "cat C-translit.h.in | gcc -E - |" || die "Cannot preprocess input file";
5 sub cstrlen {
6 my($str) = @_;
7 my($len) = length($str);
8 my($cnt);
9 my($res) = 0;
11 for ($cnt = 0; $cnt < $len; ++$cnt) {
12 if (substr($str, $cnt, 1) eq '\\') {
13 # Recognize the escape sequence.
14 if (substr($str, $cnt + 1, 1) eq 'x') {
15 my($inner);
16 for ($inner = $cnt + 2; $inner < $len && $inner < $cnt + 10; ++$inner) {
17 my($ch) = substr($str, $inner, 1);
18 next if (($ch ge '0' && $ch le '9')
19 || ($ch ge 'a' && $ch le 'f')
20 || ($ch ge 'A' && $ch le 'F'));
21 last;
23 $cnt = $inner;
24 ++$res;
25 } else {
26 die "invalid input" if ($cnt + 1 >= $len);
27 ++$res;
28 ++$cnt;
30 } else {
31 ++$res;
35 return $res;
38 while (<F>) {
39 next if (/^#/);
40 next if (/^[ ]*$/);
41 chop;
43 if (/"([^\"]*)"[ ]*"(.*)"/) {
44 my($from) = $1;
45 my($to) = $2;
46 my($fromlen) = cstrlen($from);
47 my($tolen) = cstrlen($to);
49 push(@froms, $from);
50 push(@fromlens, $fromlen);
51 push(@tos, $to);
52 push(@tolens, $tolen);
56 printf "#include <stdint.h>\n";
58 printf "#define NTRANSLIT %d\n", $#froms + 1;
60 printf "static const uint32_t translit_from_idx[] =\n{\n ";
61 $col = 2;
62 $total = 0;
63 for ($cnt = 0; $cnt <= $#fromlens; ++$cnt) {
64 if ($cnt != 0) {
65 if ($col + 7 >= 79) {
66 printf(",\n ");
67 $col = 2;
68 } else {
69 printf(", ");
70 $col += 2;
73 printf("%4d", $total);
74 $total += $fromlens[$cnt] + 1;
75 $col += 4;
77 printf("\n};\n");
79 printf "static const wchar_t translit_from_tbl[] =\n ";
80 $col = 1;
81 for ($cnt = 0; $cnt <= $#froms; ++$cnt) {
82 if ($cnt != 0) {
83 if ($col + 6 >= 79) {
84 printf("\n ");
85 $col = 1;
87 printf(" L\"\\0\"");
88 $col += 6;
90 if ($col > 2 && $col + length($froms[$cnt]) + 4 >= 79) {
91 printf("\n ");
92 $col = 2;
93 } else {
94 printf(" ");
95 ++$col;
97 printf("L\"$froms[$cnt]\"");
98 $col += length($froms[$cnt]) + 3;
100 printf(";\n");
102 printf "static const uint32_t translit_to_idx[] =\n{\n ";
103 $col = 2;
104 $total = 0;
105 for ($cnt = 0; $cnt <= $#tolens; ++$cnt) {
106 if ($cnt != 0) {
107 if ($col + 7 >= 79) {
108 printf(",\n ");
109 $col = 2;
110 } else {
111 printf(", ");
112 $col += 2;
115 printf("%4d", $total);
116 $total += $tolens[$cnt] + 2;
117 $col += 4;
119 printf("\n};\n");
121 printf "static const wchar_t translit_to_tbl[] =\n ";
122 $col = 1;
123 for ($cnt = 0; $cnt <= $#tos; ++$cnt) {
124 if ($cnt != 0) {
125 if ($col + 6 >= 79) {
126 printf("\n ");
127 $col = 1;
129 printf(" L\"\\0\"");
130 $col += 6;
132 if ($col > 2 && $col + length($tos[$cnt]) + 6 >= 79) {
133 printf("\n ");
134 $col = 2;
135 } else {
136 printf(" ");
137 ++$col;
139 printf("%s", "L\"$tos[$cnt]\\0\"");
140 $col += length($tos[$cnt]) + 5;
142 printf(";\n");
144 exit 0;