Skip to content

BufWriteFmt

Description

Write encoded bytes to buf from scratch. Equivalent to clearing buf and then BufAppendFmt(buf, ...) – any prior contents are discarded.

Success

Returns true; buf holds exactly the encoded bytes.

Failure

Returns false on format-parse error or grow-buffer failure. buf is cleared before the append runs, so a failed call leaves buf with whatever bytes were encoded before the failure.

Usage example (Cross-references)

Usage examples (Cross-references)
        }
        // Header: id, flags=0x0100 (RD set), qdcount=1, ancount=0, nscount=0, arcount=0.
        if (!BufWriteFmt(out, "{>2r}{>2r}{>2r}{>2r}{>2r}{>2r}", id, (u16)0x0100u, (u16)1u, (u16)0u, (u16)0u, (u16)0u)) {
            return false;
        }
        BufWriteU8(&b, 0xBB);
        // Write replaces previous contents.
        bool     ok       = BufWriteFmt(&b, "{<2r}", (u16)0x1234);
        const u8 expect[] = {0x34, 0x12};
        ok                = ok && BufLength(&b) == sizeof(expect) && MemCompare(BufData(&b), expect, sizeof(expect)) == 0;
Last updated on