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;
    
        // Little-endian u32: 0xDEADBEEF -> EF BE AD DE.
        ok = ok && BufWriteFmt(&b, "{<4r}", (u32)0xDEADBEEF);
        ok = ok && (BufLength(&b) == 4);
        {
    
        // Big-endian u32: 0xDEADBEEF -> DE AD BE EF.
        ok = ok && BufWriteFmt(&b, "{>4r}", (u32)0xDEADBEEF);
        {
            const u8 *d = BufData(&b);
    
        // Round-trip a u16 and a u64 through both orders.
        ok = ok && BufWriteFmt(&b, "{<2r}{>8r}", (u16)0x1234, (u64)0x0102030405060708ULL);
        {
            BufIter it    = BufIterFromBuf(&b);
    
        // "TZif" literal magic + a version byte, emitted verbatim.
        ok = ok && BufWriteFmt(&b, "TZif{>1r}", (u8)'2');
        ok = ok && (BufLength(&b) == 5);
        {
        bool             ok    = true;
    
        ok = ok && BufWriteFmt(&b, "{​{{>1r}", (u8)0x41); // literal '{' then 'A'
        ok = ok && (BufLength(&b) == 2) && (BufData(&b)[0] == '{') && (BufData(&b)[1] == 0x41);
        // Bytes 'Q','Z': read the Q, then match the trailing 'Z' literal with
        // exactly one byte remaining.
        ok = ok && BufWriteFmt(&b, "{>1r}Z", (u8)'Q');
        ok = ok && (BufLength(&b) == 2);
        {
        // Same, but the trailing literal is an escaped '{'.
        StrClear((Str *)&b);
        ok = ok && BufWriteFmt(&b, "{>1r}{​{", (u8)'Q');
        {
            BufIter it = BufIterFromBuf(&b);
        bool             ok    = true;
    
        ok = ok && BufWriteFmt(&b, "{<1r}", (u8)0x05);
        ok = ok && (BufLength(&b) == 1);
        bool             ok    = true;
    
        ok = ok && BufWriteFmt(&b, "{<4r}", (u32)0xDEADBEEF);
        ok = ok && (BufLength(&b) == 4);
        bool             ok    = true;
    
        ok = ok && BufWriteFmt(&b, "{>8r}", (u64)0x0102030405060708ULL);
        ok = ok && (BufLength(&b) == 8);
        bool             ok    = true;
    
        ok = ok && BufWriteFmt(&b, "{<8r}", (i64)-1234567890123LL);
        ok = ok && (BufLength(&b) == 8);
        // Lay down 2 bytes so the mutant's (wrongly accepted) width-2 read has
        // enough input and would otherwise succeed silently.
        BufWriteFmt(&b, "{<2r}", (u16)0x1234);
    
        BufIter it  = BufIterFromBuf(&b);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = BufWriteFmt(&b, "{<1r}", (u8)0xAB);
        ok                     = ok && (BufLength(&b) == 1) && (BufData(&b)[0] == 0xAB);
        BufDeinit(&b);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteFmt(&b, "{4}", (u32)0x11223344); // real: LOG_FATAL before return
        BufDeinit(&b);
        DefaultAllocatorDeinit(&alloc);
        Buf              b     = BufInit(&alloc);
    
        bool      ok = BufWriteFmt(&b, "{>2r}", (u16)0x1234);
        const u8 *d  = BufData(&b);
        ok           = ok && (BufLength(&b) == 2) && d[0] == 0x12 && d[1] == 0x34;
        Buf              b     = BufInit(&alloc);
    
        bool      ok = BufWriteFmt(&b, "{^2r}", (u16)0x1234);
        const u8 *d  = BufData(&b);
        // Host is little-endian: low byte first.
        Buf              b     = BufInit(&alloc);
    
        bool      ok = BufWriteFmt(&b, "{^4r}", (u32)0x11223344);
        const u8 *d  = BufData(&b);
        ok           = ok && (BufLength(&b) == 4) && d[0] == 0x44 && d[1] == 0x33 && d[2] == 0x22 && d[3] == 0x11;
        Buf              b     = BufInit(&alloc);
    
        bool      ok = BufWriteFmt(&b, "{^8r}", (u64)0x0102030405060708ULL);
        const u8 *d  = BufData(&b);
        ok = ok && (BufLength(&b) == 8) && d[0] == 0x08 && d[1] == 0x07 && d[2] == 0x06 && d[3] == 0x05 && d[4] == 0x04 &&
        bool             ok    = true;
    
        ok = ok && BufWriteFmt(&b, "{>8r}", (u64)0x0102030405060708ULL);
    
        BufIter it  = BufIterFromBuf(&b);
        bool             ok    = true;
    
        ok = ok && BufWriteFmt(&b, "{^2r}", (u16)0x1234);
        ok = ok && (BufLength(&b) == 2);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteFmt(&b, "{<1r}{<1r}", (u8)0x11, (u8)0x22); // 2 bytes available
        BufIter it = BufIterFromBuf(&b);
        u8      v8 = 0;
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = BufWriteFmt(&b, "{<2r}{>4r}", (u16)0x1234, (u32)0xAABBCCDD);
        ok                     = ok && (BufLength(&b) == 6);
        BufIter it             = BufIterFromBuf(&b);
        bool             ok    = true;
    
        ok = ok && BufWriteFmt(&b, "{^4r}", (u32)0xAABBCCDDu);
        ok = ok && (BufLength(&b) == 4);
        u64 expected = 0x0102030405060708ULL;
    
        ok = ok && BufWriteFmt(&b, "{<8r}", expected);
        ok = ok && (BufLength(&b) == 8);
        u64 expected = 0x1122334455667788ULL;
    
        ok = ok && BufWriteFmt(&b, "{<8r}", expected);
        ok = ok && (BufLength(&b) == 8);
        u64 expected = 0x0102030405060708ULL;
    
        ok = ok && BufWriteFmt(&b, "{^8r}", expected);
        ok = ok && (BufLength(&b) == 8);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = BufWriteFmt(&b, "{<2r}", (u16)0xBEEF);
        BufIter          it    = BufIterFromBuf(&b);
        u16              v     = 0;
            u64 u;
        } got      = {0};
        bool    ok = BufWriteFmt(&b, "{<8r}", (u64)0x0102030405060708ULL);
        BufIter it = BufIterFromBuf(&b);
        ok         = ok && BufReadFmt(&it, "{<8r}", got.f) && (got.u == 0x0102030405060708ULL);
Last updated on