add dependencies textencoder
This commit is contained in:
parent
feb24ff4af
commit
85732a042e
154
dist/pluie-bin.js
vendored
154
dist/pluie-bin.js
vendored
|
@ -6,10 +6,10 @@ require('./lib/bin')(pluie.bin);
|
||||||
module.exports = pluie;
|
module.exports = pluie;
|
||||||
|
|
||||||
},{"./lib/bin":2}],2:[function(require,module,exports){
|
},{"./lib/bin":2}],2:[function(require,module,exports){
|
||||||
'use strict';
|
|
||||||
|
|
||||||
module.exports = function (ns) {
|
module.exports = function (ns) {
|
||||||
|
|
||||||
|
require('./encoder')(ns);
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
String.prototype.removeEndNullBytes = function() {
|
String.prototype.removeEndNullBytes = function() {
|
||||||
return this.replace(/\0+$/g, '');
|
return this.replace(/\0+$/g, '');
|
||||||
|
@ -20,8 +20,8 @@ module.exports = function (ns) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
ns.enc = new TextEncoder("utf-8");
|
ns.enc = new ns.TextEncoder("utf-8");
|
||||||
ns.dec = new TextDecoder("utf-8", {fatal: true});
|
ns.dec = new ns.TextDecoder("utf-8", {fatal: true});
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
ns.intFromBytes = function(bytes)
|
ns.intFromBytes = function(bytes)
|
||||||
{
|
{
|
||||||
|
@ -103,9 +103,149 @@ module.exports = function (ns) {
|
||||||
return ns;
|
return ns;
|
||||||
}
|
}
|
||||||
|
|
||||||
},{"./file":3}],3:[function(require,module,exports){
|
},{"./encoder":3,"./file":4}],3:[function(require,module,exports){
|
||||||
|
function TextEncoderLite() {
|
||||||
|
}
|
||||||
|
function TextDecoderLite() {
|
||||||
|
}
|
||||||
|
|
||||||
|
(function () {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
// Taken from https://github.com/feross/buffer/blob/master/index.js
|
||||||
|
// Thanks Feross et al! :-)
|
||||||
|
|
||||||
|
function utf8ToBytes (string, units) {
|
||||||
|
units = units || Infinity
|
||||||
|
var codePoint
|
||||||
|
var length = string.length
|
||||||
|
var leadSurrogate = null
|
||||||
|
var bytes = []
|
||||||
|
var i = 0
|
||||||
|
|
||||||
|
for (; i < length; i++) {
|
||||||
|
codePoint = string.charCodeAt(i)
|
||||||
|
|
||||||
|
// is surrogate component
|
||||||
|
if (codePoint > 0xD7FF && codePoint < 0xE000) {
|
||||||
|
// last char was a lead
|
||||||
|
if (leadSurrogate) {
|
||||||
|
// 2 leads in a row
|
||||||
|
if (codePoint < 0xDC00) {
|
||||||
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||||
|
leadSurrogate = codePoint
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
// valid surrogate pair
|
||||||
|
codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000
|
||||||
|
leadSurrogate = null
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// no lead yet
|
||||||
|
|
||||||
|
if (codePoint > 0xDBFF) {
|
||||||
|
// unexpected trail
|
||||||
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||||
|
continue
|
||||||
|
} else if (i + 1 === length) {
|
||||||
|
// unpaired lead
|
||||||
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
// valid lead
|
||||||
|
leadSurrogate = codePoint
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (leadSurrogate) {
|
||||||
|
// valid bmp char, but last char was a lead
|
||||||
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||||
|
leadSurrogate = null
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode utf8
|
||||||
|
if (codePoint < 0x80) {
|
||||||
|
if ((units -= 1) < 0) break
|
||||||
|
bytes.push(codePoint)
|
||||||
|
} else if (codePoint < 0x800) {
|
||||||
|
if ((units -= 2) < 0) break
|
||||||
|
bytes.push(
|
||||||
|
codePoint >> 0x6 | 0xC0,
|
||||||
|
codePoint & 0x3F | 0x80
|
||||||
|
)
|
||||||
|
} else if (codePoint < 0x10000) {
|
||||||
|
if ((units -= 3) < 0) break
|
||||||
|
bytes.push(
|
||||||
|
codePoint >> 0xC | 0xE0,
|
||||||
|
codePoint >> 0x6 & 0x3F | 0x80,
|
||||||
|
codePoint & 0x3F | 0x80
|
||||||
|
)
|
||||||
|
} else if (codePoint < 0x200000) {
|
||||||
|
if ((units -= 4) < 0) break
|
||||||
|
bytes.push(
|
||||||
|
codePoint >> 0x12 | 0xF0,
|
||||||
|
codePoint >> 0xC & 0x3F | 0x80,
|
||||||
|
codePoint >> 0x6 & 0x3F | 0x80,
|
||||||
|
codePoint & 0x3F | 0x80
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
throw new Error('Invalid code point')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
function utf8Slice (buf, start, end) {
|
||||||
|
var res = ''
|
||||||
|
var tmp = ''
|
||||||
|
end = Math.min(buf.length, end || Infinity)
|
||||||
|
start = start || 0;
|
||||||
|
|
||||||
|
for (var i = start; i < end; i++) {
|
||||||
|
if (buf[i] <= 0x7F) {
|
||||||
|
res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i])
|
||||||
|
tmp = ''
|
||||||
|
} else {
|
||||||
|
tmp += '%' + buf[i].toString(16)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res + decodeUtf8Char(tmp)
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeUtf8Char (str) {
|
||||||
|
try {
|
||||||
|
return decodeURIComponent(str)
|
||||||
|
} catch (err) {
|
||||||
|
return String.fromCharCode(0xFFFD) // UTF 8 invalid char
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TextEncoderLite.prototype.encode = function (str) {
|
||||||
|
var result;
|
||||||
|
|
||||||
|
if ('undefined' === typeof Uint8Array) {
|
||||||
|
result = utf8ToBytes(str);
|
||||||
|
} else {
|
||||||
|
result = new Uint8Array(utf8ToBytes(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
TextDecoderLite.prototype.decode = function (bytes) {
|
||||||
|
return utf8Slice(bytes, 0, bytes.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
}());
|
||||||
|
|
||||||
|
module.exports = function(ns) {
|
||||||
|
ns.TextEncoder = TextEncoderLite;
|
||||||
|
ns.TextDecoder = TextDecoderLite;
|
||||||
|
}
|
||||||
|
|
||||||
|
},{}],4:[function(require,module,exports){
|
||||||
module.exports = function (ns) {
|
module.exports = function (ns) {
|
||||||
|
|
||||||
require('./header')(ns);
|
require('./header')(ns);
|
||||||
|
@ -169,9 +309,7 @@ module.exports = function (ns) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
},{"./header":4}],4:[function(require,module,exports){
|
},{"./header":5}],5:[function(require,module,exports){
|
||||||
'use strict';
|
|
||||||
|
|
||||||
module.exports = function (ns) {
|
module.exports = function (ns) {
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
2
dist/pluie-bin.min.js
vendored
2
dist/pluie-bin.min.js
vendored
|
@ -1 +1 @@
|
||||||
!function e(n,t,r){function i(a,o){if(!t[a]){if(!n[a]){var s="function"==typeof require&&require;if(!o&&s)return s(a,!0);if(u)return u(a,!0);var f=new Error("Cannot find module '"+a+"'");throw f.code="MODULE_NOT_FOUND",f}var c=t[a]={exports:{}};n[a][0].call(c.exports,function(e){var t=n[a][1][e];return i(t?t:e)},c,c.exports,e,n,t,r)}return t[a].exports}for(var u="function"==typeof require&&require,a=0;a<r.length;a++)i(r[a]);return i}({1:[function(e,n,t){"undefined"==typeof pluie&&(pluie={bin:{}}),e("./lib/bin")(pluie.bin),n.exports=pluie},{"./lib/bin":2}],2:[function(e,n,t){"use strict";n.exports=function(n){return String.prototype.removeEndNullBytes=function(){return this.replace(/\0+$/g,"")},String.prototype.removeStartNullBytes=function(){return this.replace(/^\0+/g,"")},n.enc=new TextEncoder("utf-8"),n.dec=new TextDecoder("utf-8",{fatal:!0}),n.intFromBytes=function(e){var n=null;if(null!=e&&e.length>0)for(var t=n=0,r=e.length;t<r;t++)n=256*n+e[t];return n},n.bytesFromInt=function(e,n){for(var t=new Uint8Array(n);e;)t.set([255&e],--n),e>>=8;return t},n.pack=function(e,n,t){var r=null;return"n"==n||"N"==n?t>=0&&t<=2147483647&&(r=this.bytesFromInt(t,"n"==n?2:4)):"a"==n&&(r=new Uint8Array(e),r.set(this.enc.encode(t),0)),r},n.unpack=function(e,n,t){var r=null;if("n"==n||"N"==n){var i=t.slice(0,e*("n"==n?2:4));r=this.intFromBytes(i)}else"a"==n&&(r=this.dec.decode(t.slice(0,e)).removeEndNullBytes());return r},n.mergeBuffers=function(e){for(var n=0,t=0,r=e.length;t<r;t++)null!=e[t]&&(n+=e[t].byteLength);for(var i=new Uint8Array(n),t=0,u=0,r=e.length;t<r;t++)null!=e[t]&&(i.set(new Uint8Array(e[t]),u),u+=e[t].byteLength);return i},n.getBuffer=function(e,n){var t=new Uint8Array(n?n:e.length);return t.set(new Uint8Array(e),0),t},e("./file")(n),n}},{"./file":3}],3:[function(e,n,t){"use strict";n.exports=function(n){e("./header")(n),n.File=function(e,t){this.header=t,this.binary=e,this.extractProperties=function(e){e||(e=this.binary),null!=e&&e.length>=this.header.def.length&&this.header.def.map.forEach(function(t){var r=e.slice(t.offset,t.offset+t.length),i=n.unpack(t.size,t.type,r);t.value=i})},this.buildProperties=function(e){var t=[];this.header.buildProperties(e),this.header.def.map.forEach(function(e){var r=n.pack(e.size,e.type,e.value);t.push(r)}),t=n.mergeBuffers(t)},this.extractProperties(e)},n.smpFile=function(e){return new n.File(e,new n.Header([["signature","a",9,"∢SMP0.2"],["size","N",1,""],["id","N",1,""],["from","a",32,""],["to","a",32,""],["channel","a",32,""],["command","n",1,""],["index","n",1,""],["split","n",1,""]]))},n.smpsFile=function(e){return new n.File(e,new n.Header([["signature","a",13,"ﷸSMPS0.2∢"],["size","N",1,""],["nonce","a",12,""],["cypher","a",135,""]]))},n.smpaFile=function(e){return new n.BaseFile(e,new n.Header([["signature","a",13,"ﷸSMPA0.2∢"],["size","N",1,""],["nonce","a",24,""],["cypher","a",199,""]]))}}},{"./header":4}],4:[function(e,n,t){"use strict";n.exports=function(e){var n=function(e,n,t,r){this.name=e,this.type=n,this.size=t,this.length="N"==n?4*t:"n"==n?2*t:t,this.value=r},t=function(e){var t=[];e.forEach(function(e){t.push(new n(e[0],e[1],e[2],e[3]))}),this.map=t;var r=[],i={},u=0,a=0;this.map.forEach(function(e){e.offset=u,r[r.length]=e.name,u+=e.length,i[e.name]=a++}),this.indexes=i,this.keys=r,this.length=u,this.getProperty=function(e){return this.map[this.indexes[e]]}};e.Header=function(e){this.def=new t(e),this.value=function(e){return this.def.getProperty(e).value},this.buildProperties=function(e){this.def.map.forEach(function(n){"signature"!=n.name&&"undefined"!=typeof e[n.name]&&(n.value=e[n.name])})}}}},{}]},{},[1]);
|
!function e(n,t,r){function i(u,f){if(!t[u]){if(!n[u]){var a="function"==typeof require&&require;if(!f&&a)return a(u,!0);if(o)return o(u,!0);var s=new Error("Cannot find module '"+u+"'");throw s.code="MODULE_NOT_FOUND",s}var c=t[u]={exports:{}};n[u][0].call(c.exports,function(e){var t=n[u][1][e];return i(t?t:e)},c,c.exports,e,n,t,r)}return t[u].exports}for(var o="function"==typeof require&&require,u=0;u<r.length;u++)i(r[u]);return i}({1:[function(e,n,t){"undefined"==typeof pluie&&(pluie={bin:{}}),e("./lib/bin")(pluie.bin),n.exports=pluie},{"./lib/bin":2}],2:[function(e,n,t){n.exports=function(n){return e("./encoder")(n),String.prototype.removeEndNullBytes=function(){return this.replace(/\0+$/g,"")},String.prototype.removeStartNullBytes=function(){return this.replace(/^\0+/g,"")},n.enc=new n.TextEncoder("utf-8"),n.dec=new n.TextDecoder("utf-8",{fatal:!0}),n.intFromBytes=function(e){var n=null;if(null!=e&&e.length>0)for(var t=n=0,r=e.length;t<r;t++)n=256*n+e[t];return n},n.bytesFromInt=function(e,n){for(var t=new Uint8Array(n);e;)t.set([255&e],--n),e>>=8;return t},n.pack=function(e,n,t){var r=null;return"n"==n||"N"==n?t>=0&&t<=2147483647&&(r=this.bytesFromInt(t,"n"==n?2:4)):"a"==n&&(r=new Uint8Array(e),r.set(this.enc.encode(t),0)),r},n.unpack=function(e,n,t){var r=null;if("n"==n||"N"==n){var i=t.slice(0,e*("n"==n?2:4));r=this.intFromBytes(i)}else"a"==n&&(r=this.dec.decode(t.slice(0,e)).removeEndNullBytes());return r},n.mergeBuffers=function(e){for(var n=0,t=0,r=e.length;t<r;t++)null!=e[t]&&(n+=e[t].byteLength);for(var i=new Uint8Array(n),t=0,o=0,r=e.length;t<r;t++)null!=e[t]&&(i.set(new Uint8Array(e[t]),o),o+=e[t].byteLength);return i},n.getBuffer=function(e,n){var t=new Uint8Array(n?n:e.length);return t.set(new Uint8Array(e),0),t},e("./file")(n),n}},{"./encoder":3,"./file":4}],3:[function(e,n,t){function r(){}function i(){}!function(){"use strict";function e(e,n){n=n||1/0;for(var t,r=e.length,i=null,o=[],u=0;u<r;u++){if(t=e.charCodeAt(u),t>55295&&t<57344){if(!i){if(t>56319){(n-=3)>-1&&o.push(239,191,189);continue}if(u+1===r){(n-=3)>-1&&o.push(239,191,189);continue}i=t;continue}if(t<56320){(n-=3)>-1&&o.push(239,191,189),i=t;continue}t=i-55296<<10|t-56320|65536,i=null}else i&&((n-=3)>-1&&o.push(239,191,189),i=null);if(t<128){if((n-=1)<0)break;o.push(t)}else if(t<2048){if((n-=2)<0)break;o.push(t>>6|192,63&t|128)}else if(t<65536){if((n-=3)<0)break;o.push(t>>12|224,t>>6&63|128,63&t|128)}else{if(!(t<2097152))throw new Error("Invalid code point");if((n-=4)<0)break;o.push(t>>18|240,t>>12&63|128,t>>6&63|128,63&t|128)}}return o}function n(e,n,r){var i="",o="";r=Math.min(e.length,r||1/0),n=n||0;for(var u=n;u<r;u++)e[u]<=127?(i+=t(o)+String.fromCharCode(e[u]),o=""):o+="%"+e[u].toString(16);return i+t(o)}function t(e){try{return decodeURIComponent(e)}catch(e){return String.fromCharCode(65533)}}r.prototype.encode=function(n){var t;return t="undefined"==typeof Uint8Array?e(n):new Uint8Array(e(n))},i.prototype.decode=function(e){return n(e,0,e.length)}}(),n.exports=function(e){e.TextEncoder=r,e.TextDecoder=i}},{}],4:[function(e,n,t){n.exports=function(n){e("./header")(n),n.File=function(e,t){this.header=t,this.binary=e,this.extractProperties=function(e){e||(e=this.binary),null!=e&&e.length>=this.header.def.length&&this.header.def.map.forEach(function(t){var r=e.slice(t.offset,t.offset+t.length),i=n.unpack(t.size,t.type,r);t.value=i})},this.buildProperties=function(e){var t=[];this.header.buildProperties(e),this.header.def.map.forEach(function(e){var r=n.pack(e.size,e.type,e.value);t.push(r)}),t=n.mergeBuffers(t)},this.extractProperties(e)},n.smpFile=function(e){return new n.File(e,new n.Header([["signature","a",9,"∢SMP0.2"],["size","N",1,""],["id","N",1,""],["from","a",32,""],["to","a",32,""],["channel","a",32,""],["command","n",1,""],["index","n",1,""],["split","n",1,""]]))},n.smpsFile=function(e){return new n.File(e,new n.Header([["signature","a",13,"ﷸSMPS0.2∢"],["size","N",1,""],["nonce","a",12,""],["cypher","a",135,""]]))},n.smpaFile=function(e){return new n.BaseFile(e,new n.Header([["signature","a",13,"ﷸSMPA0.2∢"],["size","N",1,""],["nonce","a",24,""],["cypher","a",199,""]]))}}},{"./header":5}],5:[function(e,n,t){n.exports=function(e){var n=function(e,n,t,r){this.name=e,this.type=n,this.size=t,this.length="N"==n?4*t:"n"==n?2*t:t,this.value=r},t=function(e){var t=[];e.forEach(function(e){t.push(new n(e[0],e[1],e[2],e[3]))}),this.map=t;var r=[],i={},o=0,u=0;this.map.forEach(function(e){e.offset=o,r[r.length]=e.name,o+=e.length,i[e.name]=u++}),this.indexes=i,this.keys=r,this.length=o,this.getProperty=function(e){return this.map[this.indexes[e]]}};e.Header=function(e){this.def=new t(e),this.value=function(e){return this.def.getProperty(e).value},this.buildProperties=function(e){this.def.map.forEach(function(n){"signature"!=n.name&&"undefined"!=typeof e[n.name]&&(n.value=e[n.name])})}}}},{}]},{},[1]);
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
module.exports = function (ns) {
|
module.exports = function (ns) {
|
||||||
|
|
||||||
|
require('./encoder')(ns);
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
String.prototype.removeEndNullBytes = function() {
|
String.prototype.removeEndNullBytes = function() {
|
||||||
return this.replace(/\0+$/g, '');
|
return this.replace(/\0+$/g, '');
|
||||||
|
@ -12,8 +12,8 @@ module.exports = function (ns) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
ns.enc = new TextEncoder("utf-8");
|
ns.enc = new ns.TextEncoder("utf-8");
|
||||||
ns.dec = new TextDecoder("utf-8", {fatal: true});
|
ns.dec = new ns.TextDecoder("utf-8", {fatal: true});
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
ns.intFromBytes = function(bytes)
|
ns.intFromBytes = function(bytes)
|
||||||
{
|
{
|
||||||
|
|
140
lib/encoder.js
Normal file
140
lib/encoder.js
Normal file
|
@ -0,0 +1,140 @@
|
||||||
|
function TextEncoderLite() {
|
||||||
|
}
|
||||||
|
function TextDecoderLite() {
|
||||||
|
}
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
// Taken from https://github.com/feross/buffer/blob/master/index.js
|
||||||
|
// Thanks Feross et al! :-)
|
||||||
|
|
||||||
|
function utf8ToBytes (string, units) {
|
||||||
|
units = units || Infinity
|
||||||
|
var codePoint
|
||||||
|
var length = string.length
|
||||||
|
var leadSurrogate = null
|
||||||
|
var bytes = []
|
||||||
|
var i = 0
|
||||||
|
|
||||||
|
for (; i < length; i++) {
|
||||||
|
codePoint = string.charCodeAt(i)
|
||||||
|
|
||||||
|
// is surrogate component
|
||||||
|
if (codePoint > 0xD7FF && codePoint < 0xE000) {
|
||||||
|
// last char was a lead
|
||||||
|
if (leadSurrogate) {
|
||||||
|
// 2 leads in a row
|
||||||
|
if (codePoint < 0xDC00) {
|
||||||
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||||
|
leadSurrogate = codePoint
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
// valid surrogate pair
|
||||||
|
codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000
|
||||||
|
leadSurrogate = null
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// no lead yet
|
||||||
|
|
||||||
|
if (codePoint > 0xDBFF) {
|
||||||
|
// unexpected trail
|
||||||
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||||
|
continue
|
||||||
|
} else if (i + 1 === length) {
|
||||||
|
// unpaired lead
|
||||||
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
// valid lead
|
||||||
|
leadSurrogate = codePoint
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (leadSurrogate) {
|
||||||
|
// valid bmp char, but last char was a lead
|
||||||
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||||
|
leadSurrogate = null
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode utf8
|
||||||
|
if (codePoint < 0x80) {
|
||||||
|
if ((units -= 1) < 0) break
|
||||||
|
bytes.push(codePoint)
|
||||||
|
} else if (codePoint < 0x800) {
|
||||||
|
if ((units -= 2) < 0) break
|
||||||
|
bytes.push(
|
||||||
|
codePoint >> 0x6 | 0xC0,
|
||||||
|
codePoint & 0x3F | 0x80
|
||||||
|
)
|
||||||
|
} else if (codePoint < 0x10000) {
|
||||||
|
if ((units -= 3) < 0) break
|
||||||
|
bytes.push(
|
||||||
|
codePoint >> 0xC | 0xE0,
|
||||||
|
codePoint >> 0x6 & 0x3F | 0x80,
|
||||||
|
codePoint & 0x3F | 0x80
|
||||||
|
)
|
||||||
|
} else if (codePoint < 0x200000) {
|
||||||
|
if ((units -= 4) < 0) break
|
||||||
|
bytes.push(
|
||||||
|
codePoint >> 0x12 | 0xF0,
|
||||||
|
codePoint >> 0xC & 0x3F | 0x80,
|
||||||
|
codePoint >> 0x6 & 0x3F | 0x80,
|
||||||
|
codePoint & 0x3F | 0x80
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
throw new Error('Invalid code point')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
function utf8Slice (buf, start, end) {
|
||||||
|
var res = ''
|
||||||
|
var tmp = ''
|
||||||
|
end = Math.min(buf.length, end || Infinity)
|
||||||
|
start = start || 0;
|
||||||
|
|
||||||
|
for (var i = start; i < end; i++) {
|
||||||
|
if (buf[i] <= 0x7F) {
|
||||||
|
res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i])
|
||||||
|
tmp = ''
|
||||||
|
} else {
|
||||||
|
tmp += '%' + buf[i].toString(16)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res + decodeUtf8Char(tmp)
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeUtf8Char (str) {
|
||||||
|
try {
|
||||||
|
return decodeURIComponent(str)
|
||||||
|
} catch (err) {
|
||||||
|
return String.fromCharCode(0xFFFD) // UTF 8 invalid char
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TextEncoderLite.prototype.encode = function (str) {
|
||||||
|
var result;
|
||||||
|
|
||||||
|
if ('undefined' === typeof Uint8Array) {
|
||||||
|
result = utf8ToBytes(str);
|
||||||
|
} else {
|
||||||
|
result = new Uint8Array(utf8ToBytes(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
TextDecoderLite.prototype.decode = function (bytes) {
|
||||||
|
return utf8Slice(bytes, 0, bytes.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
}());
|
||||||
|
|
||||||
|
module.exports = function(ns) {
|
||||||
|
ns.TextEncoder = TextEncoderLite;
|
||||||
|
ns.TextDecoder = TextDecoderLite;
|
||||||
|
}
|
|
@ -1,5 +1,3 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
module.exports = function (ns) {
|
module.exports = function (ns) {
|
||||||
|
|
||||||
require('./header')(ns);
|
require('./header')(ns);
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
module.exports = function (ns) {
|
module.exports = function (ns) {
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
Loading…
Reference in New Issue
Block a user