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

94.73% Statements 18/19
64.28% Branches 9/14
80% Functions 4/5
93.33% Lines 14/15

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 305x 5x       5x 5x 5x   17x   5x   5x 6x               5x 25x 8x 6x     5x  
import {decodeAscii, decodeAsciiMax15} from '../decodeAscii';
import v18 from './v18';
 
type Decoder = (buf: Uint8Array, start: number, length: number) => string;
 
const hasBuffer = typeof Buffer !== 'undefined';
const utf8Slice = hasBuffer ? Buffer.prototype.utf8Slice : null;
const from = hasBuffer ? Buffer.from : null;
 
const shortDecoder: Decoder = (buf, start, length) => decodeAsciiMax15(buf, start, length) ?? v18(buf, start, length);
 
const midDecoder: Decoder = (buf, start, length) => decodeAscii(buf, start, length) ?? v18(buf, start, length);
 
const longDecoder: Decoder = utf8Slice
  ? (buf, start, length) => utf8Slice.call(buf, start, start + length)
  : from
    ? (buf, start, length) =>
        from(buf)
          .subarray(start, start + length)
          .toString('utf8')
    : v18;
 
const decoder: Decoder = (buf, start, length): string => {
  if (length < 16) return shortDecoder(buf, start, length);
  if (length < 32) return midDecoder(buf, start, length);
  return longDecoder(buf, start, length);
};
 
export default decoder;