Blame view
node_modules/to-utf8/index.js
1.48 KB
f7563de62
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
module.exports = to_utf8 var out = [] , col = [] , fcc = String.fromCharCode , mask = [0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01] , unmask = [ 0x00 , 0x01 , 0x02 | 0x01 , 0x04 | 0x02 | 0x01 , 0x08 | 0x04 | 0x02 | 0x01 , 0x10 | 0x08 | 0x04 | 0x02 | 0x01 , 0x20 | 0x10 | 0x08 | 0x04 | 0x02 | 0x01 , 0x40 | 0x20 | 0x10 | 0x08 | 0x04 | 0x02 | 0x01 ] function to_utf8(bytes, start, end) { start = start === undefined ? 0 : start end = end === undefined ? bytes.length : end var idx = 0 , hi = 0x80 , collecting = 0 , pos , by col.length = out.length = 0 while(idx < bytes.length) { by = bytes[idx] if(!collecting && by & hi) { pos = find_pad_position(by) collecting += pos if(pos < 8) { col[col.length] = by & unmask[6 - pos] } } else if(collecting) { col[col.length] = by & unmask[6] --collecting if(!collecting && col.length) { out[out.length] = fcc(reduced(col, pos)) col.length = 0 } } else { out[out.length] = fcc(by) } ++idx } if(col.length && !collecting) { out[out.length] = fcc(reduced(col, pos)) col.length = 0 } return out.join('') } function find_pad_position(byt) { for(var i = 0; i < 7; ++i) { if(!(byt & mask[i])) { break } } return i } function reduced(list) { var out = 0 for(var i = 0, len = list.length; i < len; ++i) { out |= list[i] << ((len - i - 1) * 6) } return out } |