1 /*
2 Copyright (c) 2017 Thomas Wolff towo@towo.net
3 
4 
5  */
6 #include <wctype.h>
7 #include <stdint.h>
8 #include "categories.h"
9 
10 struct _category {
11   enum category cat: 8;
12   uint_least32_t first: 24;
13   uint_least16_t delta;
14 }
15 #ifdef _HAVE_BITFIELDS_IN_PACKED_STRUCTS
16 __attribute__((packed))
17 #endif
18 ;
19 
20 static const struct _category categories[] = {
21 #include "categories.t"
22 };
23 
24 static enum category
bisearch_cat(uint32_t ucs,const struct _category * table,int max)25 bisearch_cat(uint32_t ucs, const struct _category *table, int max)
26 {
27   int min = 0;
28   int mid;
29 
30   if (ucs < table[0].first || ucs > (uint32_t) (table[max].first + table[max].delta))
31     return CAT_error;
32   while (max >= min)
33     {
34       mid = (min + max) / 2;
35       if (ucs > (uint32_t) (table[mid].first + table[mid].delta))
36 	min = mid + 1;
37       else if (ucs < table[mid].first)
38 	max = mid - 1;
39       else
40 	return table[mid].cat;
41     }
42   return CAT_error;
43 }
44 
category(uint32_t ucs)45 enum category category(uint32_t ucs)
46 {
47   return bisearch_cat(ucs, categories,
48 		      sizeof(categories) / sizeof(*categories) - 1);
49 }
50