Imported Upstream version 0.66.1
[hcoop/debian/courier-authlib.git] / libs / unicode / unicode_tablookup.c
1 /*
2 ** Copyright 2011 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 **
5 */
6
7 #include "unicode_config.h"
8 #include "unicode.h"
9
10 #define BLOCK_SIZE 256
11
12 uint8_t unicode_tab_lookup(unicode_char ch,
13 const size_t *unicode_indextab,
14 size_t unicode_indextab_sizeof,
15 const uint8_t (*unicode_rangetab)[2],
16 const uint8_t *unicode_classtab,
17 uint8_t uclass)
18 {
19 size_t cl=ch / BLOCK_SIZE;
20
21 if (cl < unicode_indextab_sizeof-1)
22 {
23 const size_t start_pos=unicode_indextab[cl];
24 const uint8_t (*p)[2]=unicode_rangetab + start_pos;
25 size_t b=0, e=unicode_indextab[cl+1] - start_pos;
26 uint8_t chmodcl= ch & (BLOCK_SIZE-1);
27
28 while (b < e)
29 {
30 size_t n=b + (e-b)/2;
31
32 if (chmodcl >= p[n][0])
33 {
34 if (chmodcl <= p[n][1])
35 {
36 uclass=unicode_classtab[start_pos+n];
37 break;
38 }
39 b=n+1;
40 }
41 else
42 {
43 e=n;
44 }
45 }
46 }
47
48 return uclass;
49 }