"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BytesWriter = void 0;
// Writes bytes to a fixed size buffer with a capacity that is known upfront.
class BytesWriter {
    constructor(capacity) {
        this.offset = 0;
        this.data = new Uint8Array(capacity);
    }
    // Write a hex string to this buffer
    // The hex should start with 0x and have an even number of hexadecimal characters.
    // This does no validation at all. Make sure the input is correct and that the
    // writer has capacity.
    writeHex(hex) {
        // Help the optimizer maybe by lifting local variables
        let offset = this.offset;
        const data = this.data;
        // TODO: Performance
        // There are no great answers in JS,
        // but individual characters might be better. Need to profile.
        let iRead = 0;
        if (hex.startsWith('0x')) {
            iRead = 2;
        }
        while (iRead < hex.length) {
            const num = hex.slice(iRead, iRead + 2);
            data[offset++] = parseInt(num, 16);
            iRead += 2;
        }
        this.offset = offset;
    }
    writeZeroes(bytes) {
        this.offset += bytes;
    }
    unwrap() {
        if (this.offset !== this.data.length) {
            throw new Error('Did not write correct number of bytes');
        }
        return this.data;
    }
}
exports.BytesWriter = BytesWriter;
//# sourceMappingURL=bytes.js.map