| 1 | /* |
| 2 | * Copyright (c) 1999, 2002 Michael J. Roberts |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Unicode Case Mapping Generator |
| 10 | * |
| 11 | * Read the Unicode character database files, and use the information in |
| 12 | * the file to generate C++ source code for functions that perform |
| 13 | * lower-to-upper and upper-to-lower case mapping and character |
| 14 | * classifications for upper-case, lower-case, alphabetic, and numeric |
| 15 | * characters. |
| 16 | * |
| 17 | * We read the following files: |
| 18 | * |
| 19 | *. UnicodeData.txt - the main character database file |
| 20 | *. SpecialCasing.txt - special upper/lower case conversions |
| 21 | *. CaseFolding.txt - case folding table |
| 22 | * |
| 23 | * The command line must provide the path prefix to the Unicode source |
| 24 | * files, in a format where we can simply append the filenames to the |
| 25 | * prefix to get the full path. E.g., on Windows, specify "unidata\", on |
| 26 | * Unix "unidata/". |
| 27 | * |
| 28 | * We generate our tables as a two-level array, since the mappings are |
| 29 | * sparse (i.e., the majority of character positions have no mappings, |
| 30 | * because they do not contain letters or contain letters with no case |
| 31 | * conversions). |
| 32 | * |
| 33 | * The size of the lower-level array page can be set via the CASE_PAGE_SIZE |
| 34 | * macro defined below. This value is arbitrary, but it's probably most |
| 35 | * efficient to choose a power of two so that the compiler can generate |
| 36 | * code that computes quotients and remainders using bit mask and shift |
| 37 | * operations. Most importantly, though, CASE_PAGE_SIZE should be chosen |
| 38 | * to yield the smallest overall data size. It's not trivial to guess what |
| 39 | * might give us the smallest overall data size, because the arrangement of |
| 40 | * the characters in the Unicode character set is somewhat arbitrary. |
| 41 | * Experimenting with different CASE_PAGE_SIZE values is the easiest way to |
| 42 | * find a good value. To facilitate choosing a good size, we insert a |
| 43 | * comment at the end of our derived output with the total static data |
| 44 | * size. Whenever we generate a new mapping file, we should experiment |
| 45 | * with a few values of CASE_PAGE_SIZE (powers of two from 32 to 2048 are |
| 46 | * good things to try) to find an efficient size. As of this writing, the |
| 47 | * value chosen below seems to produce the smallest overall table data. |
| 48 | * |
| 49 | * THE UNICODE CHARACTER LISTING SOURCE FILE IS NOT PART OF THE T3 |
| 50 | * DISTRIBUTION. Obtain the character listing file from the Unicode web |
| 51 | * site at: |
| 52 | * |
| 53 | * ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData-Latest.txt |
| 54 | */ |
| 55 | |
| 56 | #include <stdio.h> |
| 57 | #include <stdlib.h> |
| 58 | #include <string.h> |
| 59 | #include <stdarg.h> |
| 60 | #include <ctype.h> |
| 61 | |
| 62 | #include "vmuni.h" |
| 63 | |
| 64 | #define TRUE 1 |
| 65 | #define FALSE 0 |
| 66 | #define countof(x) (sizeof(x)/sizeof((x)[0])) |
| 67 | |
| 68 | #define PAGE_SIZE 128 |
| 69 | #define PAGE_COUNT (65536/PAGE_SIZE) |
| 70 | #define PAGE_SHIFT 7 |
| 71 | #define PAGE_MASK 0x7F |
| 72 | |
| 73 | #define POINTER_BYTES 4 |
| 74 | |
| 75 | /* strip comments from an input line */ |
| 76 | static void strip_comments(char *buf) |
| 77 | { |
| 78 | char *p = strchr(buf, '#'); |
| 79 | if (p != 0) |
| 80 | { |
| 81 | /* skip any spaces before the comment */ |
| 82 | while (p > buf && isspace(*(p-1))) |
| 83 | --p; |
| 84 | |
| 85 | /* end the line at the first space or '#' */ |
| 86 | *p = '\0'; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /* read a hex field */ |
| 91 | static void get_hex_field(char **p, unsigned long *val) |
| 92 | { |
| 93 | /* presume failure */ |
| 94 | *val = 0; |
| 95 | |
| 96 | /* read the field value */ |
| 97 | sscanf(*p, "%x", val); |
| 98 | |
| 99 | /* skip to the next field */ |
| 100 | while (**p != '\0' && **p != ';') |
| 101 | ++(*p); |
| 102 | |
| 103 | /* skip the separator */ |
| 104 | if (**p == ';') |
| 105 | ++(*p); |
| 106 | } |
| 107 | |
| 108 | /* read a multi-hex field - a list of hex character codes */ |
| 109 | static void get_multihex_field(char **p, unsigned long *val, size_t valcnt) |
| 110 | { |
| 111 | /* read fields */ |
| 112 | for (;;) |
| 113 | { |
| 114 | /* skip spaces */ |
| 115 | for ( ; isspace(**p) ; ++*p) ; |
| 116 | |
| 117 | /* if we're not at a hex digit, we're done */ |
| 118 | if (!isxdigit(**p)) |
| 119 | break; |
| 120 | |
| 121 | /* if we're out of space, flag an error */ |
| 122 | if (valcnt <= 1) |
| 123 | { |
| 124 | /* |
| 125 | * not very good diagnostics, but it should never happen; if it |
| 126 | * ever does we can upgrade this as needed |
| 127 | */ |
| 128 | printf("#error get_multihex_field() value too long!\n"); |
| 129 | exit(1); |
| 130 | } |
| 131 | |
| 132 | /* read the value */ |
| 133 | sscanf(*p, "%x", val); |
| 134 | |
| 135 | /* advance past this item in the output */ |
| 136 | ++val, --valcnt; |
| 137 | |
| 138 | /* skip to the next field */ |
| 139 | for ( ; isxdigit(**p) ; ++*p) ; |
| 140 | } |
| 141 | |
| 142 | /* add a null terminator */ |
| 143 | *val = 0; |
| 144 | |
| 145 | /* if we're at the semicolon separator, skip it */ |
| 146 | if (**p == ';') |
| 147 | ++(*p); |
| 148 | } |
| 149 | |
| 150 | /* read a decimal field */ |
| 151 | static void get_dec_field(char **p, unsigned int *val) |
| 152 | { |
| 153 | /* presume failure */ |
| 154 | *val = 0; |
| 155 | |
| 156 | /* read the field value */ |
| 157 | sscanf(*p, "%d", val); |
| 158 | |
| 159 | /* skip to the next field */ |
| 160 | while (**p != '\0' && **p != ';') |
| 161 | ++(*p); |
| 162 | |
| 163 | /* skip the separator */ |
| 164 | if (**p == ';') |
| 165 | ++(*p); |
| 166 | } |
| 167 | |
| 168 | /* read a string field */ |
| 169 | static void get_str_field(char **p, char *dst, size_t dstlen) |
| 170 | { |
| 171 | /* skip leading spaces */ |
| 172 | for ( ; isspace(**p) ; ++(*p)) ; |
| 173 | |
| 174 | /* |
| 175 | * find the end of the field, copying characters as we go (up to the |
| 176 | * result buffer size) |
| 177 | */ |
| 178 | for ( ; **p != '\0' && **p != ';' ; ++(*p)) |
| 179 | { |
| 180 | /* copy this character if we have room for it */ |
| 181 | if (dstlen > 1) |
| 182 | { |
| 183 | /* copy the character */ |
| 184 | *dst++ = **p; |
| 185 | |
| 186 | /* that's one less character we have room for */ |
| 187 | --dstlen; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* skip the separator */ |
| 192 | if (**p == ';') |
| 193 | ++(*p); |
| 194 | |
| 195 | /* terminate the result if possible */ |
| 196 | if (dstlen > 0) |
| 197 | *dst = '\0'; |
| 198 | } |
| 199 | |
| 200 | /* terminate with a printf-style formatted error message */ |
| 201 | static void errexit(const char *msg, ...) |
| 202 | { |
| 203 | va_list args; |
| 204 | va_start(args, msg); |
| 205 | vfprintf(stderr, msg, args); |
| 206 | va_end(args); |
| 207 | |
| 208 | fprintf(stderr, "\n"); |
| 209 | exit(1); |
| 210 | } |
| 211 | |
| 212 | /* character table entry */ |
| 213 | struct chardef |
| 214 | { |
| 215 | /* character name */ |
| 216 | char *name; |
| 217 | |
| 218 | /* type of this character as a T3_CTYPE_xxx value */ |
| 219 | unsigned char attr; |
| 220 | |
| 221 | const char *attr_name() const |
| 222 | { |
| 223 | static const char *names[] = { T3_CTYPE_NAMES }; |
| 224 | return names[attr]; |
| 225 | } |
| 226 | |
| 227 | /* lower-case conversion */ |
| 228 | wchar_t *lower; |
| 229 | |
| 230 | /* upper-case conversion */ |
| 231 | wchar_t *upper; |
| 232 | |
| 233 | /* title case conversion */ |
| 234 | wchar_t *title; |
| 235 | |
| 236 | /* case folding conversion */ |
| 237 | wchar_t *fold; |
| 238 | |
| 239 | /* does this entry have a simple folding? */ |
| 240 | int has_simple_folding; |
| 241 | |
| 242 | void set_name(const char *str) |
| 243 | { |
| 244 | name = new char[strlen(str) + 1]; |
| 245 | strcpy(name, str); |
| 246 | } |
| 247 | |
| 248 | int name_ends_with(const char *str) |
| 249 | { |
| 250 | size_t slen = strlen(str); |
| 251 | size_t nlen = strlen(name); |
| 252 | return (nlen > slen && stricmp(name + nlen - slen, str) == 0); |
| 253 | } |
| 254 | |
| 255 | void derive_name(const char *tpl, int ch) |
| 256 | { |
| 257 | char buf[256]; |
| 258 | sprintf(buf, "%s-%04X", tpl, ch); |
| 259 | set_name(buf); |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | /* the character table */ |
| 264 | static chardef ctab[65536]; |
| 265 | |
| 266 | static int page_has_cases(int pg) |
| 267 | { |
| 268 | chardef *c = ctab + pg*PAGE_SIZE; |
| 269 | for (int i = 0 ; i < PAGE_SIZE ; ++i, ++c) |
| 270 | { |
| 271 | if (c->lower != 0 || c->upper != 0 || c->title != 0 || c->fold != 0) |
| 272 | return TRUE; |
| 273 | } |
| 274 | return FALSE; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * allocate a wchar_t string from a string of 32-bit characters; if the |
| 279 | * string contains any invalid characters, we'll fail and return null |
| 280 | */ |
| 281 | static wchar_t *alloc_wstr(const unsigned long *s, size_t len) |
| 282 | { |
| 283 | /* check for invalid characters */ |
| 284 | size_t i; |
| 285 | for (i = 0 ; i < len ; ++i) |
| 286 | { |
| 287 | /* if this is a null terminator, stop here */ |
| 288 | if (s[i] == 0) |
| 289 | { |
| 290 | len = i; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | /* if this is an invalid character, return failure */ |
| 295 | if (s[i] > 0xffff) |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | /* if the length is zero, do nothing */ |
| 300 | if (len == 0) |
| 301 | return 0; |
| 302 | |
| 303 | /* allocate space */ |
| 304 | wchar_t *ret = new wchar_t[len+1]; |
| 305 | |
| 306 | /* copy the characters */ |
| 307 | for (i = 0 ; i < len ; ++i) |
| 308 | ret[i] = (wchar_t)s[i]; |
| 309 | |
| 310 | /* add the null terminator */ |
| 311 | ret[i] = 0; |
| 312 | |
| 313 | /* return the new string */ |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | /* get the length in characters of a null-terminated wchar_t string */ |
| 318 | static int wstr_len(const wchar_t *s) |
| 319 | { |
| 320 | if (s == 0) |
| 321 | return 0; |
| 322 | |
| 323 | int l; |
| 324 | for (l = 0 ; *s != 0 ; ++s, ++l); |
| 325 | return l; |
| 326 | } |
| 327 | |
| 328 | /* compare two null-terminated wchar_t strings for equality */ |
| 329 | static int wstr_eq(const wchar_t *a, const wchar_t *b) |
| 330 | { |
| 331 | for ( ; *a != 0 && *b != 0 ; ++a, ++b) |
| 332 | { |
| 333 | if (*a != *b) |
| 334 | return FALSE; |
| 335 | } |
| 336 | return *a == *b; |
| 337 | } |
| 338 | |
| 339 | /* keep a case mapping if it's not empty and it's not a self reference */ |
| 340 | static int keep_case(unsigned long *str, unsigned long ch) |
| 341 | { |
| 342 | return (str[0] != 0 |
| 343 | && !(str[0] == ch && str[1] == 0)); |
| 344 | } |
| 345 | |
| 346 | /* copy a case expansion */ |
| 347 | static int copy_case_exp(wchar_t *caseexp, int &caseofs, const wchar_t *str) |
| 348 | { |
| 349 | /* if there's no string, or it's empty, there's no expansion */ |
| 350 | if (str == 0 || str[0] == 0) |
| 351 | return 0; |
| 352 | |
| 353 | /* the return value will be the current offset */ |
| 354 | int ret = caseofs; |
| 355 | |
| 356 | /* copy the string into the expansion table */ |
| 357 | do { |
| 358 | caseexp[caseofs++] = *str; |
| 359 | } while (*str++ != 0); |
| 360 | |
| 361 | /* return the start of the expansion copy */ |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Main entrypoint |
| 367 | */ |
| 368 | int main(int argc, char **argv) |
| 369 | { |
| 370 | /* check arguments */ |
| 371 | if (argc < 2) |
| 372 | errexit("usage: uni_case <database_dir_prefix>"); |
| 373 | |
| 374 | /* get the path prefix for the database files */ |
| 375 | const char *path = argv[1]; |
| 376 | |
| 377 | /* clear the character table */ |
| 378 | memset(ctab, 0, sizeof(ctab)); |
| 379 | |
| 380 | /* start the output file */ |
| 381 | /* copyright-date-string */ |
| 382 | printf("/* Copyright (c) 1999, 2012 Michael J. Roberts */\n" |
| 383 | "/*\n" |
| 384 | " * TADS 3 Case Conversion Table\n" |
| 385 | " *\n" |
| 386 | " * THIS IS A GENERATED FILE. DO NOT EDIT - any changes will\n" |
| 387 | " * be lost on the next build.\n" |
| 388 | " *\n" |
| 389 | " * This file is mechanically derived from the Unicode\n" |
| 390 | " * character database listing.\n" |
| 391 | " */\n" |
| 392 | "\n" |
| 393 | "#include <stdlib.h>\n" |
| 394 | "\n" |
| 395 | "#include \"vmuni.h\"\n" |
| 396 | "\n"); |
| 397 | |
| 398 | /* no data allocated yet */ |
| 399 | size_t data_size = 0; |
| 400 | |
| 401 | /* start with the main character database file */ |
| 402 | char fname[256]; |
| 403 | sprintf(fname, "%s%s", path, "UnicodeData.Txt"); |
| 404 | FILE *fp = fopen(fname, "r"); |
| 405 | if (fp == 0) |
| 406 | errexit("unable to open main database file %s", fname); |
| 407 | |
| 408 | /* process the file */ |
| 409 | for (chardef *prv = 0 ; ; ) |
| 410 | { |
| 411 | |
| 412 | /* read the next line; stop at EOF */ |
| 413 | char buf[256]; |
| 414 | if (fgets(buf, sizeof(buf), fp) == 0) |
| 415 | { |
| 416 | fclose(fp); |
| 417 | break; |
| 418 | } |
| 419 | |
| 420 | /* start parsing at the beginning of the buffer */ |
| 421 | char *p = buf; |
| 422 | |
| 423 | /* read the fields */ |
| 424 | unsigned long ch, upper, lower, title; |
| 425 | unsigned int decimal_val, digit_val, combining; |
| 426 | char cname[40], chartype[40], bidi[10], decomp[128], numeric_val[20]; |
| 427 | char mirror[5], uni1name[40], comment[20]; |
| 428 | get_hex_field(&p, &ch); |
| 429 | get_str_field(&p, cname, sizeof(cname)); |
| 430 | get_str_field(&p, chartype, sizeof(chartype)); |
| 431 | get_dec_field(&p, &combining); |
| 432 | get_str_field(&p, bidi, sizeof(bidi)); |
| 433 | get_str_field(&p, decomp, sizeof(decomp)); |
| 434 | get_dec_field(&p, &decimal_val); |
| 435 | get_dec_field(&p, &digit_val); |
| 436 | get_str_field(&p, numeric_val, sizeof(numeric_val)); |
| 437 | get_str_field(&p, mirror, sizeof(mirror)); |
| 438 | get_str_field(&p, uni1name, sizeof(uni1name)); |
| 439 | get_str_field(&p, comment, sizeof(comment)); |
| 440 | get_hex_field(&p, &upper); |
| 441 | get_hex_field(&p, &lower); |
| 442 | get_hex_field(&p, &title); |
| 443 | |
| 444 | /* |
| 445 | * If the character code is zero, it means either that this is the |
| 446 | * null character definition or that this is a comment line or a |
| 447 | * blank line - ignore all of these cases. If the character is |
| 448 | * over 0xffff, ignore it as well, since we only handle the "base |
| 449 | * multilingual plane" characters, in range 0-ffff. |
| 450 | */ |
| 451 | if (ch == 0 || ch > 0xffff) |
| 452 | continue; |
| 453 | |
| 454 | /* get the character table entry */ |
| 455 | chardef *cd = &ctab[ch]; |
| 456 | |
| 457 | /* save this character name */ |
| 458 | cd->set_name(cname); |
| 459 | |
| 460 | /* presume we won't find any special attributes */ |
| 461 | cd->attr = T3_CTYPE_OTHER; |
| 462 | |
| 463 | /* check the character type and set the attributes accordingly */ |
| 464 | switch(chartype[0]) |
| 465 | { |
| 466 | case 'L': |
| 467 | /* get the case */ |
| 468 | switch(chartype[1]) |
| 469 | { |
| 470 | case 'u': |
| 471 | /* upper-case letter */ |
| 472 | cd->attr = T3_CTYPE_UPPER; |
| 473 | break; |
| 474 | |
| 475 | case 't': |
| 476 | /* |
| 477 | * title-case letter (actually, letters - I'm pretty sure |
| 478 | * these are all ligatures of an upper-case and lower-case |
| 479 | * letter) |
| 480 | */ |
| 481 | cd->attr = T3_CTYPE_TITLE; |
| 482 | break; |
| 483 | |
| 484 | case 'l': |
| 485 | /* lower-case letter */ |
| 486 | cd->attr = T3_CTYPE_LOWER; |
| 487 | break; |
| 488 | |
| 489 | case 'o': |
| 490 | /* caseless letter */ |
| 491 | cd->attr = T3_CTYPE_ALPHA; |
| 492 | break; |
| 493 | } |
| 494 | break; |
| 495 | |
| 496 | case 'N': |
| 497 | /* if it's a decimal digit, note it */ |
| 498 | if (chartype[1] == 'd') |
| 499 | cd->attr = T3_CTYPE_DIGIT; |
| 500 | break; |
| 501 | |
| 502 | case 'Z': |
| 503 | /* Zp, Zl -> vertical space; otherwise it's a horizontal space */ |
| 504 | cd->attr = (chartype[1] == 'p' || chartype[1] == 'l' |
| 505 | ? T3_CTYPE_VSPAC : T3_CTYPE_SPACE); |
| 506 | break; |
| 507 | |
| 508 | case 'P': |
| 509 | /* punctuation of some kind */ |
| 510 | cd->attr = T3_CTYPE_PUNCT; |
| 511 | break; |
| 512 | |
| 513 | case 'C': |
| 514 | /* |
| 515 | * Control character - check the bidi class to see if it's a |
| 516 | * spacing character of some kind. |
| 517 | * |
| 518 | *. WS - ordinary horizontal whitespace |
| 519 | *. S - horizontal separator (tab, etc); counts as whitespace |
| 520 | *. B - vertical whitespace (CR, LF, etc) |
| 521 | */ |
| 522 | if (ch == 0x0B) |
| 523 | { |
| 524 | /* TADS-specific \b character -> vertical whitespace */ |
| 525 | cd->attr = T3_CTYPE_VSPAC; |
| 526 | } |
| 527 | else if (ch == 0x0E || ch == 0x0F) |
| 528 | { |
| 529 | /* TADS-specific \v and \^ -> device controls */ |
| 530 | cd->attr = T3_CTYPE_OTHER; |
| 531 | } |
| 532 | else if (ch == 0x15) |
| 533 | { |
| 534 | /* TADS-specific '\ ' quoted space -> ordinary character */ |
| 535 | cd->attr = T3_CTYPE_OTHER; |
| 536 | } |
| 537 | else if ((bidi[0] == 'W' && bidi[1] == 'S' && bidi[2] == '\0') |
| 538 | || (bidi[0] == 'S' && bidi[1] == '\0')) |
| 539 | { |
| 540 | /* WS, S -> whitespace */ |
| 541 | cd->attr = T3_CTYPE_SPACE; |
| 542 | } |
| 543 | else if (bidi[0] == 'B' && bidi[1] == '\0') |
| 544 | { |
| 545 | /* B -> vertical whitespace */ |
| 546 | cd->attr = T3_CTYPE_VSPAC; |
| 547 | } |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | /* set the case conversion mappings */ |
| 552 | cd->upper = alloc_wstr(&upper, 1); |
| 553 | cd->lower = alloc_wstr(&lower, 1); |
| 554 | cd->title = alloc_wstr(&title, 1); |
| 555 | |
| 556 | /* |
| 557 | * If the name ends in "last>", and the previous item's name ends |
| 558 | * in "first>", we have a range of identical character definitions. |
| 559 | * Repeat the attributes over the range. |
| 560 | */ |
| 561 | if (prv != 0 && cd->name_ends_with(", last>") |
| 562 | && prv->name_ends_with(", first>")) |
| 563 | { |
| 564 | char *name = prv->name + 1; |
| 565 | name[strlen(name) - 8] = '\0'; |
| 566 | for (chardef *cur = prv ; cur <= cd ; ++cur) |
| 567 | { |
| 568 | *cur = *prv; |
| 569 | cur->derive_name(name, cur - ctab); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /* this is now the previous item */ |
| 574 | prv = cd; |
| 575 | } |
| 576 | |
| 577 | /* open the special case mapping file */ |
| 578 | sprintf(fname, "%s%s", path, "SpecialCasing.txt"); |
| 579 | if ((fp = fopen(fname, "r")) == 0) |
| 580 | errexit("unable to open special case mapping file %s", fname); |
| 581 | |
| 582 | /* process it */ |
| 583 | for (;;) |
| 584 | { |
| 585 | /* read the next line */ |
| 586 | char buf[256]; |
| 587 | if (fgets(buf, sizeof(buf), fp) == 0) |
| 588 | { |
| 589 | fclose(fp); |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | /* remove any trailing comment (and spaces leading up to it) */ |
| 594 | strip_comments(buf); |
| 595 | |
| 596 | /* decode the fields */ |
| 597 | unsigned long ch, lower[40], title[40], upper[40]; |
| 598 | char cond[128]; |
| 599 | char *p = buf; |
| 600 | get_hex_field(&p, &ch); |
| 601 | get_multihex_field(&p, lower, countof(lower)); |
| 602 | get_multihex_field(&p, title, countof(title)); |
| 603 | get_multihex_field(&p, upper, countof(upper)); |
| 604 | get_str_field(&p, cond, sizeof(cond)); |
| 605 | |
| 606 | /* skip blank lines and characters outside the BMP */ |
| 607 | if (ch == 0 || ch > 0xffff) |
| 608 | continue; |
| 609 | |
| 610 | /* if this item has a condition, skip it - we don't handle these */ |
| 611 | if (cond[0] != '\0') |
| 612 | continue; |
| 613 | |
| 614 | /* |
| 615 | * set the updated case conversion fields; omit self-references to |
| 616 | * save space |
| 617 | */ |
| 618 | chardef *cd = &ctab[ch]; |
| 619 | if (keep_case(lower, ch)) |
| 620 | cd->lower = alloc_wstr(lower, countof(lower)); |
| 621 | if (keep_case(title, ch)) |
| 622 | cd->title = alloc_wstr(title, countof(title)); |
| 623 | if (keep_case(upper, ch)) |
| 624 | cd->upper = alloc_wstr(upper, countof(upper)); |
| 625 | } |
| 626 | |
| 627 | /* open the case folding file */ |
| 628 | sprintf(fname, "%s%s", path, "CaseFolding.txt"); |
| 629 | if ((fp = fopen(fname, "r")) == 0) |
| 630 | errexit("unable to open special case mapping file %s", fname); |
| 631 | |
| 632 | /* process it */ |
| 633 | for (;;) |
| 634 | { |
| 635 | /* read the next line */ |
| 636 | char buf[256]; |
| 637 | if (fgets(buf, sizeof(buf), fp) == 0) |
| 638 | { |
| 639 | fclose(fp); |
| 640 | break; |
| 641 | } |
| 642 | |
| 643 | /* remove any trailing comment (and spaces leading up to it) */ |
| 644 | strip_comments(buf); |
| 645 | |
| 646 | /* decode the fields */ |
| 647 | unsigned long ch, fold[40]; |
| 648 | char status[40]; |
| 649 | char *p = buf; |
| 650 | get_hex_field(&p, &ch); |
| 651 | get_str_field(&p, status, sizeof(status)); |
| 652 | get_multihex_field(&p, fold, countof(fold)); |
| 653 | |
| 654 | /* skip blank lines and charaters outside the BMP */ |
| 655 | if (ch == 0 || ch > 0xffff) |
| 656 | continue; |
| 657 | |
| 658 | /* get the character table entry */ |
| 659 | chardef *cd = &ctab[ch]; |
| 660 | |
| 661 | /* |
| 662 | * If it's a "simple" folding, skip it, but verify our assumption |
| 663 | * that all simple foldings are identical to the single-character |
| 664 | * lower-case conversion in the main UnicodeData.txt table. At |
| 665 | * run-time, we use the lower-case conversion in instances where we |
| 666 | * want a 1:1 folding. |
| 667 | */ |
| 668 | if (status[0] == 'S') |
| 669 | { |
| 670 | /* validate our assumption that Simple Folding == lower case */ |
| 671 | if (cd->lower == 0 |
| 672 | || wstr_len(cd->lower) != 1 |
| 673 | || cd->lower[0] != fold[0]) |
| 674 | { |
| 675 | printf("#error Simple folding ('S') for %04x in " |
| 676 | "CaseFolding.txt doesn't match " |
| 677 | "1:1 lower-case conversion in UnicodeData.txt\n", ch); |
| 678 | } |
| 679 | |
| 680 | /* mark the entry as having a simple folding */ |
| 681 | cd->has_simple_folding = TRUE; |
| 682 | |
| 683 | /* otherwise ignore simple foldings */ |
| 684 | continue; |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * if it's a Common entry, it counts as both a Simple and Full |
| 689 | * folding - mark the entry as having a Simple folding |
| 690 | */ |
| 691 | if (status[0] == 'C') |
| 692 | cd->has_simple_folding = TRUE; |
| 693 | |
| 694 | /* |
| 695 | * Ignore the Turkic-only folding rules. We don't currently have a |
| 696 | * mechanism to select rules by language, so we can't use these. |
| 697 | */ |
| 698 | if (status[0] == 'T') |
| 699 | continue; |
| 700 | |
| 701 | /* store it */ |
| 702 | cd->fold = alloc_wstr(fold, countof(fold)); |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Generate the character type table. We only have 9 character types |
| 707 | * currently, so a character type fits in 4 bits; we can thus pack two |
| 708 | * types in a byte. So pack each pair of characters, with the even |
| 709 | * character in the low nybble and the odd character in the high |
| 710 | * nybble. (This lets us do the decoding entirely with bit masks and |
| 711 | * bit shifts, which might be a little quicker than a conditional.) |
| 712 | */ |
| 713 | data_size += 0x8000; |
| 714 | printf("static const unsigned char chartype[32768] =\n{\n"); |
| 715 | int i; |
| 716 | for (i = 0 ; i < 0x10000 ; i += 2) |
| 717 | { |
| 718 | /* pack the next two characters */ |
| 719 | printf(" (%s << 4) | %s%c /* %04x %s | %04x %s */\n", |
| 720 | ctab[i+1].attr_name(), ctab[i].attr_name(), |
| 721 | i + 2 == 0x10000 ? ' ' : ',', |
| 722 | i+1, ctab[i+1].name, i, ctab[i].name); |
| 723 | } |
| 724 | printf("};\n" |
| 725 | "int t3_get_chartype(wchar_t ch)\n" |
| 726 | "{\n" |
| 727 | " return (chartype[ch >> 1] >> ((ch & 1) << 2)) & 0x0F;\n" |
| 728 | "}\n" |
| 729 | "\n"); |
| 730 | |
| 731 | /* |
| 732 | * Build the case conversion tables. Break these up into pages, since |
| 733 | * the case conversions are only used sparsely. |
| 734 | * |
| 735 | * Each case conversion entry is a 16-bit offset into the case |
| 736 | * expansion array, which consists of null-terminated strings of |
| 737 | * wchar_t's (explicitly as uint16's, since we don't currently support |
| 738 | * anything outside the BMP). E.g., the upper-case conversion for 'a' |
| 739 | * will point via an array offset to the string 'A', '\0'. Empirically |
| 740 | * we need well under 64k entries, which is why we can get away with a |
| 741 | * 16-bit offset; we want to use the smallest type for the offset to |
| 742 | * minimize space to store the table. Since we know this is a hard |
| 743 | * upper bound, we can pre-allocate the expansion table now at this |
| 744 | * size; when we write it, of course, we'll only write as much as we |
| 745 | * actually end up populating. |
| 746 | */ |
| 747 | wchar_t *caseexp = new wchar_t[65536]; |
| 748 | |
| 749 | /* |
| 750 | * start the table with a null - we use index 0 to mean null, so we'll |
| 751 | * never point anything here, and in addition we want to ensure there's |
| 752 | * a null character before the start of each entry, so zeroing this |
| 753 | * element satisfies both purposes |
| 754 | */ |
| 755 | caseexp[0] = 0; |
| 756 | |
| 757 | /* start the table */ |
| 758 | printf("struct case_table\n" |
| 759 | "{\n" |
| 760 | " uint16_t lower;\n" |
| 761 | " uint16_t title;\n" |
| 762 | " uint16_t upper;\n" |
| 763 | " uint16_t fold;\n" |
| 764 | "};\n" |
| 765 | "\n"); |
| 766 | int caseofs = 1; |
| 767 | for (i = 0 ; i < PAGE_COUNT ; ++i) |
| 768 | { |
| 769 | /* build this page only if it has any entries */ |
| 770 | if (page_has_cases(i)) |
| 771 | { |
| 772 | data_size += 4*2*PAGE_SIZE; |
| 773 | |
| 774 | printf("static case_table case_pg_%03x[%d] =\n{\n", i, PAGE_SIZE); |
| 775 | chardef *ch = &ctab[i*PAGE_SIZE]; |
| 776 | for (int j = 0 ; j < PAGE_SIZE ; ++j, ++ch) |
| 777 | { |
| 778 | /* |
| 779 | * Check our assumption that all lower-case mappings are |
| 780 | * single characters, with a single exception (see below). |
| 781 | * The reason we make this assumption is that it allows us |
| 782 | * to use the lower mapping as the simple case folding, so |
| 783 | * that we don't have to store a separate field (and more |
| 784 | * data) for the simple folding. |
| 785 | * |
| 786 | * U+0130, capital I with dot above, is the only entry in |
| 787 | * the current database that violates this assumption. It |
| 788 | * does so because the regular lower case i already has a |
| 789 | * dot above in its visual rendering, and there's no such |
| 790 | * character as "lower case i with the normal dot above and |
| 791 | * also an accent dot". We need such a character in order |
| 792 | * to retain the information that there's this dot accent |
| 793 | * as opposed to the ordinary 'i' dot. The information is |
| 794 | * important for comparisons, conversions back to upper |
| 795 | * case, etc. Since there's no such character, we have to |
| 796 | * compose one out of an 'i' and a combining dot above |
| 797 | * character. Anyway, U+0130 doesn't have a simple folding |
| 798 | * at all, meaning it can only match itself in folded text |
| 799 | * (i.e., there's no lower-case character it can match). |
| 800 | * We handle this by treating the lower-case mapping for a |
| 801 | * character as its simple folding expansion only when the |
| 802 | * lower-case mapping is one character long; if the mapping |
| 803 | * is longer, we treat the character as having no simple |
| 804 | * folding. |
| 805 | */ |
| 806 | if (ch->lower != 0 && ch->lower[0] != 0 && ch->lower[1] != 0 |
| 807 | && ch != &ctab[0x130]) |
| 808 | printf("#error Lower-case mapping for %04x is longer " |
| 809 | "than one character\n", (int)(ch - ctab)); |
| 810 | |
| 811 | /* |
| 812 | * Now verify our second assumption, that any character |
| 813 | * with a simple single-character lower-case mapping has a |
| 814 | * simple case folding. |
| 815 | */ |
| 816 | if (ch->lower != 0 && ch->lower[0] != 0 && ch->lower[1] == 0 |
| 817 | && !ch->has_simple_folding) |
| 818 | printf("#error %04x has a one-character lower-case " |
| 819 | "mapping but no simple folding\n", |
| 820 | (int)(ch - ctab)); |
| 821 | |
| 822 | int upper = 0, title = 0, lower = 0, fold = 0; |
| 823 | lower = copy_case_exp(caseexp, caseofs, ch->lower); |
| 824 | title = copy_case_exp(caseexp, caseofs, ch->title); |
| 825 | |
| 826 | /* |
| 827 | * in many cases (most, actually), the title and upper |
| 828 | * strings are identical - if so, point them both to the |
| 829 | * same table entry to save space |
| 830 | */ |
| 831 | if (ch->title != 0 && ch->upper != 0 |
| 832 | && wstr_eq(ch->title, ch->upper)) |
| 833 | upper = title; |
| 834 | else |
| 835 | upper = copy_case_exp(caseexp, caseofs, ch->upper); |
| 836 | |
| 837 | /* |
| 838 | * similarly, most case folding strings are identical to |
| 839 | * the lower-case strings |
| 840 | */ |
| 841 | if (ch->fold != 0 && ch->lower != 0 |
| 842 | && wstr_eq(ch->fold, ch->lower)) |
| 843 | fold = lower; |
| 844 | else |
| 845 | fold = copy_case_exp(caseexp, caseofs, ch->fold); |
| 846 | |
| 847 | printf(" { 0x%04x, 0x%04x, 0x%04x, 0x%04x }%c" |
| 848 | " /* %04x %s */\n", |
| 849 | lower, title, upper, fold, |
| 850 | j + 1 < PAGE_SIZE ? ',' : ' ', |
| 851 | ch - ctab, ch->name); |
| 852 | } |
| 853 | printf("};\n\n"); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /* build the case conversion page lookup table */ |
| 858 | data_size += POINTER_BYTES * PAGE_COUNT; |
| 859 | printf("static case_table *case_tab[%d] =\n{\n", PAGE_COUNT); |
| 860 | for (i = 0 ; i < PAGE_COUNT ; ++i) |
| 861 | { |
| 862 | char comma = (i + 1 < PAGE_COUNT ? ',' : ' '); |
| 863 | if (page_has_cases(i)) |
| 864 | printf(" case_pg_%03x%c\n", i, comma); |
| 865 | else |
| 866 | printf(" 0%c\n", comma); |
| 867 | } |
| 868 | printf("};\n\n"); |
| 869 | |
| 870 | /* write the case conversion expansion table */ |
| 871 | data_size += caseofs*2; |
| 872 | printf("static wchar_t case_expansion[] =\n{\n /* 0000 */ "); |
| 873 | for (i = 0 ; i < caseofs ; ++i) |
| 874 | { |
| 875 | printf("0x%04x", caseexp[i]); |
| 876 | if (i + 1 < caseofs) |
| 877 | { |
| 878 | if (caseexp[i] == 0) |
| 879 | printf(",\n /* %04x */ ", i+1); |
| 880 | else |
| 881 | printf(", "); |
| 882 | } |
| 883 | } |
| 884 | printf("\n};\n\n"); |
| 885 | |
| 886 | /* generate the case conversion lookup functions */ |
| 887 | static const char *case_name[] = { "lower", "title", "upper", "fold" }; |
| 888 | for (i = 0 ; i < countof(case_name) ; ++i) |
| 889 | { |
| 890 | printf("const wchar_t *t3_to_%s(wchar_t ch)\n" |
| 891 | "{\n" |
| 892 | " case_table *e = case_tab[ch >> %d];\n" |
| 893 | " if (e == 0)\n" |
| 894 | " return 0;\n" |
| 895 | " int ofs = e[ch & %d].%s;\n" |
| 896 | " return ofs == 0 ? 0 : &case_expansion[ofs];\n" |
| 897 | "}\n\n", |
| 898 | case_name[i], |
| 899 | PAGE_SHIFT, PAGE_MASK, |
| 900 | case_name[i]); |
| 901 | } |
| 902 | |
| 903 | /* generate the simple folding lookup function */ |
| 904 | printf("wchar_t t3_simple_case_fold(wchar_t ch)\n" |
| 905 | "{\n" |
| 906 | " case_table *e = case_tab[ch >> %d];\n" |
| 907 | " if (e == 0)\n" |
| 908 | " return 0;\n" |
| 909 | " int ofs = e[ch & %d].lower;\n" |
| 910 | " return ofs == 0 ? ch :\n" |
| 911 | " case_expansion[ofs + 1] != 0 ? ch :\n" |
| 912 | " case_expansion[ofs];\n" |
| 913 | "}\n\n", |
| 914 | PAGE_SHIFT, PAGE_MASK); |
| 915 | |
| 916 | /* |
| 917 | * Make a note of the total data size. Count the page data plus the |
| 918 | * 4-byte pointers to the tables (the null pointers count in the |
| 919 | * master page table, too, of course). |
| 920 | */ |
| 921 | printf("/* total static data size (%d-bit pointers) = %d bytes */\n", |
| 922 | POINTER_BYTES*4, data_size); |
| 923 | |
| 924 | /* success */ |
| 925 | return 0; |
| 926 | } |
| 927 |