All files / src/common/buffer copy.ts

63.33% Statements 19/30
50% Branches 12/24
100% Functions 1/1
85% Lines 17/20

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 40 41 4268x             16x 16x 16x 16x 16x     16x 16x     16x     16x 16x     16x 16x       16x   16x       16x     16x    
export function copy(
  currentBuff: Uint8Array,
  target: Uint8Array,
  targetStart: number,
  start?: number,
  end?: number
) {
  if (!start) start = 0;
  if (!end && end !== 0) end = currentBuff.length;
  Iif (targetStart >= target.length) targetStart = target.length;
  Iif (!targetStart) targetStart = 0;
  Iif (end > 0 && end < start) end = start;
 
  // Copy 0 bytes; we're done
  Iif (end === start) return 0;
  Iif (target.length === 0 || currentBuff.length === 0) return 0;
 
  // Fatal error conditions
  Iif (targetStart < 0) {
    throw new RangeError('targetStart out of bounds');
  }
  Iif (start < 0 || start >= currentBuff.length) throw new RangeError('Index out of range');
  Iif (end < 0) throw new RangeError('sourceEnd out of bounds');
 
  // Are we oob?
  Iif (end > currentBuff.length) end = currentBuff.length;
  Iif (target.length - targetStart < end - start) {
    end = target.length - targetStart + start;
  }
 
  const len = end - start;
 
  Iif (currentBuff === target && typeof Uint8Array.prototype.copyWithin === 'function') {
    // Use built-in when available, missing from IE11
    currentBuff.copyWithin(targetStart, start, end);
  } else {
    Uint8Array.prototype.set.call(target, currentBuff.subarray(start, end), targetStart);
  }
 
  return len;
}