xref: /netbsd-src/external/bsd/less/dist/lesstest/unicode.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 #include "lt_types.h"
2 
3 typedef struct wchar_range { wchar first, last; } wchar_range;
4 
5 static wchar_range wide_chars[] = {
6 #include "../wide.uni"
7 };
8 static wchar_range compose_table[] = {
9 #include "../compose.uni"
10 };
11 static wchar_range fmt_table[] = {
12 #include "../fmt.uni"
13 };
14 static wchar_range comb_table[] = {
15 	{0x0644,0x0622}, {0x0644,0x0623}, {0x0644,0x0625}, {0x0644,0x0627},
16 };
17 
18 static int is_in_table(wchar ch, wchar_range table[], int count) {
19 	if (ch < table[0].first)
20 		return 0;
21 	int lo = 0;
22 	int hi = count - 1;
23 	while (lo <= hi) {
24 		int mid = (lo + hi) / 2;
25 		if (ch > table[mid].last)
26 			lo = mid + 1;
27 		else if (ch < table[mid].first)
28 			hi = mid - 1;
29 		else
30 			return 1;
31 	}
32 	return 0;
33 }
34 
35 int is_wide_char(wchar ch) {
36 	return is_in_table(ch, wide_chars, countof(wide_chars));
37 }
38 
39 int is_composing_char(wchar ch) {
40 	return is_in_table(ch, compose_table, countof(compose_table)) ||
41 	       is_in_table(ch, fmt_table, countof(fmt_table));
42 }
43 
44 int is_combining_char(wchar ch1, wchar ch2) {
45 	int i;
46 	for (i = 0; i < countof(comb_table); i++) {
47 		if (ch1 == comb_table[i].first &&
48 		    ch2 == comb_table[i].last)
49 			return 1;
50 	}
51 	return 0;
52 }
53