All files / buffers/src/utf8/decodeUtf8 v10.ts

93.1% Statements 27/29
71.42% Branches 5/7
100% Functions 1/1
93.1% Lines 27/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 401x   1x 9x 9x 9x 9x 33444x 33444x 33434x 33434x   10x 10x 3x 3x   7x 7x 3x 3x   4x 4x 4x 4x 4x 4x 4x 4x               9x    
const fromCharCode = String.fromCharCode;
 
export default (buf: Uint8Array, start: number, length: number): string => {
  let offset = start;
  const end = offset + length;
  let str = '';
  while (offset < end) {
    const octet1 = buf[offset++]!;
    if ((octet1 & 0x80) === 0) {
      str += fromCharCode(octet1);
      continue;
    }
    const octet2 = buf[offset++]! & 0x3f;
    if ((octet1 & 0xe0) === 0xc0) {
      str += fromCharCode(((octet1 & 0x1f) << 6) | octet2);
      continue;
    }
    const octet3 = buf[offset++]! & 0x3f;
    if ((octet1 & 0xf0) === 0xe0) {
      str += fromCharCode(((octet1 & 0x1f) << 12) | (octet2 << 6) | octet3);
      continue;
    }
    if ((octet1 & 0xf8) === 0xf0) {
      const octet4 = buf[offset++]! & 0x3f;
      let unit = ((octet1 & 0x07) << 0x12) | (octet2 << 0x0c) | (octet3 << 0x06) | octet4;
      if (unit > 0xffff) {
        unit -= 0x10000;
        const unit0 = ((unit >>> 10) & 0x3ff) | 0xd800;
        unit = 0xdc00 | (unit & 0x3ff);
        str += fromCharCode(unit0, unit);
      } else E{
        str += fromCharCode(unit);
      }
    } else E{
      str += fromCharCode(octet1);
    }
  }
  return str;
};