OSDN Git Service

Ok... here's the summary:
[uclinux-h8/uClibc.git] / extra / locale / gen_wctype.c
1
2 #define _GNU_SOURCE
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <locale.h>
7 #include <wctype.h>
8 #include <limits.h>
9 #include <stdint.h>
10 #include <wchar.h>
11 #include <ctype.h>
12
13 #ifndef __UCLIBC__
14 #ifndef _WCTYPE_H
15 #define _WCTYPE_H
16 #endif
17 #include "../../libc/sysdeps/linux/common/bits/uClibc_ctype.h"
18 #endif
19
20 /*       0x9 : space  blank */
21 /*       0xa : space */
22 /*       0xb : space */
23 /*       0xc : space */
24 /*       0xd : space */
25 /*      0x20 : space  blank */
26 /*    0x1680 : space  blank */
27 /*    0x2000 : space  blank */
28 /*    0x2001 : space  blank */
29 /*    0x2002 : space  blank */
30 /*    0x2003 : space  blank */
31 /*    0x2004 : space  blank */
32 /*    0x2005 : space  blank */
33 /*    0x2006 : space  blank */
34 /*    0x2008 : space  blank */
35 /*    0x2009 : space  blank */
36 /*    0x200a : space  blank */
37 /*    0x200b : space  blank */
38 /*    0x2028 : space */
39 /*    0x2029 : space */
40 /*    0x3000 : space  blank */
41
42 /*  typecount[ 0] =    88670  C_alpha_nonupper_nonlower */
43 /*  typecount[ 1] =      742  C_alpha_lower */
44 /*  typecount[ 2] =        4  C_alpha_upper_lower */
45 /*  typecount[ 3] =      731  C_alpha_upper */
46 /*  typecount[ 4] =       10  C_digit */
47 /*  typecount[ 5] =    10270  C_punct */
48 /*  typecount[ 6] =        0  C_graph */
49 /*  typecount[ 7] =        0  C_print_space_nonblank */
50 /*  typecount[ 8] =       14  C_print_space_blank */
51 /*  typecount[ 9] =        0  C_space_nonblank_noncntrl */
52 /*  typecount[10] =        0  C_space_blank_noncntrl */
53 /*  typecount[11] =        6  C_cntrl_space_nonblank */
54 /*  typecount[12] =        1  C_cntrl_space_blank */
55 /*  typecount[13] =       60  C_cntrl_nonspace */
56 /*  typecount[14] =    96100  C_unclassified */
57 /*  typecount[15] =        0  empty_slot */
58
59
60
61 /* Set to #if 0 to restrict wchars to 16 bits. */
62 #if 1
63 #define RANGE 0x2ffffUL
64 #elif 0
65 #define RANGE 0x1ffffUL
66 #else
67 #define RANGE 0xffffUL                  /* Restrict for 16-bit wchar_t... */
68 #endif
69
70 #if 0
71 /* Classification codes. */
72
73 static const char *typename[] = {
74         "C_unclassified",
75         "C_alpha_nonupper_nonlower",
76         "C_alpha_lower",
77         "C_alpha_upper_lower",
78         "C_alpha_upper",
79         "C_digit",
80         "C_punct",
81         "C_graph",
82         "C_print_space_nonblank",
83         "C_print_space_blank",
84         "C_space_nonblank_noncntrl",
85         "C_space_blank_noncntrl",
86         "C_cntrl_space_nonblank",
87         "C_cntrl_space_blank",
88         "C_cntrl_nonspace",
89         "empty_slot"
90 };
91 #endif
92
93 #if 0
94 /* Taking advantage of the C99 mutual-exclusion guarantees for the various
95  * (w)ctype classes, including the descriptions of printing and control
96  * (w)chars, we can place each in one of the following mutually-exlusive
97  * subsets.  Since there are less than 16, we can store the data for
98  * each (w)chars in a nibble. In contrast, glibc uses an unsigned int
99  * per (w)char, with one bit flag for each is* type.  While this allows
100  * a simple '&' operation to determine the type vs. a range test and a
101  * little special handling for the "blank" and "xdigit" types in my
102  * approach, it also uses 8 times the space for the tables on the typical
103  * 32-bit archs we supported.*/
104 enum {
105         __CTYPE_unclassified = 0,
106         __CTYPE_alpha_nonupper_nonlower,
107         __CTYPE_alpha_lower,
108         __CTYPE_alpha_upper_lower,
109         __CTYPE_alpha_upper,
110         __CTYPE_digit,
111         __CTYPE_punct,
112         __CTYPE_graph,
113         __CTYPE_print_space_nonblank,
114         __CTYPE_print_space_blank,
115         __CTYPE_space_nonblank_noncntrl,
116         __CTYPE_space_blank_noncntrl,
117         __CTYPE_cntrl_space_nonblank,
118         __CTYPE_cntrl_space_blank,
119         __CTYPE_cntrl_nonspace,
120 };
121 #endif
122
123 #define __CTYPE_isxdigit(D,X) \
124         (__CTYPE_isdigit(D) || (((unsigned int)(((X)|0x20) - 'a')) <= 5))
125
126 #define mywalnum(x)             __CTYPE_isalnum(d)
127 #define mywalpha(x)             __CTYPE_isalpha(d)
128 #define mywblank(x)     __CTYPE_isblank(d)
129 #define mywcntrl(x)             __CTYPE_iscntrl(d)
130 #define mywdigit(x)             __CTYPE_isdigit(d)
131 #define mywgraph(x)             __CTYPE_isgraph(d)
132 #define mywlower(x)             __CTYPE_islower(d)
133 #define mywprint(x)             __CTYPE_isprint(d)
134 #define mywpunct(x)             __CTYPE_ispunct(d)
135 #define mywspace(x)             __CTYPE_isspace(d)
136 #define mywupper(x)             __CTYPE_isupper(d)
137 #define mywxdigit(x)    __CTYPE_isxdigit(d,x)
138
139 typedef struct {
140         short l;
141         short u;
142 } uldiff_entry;
143
144 typedef struct {
145         uint16_t ii_len;
146         uint16_t ti_len;
147         uint16_t ut_len;
148
149         unsigned char ii_shift;
150         unsigned char ti_shift;
151
152         unsigned char *ii;
153         unsigned char *ti;
154         unsigned char *ut;
155 } table_data;
156
157
158 void output_table(FILE *fp, const char *name, table_data *tbl)
159 {
160         size_t i;
161
162         fprintf(fp, "#define WC%s_II_LEN    %7u\n", name, tbl->ii_len);
163         fprintf(fp, "#define WC%s_TI_LEN    %7u\n", name, tbl->ti_len);
164         fprintf(fp, "#define WC%s_UT_LEN    %7u\n", name, tbl->ut_len);
165
166         fprintf(fp, "#define WC%s_II_SHIFT  %7u\n", name, tbl->ii_shift);
167         fprintf(fp, "#define WC%s_TI_SHIFT  %7u\n", name, tbl->ti_shift);
168
169         fprintf(fp, "\n#ifdef WANT_WC%s_data\n", name);
170
171         i = tbl->ii_len + tbl->ti_len + tbl->ut_len;
172         fprintf(fp, "\nstatic const unsigned char WC%s_data[%zu] = {", name, i);
173         for (i=0 ; i < tbl->ii_len ; i++) {
174                 if (i % 12 == 0) {
175                         fprintf(fp, "\n");
176                 }
177                 fprintf(fp, " %#04x,", tbl->ii[i]);
178         }
179         for (i=0 ; i < tbl->ti_len ; i++) {
180                 if (i % 12 == 0) {
181                         fprintf(fp, "\n");
182                 }
183                 fprintf(fp, " %#04x,", tbl->ti[i]);
184         }
185         for (i=0 ; i < tbl->ut_len ; i++) {
186                 if (i % 12 == 0) {
187                         fprintf(fp, "\n");
188                 }
189                 fprintf(fp, " %#04x,", tbl->ut[i]);
190         }
191         fprintf(fp, "\n};\n\n");
192
193         fprintf(fp, "#endif /* WANT_WC%s_data */\n\n", name);
194 }
195
196 static void dump_table_data(table_data *tbl)
197 {
198         printf("ii_shift = %d  ti_shift = %d\n"
199                    "ii_len = %d  ti_len = %d  ut_len = %d\n"
200                    "total = %d\n",
201                    tbl->ii_shift, tbl->ti_shift,
202                    tbl->ii_len, tbl->ti_len, tbl->ut_len,
203                    (int) tbl->ii_len + (int) tbl->ti_len + (int) tbl->ut_len);
204 }
205
206 /* For sorting the blocks of unsigned chars. */
207 static size_t nu_val;
208
209 int nu_memcmp(const void *a, const void *b)
210 {
211         return memcmp(*(unsigned char**)a, *(unsigned char**)b, nu_val);
212 }
213
214 static size_t newopt(unsigned char *ut, size_t usize, int shift, table_data *tbl);
215
216 #define MAXTO           255                     /* Restrict to minimal unsigned char max. */
217
218 int main(int argc, char **argv)
219 {
220         long int u, l, tt;
221         size_t smallest, t;
222         unsigned int c;
223         unsigned int d;
224         int i, n;
225         int ul_count = 0;
226         uldiff_entry uldiff[MAXTO];
227         table_data cttable;
228         table_data ultable;
229         table_data combtable;
230         table_data widthtable;
231         long int last_comb = 0;
232
233         unsigned char wct[(RANGE/2)+1]; /* wctype table (nibble per wchar) */
234         unsigned char ult[RANGE+1];     /* upper/lower table */
235         unsigned char combt[(RANGE/4)+1];       /* combining */
236         unsigned char widtht[(RANGE/4)+1];      /* width */
237         wctrans_t totitle;
238         wctype_t is_comb, is_comb3;
239
240         long int typecount[16];
241         const char *typename[16];
242         static const char empty_slot[] = "empty_slot";
243         int built = 0;
244
245 #define INIT_TYPENAME(X) typename[__CTYPE_##X] = "C_" #X
246
247         for (i=0 ; i < 16 ; i++) {
248                 typename[i] = empty_slot;
249         }
250
251         INIT_TYPENAME(unclassified);
252         INIT_TYPENAME(alpha_nonupper_nonlower);
253         INIT_TYPENAME(alpha_lower);
254         INIT_TYPENAME(alpha_upper_lower);
255         INIT_TYPENAME(alpha_upper);
256         INIT_TYPENAME(digit);
257         INIT_TYPENAME(punct);
258         INIT_TYPENAME(graph);
259         INIT_TYPENAME(print_space_nonblank);
260         INIT_TYPENAME(print_space_blank);
261         INIT_TYPENAME(space_nonblank_noncntrl);
262         INIT_TYPENAME(space_blank_noncntrl);
263         INIT_TYPENAME(cntrl_space_nonblank);
264         INIT_TYPENAME(cntrl_space_blank);
265         INIT_TYPENAME(cntrl_nonspace);
266
267         setvbuf(stdout, NULL, _IONBF, 0);
268
269         while (--argc) {
270                 if (!setlocale(LC_CTYPE, *++argv)) {
271                         printf("setlocale(LC_CTYPE,%s) failed!\n", *argv);
272                         continue;
273                 }
274
275                 if (!(totitle = wctrans("totitle"))) {
276                         printf("no totitle transformation.\n");
277                 }
278                 if (!(is_comb = wctype("combining"))) {
279                         printf("no combining wctype.\n");
280                 }
281                 if (!(is_comb3 = wctype("combining_level3"))) {
282                         printf("no combining_level3 wctype.\n");
283                 }
284
285                 if (!built) {
286                 built = 1;
287                 ul_count = 1;
288                 uldiff[0].u = uldiff[0].l = 0;
289
290                 memset(wct, 0, sizeof(wct));
291                 memset(combt, 0, sizeof(combt));
292                 memset(widtht, 0, sizeof(widtht));
293
294                 for (i = 0 ; i < 16 ; i++) {
295                         typecount[i] = 0;
296                 }
297
298                 for (c=0 ; c <= RANGE ; c++) {
299                         if (iswdigit(c)) {
300                                 d = __CTYPE_digit;
301                         } else if (iswalpha(c)) {
302                                 d = __CTYPE_alpha_nonupper_nonlower;
303                                 if (iswlower(c)) {
304                                         d = __CTYPE_alpha_lower;
305                                         if (iswupper(c)) {
306                                                 d = __CTYPE_alpha_upper_lower;
307                                         }
308                                 } else if (iswupper(c)) {
309                                         d = __CTYPE_alpha_upper;
310                                 }
311                         } else if (iswpunct(c)) {
312                                 d = __CTYPE_punct;
313                         } else if (iswgraph(c)) {
314                                 d = __CTYPE_graph;
315                         } else if (iswprint(c)) {
316                                 d = __CTYPE_print_space_nonblank;
317                                 if (iswblank(c)) {
318                                         d = __CTYPE_print_space_blank;
319                                 }
320                         } else if (iswspace(c) && !iswcntrl(c)) {
321                                 d = __CTYPE_space_nonblank_noncntrl;
322                                 if (iswblank(c)) {
323                                         d = __CTYPE_space_blank_noncntrl;
324                                 }
325                         } else if (iswcntrl(c)) {
326                                 d = __CTYPE_cntrl_nonspace;
327                                 if (iswspace(c)) {
328                                         d = __CTYPE_cntrl_space_nonblank;
329                                         if (iswblank(c)) {
330                                                 d = __CTYPE_cntrl_space_blank;
331                                         }
332                                 }
333                         } else {
334                                 d = __CTYPE_unclassified;
335                         }
336
337                         ++typecount[d];
338
339 #if 0
340                         if (iswspace(c)) {
341                                 if (iswblank(c)) {
342                                         printf("%#8x : space  blank\n", c);
343                                 } else {
344                                         printf("%#8x : space\n", c);
345                                 }
346                         }
347 #endif
348
349 #if 0
350                         if (c < 256) {
351                                 unsigned int glibc;
352
353                                 glibc = 0;
354                                 if (isalnum(c)) ++glibc; glibc <<= 1;
355                                 if (isalpha(c)) ++glibc; glibc <<= 1;
356                                 if (isblank(c)) ++glibc; glibc <<= 1;
357                                 if (iscntrl(c)) ++glibc; glibc <<= 1;
358                                 if (isdigit(c)) ++glibc; glibc <<= 1;
359                                 if (isgraph(c)) ++glibc; glibc <<= 1;
360                                 if (islower(c)) ++glibc; glibc <<= 1;
361                                 if (isprint(c)) ++glibc; glibc <<= 1;
362                                 if (ispunct(c)) ++glibc; glibc <<= 1;
363                                 if (isspace(c)) ++glibc; glibc <<= 1;
364                                 if (isupper(c)) ++glibc; glibc <<= 1;
365                                 if (isxdigit(c)) ++glibc;
366                                 printf("%#8x : ctype %#4x\n", c, glibc);
367                         }
368 #endif
369
370 #if 1
371                         /* Paranoid checking... */
372                         {
373                                 unsigned int glibc;
374                                 unsigned int mine;
375
376                                 glibc = 0;
377                                 if (iswalnum(c)) ++glibc; glibc <<= 1;
378                                 if (iswalpha(c)) ++glibc; glibc <<= 1;
379                                 if (iswblank(c)) ++glibc; glibc <<= 1;
380                                 if (iswcntrl(c)) ++glibc; glibc <<= 1;
381                                 if (iswdigit(c)) ++glibc; glibc <<= 1;
382                                 if (iswgraph(c)) ++glibc; glibc <<= 1;
383                                 if (iswlower(c)) ++glibc; glibc <<= 1;
384                                 if (iswprint(c)) ++glibc; glibc <<= 1;
385                                 if (iswpunct(c)) ++glibc; glibc <<= 1;
386                                 if (iswspace(c)) ++glibc; glibc <<= 1;
387                                 if (iswupper(c)) ++glibc; glibc <<= 1;
388                                 if (iswxdigit(c)) ++glibc;
389
390                                 mine = 0;
391                                 if (mywalnum(c)) ++mine; mine <<= 1;
392                                 if (mywalpha(c)) ++mine; mine <<= 1;
393                                 if (mywblank(c)) ++mine; mine <<= 1;
394                                 if (mywcntrl(c)) ++mine; mine <<= 1;
395                                 if (mywdigit(c)) ++mine; mine <<= 1;
396                                 if (mywgraph(c)) ++mine; mine <<= 1;
397                                 if (mywlower(c)) ++mine; mine <<= 1;
398                                 if (mywprint(c)) ++mine; mine <<= 1;
399                                 if (mywpunct(c)) ++mine; mine <<= 1;
400                                 if (mywspace(c)) ++mine; mine <<= 1;
401                                 if (mywupper(c)) ++mine; mine <<= 1;
402                                 if (mywxdigit(c)) ++mine;
403
404                                 if (glibc != mine) {
405                                         printf("%#8x : glibc %#4x != %#4x mine  %u\n", c, glibc, mine, d);
406                                         return EXIT_FAILURE;
407                                 }
408
409 #if 0
410                                 if (iswctype(c,is_comb) || iswctype(c,is_comb3)) {
411 /*                                      if (!iswpunct(c)) { */
412                                                 printf("%#8x : %d %d %#4x\n",
413                                                            c, iswctype(c,is_comb),iswctype(c,is_comb3), glibc);
414 /*                                      } */
415                                 }
416 #endif
417 #if 0
418                                 if (iswctype(c,is_comb) || iswctype(c,is_comb3)) {
419                                         if (!last_comb) {
420                                                 printf("%#8x - ", c);
421                                                 last_comb = c;
422                                         } else if (last_comb + 1 < c) {
423                                                 printf("%#8x\n%#8x - ", last_comb, c);
424                                                 last_comb = c;
425                                         } else {
426                                                 last_comb = c;
427                                         }
428                                 }
429 #endif
430                         }
431 #endif
432
433                         combt[c/4] |= ((((!!iswctype(c,is_comb)) << 1) | !!iswctype(c,is_comb3))
434                                                    << ((c & 3) << 1));
435 /*                      comb3t[c/8] |= ((!!iswctype(c,is_comb3)) << (c & 7)); */
436
437 /*                      widtht[c/4] |= (wcwidth(c) << ((c & 3) << 1)); */
438
439                         if (c & 1) {    /* Use the high nibble for odd numbered wchars. */
440                                 d <<= 4;
441                         }
442                         wct[c/2] |= d;
443
444                         l = towlower(c) - c;
445                         u = towupper(c) - c;
446                         ult[c] = 0;
447                         if (l || u) {
448                                 if ((l != (short)l) || (u != (short)u)) {
449                                         printf("range assumption error!  %x  %ld  %ld\n", c, l, u);
450                                         return EXIT_FAILURE;
451                                 }
452                                 for (i=0 ; i < ul_count ; i++) {
453                                         if ((l == uldiff[i].l) && (u == uldiff[i].u)) {
454                                                 goto found;
455                                         }
456                                 }
457                                 uldiff[ul_count].l = l;
458                                 uldiff[ul_count].u = u;
459                                 ++ul_count;
460                                 if (ul_count > MAXTO) {
461                                         printf("too many touppers/tolowers!\n");
462                                         return EXIT_FAILURE;
463                                 }
464                         found:
465                                 ult[c] = i;
466                         }
467                 }
468
469                 for (i = 0 ; i < 16 ; i++) {
470                         printf("typecount[%2d] = %8ld  %s\n", i, typecount[i], typename[i]);
471                 }
472
473                 printf("optimizing is* table..\n");
474                 n = -1;
475                 smallest = SIZE_MAX;
476                 cttable.ii = NULL;
477                 for (i=0 ; i < 14 ; i++) {
478                         t = newopt(wct, (RANGE/2)+1, i, &cttable);
479                         if (smallest >= t) {
480                                 n = i;
481                                 smallest = t;
482 /*                      } else { */
483 /*                              break; */
484                         }
485                 }
486                 printf("smallest = %zu\n", smallest);
487                 if (!(cttable.ii = malloc(smallest))) {
488                         printf("couldn't allocate space!\n");
489                         return EXIT_FAILURE;
490                 }
491                 smallest = SIZE_MAX;
492                 newopt(wct, (RANGE/2)+1, n, &cttable);
493                 ++cttable.ti_shift;             /* correct for nibble mode */
494
495
496
497                 printf("optimizing u/l-to table..\n");
498                 smallest = SIZE_MAX;
499                 ultable.ii = NULL;
500                 for (i=0 ; i < 14 ; i++) {
501                         t = newopt(ult, RANGE+1, i, &ultable);
502                         if (smallest >= t) {
503                                 n = i;
504                                 smallest = t;
505 /*                      } else { */
506 /*                              break; */
507                         }
508                 }
509                 printf("%zu (smallest) + %zu (u/l diffs) = %zu\n",
510                            smallest, 4 * ul_count, smallest + 4 * ul_count);
511                 printf("smallest = %zu\n", smallest);
512                 if (!(ultable.ii = malloc(smallest))) {
513                         printf("couldn't allocate space!\n");
514                         return EXIT_FAILURE;
515                 }
516                 smallest = SIZE_MAX;
517                 newopt(ult, RANGE+1, n, &ultable);
518
519
520 #if 0
521                 printf("optimizing comb table..\n");
522                 smallest = SIZE_MAX;
523                 combtable.ii = NULL;
524                 for (i=0 ; i < 14 ; i++) {
525                         t = newopt(combt, sizeof(combt), i, &combtable);
526                         if (smallest >= t) {
527                                 n = i;
528                                 smallest = t;
529 /*                      } else { */
530 /*                              break; */
531                         }
532                 }
533                 printf("smallest = %zu\n", smallest);
534                 if (!(combtable.ii = malloc(smallest))) {
535                         printf("couldn't allocate space!\n");
536                         return EXIT_FAILURE;
537                 }
538                 smallest = SIZE_MAX;
539                 newopt(combt, sizeof(combt), n, &combtable);
540                 combtable.ti_shift += 4; /* correct for 4 entries per */
541 #endif
542
543
544 #if 0
545                 printf("optimizing width table..\n");
546                 smallest = SIZE_MAX;
547                 widthtable.ii = NULL;
548                 for (i=0 ; i < 14 ; i++) {
549                         t = newopt(widtht, sizeof(widtht), i, &widthtable);
550                         if (smallest >= t) {
551                                 n = i;
552                                 smallest = t;
553 /*                      } else { */
554 /*                              break; */
555                         }
556                 }
557                 printf("smallest = %zu\n", smallest);
558                 if (!(widthtable.ii = malloc(smallest))) {
559                         printf("couldn't allocate space!\n");
560                         return EXIT_FAILURE;
561                 }
562                 smallest = SIZE_MAX;
563                 newopt(widtht, sizeof(widtht), n, &widthtable);
564                 widthtable.ti_shift += 4; /* correct for 4 entries per */
565 #endif
566
567 #if 0
568                 printf("optimizing comb3 table..\n");
569                 smallest = SIZE_MAX;
570                 comb3table.ii = NULL;
571                 for (i=0 ; i < 14 ; i++) {
572                         t = newopt(comb3t, sizeof(comb3t), i, &comb3table);
573                         if (smallest >= t) {
574                                 n = i;
575                                 smallest = t;
576 /*                      } else { */
577 /*                              break; */
578                         }
579                 }
580                 printf("smallest = %zu\n", smallest);
581                 if (!(comb3table.ii = malloc(smallest))) {
582                         printf("couldn't allocate space!\n");
583                         return EXIT_FAILURE;
584                 }
585                 smallest = SIZE_MAX;
586                 newopt(comb3t, sizeof(comb3t), n, &comb3table);
587                 comb3table.ti_shift += 8; /* correct for 4 entries per */
588 #endif
589
590                 dump_table_data(&cttable);
591                 dump_table_data(&ultable);
592                 dump_table_data(&combtable);
593                 }
594
595                 printf("verifying for %s...\n", *argv);
596 #if RANGE == 0xffffU
597                 for (c=0 ; c <= 0xffffUL ; c++)
598 #else
599                 for (c=0 ; c <= 0x10ffffUL ; c++)
600 #endif
601                         {
602                         unsigned int glibc;
603                         unsigned int mine;
604                         unsigned int upper, lower;
605
606 #if 0
607 #if RANGE < 0x10000UL
608                         if (c == 0x10000UL) {
609                                 c = 0x30000UL;  /* skip 1st and 2nd sup planes */
610                         }
611 #elif RANGE < 0x20000UL
612                         if (c == 0x20000UL) {
613                                 c = 0x30000UL;  /* skip 2nd sup planes */
614                         }
615 #endif
616 #endif
617
618                         glibc = 0;
619                         if (iswalnum(c)) ++glibc; glibc <<= 1;
620                         if (iswalpha(c)) ++glibc; glibc <<= 1;
621                         if (iswblank(c)) ++glibc; glibc <<= 1;
622                         if (iswcntrl(c)) ++glibc; glibc <<= 1;
623                         if (iswdigit(c)) ++glibc; glibc <<= 1;
624                         if (iswgraph(c)) ++glibc; glibc <<= 1;
625                         if (iswlower(c)) ++glibc; glibc <<= 1;
626                         if (iswprint(c)) ++glibc; glibc <<= 1;
627                         if (iswpunct(c)) ++glibc; glibc <<= 1;
628                         if (iswspace(c)) ++glibc; glibc <<= 1;
629                         if (iswupper(c)) ++glibc; glibc <<= 1;
630                         if (iswxdigit(c)) ++glibc;
631
632                         {
633                                 unsigned int u;
634                                 int n, sc;
635                                 int i0, i1;
636
637                                 u = c;
638                                 if (u <= RANGE) {
639                                         sc = u & ((1 << cttable.ti_shift) - 1);
640                                         u >>= cttable.ti_shift;
641                                         n = u & ((1 << cttable.ii_shift) - 1);
642                                         u >>= cttable.ii_shift;
643
644                                         i0 = cttable.ii[u];
645                                         i0 <<= cttable.ii_shift;
646                                         i1 = cttable.ti[i0 + n];
647                                         i1 <<= (cttable.ti_shift-1);
648                                         d = cttable.ut[i1 + (sc >> 1)];
649
650                                         if (sc & 1) {
651                                                 d >>= 4;
652                                         }
653                                         d &= 0x0f;
654                                 } else if ((((unsigned int)(c - 0xe0020UL)) <= 0x5f) || (c == 0xe0001UL)){
655                                         d = __CTYPE_punct;
656                                 } else if (((unsigned int)(c - 0xf0000UL)) < 0x20000UL) {
657                                         if ((c & 0xffffU) <= 0xfffdU) {
658                                                 d = __CTYPE_punct;
659                                         } else {
660                                                 d = __CTYPE_unclassified;
661                                         }
662                                 } else {
663                                         d = __CTYPE_unclassified;
664                                 }
665
666                         mine = 0;
667                         if (mywalnum(c)) ++mine; mine <<= 1;
668                         if (mywalpha(c)) ++mine; mine <<= 1;
669                         if (mywblank(c)) ++mine; mine <<= 1;
670                         if (mywcntrl(c)) ++mine; mine <<= 1;
671                         if (mywdigit(c)) ++mine; mine <<= 1;
672                         if (mywgraph(c)) ++mine; mine <<= 1;
673                         if (mywlower(c)) ++mine; mine <<= 1;
674                         if (mywprint(c)) ++mine; mine <<= 1;
675                         if (mywpunct(c)) ++mine; mine <<= 1;
676                         if (mywspace(c)) ++mine; mine <<= 1;
677                         if (mywupper(c)) ++mine; mine <<= 1;
678                         if (mywxdigit(c)) ++mine;
679
680                         if (glibc != mine) {
681                                 printf("%#8x : glibc %#4x != %#4x mine %d\n", c, glibc, mine, d);
682                                 if (c < 0x30000UL) {
683                                         printf("sc=%#x u=%#x n=%#x i0=%#x i1=%#x\n", sc, u, n, i0, i1);
684                                 }
685                         }
686                                 upper = lower = u = c;
687                                 if (u <= RANGE) {
688                                         sc = u & ((1 << ultable.ti_shift) - 1);
689                                         u >>= ultable.ti_shift;
690                                         n = u & ((1 << ultable.ii_shift) - 1);
691                                         u >>= ultable.ii_shift;
692
693                                         i0 = ultable.ii[u];
694                                         i0 <<= ultable.ii_shift;
695                                         i1 = ultable.ti[i0 + n];
696                                         i1 <<= (ultable.ti_shift);
697                                         i1 += sc;
698                                         i0 = ultable.ut[i1];
699                                         upper = c + uldiff[i0].u;
700                                         lower = c + uldiff[i0].l;
701                                 }
702
703                         if (towupper(c) != upper) {
704                                 printf("%#8x : towupper glibc %#4x != %#4x mine\n",
705                                            c, towupper(c), upper);
706                         }
707                                 
708                         if (towlower(c) != lower) {
709                                 printf("%#8x : towlower glibc %#4x != %#4x mine   i0 = %d\n",
710                                            c, towlower(c), lower, i0);
711                         }
712
713                         if (totitle && ((tt = towctrans(c, totitle)) != upper)) {
714                                 printf("%#8x : totitle glibc %#4lx != %#4x mine   i0 = %d\n",
715                                            c, tt, upper, i0);
716                         }
717                         }
718
719
720                         if ((c & 0xfff) == 0xfff) printf(".");
721                 }
722                 printf("done\n");
723         }
724
725         if (1) {
726                 FILE *fp;
727
728                 if (!(fp = fopen("wctables.h", "w"))) {
729                         printf("couldn't open wctables.h!\n");
730                         return EXIT_FAILURE;
731                 }
732
733                 fprintf(fp, "#define WC_TABLE_DOMAIN_MAX  %#8lx\n\n",
734                                 (unsigned long) RANGE);
735                 output_table(fp, "ctype", &cttable);
736                 output_table(fp, "uplow", &ultable);
737         
738
739 #warning fix the upper bound on the upper/lower tables... save 200 bytes or so
740                 fprintf(fp, "#define WCuplow_diffs  %7u\n", ul_count);
741                 fprintf(fp, "\n#ifdef WANT_WCuplow_diff_data\n\n");
742                 fprintf(fp, "\nstatic const short WCuplow_diff_data[%zu] = {",
743                            2 * (size_t) ul_count);
744                 for (i=0 ; i < ul_count ; i++) {
745                         if (i % 4 == 0) {
746                                 fprintf(fp, "\n");
747                         }
748                         fprintf(fp, " %6d, %6d,", uldiff[i].u, uldiff[i].l);
749                 }
750                 fprintf(fp, "\n};\n\n");
751                 fprintf(fp, "#endif /* WANT_WCuplow_diff_data */\n\n");
752
753
754 /*              output_table(fp, "comb", &combtable); */
755 /*              output_table(fp, "width", &widthtable); */
756
757                 fclose(fp);
758         }
759
760         return EXIT_SUCCESS;
761 }
762
763 size_t newopt(unsigned char *ut, size_t usize, int shift, table_data *tbl)
764 {
765         static int recurse = 0;
766         unsigned char *ti[RANGE+1];     /* table index */
767         size_t numblocks;
768         size_t blocksize;
769         size_t uniq;
770         size_t i, j;
771         size_t smallest, t;
772         unsigned char *ii_save;
773         int uniqblock[256];
774         unsigned char uit[RANGE+1];
775         int shift2;
776
777         ii_save = NULL;
778         blocksize = 1 << shift;
779         numblocks = usize >> shift;
780
781         /* init table index */
782         for (i=j=0 ; i < numblocks ; i++) {
783                 ti[i] = ut + j;
784                 j += blocksize;
785         }
786
787         /* sort */
788         nu_val = blocksize;
789         qsort(ti, numblocks, sizeof(unsigned char *), nu_memcmp);
790         
791         uniq = 1;
792         uit[(ti[0]-ut)/blocksize] = 0;
793         for (i=1 ; i < numblocks ; i++) {
794                 if (memcmp(ti[i-1], ti[i], blocksize) < 0) {
795                         if (++uniq > 255) {
796                                 break;
797                         }
798                         uniqblock[uniq - 1] = i;
799                 }
800 #if 1
801                 else if (memcmp(ti[i-1], ti[i], blocksize) > 0) {
802                         printf("bad sort %i!\n", i);
803                         abort();
804                 }
805 #endif
806                 uit[(ti[i]-ut)/blocksize] = uniq - 1;
807         }
808
809         smallest = SIZE_MAX;
810         shift2 = -1;
811         if (uniq <= 255) {
812                 smallest = numblocks + uniq * blocksize;
813                 if (!recurse) {
814                         ++recurse;
815                         for (j=1 ; j < 14 ; j++) {
816                                 if ((numblocks >> j) < 2) break;
817                                 if (tbl) {
818                                         ii_save = tbl->ii;
819                                         tbl->ii = NULL;
820                                 }
821                                 if ((t = newopt(uit, numblocks, j, tbl)) < SIZE_MAX) {
822                                         t += uniq * blocksize;
823                                 }
824                                 if (tbl) {
825                                         tbl->ii = ii_save;
826                                 }
827                                 if (smallest >= t) {
828                                         shift2 = j;
829                                         smallest = t;
830                                         if (!tbl->ii) {
831                                                 printf("ishift %zu  tshift %zu  size %zu\n",
832                                                            shift2, shift, t);
833                                         }
834 /*                              } else { */
835 /*                                      break; */
836                                 }
837                         }
838                         --recurse;
839                 }
840         } else {
841                 return SIZE_MAX;
842         }
843
844         if (tbl->ii) {
845                 if (recurse) {
846                         tbl->ii_shift = shift;
847                         tbl->ii_len = numblocks;
848                         memcpy(tbl->ii, uit, numblocks);
849                         tbl->ti = tbl->ii + tbl->ii_len;
850                         tbl->ti_len = uniq * blocksize;
851                         for (i=0 ; i < uniq ; i++) {
852                                 memcpy(tbl->ti + i * blocksize, ti[uniqblock[i]], blocksize);
853                         }
854                 } else {
855                         ++recurse;
856                         printf("setting ishift %zu  tshift %zu\n",
857                                                            shift2, shift);
858                         newopt(uit, numblocks, shift2, tbl);
859                         --recurse;
860                         tbl->ti_shift = shift;
861                         tbl->ut_len = uniq * blocksize;
862                         tbl->ut = tbl->ti + tbl->ti_len;
863                         for (i=0 ; i < uniq ; i++) {
864                                 memcpy(tbl->ut + i * blocksize, ti[uniqblock[i]], blocksize);
865                         }
866                 }
867         }
868         return smallest;
869 }