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

96.96% Statements 32/33
90.9% Branches 10/11
100% Functions 1/1
96.87% Lines 31/32

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 391x 5x 5x 5x 5x 5x 33438x 33438x 33430x 8x 3x 3x 5x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x       33438x 33x 33x     5x 5x    
export default (buf: Uint8Array, start: number, length: number): string => {
  let offset = start;
  const end = offset + length;
  const units: Array<number> = [];
  let result = '';
  while (offset < end) {
    const byte1 = buf[offset++]!;
    if ((byte1 & 0x80) === 0) {
      units.push(byte1);
    } else if ((byte1 & 0xe0) === 0xc0) {
      const byte2 = buf[offset++]! & 0x3f;
      units.push(((byte1 & 0x1f) << 6) | byte2);
    } else if ((byte1 & 0xf0) === 0xe0) {
      const byte2 = buf[offset++]! & 0x3f;
      const byte3 = buf[offset++]! & 0x3f;
      units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
    } else if ((byte1 & 0xf8) === 0xf0) {
      const byte2 = buf[offset++]! & 0x3f;
      const byte3 = buf[offset++]! & 0x3f;
      const byte4 = buf[offset++]! & 0x3f;
      let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
      if (unit > 0xffff) {
        unit -= 0x10000;
        units.push(((unit >>> 10) & 0x3ff) | 0xd800);
        unit = 0xdc00 | (unit & 0x3ff);
      }
      units.push(unit);
    } else E{
      units.push(byte1);
    }
    if (units.length >= 1000) {
      result += String.fromCharCode(...units);
      units.length = 0;
    }
  }
  if (units.length > 0) result += String.fromCharCode(...units);
  return result;
};