demo/full/scripts/lib/bytes.js
/**
 * Creates a string from the given Uint8Array containing utf-8 code units.
 * @param {Uint8Array} bytes
 * @returns {string}
 */
Creates a string from the given Uint8Array containing utf-8 code units.
@param {Uint8Array} bytes
@returns {string}
Creates a string from the given Uint8Array containing utf - 8 code units . @param { Uint8Array } bytes @returns { string }
function utf8ToStr(data) {
  let uint8 = data;
  if (uint8[0] === 0xEF && uint8[1] === 0xBB && uint8[2] === 0xBF) {
    uint8 = uint8.subarray(3);
  }
  const utf8Str = stringFromCharCodes(uint8);
  let escaped;
  if (typeof window.escape !== "function") {
    escaped = escape(utf8Str);
  } else {
    const nonEscapedChar = /[A-Za-z0-9*_+-./]/;
    escaped = "";
    for (let i = 0; i < utf8Str.length; i++) {
      if (nonEscapedChar.test(utf8Str[i])) {
        escaped += utf8Str[i];
      } else {
        const charCode = utf8Str.charCodeAt(i);
        escaped += charCode >= 256 ?
          "%u" + intToHex(charCode, 4) :
          "%" + intToHex(charCode, 2);
      }
    }
  }
  return decodeURIComponent(escaped);
}
function utf8ToStr ( data ) { let uint8 = data ; if ( uint8 [ 0 ] === 0xEF && uint8 [ 1 ] === 0xBB && uint8 [ 2 ] === 0xBF ) { uint8 = uint8 . subarray ( 3 ) ; } const utf8Str = stringFromCharCodes ( uint8 ) ; let escaped ; if ( typeof window . escape !== "function" ) { escaped = escape ( utf8Str ) ; } else { const nonEscapedChar = / [A-Za-z0-9*_+-./] / ; escaped = "" ; for ( let i = 0 ; i < utf8Str . length ; i ++ ) { if ( nonEscapedChar . test ( utf8Str [ i ] ) ) { escaped += utf8Str [ i ] ; } else { const charCode = utf8Str . charCodeAt ( i ) ; escaped += charCode >= 256 ? "%u" + intToHex ( charCode , 4 ) : "%" + intToHex ( charCode , 2 ) ; } } } return decodeURIComponent ( escaped ) ; }
Creates a string from the given Uint8Array containing utf-8 code units.
Creates a string from the given Uint8Array containing utf - 8 code units .
// If present, strip off the UTF-8 BOM. // We're basically doing strToUtf8 in reverse. // You can look at that other function for the whole story. // Generate string containing escaped UTF-8 code units // Transform UTF-8 escape sequence into percent-encoded escape sequences. // Let's implement a simple escape function // http://ecma-international.org/ecma-262/9.0/#sec-escape-string // Decode the percent-encoded UTF-8 string into the proper JS string. // Example: "g#%E3%82%AC" -> "g#€"