|
32 | 32 |
|
33 | 33 | var sfcc = function sfcc(x) { return String.fromCharCode(x); };
|
34 | 34 | var cca = function cca(x){ return x.charCodeAt(0); };
|
35 |
| - if(typeof Buffer !== 'undefined') { |
| 35 | + |
| 36 | + var has_buf = (typeof Buffer !== 'undefined'); |
| 37 | + if(has_buf) { |
36 | 38 | var mdl = 1024, mdb = new Buffer(mdl);
|
37 | 39 | var make_EE = function make_EE(E){
|
38 | 40 | var EE = new Buffer(65536);
|
|
67 | 69 | }
|
68 | 70 | }
|
69 | 71 | out.length = j;
|
| 72 | + out = out.slice(0,j); |
70 | 73 | } else {
|
71 | 74 | out = Buffer(len);
|
72 | 75 | for(i = 0; i < len; ++i) out[i] = EE[data[i].charCodeAt(0)];
|
|
125 | 128 | out[k++] = EE[j+1] || EE[j]; if(EE[j+1] > 0) out[k++] = EE[j];
|
126 | 129 | }
|
127 | 130 | out.length = k;
|
| 131 | + out = out.slice(0,k); |
128 | 132 | } else if(Buffer.isBuffer(data)) {
|
129 | 133 | for(i = k = 0; i < len; ++i) {
|
130 | 134 | D = data[i];
|
|
139 | 143 | }
|
140 | 144 | }
|
141 | 145 | out.length = k;
|
| 146 | + out = out.slice(0,k); |
142 | 147 | } else {
|
143 | 148 | for(i = k = 0; i < len; i++) {
|
144 | 149 | j = data[i].charCodeAt(0)*2;
|
|
237 | 242 | }
|
238 | 243 |
|
239 | 244 | var encache = function encache() {
|
240 |
| - if(typeof Buffer !== 'undefined') { |
| 245 | + if(has_buf) { |
241 | 246 | if(cpdcache[sbcs_cache[0]]) return;
|
242 | 247 | var i, s;
|
243 | 248 | for(i = 0; i < sbcs_cache.length; ++i) {
|
|
263 | 268 | };
|
264 | 269 | var cp_decache = function cp_decache(cp) { cpdcache[cp] = cpecache[cp] = undefined; };
|
265 | 270 | var decache = function decache() {
|
266 |
| - if(typeof Buffer !== 'undefined') { |
| 271 | + if(has_buf) { |
267 | 272 | if(!cpdcache[sbcs_cache[0]]) return;
|
268 | 273 | sbcs_cache.forEach(cp_decache);
|
269 | 274 | dbcs_cache.forEach(cp_decache);
|
|
286 | 291 | var encode = function encode(cp, data, ofmt) {
|
287 | 292 | if(cp === last_cp) { return last_enc(data, ofmt); }
|
288 | 293 | if(cpecache[cp] !== undefined) { last_enc = cpecache[last_cp=cp]; return last_enc(data, ofmt); }
|
289 |
| - if(typeof Buffer !== 'undefined' && Buffer.isBuffer(data)) data = data.toString('utf8'); |
| 294 | + if(has_buf && Buffer.isBuffer(data)) data = data.toString('utf8'); |
290 | 295 | var len = data.length;
|
291 |
| - var out = typeof Buffer !== 'undefined' ? new Buffer(4*len) : [], w, i, j = 0, c, tt, ww; |
| 296 | + var out = has_buf ? new Buffer(4*len) : [], w, i, j = 0, c, tt, ww; |
292 | 297 | var C = cpt[cp], E, M;
|
293 | 298 | if(C && (E=C.enc)) for(i = 0; i < len; ++i, ++j) {
|
294 | 299 | w = E[data[i]];
|
|
300 | 305 | }
|
301 | 306 | else if((M=magic[cp])) switch(M) {
|
302 | 307 | case "utf8":
|
303 |
| - if(typeof Buffer !== 'undefined' && typeof data === "string") { out = new Buffer(data, M); j = out.length; break; } |
| 308 | + if(has_buf && typeof data === "string") { out = new Buffer(data, M); j = out.length; break; } |
304 | 309 | for(i = 0; i < len; ++i, ++j) {
|
305 | 310 | w = data[i].charCodeAt(0);
|
306 | 311 | if(w <= 0x007F) out[j] = w;
|
|
322 | 327 | }
|
323 | 328 | break;
|
324 | 329 | case "ascii":
|
325 |
| - if(typeof Buffer !== 'undefined' && typeof data === "string") { out = new Buffer(data, M); j = out.length; break; } |
| 330 | + if(has_buf && typeof data === "string") { out = new Buffer(data, M); j = out.length; break; } |
326 | 331 | for(i = 0; i < len; ++i, ++j) {
|
327 | 332 | w = data[i].charCodeAt(0);
|
328 | 333 | if(w <= 0x007F) out[j] = w;
|
329 | 334 | else throw new Error("bad ascii " + w);
|
330 | 335 | }
|
331 | 336 | break;
|
332 | 337 | case "utf16le":
|
333 |
| - if(typeof Buffer !== 'undefined' && typeof data === "string") { out = new Buffer(data, M); j = out.length; break; } |
| 338 | + if(has_buf && typeof data === "string") { out = new Buffer(data, M); j = out.length; break; } |
334 | 339 | for(i = 0; i < len; ++i) {
|
335 | 340 | w = data[i].charCodeAt(0);
|
336 | 341 | out[j++] = w&255;
|
|
382 | 387 | }
|
383 | 388 | else throw new Error("Unrecognized CP: " + cp);
|
384 | 389 | out.length = j;
|
| 390 | + out = out.slice(0,j); |
385 | 391 | if(typeof Buffer === 'undefined') return (ofmt == 'str') ? out.map(sfcc).join("") : out;
|
386 | 392 | if(ofmt === undefined || ofmt === 'buf') return out;
|
387 | 393 | if(ofmt !== 'arr') return out.toString('binary');
|
|
422 | 428 | }
|
423 | 429 | break;
|
424 | 430 | case "ascii":
|
425 |
| - if(typeof Buffer !== 'undefined' && Buffer.isBuffer(data)) return data.toString(M); |
| 431 | + if(has_buf && Buffer.isBuffer(data)) return data.toString(M); |
426 | 432 | for(i = 0; i < len; i++) out[i] = String.fromCharCode(data[i]);
|
427 | 433 | k = len; break;
|
428 | 434 | case "utf16le":
|
429 | 435 | i = 0;
|
430 | 436 | if(len >= 2 && data[0] == 0xFF) if(data[1] == 0xFE) i = 2;
|
431 |
| - if(typeof Buffer !== 'undefined' && Buffer.isBuffer(data)) return data.toString(M); |
| 437 | + if(has_buf && Buffer.isBuffer(data)) return data.toString(M); |
432 | 438 | j = 2;
|
433 | 439 | for(; i < len; i+=j) {
|
434 | 440 | out[k++] = String.fromCharCode((data[i+1]<<8) + data[i]);
|
|
0 commit comments