CoolFace
Apppublic

Pinsave/counterstrike

sourceHugging Faceupdated 3mo agoView on Hugging Face
1likes
strings.js188 linesDownload Raw Back to utils
1// String encode/decode helpers2'use strict';3 4 5var utils = require('./common');6 7 8// Quick check if we can use fast array to bin string conversion9//10// - apply(Array) can fail on Android 2.211// - apply(Uint8Array) can fail on iOS 5.1 Safari12//13var STR_APPLY_OK = true;14var STR_APPLY_UIA_OK = true;15 16try { String.fromCharCode.apply(null, [ 0 ]); } catch (__) { STR_APPLY_OK = false; }17try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }18 19 20// Table with utf8 lengths (calculated by first byte of sequence)21// Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,22// because max possible codepoint is 0x10ffff23var _utf8len = new utils.Buf8(256);24for (var q = 0; q < 256; q++) {25  _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);26}27_utf8len[254] = _utf8len[254] = 1; // Invalid sequence start28 29 30// convert string to array (typed, when possible)31exports.string2buf = function (str) {32  var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;33 34  // count binary size35  for (m_pos = 0; m_pos < str_len; m_pos++) {36    c = str.charCodeAt(m_pos);37    if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {38      c2 = str.charCodeAt(m_pos + 1);39      if ((c2 & 0xfc00) === 0xdc00) {40        c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);41        m_pos++;42      }43    }44    buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;45  }46 47  // allocate buffer48  buf = new utils.Buf8(buf_len);49 50  // convert51  for (i = 0, m_pos = 0; i < buf_len; m_pos++) {52    c = str.charCodeAt(m_pos);53    if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {54      c2 = str.charCodeAt(m_pos + 1);55      if ((c2 & 0xfc00) === 0xdc00) {56        c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);57        m_pos++;58      }59    }60    if (c < 0x80) {61      /* one byte */62      buf[i++] = c;63    } else if (c < 0x800) {64      /* two bytes */65      buf[i++] = 0xC0 | (c >>> 6);66      buf[i++] = 0x80 | (c & 0x3f);67    } else if (c < 0x10000) {68      /* three bytes */69      buf[i++] = 0xE0 | (c >>> 12);70      buf[i++] = 0x80 | (c >>> 6 & 0x3f);71      buf[i++] = 0x80 | (c & 0x3f);72    } else {73      /* four bytes */74      buf[i++] = 0xf0 | (c >>> 18);75      buf[i++] = 0x80 | (c >>> 12 & 0x3f);76      buf[i++] = 0x80 | (c >>> 6 & 0x3f);77      buf[i++] = 0x80 | (c & 0x3f);78    }79  }80 81  return buf;82};83 84// Helper (used in 2 places)85function buf2binstring(buf, len) {86  // On Chrome, the arguments in a function call that are allowed is `65534`.87  // If the length of the buffer is smaller than that, we can use this optimization,88  // otherwise we will take a slower path.89  if (len < 65534) {90    if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {91      return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));92    }93  }94 95  var result = '';96  for (var i = 0; i < len; i++) {97    result += String.fromCharCode(buf[i]);98  }99  return result;100}101 102 103// Convert byte array to binary string104exports.buf2binstring = function (buf) {105  return buf2binstring(buf, buf.length);106};107 108 109// Convert binary string (typed, when possible)110exports.binstring2buf = function (str) {111  var buf = new utils.Buf8(str.length);112  for (var i = 0, len = buf.length; i < len; i++) {113    buf[i] = str.charCodeAt(i);114  }115  return buf;116};117 118 119// convert array to string120exports.buf2string = function (buf, max) {121  var i, out, c, c_len;122  var len = max || buf.length;123 124  // Reserve max possible length (2 words per char)125  // NB: by unknown reasons, Array is significantly faster for126  //     String.fromCharCode.apply than Uint16Array.127  var utf16buf = new Array(len * 2);128 129  for (out = 0, i = 0; i < len;) {130    c = buf[i++];131    // quick process ascii132    if (c < 0x80) { utf16buf[out++] = c; continue; }133 134    c_len = _utf8len[c];135    // skip 5 & 6 byte codes136    if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len - 1; continue; }137 138    // apply mask on first byte139    c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;140    // join the rest141    while (c_len > 1 && i < len) {142      c = (c << 6) | (buf[i++] & 0x3f);143      c_len--;144    }145 146    // terminated by end of string?147    if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }148 149    if (c < 0x10000) {150      utf16buf[out++] = c;151    } else {152      c -= 0x10000;153      utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);154      utf16buf[out++] = 0xdc00 | (c & 0x3ff);155    }156  }157 158  return buf2binstring(utf16buf, out);159};160 161 162// Calculate max possible position in utf8 buffer,163// that will not break sequence. If that's not possible164// - (very small limits) return max size as is.165//166// buf[] - utf8 bytes array167// max   - length limit (mandatory);168exports.utf8border = function (buf, max) {169  var pos;170 171  max = max || buf.length;172  if (max > buf.length) { max = buf.length; }173 174  // go back from last position, until start of sequence found175  pos = max - 1;176  while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }177 178  // Very small and broken sequence,179  // return max, because we should return something anyway.180  if (pos < 0) { return max; }181 182  // If we came to start of buffer - that means buffer is too small,183  // return max too.184  if (pos === 0) { return max; }185 186  return (pos + _utf8len[buf[pos]] > max) ? pos : max;187};188