{“ast”:null,“code”:“// String encode/decode helpersn'use strict';nnvar utils = require('./common'); // Quick check if we can use fast array to bin string conversionn//n// - apply(Array) can fail on Android 2.2n// - apply(Uint8Array) can fail on iOS 5.1 Safarin//nnnvar STR_APPLY_OK = true;nvar STR_APPLY_UIA_OK = true;nntry {n String.fromCharCode.apply(null, [0]);n} catch (__) {n STR_APPLY_OK = false;n}nntry {n String.fromCharCode.apply(null, new Uint8Array(1));n} catch (__) {n STR_APPLY_UIA_OK = false;n} // Table with utf8 lengths (calculated by first byte of sequence)n// Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,n// because max possible codepoint is 0x10ffffnnnvar _utf8len = new utils.Buf8(256);nnfor (var q = 0; q < 256; q++) {n _utf8len = q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1;n}nn_utf8len = _utf8len = 1; // Invalid sequence startn// convert string to array (typed, when possible)nnexports.string2buf = function (str) {n var buf,n c,n c2,n m_pos,n i,n str_len = str.length,n buf_len = 0; // count binary sizenn for (m_pos = 0; m_pos < str_len; m_pos++) {n c = str.charCodeAt(m_pos);nn if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) {n c2 = str.charCodeAt(m_pos + 1);nn if ((c2 & 0xfc00) === 0xdc00) {n c = 0x10000 + (c - 0xd800 << 10) + (c2 - 0xdc00);n m_pos++;n }n }nn buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;n } // allocate buffernnn buf = new utils.Buf8(buf_len); // convertnn for (i = 0, m_pos = 0; i < buf_len; m_pos++) {n c = str.charCodeAt(m_pos);nn if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) {n c2 = str.charCodeAt(m_pos + 1);nn if ((c2 & 0xfc00) === 0xdc00) {n c = 0x10000 + (c - 0xd800 << 10) + (c2 - 0xdc00);n m_pos++;n }n }nn if (c < 0x80) {n /* one byte */n buf = c;n } else if (c < 0x800) {n /* two bytes */n buf = 0xC0 | c >>> 6;n buf = 0x80 | c & 0x3f;n } else if (c < 0x10000) {n /* three bytes */n buf = 0xE0 | c >>> 12;n buf = 0x80 | c >>> 6 & 0x3f;n buf = 0x80 | c & 0x3f;n } else {n /* four bytes */n buf = 0xf0 | c >>> 18;n buf = 0x80 | c >>> 12 & 0x3f;n buf = 0x80 | c >>> 6 & 0x3f;n buf = 0x80 | c & 0x3f;n }n }nn return buf;n}; // Helper (used in 2 places)nnnfunction buf2binstring(buf, len) {n // On Chrome, the arguments in a function call that are allowed is `65534`.n // If the length of the buffer is smaller than that, we can use this optimization,n // otherwise we will take a slower path.n if (len < 65534) {n if (buf.subarray && STR_APPLY_UIA_OK || !buf.subarray && STR_APPLY_OK) {n return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));n }n }nn var result = '';nn for (var i = 0; i < len; i++) {n result += String.fromCharCode(buf);n }nn return result;n} // Convert byte array to binary stringnnnexports.buf2binstring = function (buf) {n return buf2binstring(buf, buf.length);n}; // Convert binary string (typed, when possible)nnnexports.binstring2buf = function (str) {n var buf = new utils.Buf8(str.length);nn for (var i = 0, len = buf.length; i < len; i++) {n buf = str.charCodeAt(i);n }nn return buf;n}; // convert array to stringnnnexports.buf2string = function (buf, max) {n var i, out, c, c_len;n var len = max || buf.length; // Reserve max possible length (2 words per char)n // NB: by unknown reasons, Array is significantly faster forn // String.fromCharCode.apply than Uint16Array.nn var utf16buf = new Array(len * 2);nn for (out = 0, i = 0; i < len;) {n c = buf; // quick process asciinn if (c < 0x80) {n utf16buf = c;n continue;n }nn c_len = _utf8len; // skip 5 & 6 byte codesnn if (c_len > 4) {n utf16buf = 0xfffd;n i += c_len - 1;n continue;n } // apply mask on first bytennn c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07; // join the restnn while (c_len > 1 && i < len) {n c = c << 6 | buf & 0x3f;n c_len–;n } // terminated by end of string?nnn if (c_len > 1) {n utf16buf = 0xfffd;n continue;n }nn if (c < 0x10000) {n utf16buf = c;n } else {n c -= 0x10000;n utf16buf = 0xd800 | c >> 10 & 0x3ff;n utf16buf = 0xdc00 | c & 0x3ff;n }n }nn return buf2binstring(utf16buf, out);n}; // Calculate max possible position in utf8 buffer,n// that will not break sequence. If that's not possiblen// - (very small limits) return max size as is.n//n// buf[] - utf8 bytes arrayn// max - length limit (mandatory);nnnexports.utf8border = function (buf, max) {n var pos;n max = max || buf.length;nn if (max > buf.length) {n max = buf.length;n } // go back from last position, until start of sequence foundnnn pos = max - 1;nn while (pos >= 0 && (buf & 0xC0) === 0x80) {n pos–;n } // Very small and broken sequence,n // return max, because we should return something anyway.nnn if (pos < 0) {n return max;n } // If we came to start of buffer - that means buffer is too small,n // return max too.nnn if (pos === 0) {n return max;n }nn return pos + _utf8len[buf] > max ? pos : max;n};”,“map”:null,“metadata”:{},“sourceType”:“module”}