unicoderange.cpp revision e9d2b0cdc683ae267aade30e361f23c30cbaef7f
351N/A#include "unicoderange.h"
351N/A
351N/A#include <stdlib.h>
351N/A#include <string.h>
351N/A
351N/Astatic unsigned int hex2int(char* s){
351N/A int res=0;
351N/A int i=0, mul=1;
351N/A while(s[i+1]!='\0') i++;
351N/A
351N/A while(i>=0){
351N/A if (s[i] > '9') res += mul * (s[i]-'A'+10);
351N/A else res += mul * (s[i]-'0');
351N/A i--;
351N/A mul*=16;
351N/A }
351N/A return res;
351N/A}
351N/A
351N/AUnicodeRange::UnicodeRange(const gchar* value){
351N/A gchar* val = (gchar*) value;
351N/A while(val[0] != '\0'){
351N/A if (val[0]=='U' && val[1]=='+'){
351N/A val += add_range(val);
351N/A } else {
351N/A// g_warning("adding unichar. unichar=%c", g_utf8_get_char(&val[0]));
351N/A this->unichars.push_back(g_utf8_get_char(&val[0]));
351N/A val++;
351N/A }
351N/A //skip spaces or commas
351N/A while(val[0]==' ' || val[0]==',') val++;
351N/A }
351N/A}
351N/A
351N/Aint
351N/AUnicodeRange::add_range(gchar* val){
351N/A Urange r;
351N/A //U+
351N/A val+=2;
351N/A int i=0, count=2;
351N/A while(val[i]!='\0' && val[i]!='-' && val[i]!=' ' && val[i]!=',') i++;
351N/A r.start = (gchar*) malloc((i+1)*sizeof(gchar*));
351N/A strncpy(r.start, val, i);
351N/A r.start[i] = '\0';
351N/A val+=i;
351N/A count+=i;
351N/A i=0;
351N/A if (val[0]=='-'){
351N/A val++;
351N/A while(val[i]!='\0' && val[i]!='-' && val[i]!=' ' && val[i]!=',') i++;
351N/A r.end = (gchar*) malloc((i+1)*sizeof(gchar*));
351N/A strncpy(r.end, val, i);
351N/A r.end[i] = '\0';
351N/A val+=i;
351N/A count+=i;
351N/A } else {
351N/A r.end=NULL;
351N/A }
351N/A// g_warning("adding range. from %s to %s", r.start, r.end);
351N/A this->range.push_back(r);
351N/A return count+1;
351N/A}
351N/A
351N/Abool UnicodeRange::contains(gchar unicode){
351N/A for(unsigned int i=0;i<this->unichars.size();i++){
351N/A if ((gunichar) unicode == this->unichars[i]) return true;
351N/A }
351N/A
351N/A unsigned int unival;
351N/A unival = g_utf8_get_char (&unicode);
351N/A// g_warning("unival=%d", unival);
351N/A char uni[9] = "00000000";
351N/A uni[8]= '\0';
351N/A unsigned char val;
351N/A for (unsigned int i=7; unival>0; i--){
351N/A val = unival & 0xf;
351N/A unival = unival >> 4;
351N/A if (val < 10) uni[i] = '0' + val;
351N/A else uni[i] = 'A'+ val - 10;
351N/A }
351N/A// g_warning("uni=%s", uni);
351N/A
351N/A bool found;
351N/A for(unsigned int i=0;i<this->range.size();i++){
351N/A Urange r = this->range[i];
351N/A if (r.end){
351N/A// g_warning("hex2int: start=%d", hex2int(r.start));
351N/A// g_warning("hex2int: end=%d", hex2int(r.end));
351N/A if (unival >= hex2int(r.start) && unival <= hex2int(r.end)) return true;
351N/A } else {
351N/A found = true;
351N/A for (int pos=0;pos<8;pos++){
351N/A if (uni[pos]!='?' && uni[pos]!=r.start[pos]) found = false;
351N/A }
351N/A if (found) return true;
351N/A }
351N/A }
351N/A return false;
351N/A}
351N/A