]> www.wagner.pp.ru Git - oss/less.git/blob - charset.c
Removed code page operation. Fix GLOB macros for 64-bit win32. Fixed debug flags...
[oss/less.git] / charset.c
1 /*
2  * Copyright (C) 1984-2015  Mark Nudelman
3  *
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.
6  *
7  * For more information, see the README file.
8  */
9
10
11 /*
12  * Functions to define the character set
13  * and do things specific to the character set.
14  */
15
16 #include "less.h"
17 #if MSDOS_COMPILER==WIN32C
18 #include <windows.h>
19 #endif
20 #if HAVE_LOCALE
21 #include <locale.h>
22 #include <ctype.h>
23 #include <langinfo.h>
24 #endif
25
26 #include "charset.h"
27
28 public int utf_mode = 0;
29
30 /*
31  * Predefined character sets,
32  * selected by the LESSCHARSET environment variable.
33  */
34 struct charset {
35         char *name;
36         int *p_flag;
37         char *desc;
38 } charsets[] = {
39         { "ascii",              NULL,       "8bcccbcc18b95.b" },
40         { "utf-8",              &utf_mode,  "8bcccbcc18b95.b126.bb" },
41         { "iso8859",            NULL,       "8bcccbcc18b95.33b." },
42         { "latin3",             NULL,       "8bcccbcc18b95.33b5.b8.b15.b4.b12.b18.b12.b." },
43         { "arabic",             NULL,       "8bcccbcc18b95.33b.3b.7b2.13b.3b.b26.5b19.b" },
44         { "greek",              NULL,       "8bcccbcc18b95.33b4.2b4.b3.b35.b44.b" },
45         { "greek2005",          NULL,       "8bcccbcc18b95.33b14.b35.b44.b" },
46         { "hebrew",             NULL,       "8bcccbcc18b95.33b.b29.32b28.2b2.b" },
47         { "koi8-r",             NULL,       "8bcccbcc18b95.b." },
48         { "KOI8-T",             NULL,       "8bcccbcc18b95.b8.b6.b8.b.b.5b7.3b4.b4.b3.b.b.3b." },
49         { "georgianps",         NULL,       "8bcccbcc18b95.3b11.4b12.2b." },
50         { "tcvn",               NULL,       "b..b...bcccbccbbb7.8b95.b48.5b." },
51         { "TIS-620",            NULL,       "8bcccbcc18b95.b.4b.11b7.8b." },
52         { "next",               NULL,       "8bcccbcc18b95.bb125.bb" },
53         { "dos",                NULL,       "8bcccbcc12bc5b95.b." },
54         { "windows-1251",       NULL,       "8bcccbcc12bc5b95.b24.b." },
55         { "windows-1252",       NULL,       "8bcccbcc12bc5b95.b.b11.b.2b12.b." },
56         { "windows-1255",       NULL,       "8bcccbcc12bc5b95.b.b8.b.5b9.b.4b." },
57         { "ebcdic",             NULL,       "5bc6bcc7bcc41b.9b7.9b5.b..8b6.10b6.b9.7b9.8b8.17b3.3b9.7b9.8b8.6b10.b.b.b." },
58         { "IBM-1047",           NULL,       "4cbcbc3b9cbccbccbb4c6bcc5b3cbbc4bc4bccbc191.b" },
59         { NULL, NULL, NULL }
60 };
61
62 /*
63  * Support "locale charmap"/nl_langinfo(CODESET) values, as well as others.
64  */
65 struct cs_alias {
66         char *name;
67         char *oname;
68 } cs_aliases[] = {
69         { "UTF-8",              "utf-8" },
70         { "ANSI_X3.4-1968",     "ascii" },
71         { "US-ASCII",           "ascii" },
72         { "latin1",             "iso8859" },
73         { "ISO-8859-1",         "iso8859" },
74         { "latin9",             "iso8859" },
75         { "ISO-8859-15",        "iso8859" },
76         { "latin2",             "iso8859" },
77         { "ISO-8859-2",         "iso8859" },
78         { "ISO-8859-3",         "latin3" },
79         { "latin4",             "iso8859" },
80         { "ISO-8859-4",         "iso8859" },
81         { "cyrillic",           "iso8859" },
82         { "ISO-8859-5",         "iso8859" },
83         { "ISO-8859-6",         "arabic" },
84         { "ISO-8859-7",         "greek" },
85         { "IBM9005",            "greek2005" },
86         { "ISO-8859-8",         "hebrew" },
87         { "latin5",             "iso8859" },
88         { "ISO-8859-9",         "iso8859" },
89         { "latin6",             "iso8859" },
90         { "ISO-8859-10",        "iso8859" },
91         { "latin7",             "iso8859" },
92         { "ISO-8859-13",        "iso8859" },
93         { "latin8",             "iso8859" },
94         { "ISO-8859-14",        "iso8859" },
95         { "latin10",            "iso8859" },
96         { "ISO-8859-16",        "iso8859" },
97         { "IBM437",             "dos" },
98         { "EBCDIC-US",          "ebcdic" },
99         { "IBM1047",            "IBM-1047" },
100         { "KOI8-R",             "koi8-r" },
101         { "KOI8-U",             "koi8-r" },
102         { "GEORGIAN-PS",        "georgianps" },
103         { "TCVN5712-1",         "tcvn" },
104         { "NEXTSTEP",           "next" },
105         { "windows",            "windows-1252" }, /* backward compatibility */
106         { "CP1251",             "windows-1251" },
107         { "CP1252",             "windows-1252" },
108         { "CP1255",             "windows-1255" },
109         { NULL, NULL }
110 };
111
112 #define IS_BINARY_CHAR  01
113 #define IS_CONTROL_CHAR 02
114
115 static char chardef[256];
116 static char *binfmt = NULL;
117 static char *utfbinfmt = NULL;
118 public int binattr = AT_STANDOUT;
119
120
121 /*
122  * Define a charset, given a description string.
123  * The string consists of 256 letters,
124  * one for each character in the charset.
125  * If the string is shorter than 256 letters, missing letters
126  * are taken to be identical to the last one.
127  * A decimal number followed by a letter is taken to be a 
128  * repetition of the letter.
129  *
130  * Each letter is one of:
131  *      . normal character
132  *      b binary character
133  *      c control character
134  */
135         static void
136 ichardef(s)
137         char *s;
138 {
139         register char *cp;
140         register int n;
141         register char v;
142
143         n = 0;
144         v = 0;
145         cp = chardef;
146         while (*s != '\0')
147         {
148                 switch (*s++)
149                 {
150                 case '.':
151                         v = 0;
152                         break;
153                 case 'c':
154                         v = IS_CONTROL_CHAR;
155                         break;
156                 case 'b':
157                         v = IS_BINARY_CHAR|IS_CONTROL_CHAR;
158                         break;
159
160                 case '0': case '1': case '2': case '3': case '4':
161                 case '5': case '6': case '7': case '8': case '9':
162                         n = (10 * n) + (s[-1] - '0');
163                         continue;
164
165                 default:
166                         error("invalid chardef", NULL_PARG);
167                         quit(QUIT_ERROR);
168                         /*NOTREACHED*/
169                 }
170
171                 do
172                 {
173                         if (cp >= chardef + sizeof(chardef))
174                         {
175                                 error("chardef longer than 256", NULL_PARG);
176                                 quit(QUIT_ERROR);
177                                 /*NOTREACHED*/
178                         }
179                         *cp++ = v;
180                 } while (--n > 0);
181                 n = 0;
182         }
183
184         while (cp < chardef + sizeof(chardef))
185                 *cp++ = v;
186 }
187
188 /*
189  * Define a charset, given a charset name.
190  * The valid charset names are listed in the "charsets" array.
191  */
192         static int
193 icharset(name, no_error)
194         register char *name;
195         int no_error;
196 {
197         register struct charset *p;
198         register struct cs_alias *a;
199
200         if (name == NULL || *name == '\0')
201                 return (0);
202
203         /* First see if the name is an alias. */
204         for (a = cs_aliases;  a->name != NULL;  a++)
205         {
206                 if (strcmp(name, a->name) == 0)
207                 {
208                         name = a->oname;
209                         break;
210                 }
211         }
212
213         for (p = charsets;  p->name != NULL;  p++)
214         {
215                 if (strcmp(name, p->name) == 0)
216                 {
217                         ichardef(p->desc);
218                         if (p->p_flag != NULL)
219                                 *(p->p_flag) = 1;
220                         return (1);
221                 }
222         }
223
224         if (!no_error) {
225                 error("invalid charset name", NULL_PARG);
226                 quit(QUIT_ERROR);
227         }
228         return (0);
229 }
230
231 #if HAVE_LOCALE
232 /*
233  * Define a charset, given a locale name.
234  */
235         static void
236 ilocale()
237 {
238         register int c;
239
240         for (c = 0;  c < (int) sizeof(chardef);  c++)
241         {
242                 if (isprint(c))
243                         chardef[c] = 0;
244                 else if (iscntrl(c))
245                         chardef[c] = IS_CONTROL_CHAR;
246                 else
247                         chardef[c] = IS_BINARY_CHAR|IS_CONTROL_CHAR;
248         }
249 }
250 #endif
251
252 /*
253  * Define the printing format for control (or binary utf) chars.
254  */
255         static void
256 setbinfmt(s, fmtvarptr, default_fmt)
257         char *s;
258         char **fmtvarptr;
259         char *default_fmt;
260 {
261         if (s && utf_mode)
262         {
263                 /* It would be too hard to account for width otherwise.  */
264                 char *t = s;
265                 while (*t)
266                 {
267                         if (*t < ' ' || *t > '~')
268                         {
269                                 s = default_fmt;
270                                 goto attr;
271                         }
272                         t++;
273                 }
274         }
275
276         /* %n is evil */
277         if (s == NULL || *s == '\0' ||
278             (*s == '*' && (s[1] == '\0' || s[2] == '\0' || strchr(s + 2, 'n'))) ||
279             (*s != '*' && strchr(s, 'n')))
280                 s = default_fmt;
281
282         /*
283          * Select the attributes if it starts with "*".
284          */
285  attr:
286         if (*s == '*')
287         {
288                 switch (s[1])
289                 {
290                 case 'd':  binattr = AT_BOLD;      break;
291                 case 'k':  binattr = AT_BLINK;     break;
292                 case 's':  binattr = AT_STANDOUT;  break;
293                 case 'u':  binattr = AT_UNDERLINE; break;
294                 default:   binattr = AT_NORMAL;    break;
295                 }
296                 s += 2;
297         }
298         *fmtvarptr = s;
299 }
300
301 /*
302  *
303  */
304         static void
305 set_charset()
306 {
307         char *s;
308
309         /*
310          * See if environment variable LESSCHARSET is defined.
311          */
312         s = lgetenv("LESSCHARSET");
313         if (icharset(s, 0))
314                 return;
315
316         /*
317          * LESSCHARSET is not defined: try LESSCHARDEF.
318          */
319         s = lgetenv("LESSCHARDEF");
320         if (s != NULL && *s != '\0')
321         {
322                 ichardef(s);
323                 return;
324         }
325 #ifdef WIN32
326         icharset("UTF-8",0);
327         
328 #else 
329 #if HAVE_LOCALE
330 #ifdef CODESET
331         /*
332          * Try using the codeset name as the charset name.
333          */
334         s = nl_langinfo(CODESET);
335         if (icharset(s, 1))
336                 return;
337 #endif
338 #endif
339
340 #if HAVE_STRSTR
341         /*
342          * Check whether LC_ALL, LC_CTYPE or LANG look like UTF-8 is used.
343          */
344         if ((s = lgetenv("LC_ALL")) != NULL ||
345             (s = lgetenv("LC_CTYPE")) != NULL ||
346             (s = lgetenv("LANG")) != NULL)
347         {
348                 if (   strstr(s, "UTF-8") != NULL || strstr(s, "utf-8") != NULL
349                     || strstr(s, "UTF8")  != NULL || strstr(s, "utf8")  != NULL)
350                         if (icharset("utf-8", 1))
351                                 return;
352         }
353 #endif
354
355 #if HAVE_LOCALE
356         /*
357          * Get character definitions from locale functions,
358          * rather than from predefined charset entry.
359          */
360         ilocale();
361 #if MSDOS_COMPILER
362         /*
363          * Default to "dos".
364          */
365         (void) icharset("dos", 1);
366 #else
367         /*
368          * Default to "latin1".
369          */
370         (void) icharset("latin1", 1);
371 #endif
372 #endif
373 #endif
374 }
375
376 /*
377  * Initialize charset data structures.
378  */
379         public void
380 init_charset()
381 {
382         char *s;
383
384 #if HAVE_LOCALE
385         setlocale(LC_ALL, "");
386 #endif
387
388         set_charset();
389
390         s = lgetenv("LESSBINFMT");
391         setbinfmt(s, &binfmt, "*s<%02X>");
392         
393         s = lgetenv("LESSUTFBINFMT");
394         setbinfmt(s, &utfbinfmt, "<U+%04lX>");
395 }
396
397 /*
398  * Is a given character a "binary" character?
399  */
400         public int
401 binary_char(c)
402         LWCHAR c;
403 {
404         if (utf_mode)
405                 return (is_ubin_char(c));
406         c &= 0377;
407         return (chardef[c] & IS_BINARY_CHAR);
408 }
409
410 /*
411  * Is a given character a "control" character?
412  */
413         public int
414 control_char(c)
415         LWCHAR c;
416 {
417         c &= 0377;
418         return (chardef[c] & IS_CONTROL_CHAR);
419 }
420
421 /*
422  * Return the printable form of a character.
423  * For example, in the "ascii" charset '\3' is printed as "^C".
424  */
425         public char *
426 prchar(c)
427         LWCHAR c;
428 {
429         /* {{ This buffer can be overrun if LESSBINFMT is a long string. }} */
430         static char buf[32];
431
432         c &= 0377;
433         if ((c < 128 || !utf_mode) && !control_char(c))
434                 SNPRINTF1(buf, sizeof(buf), "%c", (int) c);
435         else if (c == ESC)
436                 strcpy(buf, "ESC");
437 #if IS_EBCDIC_HOST
438         else if (!binary_char(c) && c < 64)
439                 SNPRINTF1(buf, sizeof(buf), "^%c",
440                 /*
441                  * This array roughly inverts CONTROL() #defined in less.h,
442                  * and should be kept in sync with CONTROL() and IBM-1047.
443                  */
444                 "@ABC.I.?...KLMNO"
445                 "PQRS.JH.XY.."
446                 "\\]^_"
447                 "......W[.....EFG"
448                 "..V....D....TU.Z"[c]);
449 #else
450         else if (c < 128 && !control_char(c ^ 0100))
451                 SNPRINTF1(buf, sizeof(buf), "^%c", (int) (c ^ 0100));
452 #endif
453         else
454                 SNPRINTF1(buf, sizeof(buf), binfmt, c);
455         return (buf);
456 }
457
458 /*
459  * Return the printable form of a UTF-8 character.
460  */
461         public char *
462 prutfchar(ch)
463         LWCHAR ch;
464 {
465         static char buf[32];
466
467         if (ch == ESC)
468                 strcpy(buf, "ESC");
469         else if (ch < 128 && control_char(ch))
470         {
471                 if (!control_char(ch ^ 0100))
472                         SNPRINTF1(buf, sizeof(buf), "^%c", ((char) ch) ^ 0100);
473                 else
474                         SNPRINTF1(buf, sizeof(buf), binfmt, (char) ch);
475         } else if (is_ubin_char(ch))
476         {
477                 SNPRINTF1(buf, sizeof(buf), utfbinfmt, ch);
478         } else
479         {
480                 char *p = buf;
481                 if (ch >= 0x80000000)
482                         ch = 0xFFFD; /* REPLACEMENT CHARACTER */
483                 put_wchar(&p, ch);
484                 *p = '\0';
485         }
486         return (buf);
487 }
488
489 /*
490  * Get the length of a UTF-8 character in bytes.
491  */
492         public int
493 utf_len(ch)
494         char ch;
495 {
496         if ((ch & 0x80) == 0)
497                 return 1;
498         if ((ch & 0xE0) == 0xC0)
499                 return 2;
500         if ((ch & 0xF0) == 0xE0)
501                 return 3;
502         if ((ch & 0xF8) == 0xF0)
503                 return 4;
504         if ((ch & 0xFC) == 0xF8)
505                 return 5;
506         if ((ch & 0xFE) == 0xFC)
507                 return 6;
508         /* Invalid UTF-8 encoding. */
509         return 1;
510 }
511
512 /*
513  * Does the parameter point to the lead byte of a well-formed UTF-8 character?
514  */
515         public int
516 is_utf8_well_formed(s, slen)
517         unsigned char *s;
518         int slen;
519 {
520         int i;
521         int len;
522
523         if (IS_UTF8_INVALID(s[0]))
524                 return (0);
525
526         len = utf_len((char) s[0]);
527         if (len > slen)
528                 return (0);
529         if (len == 1)
530                 return (1);
531         if (len == 2)
532         {
533                 if (s[0] < 0xC2)
534                     return (0);
535         } else
536         {
537                 unsigned char mask;
538                 mask = (~((1 << (8-len)) - 1)) & 0xFF;
539                 if (s[0] == mask && (s[1] & mask) == 0x80)
540                         return (0);
541         }
542
543         for (i = 1;  i < len;  i++)
544                 if (!IS_UTF8_TRAIL(s[i]))
545                         return (0);
546         return (1);
547 }
548
549 /*
550  * Return number of invalid UTF-8 sequences found in a buffer.
551  */
552         public int
553 utf_bin_count(data, len)
554         unsigned char *data;
555         int len;
556 {
557         int bin_count = 0;
558         while (len > 0)
559         {
560                 if (is_utf8_well_formed(data, len))
561                 {
562                         int clen = utf_len(*data);
563                         data += clen;
564                         len -= clen;
565                 } else
566                 {
567                         /* Skip to next lead byte. */
568                         bin_count++;
569                         do {
570                                 ++data;
571                                 --len;
572                         } while (len > 0 && !IS_UTF8_LEAD(*data));
573                 }
574         }
575         return (bin_count);
576 }
577
578 /*
579  * Get the value of a UTF-8 character.
580  */
581         public LWCHAR
582 get_wchar(p)
583         char *p;
584 {
585         switch (utf_len(p[0]))
586         {
587         case 1:
588         default:
589                 /* 0xxxxxxx */
590                 return (LWCHAR)
591                         (p[0] & 0xFF);
592         case 2:
593                 /* 110xxxxx 10xxxxxx */
594                 return (LWCHAR) (
595                         ((p[0] & 0x1F) << 6) |
596                         (p[1] & 0x3F));
597         case 3:
598                 /* 1110xxxx 10xxxxxx 10xxxxxx */
599                 return (LWCHAR) (
600                         ((p[0] & 0x0F) << 12) |
601                         ((p[1] & 0x3F) << 6) |
602                         (p[2] & 0x3F));
603         case 4:
604                 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
605                 return (LWCHAR) (
606                         ((p[0] & 0x07) << 18) |
607                         ((p[1] & 0x3F) << 12) | 
608                         ((p[2] & 0x3F) << 6) | 
609                         (p[3] & 0x3F));
610         case 5:
611                 /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
612                 return (LWCHAR) (
613                         ((p[0] & 0x03) << 24) |
614                         ((p[1] & 0x3F) << 18) | 
615                         ((p[2] & 0x3F) << 12) | 
616                         ((p[3] & 0x3F) << 6) | 
617                         (p[4] & 0x3F));
618         case 6:
619                 /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
620                 return (LWCHAR) (
621                         ((p[0] & 0x01) << 30) |
622                         ((p[1] & 0x3F) << 24) | 
623                         ((p[2] & 0x3F) << 18) | 
624                         ((p[3] & 0x3F) << 12) | 
625                         ((p[4] & 0x3F) << 6) | 
626                         (p[5] & 0x3F));
627         }
628 }
629
630 /*
631  * Store a character into a UTF-8 string.
632  */
633         public void
634 put_wchar(pp, ch)
635         char **pp;
636         LWCHAR ch;
637 {
638         if (!utf_mode || ch < 0x80) 
639         {
640                 /* 0xxxxxxx */
641                 *(*pp)++ = (char) ch;
642         } else if (ch < 0x800)
643         {
644                 /* 110xxxxx 10xxxxxx */
645                 *(*pp)++ = (char) (0xC0 | ((ch >> 6) & 0x1F));
646                 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
647         } else if (ch < 0x10000)
648         {
649                 /* 1110xxxx 10xxxxxx 10xxxxxx */
650                 *(*pp)++ = (char) (0xE0 | ((ch >> 12) & 0x0F));
651                 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
652                 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
653         } else if (ch < 0x200000)
654         {
655                 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
656                 *(*pp)++ = (char) (0xF0 | ((ch >> 18) & 0x07));
657                 *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
658                 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
659                 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
660         } else if (ch < 0x4000000)
661         {
662                 /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
663                 *(*pp)++ = (char) (0xF0 | ((ch >> 24) & 0x03));
664                 *(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
665                 *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
666                 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
667                 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
668         } else 
669         {
670                 /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
671                 *(*pp)++ = (char) (0xF0 | ((ch >> 30) & 0x01));
672                 *(*pp)++ = (char) (0x80 | ((ch >> 24) & 0x3F));
673                 *(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
674                 *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
675                 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
676                 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
677         }
678 }
679
680 /*
681  * Step forward or backward one character in a string.
682  */
683         public LWCHAR
684 step_char(pp, dir, limit)
685         char **pp;
686         signed int dir;
687         char *limit;
688 {
689         LWCHAR ch;
690         int len;
691         char *p = *pp;
692
693         if (!utf_mode)
694         {
695                 /* It's easy if chars are one byte. */
696                 if (dir > 0)
697                         ch = (LWCHAR) ((p < limit) ? *p++ : 0);
698                 else
699                         ch = (LWCHAR) ((p > limit) ? *--p : 0);
700         } else if (dir > 0)
701         {
702                 len = utf_len(*p);
703                 if (p + len > limit)
704                 {
705                         ch = 0;
706                         p = limit;
707                 } else
708                 {
709                         ch = get_wchar(p);
710                         p += len;
711                 }
712         } else
713         {
714                 while (p > limit && IS_UTF8_TRAIL(p[-1]))
715                         p--;
716                 if (p > limit)
717                         ch = get_wchar(--p);
718                 else
719                         ch = 0;
720         }
721         *pp = p;
722         return ch;
723 }
724
725 /*
726  * Unicode characters data
727  * Actual data is in the generated *.uni files.
728  */
729
730 #define DECLARE_RANGE_TABLE_START(name) \
731     static struct wchar_range name##_array[] = {
732 #define DECLARE_RANGE_TABLE_END(name) \
733     }; struct wchar_range_table name##_table = { name##_array, sizeof(name##_array)/sizeof(*name##_array) };
734
735 DECLARE_RANGE_TABLE_START(compose)
736 #include "compose.uni"
737 DECLARE_RANGE_TABLE_END(compose)
738
739 DECLARE_RANGE_TABLE_START(ubin)
740 #include "ubin.uni"
741 DECLARE_RANGE_TABLE_END(ubin)
742
743 DECLARE_RANGE_TABLE_START(wide)
744 #include "wide.uni"
745 DECLARE_RANGE_TABLE_END(wide)
746
747 /* comb_table is special pairs, not ranges. */
748 static struct wchar_range comb_table[] = {
749         {0x0644,0x0622}, {0x0644,0x0623}, {0x0644,0x0625}, {0x0644,0x0627},
750 };
751
752
753         static int
754 is_in_table(ch, table)
755         LWCHAR ch;
756         struct wchar_range_table *table;
757 {
758         int hi;
759         int lo;
760
761         /* Binary search in the table. */
762         if (ch < table->table[0].first)
763                 return 0;
764         lo = 0;
765         hi = table->count - 1;
766         while (lo <= hi)
767         {
768                 int mid = (lo + hi) / 2;
769                 if (ch > table->table[mid].last)
770                         lo = mid + 1;
771                 else if (ch < table->table[mid].first)
772                         hi = mid - 1;
773                 else
774                         return 1;
775         }
776         return 0;
777 }
778
779 /*
780  * Is a character a UTF-8 composing character?
781  * If a composing character follows any char, the two combine into one glyph.
782  */
783         public int
784 is_composing_char(ch)
785         LWCHAR ch;
786 {
787         return is_in_table(ch, &compose_table);
788 }
789
790 /*
791  * Should this UTF-8 character be treated as binary?
792  */
793         public int
794 is_ubin_char(ch)
795         LWCHAR ch;
796 {
797         return is_in_table(ch, &ubin_table);
798 }
799
800 /*
801  * Is this a double width UTF-8 character?
802  */
803         public int
804 is_wide_char(ch)
805         LWCHAR ch;
806 {
807         return is_in_table(ch, &wide_table);
808 }
809
810 /*
811  * Is a character a UTF-8 combining character?
812  * A combining char acts like an ordinary char, but if it follows
813  * a specific char (not any char), the two combine into one glyph.
814  */
815         public int
816 is_combining_char(ch1, ch2)
817         LWCHAR ch1;
818         LWCHAR ch2;
819 {
820         /* The table is small; use linear search. */
821         int i;
822         for (i = 0;  i < sizeof(comb_table)/sizeof(*comb_table);  i++)
823         {
824                 if (ch1 == comb_table[i].first &&
825                     ch2 == comb_table[i].last)
826                         return 1;
827         }
828         return 0;
829 }
830