All files / src hex.js

96.77% Statements 30/31
94.44% Branches 17/18
100% Functions 5/5
100% Lines 27/27

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 42 43 44 45 46 47 48 49147x       193x 193x     147x 147x       179388x       863604x       84251x       6x 6x 6x 6x 6x 6x 6x 3048x 3048x 3048x 3048x 78x 78x 78x     6x 6x 6x 78x   6x    
const HexAddress = /^(?:\$|&|0x)?([0-9a-f]{1,4})$/i;
 
/** @returns {number | undefined} */
export function parseAddr(s) {
    const digits = HexAddress.exec(s)?.[1];
    return digits === undefined ? undefined : parseInt(digits, 16);
}
 
const FirstPrintable = 0x20;
const PastPrintable = 0x80;
 
/** A byte as text: its character if printable, a dot if not. */
export function byteChar(byte) {
    return byte >= FirstPrintable && byte < PastPrintable ? String.fromCharCode(byte) : ".";
}
 
export function hexbyte(value) {
    return ((value >>> 4) & 0xf).toString(16) + (value & 0xf).toString(16);
}
 
export function hexword(value) {
    return hexbyte(value >>> 8) + hexbyte(value & 0xff);
}
 
export function hd(reader, start, end, opts) {
    opts = opts || {};
    const width = opts.width || 16;
    const gap = opts.gap === undefined ? 8 : opts.gap;
    const res = [];
    let str = "";
    let j = 0;
    for (let i = start; i < end; ++i) {
        str += " ";
        str += hexbyte(reader(i));
        if (++j === gap) str += " ";
        if (j === width) {
            res.push(str);
            str = "";
            j = 0;
        }
    }
    Iif (str) res.push(str);
    let joined = "";
    for (let i = 0; i < res.length; ++i) {
        joined += hexword(start + i * width) + " :" + res[i] + "\n";
    }
    return joined;
}