Annotation of src/usr.bin/less/charset.c, Revision 1.12
1.1 etheisen 1: /*
1.10 shadchin 2: * Copyright (C) 1984-2012 Mark Nudelman
1.1 etheisen 3: *
1.5 millert 4: * You may distribute under the terms of either the GNU General Public
5: * License or the Less License, as specified in the README file.
1.1 etheisen 6: *
1.10 shadchin 7: * For more information, see the README file.
1.1 etheisen 8: */
1.11 nicm 9: /*
10: * Modified for use with illumos.
11: * Copyright 2014 Garrett D'Amore <garrett@damore.org>
12: */
1.1 etheisen 13:
14: /*
15: * Functions to define the character set
16: * and do things specific to the character set.
17: */
18:
19: #include "less.h"
20: #include <locale.h>
21: #include <ctype.h>
1.8 shadchin 22: #include <langinfo.h>
1.1 etheisen 23:
1.8 shadchin 24: #include "charset.h"
25:
1.11 nicm 26: int utf_mode = 0;
1.5 millert 27:
1.1 etheisen 28: /*
29: * Predefined character sets,
30: * selected by the LESSCHARSET environment variable.
31: */
32: struct charset {
33: char *name;
1.5 millert 34: int *p_flag;
1.1 etheisen 35: char *desc;
36: } charsets[] = {
1.11 nicm 37: /* BEGIN CSTYLED */
38: { "ascii", NULL, "8bcccbcc18b95.b" },
39: { "utf-8", &utf_mode, "8bcccbcc18b95.b126.bb" },
40: { "iso8859", NULL, "8bcccbcc18b95.33b." },
41: { "latin3", NULL, "8bcccbcc18b95.33b5.b8.b15.b4.b12.b18.b12.b." },
42: { "arabic", NULL, "8bcccbcc18b95.33b.3b.7b2.13b.3b.b26.5b19.b" },
43: { "greek", NULL, "8bcccbcc18b95.33b4.2b4.b3.b35.b44.b" },
44: { "greek2005", NULL, "8bcccbcc18b95.33b14.b35.b44.b" },
45: { "hebrew", NULL, "8bcccbcc18b95.33b.b29.32b28.2b2.b" },
46: { "koi8-r", NULL, "8bcccbcc18b95.b." },
47: { "KOI8-T", NULL, "8bcccbcc18b95.b8.b6.b8.b.b.5b7.3b4.b4.b3.b.b.3b." },
48: { "georgianps", NULL, "8bcccbcc18b95.3b11.4b12.2b." },
49: { "tcvn", NULL, "b..b...bcccbccbbb7.8b95.b48.5b." },
50: { "TIS-620", NULL, "8bcccbcc18b95.b.4b.11b7.8b." },
51: { "next", NULL, "8bcccbcc18b95.bb125.bb" },
52: { "dos", NULL, "8bcccbcc12bc5b95.b." },
53: { "windows-1251", NULL, "8bcccbcc12bc5b95.b24.b." },
54: { "windows-1252", NULL, "8bcccbcc12bc5b95.b.b11.b.2b12.b." },
55: { "windows-1255", NULL, "8bcccbcc12bc5b95.b.b8.b.5b9.b.4b." },
56: { "ebcdic", NULL, "5bc6bcc7bcc41b.9b7.9b5.b..8b6.10b6.b9.7b9.8b8.17b3.3b9.7b9.8b8.6b10.b.b.b." },
57: { "IBM-1047", NULL, "4cbcbc3b9cbccbccbb4c6bcc5b3cbbc4bc4bccbc191.b" },
1.5 millert 58: { NULL, NULL, NULL }
1.11 nicm 59: /* END CSTYLED */
1.5 millert 60: };
61:
1.8 shadchin 62: /*
63: * Support "locale charmap"/nl_langinfo(CODESET) values, as well as others.
64: */
1.5 millert 65: struct cs_alias {
66: char *name;
67: char *oname;
68: } cs_aliases[] = {
1.8 shadchin 69: { "UTF-8", "utf-8" },
70: { "ANSI_X3.4-1968", "ascii" },
71: { "US-ASCII", "ascii" },
1.11 nicm 72: { "646", "ascii" },
73: { "C", "ascii" },
1.8 shadchin 74: { "latin1", "iso8859" },
75: { "ISO-8859-1", "iso8859" },
76: { "latin9", "iso8859" },
77: { "ISO-8859-15", "iso8859" },
78: { "latin2", "iso8859" },
79: { "ISO-8859-2", "iso8859" },
80: { "ISO-8859-3", "latin3" },
81: { "latin4", "iso8859" },
82: { "ISO-8859-4", "iso8859" },
83: { "cyrillic", "iso8859" },
84: { "ISO-8859-5", "iso8859" },
85: { "ISO-8859-6", "arabic" },
86: { "ISO-8859-7", "greek" },
87: { "IBM9005", "greek2005" },
88: { "ISO-8859-8", "hebrew" },
89: { "latin5", "iso8859" },
90: { "ISO-8859-9", "iso8859" },
91: { "latin6", "iso8859" },
92: { "ISO-8859-10", "iso8859" },
93: { "latin7", "iso8859" },
94: { "ISO-8859-13", "iso8859" },
95: { "latin8", "iso8859" },
96: { "ISO-8859-14", "iso8859" },
97: { "latin10", "iso8859" },
98: { "ISO-8859-16", "iso8859" },
99: { "IBM437", "dos" },
100: { "EBCDIC-US", "ebcdic" },
101: { "IBM1047", "IBM-1047" },
102: { "KOI8-R", "koi8-r" },
103: { "KOI8-U", "koi8-r" },
104: { "GEORGIAN-PS", "georgianps" },
1.12 ! deraadt 105: { "TCVN5712-1", "tcvn" },
1.8 shadchin 106: { "NEXTSTEP", "next" },
107: { "windows", "windows-1252" }, /* backward compatibility */
108: { "CP1251", "windows-1251" },
109: { "CP1252", "windows-1252" },
110: { "CP1255", "windows-1255" },
1.5 millert 111: { NULL, NULL }
1.1 etheisen 112: };
113:
114: #define IS_BINARY_CHAR 01
115: #define IS_CONTROL_CHAR 02
116:
117: static char chardef[256];
1.11 nicm 118: static const char *binfmt = NULL;
119: static const char *utfbinfmt = NULL;
120: int binattr = AT_STANDOUT;
1.1 etheisen 121:
122:
123: /*
124: * Define a charset, given a description string.
125: * The string consists of 256 letters,
126: * one for each character in the charset.
127: * If the string is shorter than 256 letters, missing letters
128: * are taken to be identical to the last one.
1.11 nicm 129: * A decimal number followed by a letter is taken to be a
1.1 etheisen 130: * repetition of the letter.
131: *
132: * Each letter is one of:
133: * . normal character
134: * b binary character
135: * c control character
136: */
1.11 nicm 137: static void
138: ichardef(char *s)
1.1 etheisen 139: {
1.11 nicm 140: char *cp;
141: int n;
142: char v;
1.1 etheisen 143:
144: n = 0;
145: v = 0;
146: cp = chardef;
1.11 nicm 147: while (*s != '\0') {
148: switch (*s++) {
1.1 etheisen 149: case '.':
150: v = 0;
151: break;
152: case 'c':
153: v = IS_CONTROL_CHAR;
154: break;
155: case 'b':
156: v = IS_BINARY_CHAR|IS_CONTROL_CHAR;
157: break;
158:
159: case '0': case '1': case '2': case '3': case '4':
160: case '5': case '6': case '7': case '8': case '9':
161: n = (10 * n) + (s[-1] - '0');
162: continue;
163:
164: default:
165: error("invalid chardef", NULL_PARG);
166: quit(QUIT_ERROR);
167: /*NOTREACHED*/
168: }
169:
1.11 nicm 170: do {
171: if (cp >= chardef + sizeof (chardef)) {
1.1 etheisen 172: error("chardef longer than 256", NULL_PARG);
173: quit(QUIT_ERROR);
174: /*NOTREACHED*/
175: }
176: *cp++ = v;
177: } while (--n > 0);
178: n = 0;
179: }
180:
1.11 nicm 181: while (cp < chardef + sizeof (chardef))
1.1 etheisen 182: *cp++ = v;
183: }
184:
185: /*
186: * Define a charset, given a charset name.
187: * The valid charset names are listed in the "charsets" array.
188: */
1.11 nicm 189: static int
190: icharset(char *name, int no_error)
1.1 etheisen 191: {
1.11 nicm 192: struct charset *p;
193: struct cs_alias *a;
1.1 etheisen 194:
195: if (name == NULL || *name == '\0')
196: return (0);
197:
1.5 millert 198: /* First see if the name is an alias. */
1.11 nicm 199: for (a = cs_aliases; a->name != NULL; a++) {
200: if (strcmp(name, a->name) == 0) {
1.5 millert 201: name = a->oname;
202: break;
203: }
204: }
205:
1.11 nicm 206: for (p = charsets; p->name != NULL; p++) {
207: if (strcmp(name, p->name) == 0) {
1.1 etheisen 208: ichardef(p->desc);
1.5 millert 209: if (p->p_flag != NULL)
210: *(p->p_flag) = 1;
1.1 etheisen 211: return (1);
212: }
213: }
214:
1.8 shadchin 215: if (!no_error) {
216: error("invalid charset name", NULL_PARG);
217: quit(QUIT_ERROR);
218: }
1.5 millert 219: return (0);
1.1 etheisen 220: }
221:
222: /*
223: * Define a charset, given a locale name.
224: */
1.11 nicm 225: static void
226: ilocale(void)
1.1 etheisen 227: {
1.11 nicm 228: int c;
1.1 etheisen 229:
1.11 nicm 230: for (c = 0; c < sizeof (chardef); c++) {
1.1 etheisen 231: if (isprint(c))
232: chardef[c] = 0;
233: else if (iscntrl(c))
234: chardef[c] = IS_CONTROL_CHAR;
235: else
236: chardef[c] = IS_BINARY_CHAR|IS_CONTROL_CHAR;
237: }
238: }
1.11 nicm 239:
240: static int
241: checkfmt(const char *s)
242: {
243: char c;
244: int seen = 0;
245:
246: /* %[<flags>][<width>][.<prec>][<type>]<fmt> */
247:
248: if (*s == '*') { /* skip leading attribute if there */
249: s++;
250: if (strchr("dksu", *s) == NULL) {
251: return (-1);
252: }
253: s++;
254: }
255:
256: while ((c = *s++) != 0) {
257: if (!isascii(c) || !isprint(c)) {
258: return (-1);
259: }
260: if (c != '%') {
261: continue;
262: }
263: if (*s == '%') { /* % escaped with second % */
264: s++;
265: continue;
266: }
267: if (seen) {
268: return (-1); /* 2nd % format item! */
269: }
270: while (strchr(" '+-0#", *s) != NULL) { /* skip flags */
271: s++;
272: }
273: while (isdigit(*s)) { /* skip width */
274: s++;
275: }
276: if (*s == '.') { /* skip precision */
277: s++;
278: while (isdigit(*s)) {
279: s++;
280: }
281: }
282: /* type width specifications, only "l", "h", and "hh" valid */
283: if (*s == 'l') {
284: s++;
285: } else if (*s == 'h') {
286: s++;
287: if (*s == 'h')
288: s++;
289: }
290:
291: if (strchr("cCdiouxX", *s) == NULL) {
292: /* bad or evil format character (%s, %n, etc.) */
293: return (-1);
294: }
295:
296: seen = 1;
297: }
298:
299: return (0);
300: }
1.1 etheisen 301:
302: /*
1.8 shadchin 303: * Define the printing format for control (or binary utf) chars.
1.1 etheisen 304: */
1.11 nicm 305: static void
306: setbinfmt(char *e, const char **fmtvarptr, const char *default_fmt)
1.1 etheisen 307: {
1.11 nicm 308: const char *s;
309:
310: if (((s = lgetenv(e)) == NULL) || (*s == 0)) {
311: s = default_fmt;
312: goto attr;
313: }
314:
315: if (s != NULL && *s != 0) {
316: if (checkfmt(s) < 0) {
317: s = default_fmt;
318: goto attr;
1.8 shadchin 319: }
320: }
321:
1.1 etheisen 322: /*
323: * Select the attributes if it starts with "*".
324: */
1.11 nicm 325: attr:
326: if (*s == '*') {
327: switch (s[1]) {
328: case 'd': binattr = AT_BOLD; break;
329: case 'k': binattr = AT_BLINK; break;
330: case 's': binattr = AT_STANDOUT; break;
1.1 etheisen 331: case 'u': binattr = AT_UNDERLINE; break;
1.11 nicm 332: default: binattr = AT_NORMAL; break;
1.1 etheisen 333: }
334: s += 2;
335: }
1.8 shadchin 336: *fmtvarptr = s;
1.1 etheisen 337: }
338:
339: /*
1.8 shadchin 340: *
1.1 etheisen 341: */
1.11 nicm 342: static void
343: set_charset(void)
1.1 etheisen 344: {
1.8 shadchin 345: char *s;
1.1 etheisen 346:
347: /*
348: * See if environment variable LESSCHARSET is defined.
349: */
1.5 millert 350: s = lgetenv("LESSCHARSET");
1.8 shadchin 351: if (icharset(s, 0))
1.1 etheisen 352: return;
1.8 shadchin 353:
1.1 etheisen 354: /*
355: * LESSCHARSET is not defined: try LESSCHARDEF.
356: */
1.5 millert 357: s = lgetenv("LESSCHARDEF");
1.11 nicm 358: if (s != NULL && *s != '\0') {
1.1 etheisen 359: ichardef(s);
360: return;
361: }
1.5 millert 362:
1.8 shadchin 363: /*
364: * Try using the codeset name as the charset name.
365: */
366: s = nl_langinfo(CODESET);
367: if (icharset(s, 1))
368: return;
369:
1.1 etheisen 370: /*
1.8 shadchin 371: * Get character definitions from locale functions,
372: * rather than from predefined charset entry.
1.1 etheisen 373: */
374: ilocale();
375: }
376:
377: /*
1.8 shadchin 378: * Initialize charset data structures.
379: */
1.11 nicm 380: void
381: init_charset(void)
1.8 shadchin 382: {
383: setlocale(LC_ALL, "");
384:
385: set_charset();
386:
1.11 nicm 387: setbinfmt("LESSBINFMT", &binfmt, "*s<%02X>");
388: setbinfmt("LESSUTFBINFMT", &utfbinfmt, "<U+%04lX>");
1.8 shadchin 389: }
390:
391: /*
1.1 etheisen 392: * Is a given character a "binary" character?
393: */
1.11 nicm 394: int
395: binary_char(LWCHAR c)
1.1 etheisen 396: {
1.8 shadchin 397: if (utf_mode)
398: return (is_ubin_char(c));
1.1 etheisen 399: c &= 0377;
400: return (chardef[c] & IS_BINARY_CHAR);
401: }
402:
403: /*
404: * Is a given character a "control" character?
405: */
1.11 nicm 406: int
407: control_char(LWCHAR c)
1.1 etheisen 408: {
409: c &= 0377;
410: return (chardef[c] & IS_CONTROL_CHAR);
411: }
412:
413: /*
414: * Return the printable form of a character.
415: * For example, in the "ascii" charset '\3' is printed as "^C".
416: */
1.11 nicm 417: char *
418: prchar(LWCHAR c)
1.1 etheisen 419: {
1.8 shadchin 420: /* {{ This buffer can be overrun if LESSBINFMT is a long string. }} */
421: static char buf[32];
1.1 etheisen 422:
423: c &= 0377;
1.8 shadchin 424: if ((c < 128 || !utf_mode) && !control_char(c))
1.11 nicm 425: (void) snprintf(buf, sizeof (buf), "%c", (int)c);
1.1 etheisen 426: else if (c == ESC)
1.11 nicm 427: (void) strlcpy(buf, "ESC", sizeof (buf));
428: else if (c < 128 && !control_char(c ^ 0100))
429: (void) snprintf(buf, sizeof (buf), "^%c", (int)(c ^ 0100));
1.1 etheisen 430: else
1.11 nicm 431: (void) snprintf(buf, sizeof (buf), binfmt, c);
1.8 shadchin 432: return (buf);
433: }
434:
435: /*
436: * Return the printable form of a UTF-8 character.
437: */
1.11 nicm 438: char *
439: prutfchar(LWCHAR ch)
1.8 shadchin 440: {
441: static char buf[32];
442:
1.11 nicm 443: if (ch == ESC) {
444: (void) strlcpy(buf, "ESC", sizeof (buf));
445: } else if (ch < 128 && control_char(ch)) {
1.8 shadchin 446: if (!control_char(ch ^ 0100))
1.11 nicm 447: (void) snprintf(buf, sizeof (buf), "^%c",
448: ((char)ch) ^ 0100);
1.8 shadchin 449: else
1.11 nicm 450: (void) snprintf(buf, sizeof (buf), binfmt, (char)ch);
451: } else if (is_ubin_char(ch)) {
452: (void) snprintf(buf, sizeof (buf), utfbinfmt, ch);
453: } else {
1.8 shadchin 454: int len;
1.11 nicm 455: if (ch >= 0x80000000) {
1.8 shadchin 456: len = 3;
457: ch = 0xFFFD;
1.11 nicm 458: } else {
459: len = (ch < 0x80) ? 1
460: : (ch < 0x800) ? 2
461: : (ch < 0x10000) ? 3
462: : (ch < 0x200000) ? 4
463: : (ch < 0x4000000) ? 5
464: : 6;
1.8 shadchin 465: }
466: buf[len] = '\0';
1.11 nicm 467: if (len == 1) {
468: *buf = (char)ch;
469: } else {
1.8 shadchin 470: *buf = ((1 << len) - 1) << (8 - len);
1.11 nicm 471: while (--len > 0) {
472: buf[len] = (char)(0x80 | (ch & 0x3F));
1.8 shadchin 473: ch >>= 6;
474: }
475: *buf |= ch;
476: }
477: }
1.1 etheisen 478: return (buf);
479: }
1.6 millert 480:
1.8 shadchin 481: /*
482: * Get the length of a UTF-8 character in bytes.
483: */
1.11 nicm 484: int
485: utf_len(char ch)
1.8 shadchin 486: {
487: if ((ch & 0x80) == 0)
1.11 nicm 488: return (1);
1.8 shadchin 489: if ((ch & 0xE0) == 0xC0)
1.11 nicm 490: return (2);
1.8 shadchin 491: if ((ch & 0xF0) == 0xE0)
1.11 nicm 492: return (3);
1.8 shadchin 493: if ((ch & 0xF8) == 0xF0)
1.11 nicm 494: return (4);
1.8 shadchin 495: if ((ch & 0xFC) == 0xF8)
1.11 nicm 496: return (5);
1.8 shadchin 497: if ((ch & 0xFE) == 0xFC)
1.11 nicm 498: return (6);
1.8 shadchin 499: /* Invalid UTF-8 encoding. */
1.11 nicm 500: return (1);
1.8 shadchin 501: }
502:
503: /*
504: * Is a UTF-8 character well-formed?
505: */
1.11 nicm 506: int
507: is_utf8_well_formed(const char *s)
1.8 shadchin 508: {
509: int i;
510: int len;
511:
512: if (IS_UTF8_INVALID(s[0]))
513: return (0);
514:
1.11 nicm 515: len = utf_len((char)s[0]);
1.8 shadchin 516: if (len == 1)
517: return (1);
1.11 nicm 518: if (len == 2) {
519: if ((unsigned char)(s[0]) < 0xC2)
520: return (0);
521: } else {
1.8 shadchin 522: unsigned char mask;
523: mask = (~((1 << (8-len)) - 1)) & 0xFF;
524: if (s[0] == mask && (s[1] & mask) == 0x80)
525: return (0);
526: }
527:
528: for (i = 1; i < len; i++)
529: if (!IS_UTF8_TRAIL(s[i]))
530: return (0);
531: return (1);
532: }
533:
534: /*
535: * Get the value of a UTF-8 character.
536: */
1.11 nicm 537: LWCHAR
538: get_wchar(const char *p)
1.8 shadchin 539: {
1.11 nicm 540: switch (utf_len(p[0])) {
1.8 shadchin 541: case 1:
542: default:
543: /* 0xxxxxxx */
544: return (LWCHAR)
1.11 nicm 545: (p[0] & 0xFF);
1.8 shadchin 546: case 2:
547: /* 110xxxxx 10xxxxxx */
548: return (LWCHAR) (
1.11 nicm 549: ((p[0] & 0x1F) << 6) |
550: (p[1] & 0x3F));
1.8 shadchin 551: case 3:
552: /* 1110xxxx 10xxxxxx 10xxxxxx */
553: return (LWCHAR) (
1.11 nicm 554: ((p[0] & 0x0F) << 12) |
555: ((p[1] & 0x3F) << 6) |
556: (p[2] & 0x3F));
1.8 shadchin 557: case 4:
558: /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
559: return (LWCHAR) (
1.11 nicm 560: ((p[0] & 0x07) << 18) |
561: ((p[1] & 0x3F) << 12) |
562: ((p[2] & 0x3F) << 6) |
563: (p[3] & 0x3F));
1.8 shadchin 564: case 5:
565: /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
566: return (LWCHAR) (
1.11 nicm 567: ((p[0] & 0x03) << 24) |
568: ((p[1] & 0x3F) << 18) |
569: ((p[2] & 0x3F) << 12) |
570: ((p[3] & 0x3F) << 6) |
571: (p[4] & 0x3F));
1.8 shadchin 572: case 6:
573: /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
574: return (LWCHAR) (
1.11 nicm 575: ((p[0] & 0x01) << 30) |
576: ((p[1] & 0x3F) << 24) |
577: ((p[2] & 0x3F) << 18) |
578: ((p[3] & 0x3F) << 12) |
579: ((p[4] & 0x3F) << 6) |
580: (p[5] & 0x3F));
1.8 shadchin 581: }
582: }
583:
584: /*
585: * Store a character into a UTF-8 string.
586: */
1.11 nicm 587: void
588: put_wchar(char **pp, LWCHAR ch)
1.8 shadchin 589: {
1.11 nicm 590: if (!utf_mode || ch < 0x80) {
1.8 shadchin 591: /* 0xxxxxxx */
1.11 nicm 592: *(*pp)++ = (char)ch;
593: } else if (ch < 0x800) {
1.8 shadchin 594: /* 110xxxxx 10xxxxxx */
1.11 nicm 595: *(*pp)++ = (char)(0xC0 | ((ch >> 6) & 0x1F));
596: *(*pp)++ = (char)(0x80 | (ch & 0x3F));
597: } else if (ch < 0x10000) {
1.8 shadchin 598: /* 1110xxxx 10xxxxxx 10xxxxxx */
1.11 nicm 599: *(*pp)++ = (char)(0xE0 | ((ch >> 12) & 0x0F));
600: *(*pp)++ = (char)(0x80 | ((ch >> 6) & 0x3F));
601: *(*pp)++ = (char)(0x80 | (ch & 0x3F));
602: } else if (ch < 0x200000) {
1.8 shadchin 603: /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
1.11 nicm 604: *(*pp)++ = (char)(0xF0 | ((ch >> 18) & 0x07));
605: *(*pp)++ = (char)(0x80 | ((ch >> 12) & 0x3F));
606: *(*pp)++ = (char)(0x80 | ((ch >> 6) & 0x3F));
607: *(*pp)++ = (char)(0x80 | (ch & 0x3F));
608: } else if (ch < 0x4000000) {
1.8 shadchin 609: /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
1.11 nicm 610: *(*pp)++ = (char)(0xF0 | ((ch >> 24) & 0x03));
611: *(*pp)++ = (char)(0x80 | ((ch >> 18) & 0x3F));
612: *(*pp)++ = (char)(0x80 | ((ch >> 12) & 0x3F));
613: *(*pp)++ = (char)(0x80 | ((ch >> 6) & 0x3F));
614: *(*pp)++ = (char)(0x80 | (ch & 0x3F));
615: } else {
1.8 shadchin 616: /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
1.11 nicm 617: *(*pp)++ = (char)(0xF0 | ((ch >> 30) & 0x01));
618: *(*pp)++ = (char)(0x80 | ((ch >> 24) & 0x3F));
619: *(*pp)++ = (char)(0x80 | ((ch >> 18) & 0x3F));
620: *(*pp)++ = (char)(0x80 | ((ch >> 12) & 0x3F));
621: *(*pp)++ = (char)(0x80 | ((ch >> 6) & 0x3F));
622: *(*pp)++ = (char)(0x80 | (ch & 0x3F));
1.8 shadchin 623: }
624: }
625:
626: /*
627: * Step forward or backward one character in a string.
628: */
1.11 nicm 629: LWCHAR
630: step_char(char **pp, int dir, char *limit)
1.8 shadchin 631: {
632: LWCHAR ch;
633: int len;
634: char *p = *pp;
635:
1.11 nicm 636: if (!utf_mode) {
1.8 shadchin 637: /* It's easy if chars are one byte. */
638: if (dir > 0)
639: ch = (LWCHAR) ((p < limit) ? *p++ : 0);
640: else
641: ch = (LWCHAR) ((p > limit) ? *--p : 0);
1.11 nicm 642: } else if (dir > 0) {
1.8 shadchin 643: len = utf_len(*p);
1.11 nicm 644: if (p + len > limit) {
1.8 shadchin 645: ch = 0;
646: p = limit;
1.11 nicm 647: } else {
1.8 shadchin 648: ch = get_wchar(p);
649: p += len;
650: }
1.11 nicm 651: } else {
1.8 shadchin 652: while (p > limit && IS_UTF8_TRAIL(p[-1]))
653: p--;
654: if (p > limit)
655: ch = get_wchar(--p);
656: else
657: ch = 0;
658: }
659: *pp = p;
1.11 nicm 660: return (ch);
1.8 shadchin 661: }
662:
663: /*
664: * Unicode characters data
665: */
666: struct wchar_range { LWCHAR first, last; };
667:
668: /*
669: * Characters with general category values
670: * Mn: Mark, Nonspacing
671: * Me: Mark, Enclosing
672: * Last synched with
673: * <http://www.unicode.org/Public/5.0.0/ucd/UnicodeData-5.0.0d7.txt>
674: * dated 2005-11-30T00:58:48Z
675: */
676: static struct wchar_range comp_table[] = {
677: { 0x0300, 0x036F} /* Mn */, { 0x0483, 0x0486} /* Mn */,
678: { 0x0488, 0x0489} /* Me */,
679: { 0x0591, 0x05BD} /* Mn */, { 0x05BF, 0x05BF} /* Mn */,
680: { 0x05C1, 0x05C2} /* Mn */, { 0x05C4, 0x05C5} /* Mn */,
681: { 0x05C7, 0x05C7} /* Mn */, { 0x0610, 0x0615} /* Mn */,
682: { 0x064B, 0x065E} /* Mn */, { 0x0670, 0x0670} /* Mn */,
683: { 0x06D6, 0x06DC} /* Mn */,
684: { 0x06DE, 0x06DE} /* Me */,
685: { 0x06DF, 0x06E4} /* Mn */, { 0x06E7, 0x06E8} /* Mn */,
686: { 0x06EA, 0x06ED} /* Mn */, { 0x0711, 0x0711} /* Mn */,
687: { 0x0730, 0x074A} /* Mn */, { 0x07A6, 0x07B0} /* Mn */,
688: { 0x07EB, 0x07F3} /* Mn */, { 0x0901, 0x0902} /* Mn */,
689: { 0x093C, 0x093C} /* Mn */, { 0x0941, 0x0948} /* Mn */,
690: { 0x094D, 0x094D} /* Mn */, { 0x0951, 0x0954} /* Mn */,
691: { 0x0962, 0x0963} /* Mn */, { 0x0981, 0x0981} /* Mn */,
692: { 0x09BC, 0x09BC} /* Mn */, { 0x09C1, 0x09C4} /* Mn */,
693: { 0x09CD, 0x09CD} /* Mn */, { 0x09E2, 0x09E3} /* Mn */,
694: { 0x0A01, 0x0A02} /* Mn */, { 0x0A3C, 0x0A3C} /* Mn */,
695: { 0x0A41, 0x0A42} /* Mn */, { 0x0A47, 0x0A48} /* Mn */,
696: { 0x0A4B, 0x0A4D} /* Mn */, { 0x0A70, 0x0A71} /* Mn */,
697: { 0x0A81, 0x0A82} /* Mn */, { 0x0ABC, 0x0ABC} /* Mn */,
698: { 0x0AC1, 0x0AC5} /* Mn */, { 0x0AC7, 0x0AC8} /* Mn */,
699: { 0x0ACD, 0x0ACD} /* Mn */, { 0x0AE2, 0x0AE3} /* Mn */,
700: { 0x0B01, 0x0B01} /* Mn */, { 0x0B3C, 0x0B3C} /* Mn */,
701: { 0x0B3F, 0x0B3F} /* Mn */, { 0x0B41, 0x0B43} /* Mn */,
702: { 0x0B4D, 0x0B4D} /* Mn */, { 0x0B56, 0x0B56} /* Mn */,
703: { 0x0B82, 0x0B82} /* Mn */, { 0x0BC0, 0x0BC0} /* Mn */,
704: { 0x0BCD, 0x0BCD} /* Mn */, { 0x0C3E, 0x0C40} /* Mn */,
705: { 0x0C46, 0x0C48} /* Mn */, { 0x0C4A, 0x0C4D} /* Mn */,
706: { 0x0C55, 0x0C56} /* Mn */, { 0x0CBC, 0x0CBC} /* Mn */,
707: { 0x0CBF, 0x0CBF} /* Mn */, { 0x0CC6, 0x0CC6} /* Mn */,
708: { 0x0CCC, 0x0CCD} /* Mn */, { 0x0CE2, 0x0CE3} /* Mn */,
709: { 0x0D41, 0x0D43} /* Mn */, { 0x0D4D, 0x0D4D} /* Mn */,
710: { 0x0DCA, 0x0DCA} /* Mn */, { 0x0DD2, 0x0DD4} /* Mn */,
711: { 0x0DD6, 0x0DD6} /* Mn */, { 0x0E31, 0x0E31} /* Mn */,
712: { 0x0E34, 0x0E3A} /* Mn */, { 0x0E47, 0x0E4E} /* Mn */,
713: { 0x0EB1, 0x0EB1} /* Mn */, { 0x0EB4, 0x0EB9} /* Mn */,
714: { 0x0EBB, 0x0EBC} /* Mn */, { 0x0EC8, 0x0ECD} /* Mn */,
715: { 0x0F18, 0x0F19} /* Mn */, { 0x0F35, 0x0F35} /* Mn */,
716: { 0x0F37, 0x0F37} /* Mn */, { 0x0F39, 0x0F39} /* Mn */,
717: { 0x0F71, 0x0F7E} /* Mn */, { 0x0F80, 0x0F84} /* Mn */,
718: { 0x0F86, 0x0F87} /* Mn */, { 0x0F90, 0x0F97} /* Mn */,
719: { 0x0F99, 0x0FBC} /* Mn */, { 0x0FC6, 0x0FC6} /* Mn */,
720: { 0x102D, 0x1030} /* Mn */, { 0x1032, 0x1032} /* Mn */,
721: { 0x1036, 0x1037} /* Mn */, { 0x1039, 0x1039} /* Mn */,
722: { 0x1058, 0x1059} /* Mn */, { 0x135F, 0x135F} /* Mn */,
723: { 0x1712, 0x1714} /* Mn */, { 0x1732, 0x1734} /* Mn */,
724: { 0x1752, 0x1753} /* Mn */, { 0x1772, 0x1773} /* Mn */,
725: { 0x17B7, 0x17BD} /* Mn */, { 0x17C6, 0x17C6} /* Mn */,
726: { 0x17C9, 0x17D3} /* Mn */, { 0x17DD, 0x17DD} /* Mn */,
727: { 0x180B, 0x180D} /* Mn */, { 0x18A9, 0x18A9} /* Mn */,
728: { 0x1920, 0x1922} /* Mn */, { 0x1927, 0x1928} /* Mn */,
729: { 0x1932, 0x1932} /* Mn */, { 0x1939, 0x193B} /* Mn */,
730: { 0x1A17, 0x1A18} /* Mn */, { 0x1B00, 0x1B03} /* Mn */,
731: { 0x1B34, 0x1B34} /* Mn */, { 0x1B36, 0x1B3A} /* Mn */,
732: { 0x1B3C, 0x1B3C} /* Mn */, { 0x1B42, 0x1B42} /* Mn */,
733: { 0x1B6B, 0x1B73} /* Mn */, { 0x1DC0, 0x1DCA} /* Mn */,
734: { 0x1DFE, 0x1DFF} /* Mn */, { 0x20D0, 0x20DC} /* Mn */,
735: { 0x20DD, 0x20E0} /* Me */,
736: { 0x20E1, 0x20E1} /* Mn */,
737: { 0x20E2, 0x20E4} /* Me */,
738: { 0x20E5, 0x20EF} /* Mn */, { 0x302A, 0x302F} /* Mn */,
739: { 0x3099, 0x309A} /* Mn */, { 0xA806, 0xA806} /* Mn */,
740: { 0xA80B, 0xA80B} /* Mn */, { 0xA825, 0xA826} /* Mn */,
741: { 0xFB1E, 0xFB1E} /* Mn */, { 0xFE00, 0xFE0F} /* Mn */,
742: { 0xFE20, 0xFE23} /* Mn */, { 0x10A01, 0x10A03} /* Mn */,
743: { 0x10A05, 0x10A06} /* Mn */, { 0x10A0C, 0x10A0F} /* Mn */,
744: { 0x10A38, 0x10A3A} /* Mn */, { 0x10A3F, 0x10A3F} /* Mn */,
745: { 0x1D167, 0x1D169} /* Mn */, { 0x1D17B, 0x1D182} /* Mn */,
746: { 0x1D185, 0x1D18B} /* Mn */, { 0x1D1AA, 0x1D1AD} /* Mn */,
747: { 0x1D242, 0x1D244} /* Mn */, { 0xE0100, 0xE01EF} /* Mn */,
748: };
749:
750: /*
751: * Special pairs, not ranges.
752: */
753: static struct wchar_range comb_table[] = {
1.11 nicm 754: {0x0644, 0x0622}, {0x0644, 0x0623}, {0x0644, 0x0625}, {0x0644, 0x0627},
1.8 shadchin 755: };
756:
757: /*
758: * Characters with general category values
759: * Cc: Other, Control
760: * Cf: Other, Format
761: * Cs: Other, Surrogate
762: * Co: Other, Private Use
763: * Cn: Other, Not Assigned
764: * Zl: Separator, Line
765: * Zp: Separator, Paragraph
766: * Last synched with
767: * <http://www.unicode.org/Public/5.0.0/ucd/UnicodeData-5.0.0d7.txt>
768: * dated 2005-11-30T00:58:48Z
769: */
770: static struct wchar_range ubin_table[] = {
1.11 nicm 771: { 0x0000, 0x0007} /* Cc */,
772: { 0x000B, 0x000C} /* Cc */,
773: { 0x000E, 0x001A} /* Cc */,
774: { 0x001C, 0x001F} /* Cc */,
1.8 shadchin 775: { 0x007F, 0x009F} /* Cc */,
776: #if 0
777: { 0x00AD, 0x00AD} /* Cf */,
778: #endif
779: { 0x0370, 0x0373} /* Cn */, { 0x0376, 0x0379} /* Cn */,
780: { 0x037F, 0x0383} /* Cn */, { 0x038B, 0x038B} /* Cn */,
781: { 0x038D, 0x038D} /* Cn */, { 0x03A2, 0x03A2} /* Cn */,
782: { 0x03CF, 0x03CF} /* Cn */, { 0x0487, 0x0487} /* Cn */,
783: { 0x0514, 0x0530} /* Cn */, { 0x0557, 0x0558} /* Cn */,
784: { 0x0560, 0x0560} /* Cn */, { 0x0588, 0x0588} /* Cn */,
785: { 0x058B, 0x0590} /* Cn */, { 0x05C8, 0x05CF} /* Cn */,
786: { 0x05EB, 0x05EF} /* Cn */, { 0x05F5, 0x05FF} /* Cn */,
787: #if 0
788: { 0x0600, 0x0603} /* Cf */,
789: #endif
790: { 0x0604, 0x060A} /* Cn */, { 0x0616, 0x061A} /* Cn */,
791: { 0x061C, 0x061D} /* Cn */, { 0x0620, 0x0620} /* Cn */,
792: { 0x063B, 0x063F} /* Cn */, { 0x065F, 0x065F} /* Cn */,
793: #if 0
794: { 0x06DD, 0x06DD} /* Cf */,
795: #endif
796: { 0x070E, 0x070E} /* Cn */,
797: #if 0
798: { 0x070F, 0x070F} /* Cf */,
799: #endif
800: { 0x074B, 0x074C} /* Cn */, { 0x076E, 0x077F} /* Cn */,
801: { 0x07B2, 0x07BF} /* Cn */, { 0x07FB, 0x0900} /* Cn */,
802: { 0x093A, 0x093B} /* Cn */, { 0x094E, 0x094F} /* Cn */,
803: { 0x0955, 0x0957} /* Cn */, { 0x0971, 0x097A} /* Cn */,
804: { 0x0980, 0x0980} /* Cn */, { 0x0984, 0x0984} /* Cn */,
805: { 0x098D, 0x098E} /* Cn */, { 0x0991, 0x0992} /* Cn */,
806: { 0x09A9, 0x09A9} /* Cn */, { 0x09B1, 0x09B1} /* Cn */,
807: { 0x09B3, 0x09B5} /* Cn */, { 0x09BA, 0x09BB} /* Cn */,
808: { 0x09C5, 0x09C6} /* Cn */, { 0x09C9, 0x09CA} /* Cn */,
809: { 0x09CF, 0x09D6} /* Cn */, { 0x09D8, 0x09DB} /* Cn */,
810: { 0x09DE, 0x09DE} /* Cn */, { 0x09E4, 0x09E5} /* Cn */,
811: { 0x09FB, 0x0A00} /* Cn */, { 0x0A04, 0x0A04} /* Cn */,
812: { 0x0A0B, 0x0A0E} /* Cn */, { 0x0A11, 0x0A12} /* Cn */,
813: { 0x0A29, 0x0A29} /* Cn */, { 0x0A31, 0x0A31} /* Cn */,
814: { 0x0A34, 0x0A34} /* Cn */, { 0x0A37, 0x0A37} /* Cn */,
815: { 0x0A3A, 0x0A3B} /* Cn */, { 0x0A3D, 0x0A3D} /* Cn */,
816: { 0x0A43, 0x0A46} /* Cn */, { 0x0A49, 0x0A4A} /* Cn */,
817: { 0x0A4E, 0x0A58} /* Cn */, { 0x0A5D, 0x0A5D} /* Cn */,
818: { 0x0A5F, 0x0A65} /* Cn */, { 0x0A75, 0x0A80} /* Cn */,
819: { 0x0A84, 0x0A84} /* Cn */, { 0x0A8E, 0x0A8E} /* Cn */,
820: { 0x0A92, 0x0A92} /* Cn */, { 0x0AA9, 0x0AA9} /* Cn */,
821: { 0x0AB1, 0x0AB1} /* Cn */, { 0x0AB4, 0x0AB4} /* Cn */,
822: { 0x0ABA, 0x0ABB} /* Cn */, { 0x0AC6, 0x0AC6} /* Cn */,
823: { 0x0ACA, 0x0ACA} /* Cn */, { 0x0ACE, 0x0ACF} /* Cn */,
824: { 0x0AD1, 0x0ADF} /* Cn */, { 0x0AE4, 0x0AE5} /* Cn */,
825: { 0x0AF0, 0x0AF0} /* Cn */, { 0x0AF2, 0x0B00} /* Cn */,
826: { 0x0B04, 0x0B04} /* Cn */, { 0x0B0D, 0x0B0E} /* Cn */,
827: { 0x0B11, 0x0B12} /* Cn */, { 0x0B29, 0x0B29} /* Cn */,
828: { 0x0B31, 0x0B31} /* Cn */, { 0x0B34, 0x0B34} /* Cn */,
829: { 0x0B3A, 0x0B3B} /* Cn */, { 0x0B44, 0x0B46} /* Cn */,
830: { 0x0B49, 0x0B4A} /* Cn */, { 0x0B4E, 0x0B55} /* Cn */,
831: { 0x0B58, 0x0B5B} /* Cn */, { 0x0B5E, 0x0B5E} /* Cn */,
832: { 0x0B62, 0x0B65} /* Cn */, { 0x0B72, 0x0B81} /* Cn */,
833: { 0x0B84, 0x0B84} /* Cn */, { 0x0B8B, 0x0B8D} /* Cn */,
834: { 0x0B91, 0x0B91} /* Cn */, { 0x0B96, 0x0B98} /* Cn */,
835: { 0x0B9B, 0x0B9B} /* Cn */, { 0x0B9D, 0x0B9D} /* Cn */,
836: { 0x0BA0, 0x0BA2} /* Cn */, { 0x0BA5, 0x0BA7} /* Cn */,
837: { 0x0BAB, 0x0BAD} /* Cn */, { 0x0BBA, 0x0BBD} /* Cn */,
838: { 0x0BC3, 0x0BC5} /* Cn */, { 0x0BC9, 0x0BC9} /* Cn */,
839: { 0x0BCE, 0x0BD6} /* Cn */, { 0x0BD8, 0x0BE5} /* Cn */,
840: { 0x0BFB, 0x0C00} /* Cn */, { 0x0C04, 0x0C04} /* Cn */,
841: { 0x0C0D, 0x0C0D} /* Cn */, { 0x0C11, 0x0C11} /* Cn */,
842: { 0x0C29, 0x0C29} /* Cn */, { 0x0C34, 0x0C34} /* Cn */,
843: { 0x0C3A, 0x0C3D} /* Cn */, { 0x0C45, 0x0C45} /* Cn */,
844: { 0x0C49, 0x0C49} /* Cn */, { 0x0C4E, 0x0C54} /* Cn */,
845: { 0x0C57, 0x0C5F} /* Cn */, { 0x0C62, 0x0C65} /* Cn */,
846: { 0x0C70, 0x0C81} /* Cn */, { 0x0C84, 0x0C84} /* Cn */,
847: { 0x0C8D, 0x0C8D} /* Cn */, { 0x0C91, 0x0C91} /* Cn */,
848: { 0x0CA9, 0x0CA9} /* Cn */, { 0x0CB4, 0x0CB4} /* Cn */,
849: { 0x0CBA, 0x0CBB} /* Cn */, { 0x0CC5, 0x0CC5} /* Cn */,
850: { 0x0CC9, 0x0CC9} /* Cn */, { 0x0CCE, 0x0CD4} /* Cn */,
851: { 0x0CD7, 0x0CDD} /* Cn */, { 0x0CDF, 0x0CDF} /* Cn */,
852: { 0x0CE4, 0x0CE5} /* Cn */, { 0x0CF0, 0x0CF0} /* Cn */,
853: { 0x0CF3, 0x0D01} /* Cn */, { 0x0D04, 0x0D04} /* Cn */,
854: { 0x0D0D, 0x0D0D} /* Cn */, { 0x0D11, 0x0D11} /* Cn */,
855: { 0x0D29, 0x0D29} /* Cn */, { 0x0D3A, 0x0D3D} /* Cn */,
856: { 0x0D44, 0x0D45} /* Cn */, { 0x0D49, 0x0D49} /* Cn */,
857: { 0x0D4E, 0x0D56} /* Cn */, { 0x0D58, 0x0D5F} /* Cn */,
858: { 0x0D62, 0x0D65} /* Cn */, { 0x0D70, 0x0D81} /* Cn */,
859: { 0x0D84, 0x0D84} /* Cn */, { 0x0D97, 0x0D99} /* Cn */,
860: { 0x0DB2, 0x0DB2} /* Cn */, { 0x0DBC, 0x0DBC} /* Cn */,
861: { 0x0DBE, 0x0DBF} /* Cn */, { 0x0DC7, 0x0DC9} /* Cn */,
862: { 0x0DCB, 0x0DCE} /* Cn */, { 0x0DD5, 0x0DD5} /* Cn */,
863: { 0x0DD7, 0x0DD7} /* Cn */, { 0x0DE0, 0x0DF1} /* Cn */,
864: { 0x0DF5, 0x0E00} /* Cn */, { 0x0E3B, 0x0E3E} /* Cn */,
865: { 0x0E5C, 0x0E80} /* Cn */, { 0x0E83, 0x0E83} /* Cn */,
866: { 0x0E85, 0x0E86} /* Cn */, { 0x0E89, 0x0E89} /* Cn */,
867: { 0x0E8B, 0x0E8C} /* Cn */, { 0x0E8E, 0x0E93} /* Cn */,
868: { 0x0E98, 0x0E98} /* Cn */, { 0x0EA0, 0x0EA0} /* Cn */,
869: { 0x0EA4, 0x0EA4} /* Cn */, { 0x0EA6, 0x0EA6} /* Cn */,
870: { 0x0EA8, 0x0EA9} /* Cn */, { 0x0EAC, 0x0EAC} /* Cn */,
871: { 0x0EBA, 0x0EBA} /* Cn */, { 0x0EBE, 0x0EBF} /* Cn */,
872: { 0x0EC5, 0x0EC5} /* Cn */, { 0x0EC7, 0x0EC7} /* Cn */,
873: { 0x0ECE, 0x0ECF} /* Cn */, { 0x0EDA, 0x0EDB} /* Cn */,
874: { 0x0EDE, 0x0EFF} /* Cn */, { 0x0F48, 0x0F48} /* Cn */,
875: { 0x0F6B, 0x0F70} /* Cn */, { 0x0F8C, 0x0F8F} /* Cn */,
876: { 0x0F98, 0x0F98} /* Cn */, { 0x0FBD, 0x0FBD} /* Cn */,
877: { 0x0FCD, 0x0FCE} /* Cn */, { 0x0FD2, 0x0FFF} /* Cn */,
878: { 0x1022, 0x1022} /* Cn */, { 0x1028, 0x1028} /* Cn */,
879: { 0x102B, 0x102B} /* Cn */, { 0x1033, 0x1035} /* Cn */,
880: { 0x103A, 0x103F} /* Cn */, { 0x105A, 0x109F} /* Cn */,
881: { 0x10C6, 0x10CF} /* Cn */, { 0x10FD, 0x10FF} /* Cn */,
882: { 0x115A, 0x115E} /* Cn */, { 0x11A3, 0x11A7} /* Cn */,
883: { 0x11FA, 0x11FF} /* Cn */, { 0x1249, 0x1249} /* Cn */,
884: { 0x124E, 0x124F} /* Cn */, { 0x1257, 0x1257} /* Cn */,
885: { 0x1259, 0x1259} /* Cn */, { 0x125E, 0x125F} /* Cn */,
886: { 0x1289, 0x1289} /* Cn */, { 0x128E, 0x128F} /* Cn */,
887: { 0x12B1, 0x12B1} /* Cn */, { 0x12B6, 0x12B7} /* Cn */,
888: { 0x12BF, 0x12BF} /* Cn */, { 0x12C1, 0x12C1} /* Cn */,
889: { 0x12C6, 0x12C7} /* Cn */, { 0x12D7, 0x12D7} /* Cn */,
890: { 0x1311, 0x1311} /* Cn */, { 0x1316, 0x1317} /* Cn */,
891: { 0x135B, 0x135E} /* Cn */, { 0x137D, 0x137F} /* Cn */,
892: { 0x139A, 0x139F} /* Cn */, { 0x13F5, 0x1400} /* Cn */,
893: { 0x1677, 0x167F} /* Cn */, { 0x169D, 0x169F} /* Cn */,
894: { 0x16F1, 0x16FF} /* Cn */, { 0x170D, 0x170D} /* Cn */,
895: { 0x1715, 0x171F} /* Cn */, { 0x1737, 0x173F} /* Cn */,
896: { 0x1754, 0x175F} /* Cn */, { 0x176D, 0x176D} /* Cn */,
897: { 0x1771, 0x1771} /* Cn */, { 0x1774, 0x177F} /* Cn */,
898: #if 0
899: { 0x17B4, 0x17B5} /* Cf */,
900: #endif
901: { 0x17DE, 0x17DF} /* Cn */, { 0x17EA, 0x17EF} /* Cn */,
902: { 0x17FA, 0x17FF} /* Cn */, { 0x180F, 0x180F} /* Cn */,
903: { 0x181A, 0x181F} /* Cn */, { 0x1878, 0x187F} /* Cn */,
904: { 0x18AA, 0x18FF} /* Cn */, { 0x191D, 0x191F} /* Cn */,
905: { 0x192C, 0x192F} /* Cn */, { 0x193C, 0x193F} /* Cn */,
906: { 0x1941, 0x1943} /* Cn */, { 0x196E, 0x196F} /* Cn */,
907: { 0x1975, 0x197F} /* Cn */, { 0x19AA, 0x19AF} /* Cn */,
908: { 0x19CA, 0x19CF} /* Cn */, { 0x19DA, 0x19DD} /* Cn */,
909: { 0x1A1C, 0x1A1D} /* Cn */, { 0x1A20, 0x1AFF} /* Cn */,
910: { 0x1B4C, 0x1B4F} /* Cn */, { 0x1B7D, 0x1CFF} /* Cn */,
911: { 0x1DCB, 0x1DFD} /* Cn */, { 0x1E9C, 0x1E9F} /* Cn */,
912: { 0x1EFA, 0x1EFF} /* Cn */, { 0x1F16, 0x1F17} /* Cn */,
913: { 0x1F1E, 0x1F1F} /* Cn */, { 0x1F46, 0x1F47} /* Cn */,
914: { 0x1F4E, 0x1F4F} /* Cn */, { 0x1F58, 0x1F58} /* Cn */,
915: { 0x1F5A, 0x1F5A} /* Cn */, { 0x1F5C, 0x1F5C} /* Cn */,
916: { 0x1F5E, 0x1F5E} /* Cn */, { 0x1F7E, 0x1F7F} /* Cn */,
917: { 0x1FB5, 0x1FB5} /* Cn */, { 0x1FC5, 0x1FC5} /* Cn */,
918: { 0x1FD4, 0x1FD5} /* Cn */, { 0x1FDC, 0x1FDC} /* Cn */,
919: { 0x1FF0, 0x1FF1} /* Cn */, { 0x1FF5, 0x1FF5} /* Cn */,
920: { 0x1FFF, 0x1FFF} /* Cn */,
921: { 0x200B, 0x200F} /* Cf */,
922: { 0x2028, 0x2028} /* Zl */,
923: { 0x2029, 0x2029} /* Zp */,
924: { 0x202A, 0x202E} /* Cf */,
925: { 0x2060, 0x2063} /* Cf */,
926: { 0x2064, 0x2069} /* Cn */,
927: { 0x206A, 0x206F} /* Cf */,
928: { 0x2072, 0x2073} /* Cn */, { 0x208F, 0x208F} /* Cn */,
929: { 0x2095, 0x209F} /* Cn */, { 0x20B6, 0x20CF} /* Cn */,
930: { 0x20F0, 0x20FF} /* Cn */, { 0x214F, 0x2152} /* Cn */,
931: { 0x2185, 0x218F} /* Cn */, { 0x23E8, 0x23FF} /* Cn */,
932: { 0x2427, 0x243F} /* Cn */, { 0x244B, 0x245F} /* Cn */,
933: { 0x269D, 0x269F} /* Cn */, { 0x26B3, 0x2700} /* Cn */,
934: { 0x2705, 0x2705} /* Cn */, { 0x270A, 0x270B} /* Cn */,
935: { 0x2728, 0x2728} /* Cn */, { 0x274C, 0x274C} /* Cn */,
936: { 0x274E, 0x274E} /* Cn */, { 0x2753, 0x2755} /* Cn */,
937: { 0x2757, 0x2757} /* Cn */, { 0x275F, 0x2760} /* Cn */,
938: { 0x2795, 0x2797} /* Cn */, { 0x27B0, 0x27B0} /* Cn */,
939: { 0x27BF, 0x27BF} /* Cn */, { 0x27CB, 0x27CF} /* Cn */,
940: { 0x27EC, 0x27EF} /* Cn */, { 0x2B1B, 0x2B1F} /* Cn */,
941: { 0x2B24, 0x2BFF} /* Cn */, { 0x2C2F, 0x2C2F} /* Cn */,
942: { 0x2C5F, 0x2C5F} /* Cn */, { 0x2C6D, 0x2C73} /* Cn */,
943: { 0x2C78, 0x2C7F} /* Cn */, { 0x2CEB, 0x2CF8} /* Cn */,
944: { 0x2D26, 0x2D2F} /* Cn */, { 0x2D66, 0x2D6E} /* Cn */,
945: { 0x2D70, 0x2D7F} /* Cn */, { 0x2D97, 0x2D9F} /* Cn */,
946: { 0x2DA7, 0x2DA7} /* Cn */, { 0x2DAF, 0x2DAF} /* Cn */,
947: { 0x2DB7, 0x2DB7} /* Cn */, { 0x2DBF, 0x2DBF} /* Cn */,
948: { 0x2DC7, 0x2DC7} /* Cn */, { 0x2DCF, 0x2DCF} /* Cn */,
949: { 0x2DD7, 0x2DD7} /* Cn */, { 0x2DDF, 0x2DFF} /* Cn */,
950: { 0x2E18, 0x2E1B} /* Cn */, { 0x2E1E, 0x2E7F} /* Cn */,
951: { 0x2E9A, 0x2E9A} /* Cn */, { 0x2EF4, 0x2EFF} /* Cn */,
952: { 0x2FD6, 0x2FEF} /* Cn */, { 0x2FFC, 0x2FFF} /* Cn */,
953: { 0x3040, 0x3040} /* Cn */, { 0x3097, 0x3098} /* Cn */,
954: { 0x3100, 0x3104} /* Cn */, { 0x312D, 0x3130} /* Cn */,
955: { 0x318F, 0x318F} /* Cn */, { 0x31B8, 0x31BF} /* Cn */,
956: { 0x31D0, 0x31EF} /* Cn */, { 0x321F, 0x321F} /* Cn */,
957: { 0x3244, 0x324F} /* Cn */, { 0x32FF, 0x32FF} /* Cn */,
958: { 0x4DB6, 0x4DBF} /* Cn */, { 0x9FBC, 0x9FFF} /* Cn */,
959: { 0xA48D, 0xA48F} /* Cn */, { 0xA4C7, 0xA6FF} /* Cn */,
960: { 0xA71B, 0xA71F} /* Cn */, { 0xA722, 0xA7FF} /* Cn */,
961: { 0xA82C, 0xA83F} /* Cn */, { 0xA878, 0xABFF} /* Cn */,
962: { 0xD7A4, 0xD7FF} /* Cn */,
963: { 0xD800, 0xDFFF} /* Cs */,
964: { 0xE000, 0xF8FF} /* Co */,
965: { 0xFA2E, 0xFA2F} /* Cn */, { 0xFA6B, 0xFA6F} /* Cn */,
966: { 0xFADA, 0xFAFF} /* Cn */, { 0xFB07, 0xFB12} /* Cn */,
967: { 0xFB18, 0xFB1C} /* Cn */, { 0xFB37, 0xFB37} /* Cn */,
968: { 0xFB3D, 0xFB3D} /* Cn */, { 0xFB3F, 0xFB3F} /* Cn */,
969: { 0xFB42, 0xFB42} /* Cn */, { 0xFB45, 0xFB45} /* Cn */,
970: { 0xFBB2, 0xFBD2} /* Cn */, { 0xFD40, 0xFD4F} /* Cn */,
971: { 0xFD90, 0xFD91} /* Cn */, { 0xFDC8, 0xFDEF} /* Cn */,
972: { 0xFDFE, 0xFDFF} /* Cn */, { 0xFE1A, 0xFE1F} /* Cn */,
973: { 0xFE24, 0xFE2F} /* Cn */, { 0xFE53, 0xFE53} /* Cn */,
974: { 0xFE67, 0xFE67} /* Cn */, { 0xFE6C, 0xFE6F} /* Cn */,
975: { 0xFE75, 0xFE75} /* Cn */, { 0xFEFD, 0xFEFE} /* Cn */,
976: { 0xFEFF, 0xFEFF} /* Cf */,
977: { 0xFF00, 0xFF00} /* Cn */, { 0xFFBF, 0xFFC1} /* Cn */,
978: { 0xFFC8, 0xFFC9} /* Cn */, { 0xFFD0, 0xFFD1} /* Cn */,
979: { 0xFFD8, 0xFFD9} /* Cn */, { 0xFFDD, 0xFFDF} /* Cn */,
980: { 0xFFE7, 0xFFE7} /* Cn */, { 0xFFEF, 0xFFF8} /* Cn */,
981: { 0xFFF9, 0xFFFB} /* Cf */,
982: { 0xFFFE, 0xFFFF} /* Cn */, { 0x1000C, 0x1000C} /* Cn */,
983: { 0x10027, 0x10027} /* Cn */, { 0x1003B, 0x1003B} /* Cn */,
984: { 0x1003E, 0x1003E} /* Cn */, { 0x1004E, 0x1004F} /* Cn */,
985: { 0x1005E, 0x1007F} /* Cn */, { 0x100FB, 0x100FF} /* Cn */,
986: { 0x10103, 0x10106} /* Cn */, { 0x10134, 0x10136} /* Cn */,
987: { 0x1018B, 0x102FF} /* Cn */, { 0x1031F, 0x1031F} /* Cn */,
988: { 0x10324, 0x1032F} /* Cn */, { 0x1034B, 0x1037F} /* Cn */,
989: { 0x1039E, 0x1039E} /* Cn */, { 0x103C4, 0x103C7} /* Cn */,
990: { 0x103D6, 0x103FF} /* Cn */,
991: { 0x1049E, 0x1049F} /* Cn */, { 0x104AA, 0x107FF} /* Cn */,
992: { 0x10806, 0x10807} /* Cn */, { 0x10809, 0x10809} /* Cn */,
993: { 0x10836, 0x10836} /* Cn */, { 0x10839, 0x1083B} /* Cn */,
994: { 0x1083D, 0x1083E} /* Cn */, { 0x10840, 0x108FF} /* Cn */,
995: { 0x1091A, 0x1091E} /* Cn */, { 0x10920, 0x109FF} /* Cn */,
996: { 0x10A04, 0x10A04} /* Cn */, { 0x10A07, 0x10A0B} /* Cn */,
997: { 0x10A14, 0x10A14} /* Cn */, { 0x10A18, 0x10A18} /* Cn */,
998: { 0x10A34, 0x10A37} /* Cn */, { 0x10A3B, 0x10A3E} /* Cn */,
999: { 0x10A48, 0x10A4F} /* Cn */, { 0x10A59, 0x11FFF} /* Cn */,
1000: { 0x1236F, 0x123FF} /* Cn */, { 0x12463, 0x1246F} /* Cn */,
1001: { 0x12474, 0x1CFFF} /* Cn */, { 0x1D0F6, 0x1D0FF} /* Cn */,
1002: { 0x1D127, 0x1D129} /* Cn */,
1003: { 0x1D173, 0x1D17A} /* Cf */,
1004: { 0x1D1DE, 0x1D1FF} /* Cn */, { 0x1D246, 0x1D2FF} /* Cn */,
1005: { 0x1D357, 0x1D35F} /* Cn */, { 0x1D372, 0x1D3FF} /* Cn */,
1006: { 0x1D455, 0x1D455} /* Cn */, { 0x1D49D, 0x1D49D} /* Cn */,
1007: { 0x1D4A0, 0x1D4A1} /* Cn */, { 0x1D4A3, 0x1D4A4} /* Cn */,
1008: { 0x1D4A7, 0x1D4A8} /* Cn */, { 0x1D4AD, 0x1D4AD} /* Cn */,
1009: { 0x1D4BA, 0x1D4BA} /* Cn */, { 0x1D4BC, 0x1D4BC} /* Cn */,
1010: { 0x1D4C4, 0x1D4C4} /* Cn */, { 0x1D506, 0x1D506} /* Cn */,
1011: { 0x1D50B, 0x1D50C} /* Cn */, { 0x1D515, 0x1D515} /* Cn */,
1012: { 0x1D51D, 0x1D51D} /* Cn */, { 0x1D53A, 0x1D53A} /* Cn */,
1013: { 0x1D53F, 0x1D53F} /* Cn */, { 0x1D545, 0x1D545} /* Cn */,
1014: { 0x1D547, 0x1D549} /* Cn */, { 0x1D551, 0x1D551} /* Cn */,
1015: { 0x1D6A6, 0x1D6A7} /* Cn */, { 0x1D7CC, 0x1D7CD} /* Cn */,
1016: { 0x1D800, 0x1FFFF} /* Cn */, { 0x2A6D7, 0x2F7FF} /* Cn */,
1017: { 0x2FA1E, 0xE0000} /* Cn */,
1018: { 0xE0001, 0xE0001} /* Cf */,
1019: { 0xE0002, 0xE001F} /* Cn */,
1020: { 0xE0020, 0xE007F} /* Cf */,
1021: { 0xE0080, 0xE00FF} /* Cn */, { 0xE01F0, 0xEFFFF} /* Cn */,
1022: { 0xF0000, 0xFFFFD} /* Co */,
1023: { 0xFFFFE, 0xFFFFF} /* Cn */,
1.11 nicm 1024: { 0x100000, 0x10FFFD} /* Co */,
1025: { 0x10FFFE, 0x10FFFF} /* Cn */,
1026: { 0x110000, 0x7FFFFFFF} /* ISO 10646?? */
1.8 shadchin 1027: };
1028:
1029: /*
1030: * Double width characters
1031: * W: East Asian Wide
1032: * F: East Asian Full-width
1033: * Unassigned code points may be included when they allow ranges to be merged.
1034: * Last synched with
1035: * <http://www.unicode.org/Public/5.0.0/ucd/EastAsianWidth-5.0.0d2.txt>
1036: * dated 2005-11-08T01:32:56Z
1037: */
1038: static struct wchar_range wide_table[] = {
1039: { 0x1100, 0x115F} /* W */, { 0x2329, 0x232A} /* W */,
1040: { 0x2E80, 0x2FFB} /* W */,
1041: { 0x3000, 0x3000} /* F */,
1042: { 0x3001, 0x303E} /* W */, { 0x3041, 0x4DB5} /* W */,
1043: { 0x4E00, 0x9FBB} /* W */, { 0xA000, 0xA4C6} /* W */,
1044: { 0xAC00, 0xD7A3} /* W */, { 0xF900, 0xFAD9} /* W */,
1045: { 0xFE10, 0xFE19} /* W */, { 0xFE30, 0xFE6B} /* W */,
1046: { 0xFF01, 0xFF60} /* F */, { 0xFFE0, 0xFFE6} /* F */,
1047: { 0x20000, 0x2FFFD} /* W */, { 0x30000, 0x3FFFD} /* W */,
1048: };
1049:
1.11 nicm 1050: static int
1051: is_in_table(LWCHAR ch, struct wchar_range table[], int tsize)
1.8 shadchin 1052: {
1053: int hi;
1054: int lo;
1055:
1056: /* Binary search in the table. */
1057: if (ch < table[0].first)
1.11 nicm 1058: return (0);
1.8 shadchin 1059: lo = 0;
1060: hi = tsize - 1;
1.11 nicm 1061: while (lo <= hi) {
1.8 shadchin 1062: int mid = (lo + hi) / 2;
1063: if (ch > table[mid].last)
1064: lo = mid + 1;
1065: else if (ch < table[mid].first)
1066: hi = mid - 1;
1067: else
1.11 nicm 1068: return (1);
1.8 shadchin 1069: }
1.11 nicm 1070: return (0);
1.8 shadchin 1071: }
1072:
1073: /*
1074: * Is a character a UTF-8 composing character?
1075: * If a composing character follows any char, the two combine into one glyph.
1076: */
1.11 nicm 1077: int
1078: is_composing_char(LWCHAR ch)
1.8 shadchin 1079: {
1.11 nicm 1080: return (is_in_table(ch, comp_table,
1081: (sizeof (comp_table) / sizeof (*comp_table))));
1.8 shadchin 1082: }
1083:
1084: /*
1085: * Should this UTF-8 character be treated as binary?
1086: */
1.11 nicm 1087: int
1088: is_ubin_char(LWCHAR ch)
1.8 shadchin 1089: {
1.11 nicm 1090: return (is_in_table(ch, ubin_table,
1091: (sizeof (ubin_table) / sizeof (*ubin_table))));
1.8 shadchin 1092: }
1093:
1094: /*
1095: * Is this a double width UTF-8 character?
1096: */
1.11 nicm 1097: int
1098: is_wide_char(LWCHAR ch)
1.8 shadchin 1099: {
1.11 nicm 1100: return (is_in_table(ch, wide_table,
1101: (sizeof (wide_table) / sizeof (*wide_table))));
1.8 shadchin 1102: }
1103:
1104: /*
1105: * Is a character a UTF-8 combining character?
1106: * A combining char acts like an ordinary char, but if it follows
1107: * a specific char (not any char), the two combine into one glyph.
1108: */
1.11 nicm 1109: int
1110: is_combining_char(LWCHAR ch1, LWCHAR ch2)
1.8 shadchin 1111: {
1112: /* The table is small; use linear search. */
1113: int i;
1.11 nicm 1114: for (i = 0; i < sizeof (comb_table) / sizeof (*comb_table); i++) {
1.8 shadchin 1115: if (ch1 == comb_table[i].first &&
1116: ch2 == comb_table[i].last)
1.11 nicm 1117: return (1);
1.8 shadchin 1118: }
1.11 nicm 1119: return (0);
1.8 shadchin 1120: }