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)
- In
Dns.c:70:
}
// 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;
}- In
Buf.c:170:
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;- In
Write.c:787:
// Little-endian u32: 0xDEADBEEF -> EF BE AD DE.
ok = ok && BufWriteFmt(&b, "{<4r}", (u32)0xDEADBEEF);
ok = ok && (BufLength(&b) == 4);
{- In
Write.c:795:
// Big-endian u32: 0xDEADBEEF -> DE AD BE EF.
ok = ok && BufWriteFmt(&b, "{>4r}", (u32)0xDEADBEEF);
{
const u8 *d = BufData(&b);- In
Write.c:802:
// Round-trip a u16 and a u64 through both orders.
ok = ok && BufWriteFmt(&b, "{<2r}{>8r}", (u16)0x1234, (u64)0x0102030405060708ULL);
{
BufIter it = BufIterFromBuf(&b);- In
Write.c:826:
// "TZif" literal magic + a version byte, emitted verbatim.
ok = ok && BufWriteFmt(&b, "TZif{>1r}", (u8)'2');
ok = ok && (BufLength(&b) == 5);
{- In
Write.c:860:
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);- In
Write.c:882:
// 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);
{- In
Write.c:891:
// Same, but the trailing literal is an escaped '{'.
StrClear((Str *)&b);
ok = ok && BufWriteFmt(&b, "{>1r}{{", (u8)'Q');
{
BufIter it = BufIterFromBuf(&b);- In
Write.c:1856:
bool ok = true;
ok = ok && BufWriteFmt(&b, "{<1r}", (u8)0x05);
ok = ok && (BufLength(&b) == 1);- In
Write.c:1882:
bool ok = true;
ok = ok && BufWriteFmt(&b, "{<4r}", (u32)0xDEADBEEF);
ok = ok && (BufLength(&b) == 4);- In
Write.c:1906:
bool ok = true;
ok = ok && BufWriteFmt(&b, "{>8r}", (u64)0x0102030405060708ULL);
ok = ok && (BufLength(&b) == 8);- In
Write.c:1930:
bool ok = true;
ok = ok && BufWriteFmt(&b, "{<8r}", (i64)-1234567890123LL);
ok = ok && (BufLength(&b) == 8);- In
Write.c:1961:
// 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);- In
Write.c:3697:
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);- In
Write.c:3738:
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteFmt(&b, "{4}", (u32)0x11223344); // real: LOG_FATAL before return
BufDeinit(&b);
DefaultAllocatorDeinit(&alloc);- In
Write.c:3897:
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;- In
Write.c:3910:
Buf b = BufInit(&alloc);
bool ok = BufWriteFmt(&b, "{^2r}", (u16)0x1234);
const u8 *d = BufData(&b);
// Host is little-endian: low byte first.
- In
Write.c:3924:
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;- In
Write.c:3937:
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 &&- In
Write.c:4092:
bool ok = true;
ok = ok && BufWriteFmt(&b, "{>8r}", (u64)0x0102030405060708ULL);
BufIter it = BufIterFromBuf(&b);- In
Write.c:4626:
bool ok = true;
ok = ok && BufWriteFmt(&b, "{^2r}", (u16)0x1234);
ok = ok && (BufLength(&b) == 2);- In
Write.c:4759:
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;- In
Write.c:5050:
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);- In
Read.c:1311:
bool ok = true;
ok = ok && BufWriteFmt(&b, "{^4r}", (u32)0xAABBCCDDu);
ok = ok && (BufLength(&b) == 4);- In
Read.c:1353:
u64 expected = 0x0102030405060708ULL;
ok = ok && BufWriteFmt(&b, "{<8r}", expected);
ok = ok && (BufLength(&b) == 8);- In
Read.c:1380:
u64 expected = 0x1122334455667788ULL;
ok = ok && BufWriteFmt(&b, "{<8r}", expected);
ok = ok && (BufLength(&b) == 8);- In
Read.c:1410:
u64 expected = 0x0102030405060708ULL;
ok = ok && BufWriteFmt(&b, "{^8r}", expected);
ok = ok && (BufLength(&b) == 8);- In
Read.c:2218:
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufWriteFmt(&b, "{<2r}", (u16)0xBEEF);
BufIter it = BufIterFromBuf(&b);
u16 v = 0;- In
Read.c:2236:
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