| 13847 | |
| 13848 | // convert array to string |
| 13849 | var buf2string = function (buf) { |
| 13850 | var str, i, out, c, c_len; |
| 13851 | var len = buf.length; |
| 13852 | |
| 13853 | // Reserve max possible length (2 words per char) |
| 13854 | // NB: by unknown reasons, Array is significantly faster for |
| 13855 | // String.fromCharCode.apply than Uint16Array. |
| 13856 | var utf16buf = new Array(len*2); |
| 13857 | |
| 13858 | for (out=0, i=0; i<len;) { |
| 13859 | c = buf[i++]; |
| 13860 | // quick process ascii |
| 13861 | if (c < 0x80) { utf16buf[out++] = c; continue; } |
| 13862 | |
| 13863 | c_len = _utf8len[c]; |
| 13864 | // skip 5 & 6 byte codes |
| 13865 | if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; } |
| 13866 | |
| 13867 | // apply mask on first byte |
| 13868 | c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07; |
| 13869 | // join the rest |
| 13870 | while (c_len > 1 && i < len) { |
| 13871 | c = (c << 6) | (buf[i++] & 0x3f); |
| 13872 | c_len--; |
| 13873 | } |
| 13874 | |
| 13875 | // terminated by end of string? |
| 13876 | if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; } |
| 13877 | |
| 13878 | if (c < 0x10000) { |
| 13879 | utf16buf[out++] = c; |
| 13880 | } else { |
| 13881 | c -= 0x10000; |
| 13882 | utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff); |
| 13883 | utf16buf[out++] = 0xdc00 | (c & 0x3ff); |
| 13884 | } |
| 13885 | } |
| 13886 | |
| 13887 | // shrinkBuf(utf16buf, out) |
| 13888 | if (utf16buf.length !== out) { |
| 13889 | if(utf16buf.subarray) { |
| 13890 | utf16buf = utf16buf.subarray(0, out); |
| 13891 | } else { |
| 13892 | utf16buf.length = out; |
| 13893 | } |
| 13894 | } |
| 13895 | |
| 13896 | // return String.fromCharCode.apply(null, utf16buf); |
| 13897 | return utils.applyFromCharCode(utf16buf); |
| 13898 | }; |
| 13899 | |
| 13900 | |
| 13901 | // That's all for the pako functions. |