react / wstein / node_modules / browserify / node_modules / browserify-zlib / node_modules / pako / lib / utils / strings.js
80547 views// String encode/decode helpers1'use strict';234var utils = require('./common');567// Quick check if we can use fast array to bin string conversion8//9// - apply(Array) can fail on Android 2.210// - apply(Uint8Array) can fail on iOS 5.1 Safary11//12var STR_APPLY_OK = true;13var STR_APPLY_UIA_OK = true;1415try { String.fromCharCode.apply(null, [0]); } catch(__) { STR_APPLY_OK = false; }16try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch(__) { STR_APPLY_UIA_OK = false; }171819// Table with utf8 lengths (calculated by first byte of sequence)20// Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,21// because max possible codepoint is 0x10ffff22var _utf8len = new utils.Buf8(256);23for (var i=0; i<256; i++) {24_utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1);25}26_utf8len[254]=_utf8len[254]=1; // Invalid sequence start272829// convert string to array (typed, when possible)30exports.string2buf = function (str) {31var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;3233// count binary size34for (m_pos = 0; m_pos < str_len; m_pos++) {35c = str.charCodeAt(m_pos);36if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {37c2 = str.charCodeAt(m_pos+1);38if ((c2 & 0xfc00) === 0xdc00) {39c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);40m_pos++;41}42}43buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;44}4546// allocate buffer47buf = new utils.Buf8(buf_len);4849// convert50for (i=0, m_pos = 0; i < buf_len; m_pos++) {51c = str.charCodeAt(m_pos);52if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {53c2 = str.charCodeAt(m_pos+1);54if ((c2 & 0xfc00) === 0xdc00) {55c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);56m_pos++;57}58}59if (c < 0x80) {60/* one byte */61buf[i++] = c;62} else if (c < 0x800) {63/* two bytes */64buf[i++] = 0xC0 | (c >>> 6);65buf[i++] = 0x80 | (c & 0x3f);66} else if (c < 0x10000) {67/* three bytes */68buf[i++] = 0xE0 | (c >>> 12);69buf[i++] = 0x80 | (c >>> 6 & 0x3f);70buf[i++] = 0x80 | (c & 0x3f);71} else {72/* four bytes */73buf[i++] = 0xf0 | (c >>> 18);74buf[i++] = 0x80 | (c >>> 12 & 0x3f);75buf[i++] = 0x80 | (c >>> 6 & 0x3f);76buf[i++] = 0x80 | (c & 0x3f);77}78}7980return buf;81};8283// Helper (used in 2 places)84function buf2binstring(buf, len) {85// use fallback for big arrays to avoid stack overflow86if (len < 65537) {87if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {88return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));89}90}9192var result = '';93for(var i=0; i < len; i++) {94result += String.fromCharCode(buf[i]);95}96return result;97}9899100// Convert byte array to binary string101exports.buf2binstring = function(buf) {102return buf2binstring(buf, buf.length);103};104105106// Convert binary string (typed, when possible)107exports.binstring2buf = function(str) {108var buf = new utils.Buf8(str.length);109for(var i=0, len=buf.length; i < len; i++) {110buf[i] = str.charCodeAt(i);111}112return buf;113};114115116// convert array to string117exports.buf2string = function (buf, max) {118var i, out, c, c_len;119var len = max || buf.length;120121// Reserve max possible length (2 words per char)122// NB: by unknown reasons, Array is significantly faster for123// String.fromCharCode.apply than Uint16Array.124var utf16buf = new Array(len*2);125126for (out=0, i=0; i<len;) {127c = buf[i++];128// quick process ascii129if (c < 0x80) { utf16buf[out++] = c; continue; }130131c_len = _utf8len[c];132// skip 5 & 6 byte codes133if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }134135// apply mask on first byte136c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;137// join the rest138while (c_len > 1 && i < len) {139c = (c << 6) | (buf[i++] & 0x3f);140c_len--;141}142143// terminated by end of string?144if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }145146if (c < 0x10000) {147utf16buf[out++] = c;148} else {149c -= 0x10000;150utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);151utf16buf[out++] = 0xdc00 | (c & 0x3ff);152}153}154155return buf2binstring(utf16buf, out);156};157158159// Calculate max possible position in utf8 buffer,160// that will not break sequence. If that's not possible161// - (very small limits) return max size as is.162//163// buf[] - utf8 bytes array164// max - length limit (mandatory);165exports.utf8border = function(buf, max) {166var pos;167168max = max || buf.length;169if (max > buf.length) { max = buf.length; }170171// go back from last position, until start of sequence found172pos = max-1;173while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }174175// Fuckup - very small and broken sequence,176// return max, because we should return something anyway.177if (pos < 0) { return max; }178179// If we came to start of buffer - that means vuffer is too small,180// return max too.181if (pos === 0) { return max; }182183return (pos + _utf8len[buf[pos]] > max) ? pos : max;184};185186187