INNER CODE UNIT · JavaScript

isHighSurrogate

increpare/flickgame · flickgame_share.js:59

  function isHighSurrogate(c) { return c >= 0xd800 && c <= 0xdbff; }
  function isLowSurrogate(c) { return c >= 0xdc00 && c <= 0xdfff; }

  // Deliberately arithmetic rather than encodeURIComponent, which throws URIError on
  // an unpaired surrogate - and user text pasted into a link field can contain one.
  function utf8ByteLength(str) {
    var bytes = 0;
    for (var i = 0; i < str.length; i++) {
      var c = str.charCodeAt(i);
      if (c < 0x80) bytes += 1;
      else if (c < 0x800) bytes += 2;
      else if (isHighSurrogate(c) && i + 1 < str.length && isLowSurrogate(str.charCodeAt(i + 1))) {
        bytes += 4;
        i++;
      } else bytes += 3;
    }
    return bytes;
  }

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…