]> www.wagner.pp.ru Git - oss/less.git/blob - charset.c
Added .gitignore
[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         SetConsoleOutputCP(65001);
327     _setmode(_fileno(stdout),_O_U8TEXT);
328     _setmode(_fileno(stderr),_O_U8TEXT);
329         if (isatty(fileno(stdin))) 
330                 _setmode(_fileno(stdin),_O_U8TEXT);
331         icharset("UTF-8",0);
332         
333 #else 
334 #if HAVE_LOCALE
335 #ifdef CODESET
336         /*
337          * Try using the codeset name as the charset name.
338          */
339         s = nl_langinfo(CODESET);
340         if (icharset(s, 1))
341                 return;
342 #endif
343 #endif
344
345 #if HAVE_STRSTR
346         /*
347          * Check whether LC_ALL, LC_CTYPE or LANG look like UTF-8 is used.
348          */
349         if ((s = lgetenv("LC_ALL")) != NULL ||
350             (s = lgetenv("LC_CTYPE")) != NULL ||
351             (s = lgetenv("LANG")) != NULL)
352         {
353                 if (   strstr(s, "UTF-8") != NULL || strstr(s, "utf-8") != NULL
354                     || strstr(s, "UTF8")  != NULL || strstr(s, "utf8")  != NULL)
355                         if (icharset("utf-8", 1))
356                                 return;
357         }
358 #endif
359
360 #if HAVE_LOCALE
361         /*
362          * Get character definitions from locale functions,
363          * rather than from predefined charset entry.
364          */
365         ilocale();
366 #if MSDOS_COMPILER
367         /*
368          * Default to "dos".
369          */
370         (void) icharset("dos", 1);
371 #else
372         /*
373          * Default to "latin1".
374          */
375         (void) icharset("latin1", 1);
376 #endif
377 #endif
378 #endif
379 }
380
381 /*
382  * Initialize charset data structures.
383  */
384         public void
385 init_charset()
386 {
387         char *s;
388
389 #if HAVE_LOCALE
390         setlocale(LC_ALL, "");
391 #endif
392
393         set_charset();
394
395         s = lgetenv("LESSBINFMT");
396         setbinfmt(s, &binfmt, "*s<%02X>");
397         
398         s = lgetenv("LESSUTFBINFMT");
399         setbinfmt(s, &utfbinfmt, "<U+%04lX>");
400 }
401
402 /*
403  * Is a given character a "binary" character?
404  */
405         public int
406 binary_char(c)
407         LWCHAR c;
408 {
409         if (utf_mode)
410                 return (is_ubin_char(c));
411         c &= 0377;
412         return (chardef[c] & IS_BINARY_CHAR);
413 }
414
415 /*
416  * Is a given character a "control" character?
417  */
418         public int
419 control_char(c)
420         LWCHAR c;
421 {
422         c &= 0377;
423         return (chardef[c] & IS_CONTROL_CHAR);
424 }
425
426 /*
427  * Return the printable form of a character.
428  * For example, in the "ascii" charset '\3' is printed as "^C".
429  */
430         public char *
431 prchar(c)
432         LWCHAR c;
433 {
434         /* {{ This buffer can be overrun if LESSBINFMT is a long string. }} */
435         static char buf[32];
436
437         c &= 0377;
438         if ((c < 128 || !utf_mode) && !control_char(c))
439                 SNPRINTF1(buf, sizeof(buf), "%c", (int) c);
440         else if (c == ESC)
441                 strcpy(buf, "ESC");
442 #if IS_EBCDIC_HOST
443         else if (!binary_char(c) && c < 64)
444                 SNPRINTF1(buf, sizeof(buf), "^%c",
445                 /*
446                  * This array roughly inverts CONTROL() #defined in less.h,
447                  * and should be kept in sync with CONTROL() and IBM-1047.
448                  */
449                 "@ABC.I.?...KLMNO"
450                 "PQRS.JH.XY.."
451                 "\\]^_"
452                 "......W[.....EFG"
453                 "..V....D....TU.Z"[c]);
454 #else
455         else if (c < 128 && !control_char(c ^ 0100))
456                 SNPRINTF1(buf, sizeof(buf), "^%c", (int) (c ^ 0100));
457 #endif
458         else
459                 SNPRINTF1(buf, sizeof(buf), binfmt, c);
460         return (buf);
461 }
462
463 /*
464  * Return the printable form of a UTF-8 character.
465  */
466         public char *
467 prutfchar(ch)
468         LWCHAR ch;
469 {
470         static char buf[32];
471
472         if (ch == ESC)
473                 strcpy(buf, "ESC");
474         else if (ch < 128 && control_char(ch))
475         {
476                 if (!control_char(ch ^ 0100))
477                         SNPRINTF1(buf, sizeof(buf), "^%c", ((char) ch) ^ 0100);
478                 else
479                         SNPRINTF1(buf, sizeof(buf), binfmt, (char) ch);
480         } else if (is_ubin_char(ch))
481         {
482                 SNPRINTF1(buf, sizeof(buf), utfbinfmt, ch);
483         } else
484         {
485                 char *p = buf;
486                 if (ch >= 0x80000000)
487                         ch = 0xFFFD; /* REPLACEMENT CHARACTER */
488                 put_wchar(&p, ch);
489                 *p = '\0';
490         }
491         return (buf);
492 }
493
494 /*
495  * Get the length of a UTF-8 character in bytes.
496  */
497         public int
498 utf_len(ch)
499         char ch;
500 {
501         if ((ch & 0x80) == 0)
502                 return 1;
503         if ((ch & 0xE0) == 0xC0)
504                 return 2;
505         if ((ch & 0xF0) == 0xE0)
506                 return 3;
507         if ((ch & 0xF8) == 0xF0)
508                 return 4;
509         if ((ch & 0xFC) == 0xF8)
510                 return 5;
511         if ((ch & 0xFE) == 0xFC)
512                 return 6;
513         /* Invalid UTF-8 encoding. */
514         return 1;
515 }
516
517 /*
518  * Does the parameter point to the lead byte of a well-formed UTF-8 character?
519  */
520         public int
521 is_utf8_well_formed(s, slen)
522         unsigned char *s;
523         int slen;
524 {
525         int i;
526         int len;
527
528         if (IS_UTF8_INVALID(s[0]))
529                 return (0);
530
531         len = utf_len((char) s[0]);
532         if (len > slen)
533                 return (0);
534         if (len == 1)
535                 return (1);
536         if (len == 2)
537         {
538                 if (s[0] < 0xC2)
539                     return (0);
540         } else
541         {
542                 unsigned char mask;
543                 mask = (~((1 << (8-len)) - 1)) & 0xFF;
544                 if (s[0] == mask && (s[1] & mask) == 0x80)
545                         return (0);
546         }
547
548         for (i = 1;  i < len;  i++)
549                 if (!IS_UTF8_TRAIL(s[i]))
550                         return (0);
551         return (1);
552 }
553
554 /*
555  * Return number of invalid UTF-8 sequences found in a buffer.
556  */
557         public int
558 utf_bin_count(data, len)
559         unsigned char *data;
560         int len;
561 {
562         int bin_count = 0;
563         while (len > 0)
564         {
565                 if (is_utf8_well_formed(data, len))
566                 {
567                         int clen = utf_len(*data);
568                         data += clen;
569                         len -= clen;
570                 } else
571                 {
572                         /* Skip to next lead byte. */
573                         bin_count++;
574                         do {
575                                 ++data;
576                                 --len;
577                         } while (len > 0 && !IS_UTF8_LEAD(*data));
578                 }
579         }
580         return (bin_count);
581 }
582
583 /*
584  * Get the value of a UTF-8 character.
585  */
586         public LWCHAR
587 get_wchar(p)
588         char *p;
589 {
590         switch (utf_len(p[0]))
591         {
592         case 1:
593         default:
594                 /* 0xxxxxxx */
595                 return (LWCHAR)
596                         (p[0] & 0xFF);
597         case 2:
598                 /* 110xxxxx 10xxxxxx */
599                 return (LWCHAR) (
600                         ((p[0] & 0x1F) << 6) |
601                         (p[1] & 0x3F));
602         case 3:
603                 /* 1110xxxx 10xxxxxx 10xxxxxx */
604                 return (LWCHAR) (
605                         ((p[0] & 0x0F) << 12) |
606                         ((p[1] & 0x3F) << 6) |
607                         (p[2] & 0x3F));
608         case 4:
609                 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
610                 return (LWCHAR) (
611                         ((p[0] & 0x07) << 18) |
612                         ((p[1] & 0x3F) << 12) | 
613                         ((p[2] & 0x3F) << 6) | 
614                         (p[3] & 0x3F));
615         case 5:
616                 /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
617                 return (LWCHAR) (
618                         ((p[0] & 0x03) << 24) |
619                         ((p[1] & 0x3F) << 18) | 
620                         ((p[2] & 0x3F) << 12) | 
621                         ((p[3] & 0x3F) << 6) | 
622                         (p[4] & 0x3F));
623         case 6:
624                 /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
625                 return (LWCHAR) (
626                         ((p[0] & 0x01) << 30) |
627                         ((p[1] & 0x3F) << 24) | 
628                         ((p[2] & 0x3F) << 18) | 
629                         ((p[3] & 0x3F) << 12) | 
630                         ((p[4] & 0x3F) << 6) | 
631                         (p[5] & 0x3F));
632         }
633 }
634
635 /*
636  * Store a character into a UTF-8 string.
637  */
638         public void
639 put_wchar(pp, ch)
640         char **pp;
641         LWCHAR ch;
642 {
643         if (!utf_mode || ch < 0x80) 
644         {
645                 /* 0xxxxxxx */
646                 *(*pp)++ = (char) ch;
647         } else if (ch < 0x800)
648         {
649                 /* 110xxxxx 10xxxxxx */
650                 *(*pp)++ = (char) (0xC0 | ((ch >> 6) & 0x1F));
651                 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
652         } else if (ch < 0x10000)
653         {
654                 /* 1110xxxx 10xxxxxx 10xxxxxx */
655                 *(*pp)++ = (char) (0xE0 | ((ch >> 12) & 0x0F));
656                 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
657                 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
658         } else if (ch < 0x200000)
659         {
660                 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
661                 *(*pp)++ = (char) (0xF0 | ((ch >> 18) & 0x07));
662                 *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
663                 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
664                 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
665         } else if (ch < 0x4000000)
666         {
667                 /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
668                 *(*pp)++ = (char) (0xF0 | ((ch >> 24) & 0x03));
669                 *(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
670                 *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
671                 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
672                 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
673         } else 
674         {
675                 /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
676                 *(*pp)++ = (char) (0xF0 | ((ch >> 30) & 0x01));
677                 *(*pp)++ = (char) (0x80 | ((ch >> 24) & 0x3F));
678                 *(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
679                 *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
680                 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
681                 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
682         }
683 }
684
685 /*
686  * Step forward or backward one character in a string.
687  */
688         public LWCHAR
689 step_char(pp, dir, limit)
690         char **pp;
691         signed int dir;
692         char *limit;
693 {
694         LWCHAR ch;
695         int len;
696         char *p = *pp;
697
698         if (!utf_mode)
699         {
700                 /* It's easy if chars are one byte. */
701                 if (dir > 0)
702                         ch = (LWCHAR) ((p < limit) ? *p++ : 0);
703                 else
704                         ch = (LWCHAR) ((p > limit) ? *--p : 0);
705         } else if (dir > 0)
706         {
707                 len = utf_len(*p);
708                 if (p + len > limit)
709                 {
710                         ch = 0;
711                         p = limit;
712                 } else
713                 {
714                         ch = get_wchar(p);
715                         p += len;
716                 }
717         } else
718         {
719                 while (p > limit && IS_UTF8_TRAIL(p[-1]))
720                         p--;
721                 if (p > limit)
722                         ch = get_wchar(--p);
723                 else
724                         ch = 0;
725         }
726         *pp = p;
727         return ch;
728 }
729
730 /*
731  * Unicode characters data
732  * Actual data is in the generated *.uni files.
733  */
734
735 #define DECLARE_RANGE_TABLE_START(name) \
736     static struct wchar_range name##_array[] = {
737 #define DECLARE_RANGE_TABLE_END(name) \
738     }; struct wchar_range_table name##_table = { name##_array, sizeof(name##_array)/sizeof(*name##_array) };
739
740 DECLARE_RANGE_TABLE_START(compose)
741 #include "compose.uni"
742 DECLARE_RANGE_TABLE_END(compose)
743
744 DECLARE_RANGE_TABLE_START(ubin)
745 #include "ubin.uni"
746 DECLARE_RANGE_TABLE_END(ubin)
747
748 DECLARE_RANGE_TABLE_START(wide)
749 #include "wide.uni"
750 DECLARE_RANGE_TABLE_END(wide)
751
752 /* comb_table is special pairs, not ranges. */
753 static struct wchar_range comb_table[] = {
754         {0x0644,0x0622}, {0x0644,0x0623}, {0x0644,0x0625}, {0x0644,0x0627},
755 };
756
757
758         static int
759 is_in_table(ch, table)
760         LWCHAR ch;
761         struct wchar_range_table *table;
762 {
763         int hi;
764         int lo;
765
766         /* Binary search in the table. */
767         if (ch < table->table[0].first)
768                 return 0;
769         lo = 0;
770         hi = table->count - 1;
771         while (lo <= hi)
772         {
773                 int mid = (lo + hi) / 2;
774                 if (ch > table->table[mid].last)
775                         lo = mid + 1;
776                 else if (ch < table->table[mid].first)
777                         hi = mid - 1;
778                 else
779                         return 1;
780         }
781         return 0;
782 }
783
784 /*
785  * Is a character a UTF-8 composing character?
786  * If a composing character follows any char, the two combine into one glyph.
787  */
788         public int
789 is_composing_char(ch)
790         LWCHAR ch;
791 {
792         return is_in_table(ch, &compose_table);
793 }
794
795 /*
796  * Should this UTF-8 character be treated as binary?
797  */
798         public int
799 is_ubin_char(ch)
800         LWCHAR ch;
801 {
802         return is_in_table(ch, &ubin_table);
803 }
804
805 /*
806  * Is this a double width UTF-8 character?
807  */
808         public int
809 is_wide_char(ch)
810         LWCHAR ch;
811 {
812         return is_in_table(ch, &wide_table);
813 }
814
815 /*
816  * Is a character a UTF-8 combining character?
817  * A combining char acts like an ordinary char, but if it follows
818  * a specific char (not any char), the two combine into one glyph.
819  */
820         public int
821 is_combining_char(ch1, ch2)
822         LWCHAR ch1;
823         LWCHAR ch2;
824 {
825         /* The table is small; use linear search. */
826         int i;
827         for (i = 0;  i < sizeof(comb_table)/sizeof(*comb_table);  i++)
828         {
829                 if (ch1 == comb_table[i].first &&
830                     ch2 == comb_table[i].last)
831                         return 1;
832         }
833         return 0;
834 }
835