gloriaJun / til

Lessoned Learned
3 stars 0 forks source link

getByteLengthOfUtf8String #129

Open gloriaJun opened 2 years ago

gloriaJun commented 2 years ago
function getByteLengthOfUtf8String(s: string) {
  // returns the byte length of an utf8 string
  if (isEmptyString(s)) {
    return 0;
  }

  let b = 0;
  let c: number;

  for (let i = 0; (c = s.charCodeAt(i++)); b += c >> 11 ? 3 : c >> 7 ? 2 : 1);
  return b;
}