UNPKG

vox-core

Version:

Runtime de aplicaciones multiplataforma

160 lines (122 loc) 5.77 kB
var fs= require("fs"); var System= core.System; var VWPointer=System.VWPointer; var npfile= __dirname+"/charinfo.nlp"; var UnicodeDataHeader,DigitValues; var CharUnicodeInfo= module.exports= function(){ } CharUnicodeInfo.getUnicodeCategory=function(/*string */s, /* int [optional]*/ index){ index=index|0; if(index>=s.length){ throw new System.ArgumentOutRangeException("index"); } return CharUnicodeInfo.internalGetUnicodeCategory(s, index); } CharUnicodeInfo.isWhiteSpace= function(/*string o char */s, /* int */index){ switch (CharUnicodeInfo.GetUnicodeCategory(s, index)+0) { case UnicodeCategory.SpaceSeparator+0: case UnicodeCategory.LineSeparator+0: case UnicodeCategory.ParagraphSeparator+0: return true; default: return false; } } CharUnicodeInfo.internalGetUnicodeCategory=function(/*string or int*/ ch, /*int*/ offset) { if(typeof ch=="string"){ if(ch.length==1){ return CharUnicodeInfo.internalGetUnicodeCategory(ch.charCodeAt(0)); } return CharUnicodeInfo.internalGetUnicodeCategory(CharUnicodeInfo.internalConvertToUtf32(ch, offset)); } offset=offset|0; var num= CharUnicodeInfo.s_pCategoryLevel1Index.get(ch >> 8); num = CharUnicodeInfo.s_pCategoryLevel1Index.get(num + (ch >> 4 & 15)); var ptr; if(!(ptr=CharUnicodeInfo.s_pCategoryLevel1Index_byteptr)){ ptr=CharUnicodeInfo.s_pCategoryLevel1Index_byteptr=CharUnicodeInfo.s_pCategoryLevel1Index.castToType('byte'); } ptr.setPtr(CharUnicodeInfo.s_pCategoryLevel1Index.addPtr(num)); var b = ptr.get((ch & 15)); var category= CharUnicodeInfo.s_pCategoriesValue.get((b * 2) + offset); return System.Globalization.UnicodeCategory.parse(category); } // Devuelve int ... CharUnicodeInfo.internalConvertToUtf32=function(/*string*/ s, /*int*/ index) { if (index < s.length - 1) { //int num = (int)(s[index] - '\ud800'); var num= s.charCodeAt(index) - 55296; if (num >= 0 && num <= 1023) { //int num2 = (int)(s[index + 1] - '\udc00'); var num2= s.charCodeAt(index+1)- 56320; if (num2 >= 0 && num2 <= 1023) { return num * 1024 + num2 + 65536; } } } return s.charCodeAt(index); } CharUnicodeInfo.initTable= function(){ var buffer= fs.readFileSync(npfile); var ptr= UnicodeDataHeader.fromBytes(buffer); /* var fstream= new System.IO.FileStream(npfile, System.IO.FileMode.open, System.IO.FileAccess.read); var buf=new Buffer(64); fstream.read(buf,0,64); var ptr= UnicodeDataHeader.fromBytes(buf); */ var globalizationResourceBytePtr=0; CharUnicodeInfo.s_pCategoryLevel1Index = VWPointer.fromBytes(buffer, 'ushort', globalizationResourceBytePtr + ptr.offsetToCategoriesIndex); CharUnicodeInfo.s_pCategoriesValue = VWPointer.fromBytes(buffer, 'byte', globalizationResourceBytePtr + ptr.offsetToCategoriesValue); CharUnicodeInfo.s_pNumericLevel1Index = VWPointer.fromBytes(buffer, 'ushort', globalizationResourceBytePtr + ptr.offsetToNumbericIndex); CharUnicodeInfo.s_pNumericValues = VWPointer.fromBytes(buffer, 'byte', globalizationResourceBytePtr + ptr.offsetToNumbericValue); CharUnicodeInfo.s_pDigitValues = DigitValues.fromBytes(buffer, globalizationResourceBytePtr + (ptr.offsetToDigitValue*2)); /* CharUnicodeInfo.s_pCategoryLevel1Index = VWPointer.fromStream(fstream, 'ushort', globalizationResourceBytePtr + ptr.offsetToCategoriesIndex); CharUnicodeInfo.s_pCategoriesValue = VWPointer.fromStream(fstream, 'byte', globalizationResourceBytePtr + ptr.offsetToCategoriesValue); CharUnicodeInfo.s_pNumericLevel1Index = VWPointer.fromStream(fstream, 'ushort', globalizationResourceBytePtr + ptr.offsetToNumbericIndex); CharUnicodeInfo.s_pNumericValues = VWPointer.fromStream(fstream, 'byte', globalizationResourceBytePtr + ptr.offsetToNumbericValue); //CharUnicodeInfo.s_pDigitValues = DigitValues.fromBytes(buffer, globalizationResourceBytePtr + (ptr.offsetToDigitValue*2)); */ return true; } DigitValues= function(){ } DigitValues.prototype = Object.create(System.Structure.prototype, { constructor: { value: DigitValues } }); DigitValues.prototype.__defineGetter__("decimalDigit",function(){ return this.$buf.readInt8(this.$position); }); DigitValues.prototype.__defineGetter__("digit",function(){ return this.$buf.readInt8(this.$position+1); }); DigitValues.fromBytes=System.Structure.fromBytes; UnicodeDataHeader=function(){ } //UnicodeDataHeader.prototype.__proto__= System.Structure.prototype; UnicodeDataHeader.prototype = Object.create(System.Structure.prototype, { constructor: { value: UnicodeDataHeader } }); UnicodeDataHeader.fromBytes=System.Structure.fromBytes; UnicodeDataHeader.prototype.__defineGetter__("version", function(){ return this.$buf.readUInt16LE(this.$position+32); }); UnicodeDataHeader.prototype.__defineGetter__("offsetToCategoriesIndex", function(){ return this.$buf.readUInt32LE(this.$position+40); }); UnicodeDataHeader.prototype.__defineGetter__("offsetToCategoriesValue", function(){ return this.$buf.readUInt32LE(this.$position+44); }); UnicodeDataHeader.prototype.__defineGetter__("offsetToNumbericIndex", function(){ return this.$buf.readUInt32LE(this.$position+48); }); UnicodeDataHeader.prototype.__defineGetter__("offsetToDigitValue", function(){ return this.$buf.readUInt32LE(this.$position+52); }); UnicodeDataHeader.prototype.__defineGetter__("offsetToNumbericValue", function(){ return this.$buf.readUInt32LE(this.$position+56); }); CharUnicodeInfo.initTable();