"use strict";function Utf8DecodeWorker(){GenericWorker.call(this,"utf-8 decode"),this.leftOver=null}function Utf8EncodeWorker(){GenericWorker.call(this,"utf-8 encode")}for(var utils=require("./utils"),support=require("./support"),nodejsUtils=require("./nodejsUtils"),GenericWorker=require("./stream/GenericWorker"),_utf8len=new Array(256),i=0;i<256;i++)_utf8len[i]=i>=252?6:i>=248?5:i>=240?4:i>=224?3:i>=192?2:1;_utf8len[254]=_utf8len[254]=1;var string2buf=function(e){var r,t,n,o,i,u=e.length,s=0;for(o=0;o<u;o++)t=e.charCodeAt(o),55296===(64512&t)&&o+1<u&&(n=e.charCodeAt(o+1),56320===(64512&n)&&(t=65536+(t-55296<<10)+(n-56320),o++)),s+=t<128?1:t<2048?2:t<65536?3:4;for(r=support.uint8array?new Uint8Array(s):new Array(s),i=0,o=0;i<s;o++)t=e.charCodeAt(o),55296===(64512&t)&&o+1<u&&(n=e.charCodeAt(o+1),56320===(64512&n)&&(t=65536+(t-55296<<10)+(n-56320),o++)),t<128?r[i++]=t:t<2048?(r[i++]=192|t>>>6,r[i++]=128|63&t):t<65536?(r[i++]=224|t>>>12,r[i++]=128|t>>>6&63,r[i++]=128|63&t):(r[i++]=240|t>>>18,r[i++]=128|t>>>12&63,r[i++]=128|t>>>6&63,r[i++]=128|63&t);return r},utf8border=function(e,r){var t;for(r=r||e.length,r>e.length&&(r=e.length),t=r-1;t>=0&&128===(192&e[t]);)t--;return t<0?r:0===t?r:t+_utf8len[e[t]]>r?t:r},buf2string=function(e){var r,t,n,o,i=e.length,u=new Array(2*i);for(t=0,r=0;r<i;)if(n=e[r++],n<128)u[t++]=n;else if(o=_utf8len[n],o>4)u[t++]=65533,r+=o-1;else{for(n&=2===o?31:3===o?15:7;o>1&&r<i;)n=n<<6|63&e[r++],o--;o>1?u[t++]=65533:n<65536?u[t++]=n:(n-=65536,u[t++]=55296|n>>10&1023,u[t++]=56320|1023&n)}return u.length!==t&&(u.subarray?u=u.subarray(0,t):u.length=t),utils.applyFromCharCode(u)};exports.utf8encode=function(e){return support.nodebuffer?nodejsUtils.newBuffer(e,"utf-8"):string2buf(e)},exports.utf8decode=function(e){return support.nodebuffer?utils.transformTo("nodebuffer",e).toString("utf-8"):(e=utils.transformTo(support.uint8array?"uint8array":"array",e),buf2string(e))},utils.inherits(Utf8DecodeWorker,GenericWorker),Utf8DecodeWorker.prototype.processChunk=function(e){var r=utils.transformTo(support.uint8array?"uint8array":"array",e.data);if(this.leftOver&&this.leftOver.length){if(support.uint8array){var t=r;r=new Uint8Array(t.length+this.leftOver.length),r.set(this.leftOver,0),r.set(t,this.leftOver.length)}else r=this.leftOver.concat(r);this.leftOver=null}var n=utf8border(r),o=r;n!==r.length&&(support.uint8array?(o=r.subarray(0,n),this.leftOver=r.subarray(n,r.length)):(o=r.slice(0,n),this.leftOver=r.slice(n,r.length))),this.push({data:exports.utf8decode(o),meta:e.meta})},Utf8DecodeWorker.prototype.flush=function(){this.leftOver&&this.leftOver.length&&(this.push({data:exports.utf8decode(this.leftOver),meta:{}}),this.leftOver=null)},exports.Utf8DecodeWorker=Utf8DecodeWorker,utils.inherits(Utf8EncodeWorker,GenericWorker),Utf8EncodeWorker.prototype.processChunk=function(e){this.push({data:exports.utf8encode(e.data),meta:e.meta})},exports.Utf8EncodeWorker=Utf8EncodeWorker;