1 #include "unicoderange.h"
3 #include <stdlib.h>
4 #include <string.h>
6 static unsigned int hex2int(char* s){
7 int res=0;
8 int i=0, mul=1;
9 while(s[i+1]!='\0') i++;
11 while(i>=0){
12 if (s[i] > '9') res += mul * (s[i]-'A'+10);
13 else res += mul * (s[i]-'0');
14 i--;
15 mul*=16;
16 }
17 return res;
18 }
20 UnicodeRange::UnicodeRange(const gchar* value){
21 if (!value) return;
22 gchar* val = (gchar*) value;
23 while(val[0] != '\0'){
24 if (val[0]=='U' && val[1]=='+'){
25 val += add_range(val);
26 } else {
27 // g_warning("adding unichar. unichar=%c", g_utf8_get_char(&val[0]));
28 this->unichars.push_back(g_utf8_get_char(&val[0]));
29 val++;
30 }
31 //skip spaces or commas
32 while(val[0]==' ' || val[0]==',') val++;
33 }
34 }
36 int
37 UnicodeRange::add_range(gchar* val){
38 Urange r;
39 //U+
40 val+=2;
41 int i=0, count=2;
42 while(val[i]!='\0' && val[i]!='-' && val[i]!=' ' && val[i]!=',') i++;
43 r.start = (gchar*) malloc((i+1)*sizeof(gchar*));
44 strncpy(r.start, val, i);
45 r.start[i] = '\0';
46 val+=i;
47 count+=i;
48 i=0;
49 if (val[0]=='-'){
50 val++;
51 while(val[i]!='\0' && val[i]!='-' && val[i]!=' ' && val[i]!=',') i++;
52 r.end = (gchar*) malloc((i+1)*sizeof(gchar*));
53 strncpy(r.end, val, i);
54 r.end[i] = '\0';
55 val+=i;
56 count+=i;
57 } else {
58 r.end=NULL;
59 }
60 // g_warning("adding range. from %s to %s", r.start, r.end);
61 this->range.push_back(r);
62 return count+1;
63 }
65 bool UnicodeRange::contains(gchar unicode){
66 for(unsigned int i=0;i<this->unichars.size();i++){
67 if ((gunichar) unicode == this->unichars[i]) return true;
68 }
70 unsigned int unival;
71 unival = g_utf8_get_char (&unicode);
72 // g_warning("unival=%d", unival);
73 char uni[9] = "00000000";
74 uni[8]= '\0';
75 unsigned char val;
76 for (unsigned int i=7; unival>0; i--){
77 val = unival & 0xf;
78 unival = unival >> 4;
79 if (val < 10) uni[i] = '0' + val;
80 else uni[i] = 'A'+ val - 10;
81 }
82 // g_warning("uni=%s", uni);
84 bool found;
85 for(unsigned int i=0;i<this->range.size();i++){
86 Urange r = this->range[i];
87 if (r.end){
88 // g_warning("hex2int: start=%d", hex2int(r.start));
89 // g_warning("hex2int: end=%d", hex2int(r.end));
90 if (unival >= hex2int(r.start) && unival <= hex2int(r.end)) return true;
91 } else {
92 found = true;
93 for (int pos=0;pos<8;pos++){
94 if (uni[pos]!='?' && uni[pos]!=r.start[pos]) found = false;
95 }
96 if (found) return true;
97 }
98 }
99 return false;
100 }