UNPKG

ilib

Version:

iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

2 lines 2.44 kB
// !data charset/UTF-8 var Charset=require("./Charset.js"),Charmap=require("./Charmap.js"),IString=require("./IString.js"),UTF8=function(options){"object"==typeof(options=options||{sync:!0}).charset&&options.charset instanceof Charset?(this.charset=options.charset,this._init(options)):new Charset({name:"UTF-8",sync:options.sync,loadParams:options.loadParams,onLoad:ilib.bind(this,function(cs){this.charset=cs,this._init(options)})})};UTF8.prototype=new Charmap({noinstance:!0}),UTF8.prototype.parent=Charmap,(UTF8.prototype.constructor=UTF8).prototype._init=function(options){this._calcExpansionFactor(),"function"==typeof options.onLoad&&options.onLoad(this)},UTF8.prototype.validate=function(bytes){for(var len,i=0;i<bytes.length;)if(0==(128&bytes[i]))i++;else{if(192==(192&bytes[i]))len=2;else if(224==(224&bytes[i]))len=3;else{if(240!=(240&bytes[i]))return!1;len=4}if(i+len>bytes.length)return!1;for(var j=1;j<len;j++)if(128!=(128&bytes[i+j]))return!1;i+=len}return!0},UTF8.prototype.mapToUnicode=function(bytes){if("undefined"!=typeof Buffer)return Buffer.from(bytes).toString("utf8");for(var ret="",i=0;i<bytes.length;)if(0===bytes[i])i=bytes.length;else if(0==(128&bytes[i]))ret+=String.fromCharCode(bytes[i++]);else if(192==(224&bytes[i])){if(i+1>=bytes.length||128!=(128&bytes[i+1]))throw"invalid utf-8 bytes";ret+=String.fromCharCode((31&bytes[i])<<6|63&bytes[i+1]),i+=2}else if(224==(240&bytes[i])){if(i+2>=bytes.length||128!=(128&bytes[i+1])||128!=(128&bytes[i+2]))throw"invalid utf-8 bytes";ret+=String.fromCharCode((15&bytes[i])<<12|(63&bytes[i+1])<<6|63&bytes[i+2]),i+=3}else{if(240!=(248&bytes[i]))throw"invalid utf-8 bytes";if(i+3>=bytes.length||128!=(128&bytes[i+1])||128!=(128&bytes[i+2])||128!=(128&bytes[i+3]))throw"invalid utf-8 bytes";ret+=IString.fromCodePoint((7&bytes[i])<<18|(63&bytes[i+1])<<12|(63&bytes[i+2])<<6|63&bytes[i+3]),i+=4}return ret},UTF8.prototype.mapToNative=function(str){if("undefined"!=typeof Buffer)return b=Buffer.from(str,"utf8"),new Uint8Array(b);for(var b=str instanceof IString?str:new IString(str),it=b.iterator(),ret=new Uint8Array(4*b.length+1),i=0;it.hasNext();){var c=it.next();127<c?2047<c?65535<c?(ret[i]=240|c>>18&3,ret[i+1]=128|c>>12&63,ret[i+2]=128|c>>6&63,ret[i+3]=128|63&c,i+=4):(ret[i]=224|c>>12&15,ret[i+1]=128|c>>6&63,ret[i+2]=128|63&c,i+=3):(ret[i]=192|c>>6&31,ret[i+1]=128|63&c,i+=2):ret[i++]=127&c}return ret[i]=0,ret},Charmap._algorithms["UTF-8"]=UTF8,module.exports=UTF8;