Skip to content

StrAppendFmt

Description

Append a formatted string to out. Existing contents are preserved; new content lands at the end of the buffer.

Parameters

Name Direction Description
out out Destination Str. Existing bytes are kept.
fmtstr in Format string with {} placeholders.

Success

out extended with the formatted content; returns true.

Failure

Returns false. May log diagnostics; out may be left partially extended on allocation failure mid-write.

Usage example (Cross-references)

Usage examples (Cross-references)
            HeapAllocator UNPL(log_alloc) = HeapAllocatorInit();                                                           \
            Str           UNPL(m)         = StrInit(&UNPL(log_alloc));                                                     \
            StrAppendFmt(&UNPL(m), __VA_ARGS__);                                                                           \
            LogWrite(LOG_MESSAGE_TYPE_FATAL, __func__, __LINE__, StrBegin(&UNPL(m)));                                      \
            StrDeinit(&UNPL(m));                                                                                           \
            HeapAllocator UNPL(log_alloc) = HeapAllocatorInit();                                                           \
            Str           UNPL(m)         = StrInit(&UNPL(log_alloc));                                                     \
            StrAppendFmt(&UNPL(m), __VA_ARGS__);                                                                           \
            LogWrite(LOG_MESSAGE_TYPE_ERROR, __func__, __LINE__, StrBegin(&UNPL(m)));                                      \
            StrDeinit(&UNPL(m));                                                                                           \
            HeapAllocator UNPL(log_alloc) = HeapAllocatorInit();                                                           \
            Str           UNPL(m)         = StrInit(&UNPL(log_alloc));                                                     \
            StrAppendFmt(&UNPL(m), __VA_ARGS__);                                                                           \
            LogWrite(LOG_MESSAGE_TYPE_INFO, __func__, __LINE__, StrBegin(&UNPL(m)));                                       \
            StrDeinit(&UNPL(m));                                                                                           \
            HeapAllocator UNPL(log_alloc) = HeapAllocatorInit();                                                           \
            Str           UNPL(m)         = StrInit(&UNPL(log_alloc));                                                     \
            StrAppendFmt(&UNPL(m), __VA_ARGS__);                                                                           \
            StrInitStack(UNPL(syserr), 256) {                                                                              \
                StrError(UNPL(sys_eno), &UNPL(syserr));                                                                    \
            StrInitStack(UNPL(syserr), 256) {                                                                              \
                StrError(UNPL(sys_eno), &UNPL(syserr));                                                                    \
                StrAppendFmt(&UNPL(m), " : {}", UNPL(syserr));                                                             \
            }                                                                                                              \
            LogWrite(LOG_MESSAGE_TYPE_FATAL, __func__, __LINE__, StrBegin(&UNPL(m)));                                      \
            HeapAllocator UNPL(log_alloc) = HeapAllocatorInit();                                                           \
            Str           UNPL(m)         = StrInit(&UNPL(log_alloc));                                                     \
            StrAppendFmt(&UNPL(m), __VA_ARGS__);                                                                           \
            StrInitStack(UNPL(syserr), 256) {                                                                              \
                StrError(UNPL(sys_eno), &UNPL(syserr));                                                                    \
            StrInitStack(UNPL(syserr), 256) {                                                                              \
                StrError(UNPL(sys_eno), &UNPL(syserr));                                                                    \
                StrAppendFmt(&UNPL(m), " : {}", UNPL(syserr));                                                             \
            }                                                                                                              \
            LogWrite(LOG_MESSAGE_TYPE_ERROR, __func__, __LINE__, StrBegin(&UNPL(m)));                                      \
            HeapAllocator UNPL(log_alloc) = HeapAllocatorInit();                                                           \
            Str           UNPL(m)         = StrInit(&UNPL(log_alloc));                                                     \
            StrAppendFmt(&UNPL(m), __VA_ARGS__);                                                                           \
            StrInitStack(UNPL(syserr), 256) {                                                                              \
                StrError(UNPL(sys_eno), &UNPL(syserr));                                                                    \
            StrInitStack(UNPL(syserr), 256) {                                                                              \
                StrError(UNPL(sys_eno), &UNPL(syserr));                                                                    \
                StrAppendFmt(&UNPL(m), " : {}", UNPL(syserr));                                                             \
            }                                                                                                              \
            LogWrite(LOG_MESSAGE_TYPE_INFO, __func__, __LINE__, StrBegin(&UNPL(m)));                                       \
        do {                                                                                                               \
            Str UNPL(buf) = StrInit();                                                                                     \
            StrAppendFmt(&UNPL(buf), __VA_ARGS__);                                                                         \
            ProcWriteToStdin((p), &UNPL(buf));                                                                             \
            StrDeinit(&UNPL(buf));                                                                                         \
        do {                                                                                                               \
            Str UNPL(buf) = StrInit();                                                                                     \
            StrAppendFmt(&UNPL(buf), __VA_ARGS__);                                                                         \
            StrPushBackR(&UNPL(buf), '\n');                                                                                 \
            ProcWriteToStdin((p), &UNPL(buf));                                                                             \
                StrPushBackR(&(j), ',');                                                                                   \
            }                                                                                                              \
            StrAppendFmt(&(j), "\"{}\":", (Zstr)(k));                                                                      \
            JW_OBJ(j, writer);                                                                                             \
        } while (0)
                StrPushBackR(&(j), ',');                                                                                   \
            }                                                                                                              \
            StrAppendFmt(&(j), "\"{}\":", (Zstr)(k));                                                                      \
            JW_ARR(j, arr, item, writer);                                                                                  \
        } while (0)
        do {                                                                                                               \
            i64 UNPL(my_int) = (i);                                                                                        \
            StrAppendFmt(&(j), "{}", UNPL(my_int));                                                                        \
        } while (0)
                StrPushBackR(&(j), ',');                                                                                   \
            }                                                                                                              \
            StrAppendFmt(&(j), "\"{}\":", (Zstr)(k));                                                                      \
            JW_INT(j, i);                                                                                                  \
        } while (0)
        do {                                                                                                               \
            f64 UNPL(my_flt) = (f);                                                                                        \
            StrAppendFmt(&(j), "{}", UNPL(my_flt));                                                                        \
        } while (0)
                StrPushBackR(&(j), ',');                                                                                   \
            }                                                                                                              \
            StrAppendFmt(&(j), "\"{}\":", (Zstr)(k));                                                                      \
            JW_FLT(j, f);                                                                                                  \
        } while (0)
            const Str *UNPL(jw_s)   = &(s);                                                                                \
            u64        UNPL(jw_len) = StrLen(UNPL(jw_s));                                                                  \
            StrAppendFmt(&(j), "\"{}\"", UNPL(jw_len) ? (Zstr)StrBegin(UNPL(jw_s)) : (Zstr) "");                           \
        } while (0)
                StrPushBackR(&(j), ',');                                                                                   \
            }                                                                                                              \
            StrAppendFmt(&(j), "\"{}\":", (Zstr)(k));                                                                      \
            JW_STR(j, s);                                                                                                  \
        } while (0)
    #define JW_BOOL(j, b)                                                                                                  \
        do {                                                                                                               \
            StrAppendFmt(&(j), "{}", (Zstr)((b) ? "true" : "false"));                                                      \
        } while (0)
                StrPushBackR(&(j), ',');                                                                                   \
            }                                                                                                              \
            StrAppendFmt(&(j), "\"{}\":", (Zstr)(k));                                                                      \
            JW_BOOL(j, b);                                                                                                 \
        } while (0)
        ValidateStr(err_str);
        StrClear(err_str);
        StrAppendFmt(err_str, "{} (errno {})", errno_description(eno), eno);
        return err_str;
    }
        HeapAllocator h    = HeapAllocatorInit();
        Str           full = StrInit(&h);
        StrAppendFmt(&full, "[{}] [{}:{}] {}\n", (Zstr)NAMES[type], (Zstr)tag, line, (Zstr)msg);
    
        File out = (type == LOG_MESSAGE_TYPE_INFO) ? FileFromFd(1) : FileFromFd(2);
            LOG_FATAL("Invalid arguments");
        }
        if (!StrAppendFmt(
                o,
                "{04}-{02}-{02}T{02}:{02}:{02}",
            return false;
        }
        if (dt->nanosecond != 0 && !StrAppendFmt(o, ".{09}", dt->nanosecond)) {
            return false;
        }
        }
        if (dt->utc_offset_seconds == 0) {
            return StrAppendFmt(o, "Z");
        }
        i32 off = dt->utc_offset_seconds;
        u32 om  = (ao % 3600) / 60;
        if (off < 0) {
            return StrAppendFmt(o, "-{02}:{02}", oh, om);
        }
        return StrAppendFmt(o, "+{02}:{02}", oh, om);
            return StrAppendFmt(o, "-{02}:{02}", oh, om);
        }
        return StrAppendFmt(o, "+{02}:{02}", oh, om);
    }
        // so emit raw IPs in this build.
        for (size i = 0; i < count; ++i) {
            StrAppendFmt(&rendered, "  #{} {x}\n", (u32)i, (u64)frames[i].ip);
        }
    #endif
            return;
    
        StrAppendFmt(out, "DebugAllocator: {} live allocation(s):\n", (u64)MapPairCount(&self->live));
        MapForeachPairPtr(&self->live, key_ptr, val_ptr) {
            StrAppendFmt(out, "  leak: {x} ({} bytes)\n", (u64)*key_ptr, (u64)val_ptr->requested_size);
        StrAppendFmt(out, "DebugAllocator: {} live allocation(s):\n", (u64)MapPairCount(&self->live));
        MapForeachPairPtr(&self->live, key_ptr, val_ptr) {
            StrAppendFmt(out, "  leak: {x} ({} bytes)\n", (u64)*key_ptr, (u64)val_ptr->requested_size);
            if (val_ptr->alloc_trace_n > 0) {
    #if !defined(LOG_NO_BACKTRACE) || !LOG_NO_BACKTRACE
    #else
                for (size i = 0; i < val_ptr->alloc_trace_n; ++i) {
                    StrAppendFmt(out, "  #{} {x}\n", (u32)i, (u64)val_ptr->alloc_trace[i].ip);
                }
    #endif
            bool inner_ok = false;
            StrInitStack(child, DIR_REMOVE_ALL_PATH_CAP) {
                StrAppendFmt(&child, trail_sep ? "{}{}" : "{}/{}", path, entry_nm);
                if (e->type == DIR_ENTRY_TYPE_DIRECTORY) {
                    inner_ok = DirRemoveAll(&child);
    
            if (named) {
                StrAppendFmt(out, "  #{} {}+{x} [{x}]", (u32)i, sym_name, (u64)sym_off, (u64)ip);
            } else {
                StrAppendFmt(out, "  #{} {x}", (u32)i, (u64)ip);
                StrAppendFmt(out, "  #{} {}+{x} [{x}]", (u32)i, sym_name, (u64)sym_off, (u64)ip);
            } else {
                StrAppendFmt(out, "  #{} {x}", (u32)i, (u64)ip);
            }
            if (g_dbghelp_initialized && SymGetLineFromAddr64(proc, ip, &line_disp, &line) && line.FileName) {
                Zstr fname = sys_basename_of(line.FileName);
                StrAppendFmt(out, " ({}:{})", fname, (u32)line.LineNumber);
            }
            StrPushBackR(out, '\n');
            if (named) {
                Zstr mod = sys_basename_of(mod_path);
                StrAppendFmt(out, "  #{} {}!{}+{x} [{x}]\n", (u32)i, mod, sym_name, (u64)sym_off, ip);
            } else if (mod_path) {
                Zstr mod = sys_basename_of(mod_path);
            } else if (mod_path) {
                Zstr mod = sys_basename_of(mod_path);
                StrAppendFmt(out, "  #{} {}+? [{x}]\n", (u32)i, mod, ip);
            } else {
                StrAppendFmt(out, "  #{} {x}\n", (u32)i, ip);
                StrAppendFmt(out, "  #{} {}+? [{x}]\n", (u32)i, mod, ip);
            } else {
                StrAppendFmt(out, "  #{} {x}\n", (u32)i, ip);
            }
        }
        if (r->symbol_name) {
            Zstr mod = sys_basename_of(r->module_path);
            StrAppendFmt(out, "  #{} {}!{}+{x} [{x}]", idx, mod, r->symbol_name, r->offset, (u64)ip);
        } else if (r->module_path) {
            Zstr mod = sys_basename_of(r->module_path);
        } else if (r->module_path) {
            Zstr mod = sys_basename_of(r->module_path);
            StrAppendFmt(out, "  #{} {}+{x} [{x}]", idx, mod, r->offset, (u64)ip);
        } else {
            StrAppendFmt(out, "  #{} {x}", idx, (u64)ip);
            StrAppendFmt(out, "  #{} {}+{x} [{x}]", idx, mod, r->offset, (u64)ip);
        } else {
            StrAppendFmt(out, "  #{} {x}", idx, (u64)ip);
        }
        if (r->source_file) {
            Zstr file = sys_basename_of(r->source_file);
            if (r->source_line > 0) {
                StrAppendFmt(out, " ({}:{})", file, r->source_line);
            } else {
                StrAppendFmt(out, " ({})", file);
                StrAppendFmt(out, " ({}:{})", file, r->source_line);
            } else {
                StrAppendFmt(out, " ({})", file);
            }
        }
                emit_resolved_line(out, (u32)i, &r, frames[i].ip);
            } else {
                StrAppendFmt(out, "  #{} {x}\n", (u32)i, (u64)frames[i].ip);
            }
        }
        if (!SymbolResolverInit(&res, alloc)) {
            for (size i = 0; i < count; ++i) {
                StrAppendFmt(out, "  #{} {x}\n", (u32)i, (u64)frames[i].ip);
            }
            return;
                StrResize(&host, ZstrLen(host_data));
                port = FROM_BIG_ENDIAN2(sa->sin_port);
                StrAppendFmt(&out, "{}:{}", (Zstr)host_data, (u32)port);
            } else if (addr->family == SOCKET_FAMILY_INET6) {
                const struct sockaddr_in6 *sa = (const struct sockaddr_in6 *)addr->raw;
                StrResize(&host, ZstrLen(host_data));
                port = FROM_BIG_ENDIAN2(sa->sin6_port);
                StrAppendFmt(&out, "[{}]:{}", (Zstr)host_data, (u32)port);
            } else {
                LOG_ERROR("SocketAddrFormat: unknown family {}", (u32)addr->family);
        }
    
        StrAppendFmt(
            &out,
            "HTTP/1.1 {}\r\n"
    
        VecForeachPtr(&response->headers, header) {
            StrAppendFmt(&out, "{}: {}\r\n", header->key, header->value);
        }
        }
    
        StrAppendFmt(&out, "\r\n");
    
        if (StrLen(&response->body)) {
    
        Str in = StrInit(alloc_base);
        StrAppendFmt(&in, "string-content");
    
        i64  wrote = FileWriteAndClose(&path, &in);
        // Build the out-of-range marker "#N " that only the `<=` mutant emits.
        Str ns = StrInit(&scratch);
        StrAppendFmt(&ns, "#{} ", n);
    
        ok = ok && (ZstrFindSubstring(StrBegin(&out), "leak:") != NULL);
        Str              s     = StrInit(&alloc);
        DateTime         d     = DateTimeFromUnixNs(T_2021 * NS_PER_SEC, 0);
        StrAppendFmt(&s, "{}", d);
        bool ok = ZstrCompare(StrBegin(&s), "2021-01-01T00:00:00Z") == 0;
        StrDeinit(&s);
        Str              s     = StrInit(&alloc);
        DateTime         d     = DateTimeFromUnixNs(T_2021 * NS_PER_SEC, IST);
        StrAppendFmt(&s, "{}", d);
        bool ok = ZstrCompare(StrBegin(&s), "2021-01-01T05:30:00+05:30") == 0;
        StrDeinit(&s);
        Str              s     = StrInit(&alloc);
        DateTime         d     = DateTimeFromUnixNs(T_2021 * NS_PER_SEC + 123456789ull, 0);
        StrAppendFmt(&s, "{}", d);
        bool ok = ZstrCompare(StrBegin(&s), "2021-01-01T00:00:00.123456789Z") == 0;
        StrDeinit(&s);
        Str              s     = StrInit(&alloc);
        DateTime         a     = DateTimeFromUnixNs(T_2021 * NS_PER_SEC + 123456789ull, MST);
        StrAppendFmt(&s, "{}", a);
    
        DateTime b = {0};
            return false;
        }
        return StrAppendFmt(o, "({}, {})", p->x, p->y);
    }
            return false;
        }
        return StrAppendFmt(o, "[{}..{}]", b->min, b->max);
    }
            return false;
        }
        return StrAppendFmt(o, "{}:{}@{}", r->id, r->bbox, r->centroid);
    }
    
        Point2D p  = {.x = 3, .y = 4};
        bool    ok = StrAppendFmt(&out, "{}", p);
        ok         = ok && (ZstrCompare(StrBegin(&out), "(3, 4)") == 0);
        Point2D p     = {.x = -1, .y = 2};
        i32     count = 7;
        bool    ok    = StrAppendFmt(&out, "got {} hits at {}", count, p);
        ok            = ok && (ZstrCompare(StrBegin(&out), "got 7 hits at (-1, 2)") == 0);
        Point2D dst = {0};
    
        bool ok = StrAppendFmt(&out, "{}", src);
        Zstr in = StrBegin(&out);
        StrReadFmt(in, "{}", dst);
            .max = {.x = 10, .y = 20}
        };
        bool ok = StrAppendFmt(&out, "{}", b);
        ok      = ok && (ZstrCompare(StrBegin(&out), "[(0, 0)..(10, 20)]") == 0);
        Bounds dst = {0};
    
        bool ok = StrAppendFmt(&out, "{}", src);
        Zstr in = StrBegin(&out);
        StrReadFmt(in, "{}", dst);
            .centroid = {                .x = 50,                    .y = 25},
        };
        bool ok = StrAppendFmt(&out, "{}", r);
        ok      = ok && (ZstrCompare(StrBegin(&out), "42:[(0, 0)..(100, 50)]@(50, 25)") == 0);
        Region dst = {0};
    
        bool ok = StrAppendFmt(&out, "{}", src);
        Zstr in = StrBegin(&out);
        StrReadFmt(in, "{}", dst);
        // arms (i32 implicit, Region, Point2D, f64) and proves nested user
        // types and built-ins coexist in one IOFMT expansion.
        bool ok = StrAppendFmt(&out, "score={.1} region={} origin={}", score, region, origin);
        ok      = ok && (ZstrCompare(StrBegin(&out), "score=1.5 region=7:[(1, 2)..(3, 4)]@(2, 3) origin=(0, 0)") == 0);
        bool success = true;
    
        StrAppendFmt(&output, "");
        success = success && (StrLen(&output) == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "Hello, world!");
        success = success && (ZstrCompare(StrBegin(&output), "Hello, world!") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{​{Hello}}");
        success = success && (ZstrCompare(StrBegin(&output), "{Hello}") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{​{{​{");
        success = success && (ZstrCompare(StrBegin(&output), "{​{") == 0);
    
        Zstr str = "Hello";
        StrAppendFmt(&output, "{}", str);
        success = success && (ZstrCompare(StrBegin(&output), "Hello") == 0);
        StrClear(&output);
    
        Zstr empty = "";
        StrAppendFmt(&output, "{}", empty);
        success = success && (StrLen(&output) == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{>10}", str);
        success = success && (ZstrCompare(StrBegin(&output), "     Hello") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{<10}", str);
        success = success && (ZstrCompare(StrBegin(&output), "Hello     ") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{^10}", str);
        success = success && (ZstrCompare(StrBegin(&output), "  Hello   ") == 0);
        StrClear(&output);
    
        Str s = StrInitFromZstr("World", &alloc);
        StrAppendFmt(&output, "{}", s);
        success = success && (ZstrCompare(StrBegin(&output), "World") == 0);
        StrDeinit(&s);
    
        i8 i8_val = -42;
        StrAppendFmt(&output, "{}", i8_val);
        success = success && (ZstrCompare(StrBegin(&output), "-42") == 0);
        StrClear(&output);
    
        i16 i16_val = -1234;
        StrAppendFmt(&output, "{}", i16_val);
        success = success && (ZstrCompare(StrBegin(&output), "-1234") == 0);
        StrClear(&output);
    
        i32 i32_val = -123456;
        StrAppendFmt(&output, "{}", i32_val);
        success = success && (ZstrCompare(StrBegin(&output), "-123456") == 0);
        StrClear(&output);
    
        i64 i64_val = -1234567890LL;
        StrAppendFmt(&output, "{}", i64_val);
        success = success && (ZstrCompare(StrBegin(&output), "-1234567890") == 0);
        StrClear(&output);
    
        u8 u8_val = 42;
        StrAppendFmt(&output, "{}", u8_val);
        success = success && (ZstrCompare(StrBegin(&output), "42") == 0);
        StrClear(&output);
    
        u16 u16_val = 1234;
        StrAppendFmt(&output, "{}", u16_val);
        success = success && (ZstrCompare(StrBegin(&output), "1234") == 0);
        StrClear(&output);
    
        u32 u32_val = 123456;
        StrAppendFmt(&output, "{}", u32_val);
        success = success && (ZstrCompare(StrBegin(&output), "123456") == 0);
        StrClear(&output);
    
        u64 u64_val = 1234567890ULL;
        StrAppendFmt(&output, "{}", u64_val);
        success = success && (ZstrCompare(StrBegin(&output), "1234567890") == 0);
        StrClear(&output);
    
        i8 i8_max = 127;
        StrAppendFmt(&output, "{}", i8_max);
        success = success && (ZstrCompare(StrBegin(&output), "127") == 0);
        StrClear(&output);
    
        i8 i8_min = -128;
        StrAppendFmt(&output, "{}", i8_min);
        success = success && (ZstrCompare(StrBegin(&output), "-128") == 0);
        StrClear(&output);
    
        u8 u8_max = 255;
        StrAppendFmt(&output, "{}", u8_max);
        success = success && (ZstrCompare(StrBegin(&output), "255") == 0);
        StrClear(&output);
    
        u8 u8_min = 0;
        StrAppendFmt(&output, "{}", u8_min);
        success = success && (ZstrCompare(StrBegin(&output), "0") == 0);
    
        u32 val = 0xDEADBEEF;
        StrAppendFmt(&output, "{x}", val);
        success = success && (ZstrCompare(StrBegin(&output), "0xdeadbeef") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{X}", val);
        success = success && (ZstrCompare(StrBegin(&output), "0xDEADBEEF") == 0);
    
        u8 val = 0xA5; // 10100101 in binary
        StrAppendFmt(&output, "{b}", val);
        success = success && (ZstrCompare(StrBegin(&output), "0b10100101") == 0);
    
        u16 val = 0777;
        StrAppendFmt(&output, "{o}", val);
        success = success && (ZstrCompare(StrBegin(&output), "0o777") == 0);
    
        f32 f32_val = 3.14159f;
        StrAppendFmt(&output, "{}", f32_val);
        success = success && (ZstrCompare(StrBegin(&output), "3.141590") == 0);
        StrClear(&output);
    
        f64 f64_val = 2.71828;
        StrAppendFmt(&output, "{}", f64_val);
        success = success && (ZstrCompare(StrBegin(&output), "2.718280") == 0);
        f64 val = 3.14159265359;
    
        StrAppendFmt(&output, "{.2}", val);
        success = success && (ZstrCompare(StrBegin(&output), "3.14") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{.0}", val);
        success = success && (ZstrCompare(StrBegin(&output), "3") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{.10}", val);
        success = success && (ZstrCompare(StrBegin(&output), "3.1415926536") == 0);
    
        f64 pos_inf = F64_INFINITY;
        StrAppendFmt(&output, "{}", pos_inf);
        success = success && (ZstrCompare(StrBegin(&output), "inf") == 0);
        StrClear(&output);
    
        f64 neg_inf = -F64_INFINITY;
        StrAppendFmt(&output, "{}", neg_inf);
        success = success && (ZstrCompare(StrBegin(&output), "-inf") == 0);
        StrClear(&output);
    
        f64 nan_val = F64_NAN;
        StrAppendFmt(&output, "{}", nan_val);
        success = success && (ZstrCompare(StrBegin(&output), "nan") == 0);
    
        i32 val = 42;
        StrAppendFmt(&output, "{5}", val);
        success = success && (ZstrCompare(StrBegin(&output), "   42") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{<5}", val);
        success = success && (ZstrCompare(StrBegin(&output), "42   ") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{^5}", val);
        success = success && (ZstrCompare(StrBegin(&output), " 42  ") == 0);
        StrClear(&output);
    
        Zstr str = "abc";
        StrAppendFmt(&output, "{5}", str);
        success = success && (ZstrCompare(StrBegin(&output), "  abc") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{<5}", str);
        success = success && (ZstrCompare(StrBegin(&output), "abc  ") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{^5}", str);
        success = success && (ZstrCompare(StrBegin(&output), " abc ") == 0);
        f64  pi    = 3.14;
    
        StrAppendFmt(&output, "{} {} {}", hello, num, pi);
        success = success && (ZstrCompare(StrBegin(&output), "Hello 42 3.140000") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{} {} {}", pi, hello, num);
        success = success && (ZstrCompare(StrBegin(&output), "3.140000 Hello 42") == 0);
    
        Zstr mixed_case = "MiXeD CaSe";
        StrAppendFmt(&output, "{c}", mixed_case);
        success = success && (ZstrCompare(StrBegin(&output), "MiXeD CaSe") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", mixed_case);
        success = success && (ZstrCompare(StrBegin(&output), "mixed case") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", mixed_case);
        success = success && (ZstrCompare(StrBegin(&output), "MIXED CASE") == 0);
        StrClear(&output);
        Str s = StrInitFromZstr("MiXeD CaSe", &alloc);
    
        StrAppendFmt(&output, "{c}", s);
        success = success && (ZstrCompare(StrBegin(&output), "MiXeD CaSe") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", s);
        success = success && (ZstrCompare(StrBegin(&output), "mixed case") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", s);
        success = success && (ZstrCompare(StrBegin(&output), "MIXED CASE") == 0);
        StrClear(&output);
        u8 lower_char = 'm';
    
        StrAppendFmt(&output, "{c}", upper_char);
        success = success && (ZstrCompare(StrBegin(&output), "M") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", upper_char);
        success = success && (ZstrCompare(StrBegin(&output), "m") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", lower_char);
        success = success && (ZstrCompare(StrBegin(&output), "M") == 0);
        StrClear(&output);
        u16 u16_value = ('A' << 8) | 'B'; // big-endian "AB"
    
        StrAppendFmt(&output, "{c}", u16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'A' && StrBegin(&output)[1] == 'B');
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", u16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'a' && StrBegin(&output)[1] == 'b');
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", u16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'A' && StrBegin(&output)[1] == 'B');
        StrClear(&output);
        i16 i16_value = ('C' << 8) | 'd'; // big-endian "Cd"
    
        StrAppendFmt(&output, "{c}", i16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'C' && StrBegin(&output)[1] == 'd');
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", i16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'c' && StrBegin(&output)[1] == 'd');
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", i16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'C' && StrBegin(&output)[1] == 'D');
        StrClear(&output);
        u32 u32_value = ('E' << 24) | ('f' << 16) | ('G' << 8) | 'h'; // big-endian "EfGh"
    
        StrAppendFmt(&output, "{c}", u32_value);
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'E' && StrBegin(&output)[1] == 'f' &&
                              StrBegin(&output)[2] == 'G' && StrBegin(&output)[3] == 'h');
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", u32_value);
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'e' && StrBegin(&output)[1] == 'f' &&
                              StrBegin(&output)[2] == 'g' && StrBegin(&output)[3] == 'h');
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", u32_value);
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'E' && StrBegin(&output)[1] == 'F' &&
                              StrBegin(&output)[2] == 'G' && StrBegin(&output)[3] == 'H');
        i32 i32_value = ('I' << 24) | ('j' << 16) | ('K' << 8) | 'l'; // big-endian "IjKl"
    
        StrAppendFmt(&output, "{c}", i32_value);
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'I' && StrBegin(&output)[1] == 'j' &&
                              StrBegin(&output)[2] == 'K' && StrBegin(&output)[3] == 'l');
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", i32_value);
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'i' && StrBegin(&output)[1] == 'j' &&
                              StrBegin(&output)[2] == 'k' && StrBegin(&output)[3] == 'l');
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", i32_value);
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'I' && StrBegin(&output)[1] == 'J' &&
                              StrBegin(&output)[2] == 'K' && StrBegin(&output)[3] == 'L');
                        ('r' << 16) | ('S' << 8) | 't'; // big-endian "MnOpQrSt"
    
        StrAppendFmt(&output, "{c}", u64_value);
        success = success && (StrLen(&output) == 8 && StrBegin(&output)[0] == 'M' && StrBegin(&output)[1] == 'n' &&
                              StrBegin(&output)[2] == 'O' && StrBegin(&output)[3] == 'p' && StrBegin(&output)[4] == 'Q' &&
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", u64_value);
        success = success && (StrLen(&output) == 8 && StrBegin(&output)[0] == 'm' && StrBegin(&output)[1] == 'n' &&
                              StrBegin(&output)[2] == 'o' && StrBegin(&output)[3] == 'p' && StrBegin(&output)[4] == 'q' &&
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", u64_value);
        success = success && (StrLen(&output) == 8 && StrBegin(&output)[0] == 'M' && StrBegin(&output)[1] == 'N' &&
                              StrBegin(&output)[2] == 'O' && StrBegin(&output)[3] == 'P' && StrBegin(&output)[4] == 'Q' &&
                        ('z' << 16) | ('1' << 8) | '2'; // big-endian "UvWxYz12"
    
        StrAppendFmt(&output, "{c}", i64_value);
        success = success && (StrLen(&output) == 8 && StrBegin(&output)[0] == 'U' && StrBegin(&output)[1] == 'v' &&
                              StrBegin(&output)[2] == 'W' && StrBegin(&output)[3] == 'x' && StrBegin(&output)[4] == 'Y' &&
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", i64_value);
        success = success && (StrLen(&output) == 8 && StrBegin(&output)[0] == 'u' && StrBegin(&output)[1] == 'v' &&
                              StrBegin(&output)[2] == 'w' && StrBegin(&output)[3] == 'x' && StrBegin(&output)[4] == 'y' &&
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", i64_value);
        success = success && (StrLen(&output) == 8 && StrBegin(&output)[0] == 'U' && StrBegin(&output)[1] == 'V' &&
                              StrBegin(&output)[2] == 'W' && StrBegin(&output)[3] == 'X' && StrBegin(&output)[4] == 'Y' &&
    
        BitVec bv1 = BitVecFromStr("10110", alloc_base);
        StrAppendFmt(&output, "{}", bv1);
        success = success && (ZstrCompare(StrBegin(&output), "10110") == 0);
        StrClear(&output);
    
        BitVec bv_empty = BitVecInit(alloc_base);
        StrAppendFmt(&output, "{}", bv_empty);
        success = success && (StrLen(&output) == 0);
        StrClear(&output);
    
        BitVec bv2 = BitVecFromInteger(0xABCD, 16, alloc_base);
        StrAppendFmt(&output, "{x}", bv2);
        success = success && (ZstrCompare(StrBegin(&output), "0xabcd") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{X}", bv2);
        success = success && (ZstrCompare(StrBegin(&output), "0xABCD") == 0);
        StrClear(&output);
    
        BitVec bv3 = BitVecFromInteger(0755, 10, alloc_base);
        StrAppendFmt(&output, "{o}", bv3);
        success = success && (ZstrCompare(StrBegin(&output), "0o755") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{>10}", bv1);
        success = success && (ZstrCompare(StrBegin(&output), "     10110") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{<10}", bv1);
        success = success && (ZstrCompare(StrBegin(&output), "10110     ") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{^10}", bv1);
        success = success && (ZstrCompare(StrBegin(&output), "  10110   ") == 0);
        StrClear(&output);
    
        BitVec bv_zero = BitVecFromInteger(0, 1, alloc_base);
        StrAppendFmt(&output, "{x}", bv_zero);
        success = success && (ZstrCompare(StrBegin(&output), "0x0") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{o}", bv_zero);
        success = success && (ZstrCompare(StrBegin(&output), "0o0") == 0);
        StrClear(&output);
        Int oct_val = IntFrom(493, alloc_base);
    
        StrAppendFmt(&output, "{}", big_dec);
        success = success && (ZstrCompare(StrBegin(&output), "123456789012345678901234567890") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{x}", hex_val);
        success = success && (ZstrCompare(StrBegin(&output), "deadbeefcafebabe1234") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{X}", hex_val);
        success = success && (ZstrCompare(StrBegin(&output), "DEADBEEFCAFEBABE1234") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{b}", bin_val);
        success = success && (ZstrCompare(StrBegin(&output), "10100011") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{o}", oct_val);
        success = success && (ZstrCompare(StrBegin(&output), "755") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{>34}", big_dec);
        success = success && (ZstrCompare(StrBegin(&output), "    123456789012345678901234567890") == 0);
        Float short_v = FloatFromStr("1.2", alloc_base);
    
        StrAppendFmt(&output, "{}", exact);
        success = success && (ZstrCompare(StrBegin(&output), "1234567890.012345") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{e}", sci);
        success = success && (ZstrCompare(StrBegin(&output), "1.234567e+04") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{E}", sci);
        success = success && (ZstrCompare(StrBegin(&output), "1.234567E+04") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{.3}", short_v);
        success = success && (ZstrCompare(StrBegin(&output), "1.200") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{>18}", sci);
        success = success && (ZstrCompare(StrBegin(&output), "          12345.67") == 0);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInit(&alloc);
        StrAppendFmt(&s, "old prefix ");
        StrWriteFmt(&s, "fresh {}", LVAL(42));
        bool ok = (StrLen(&s) == 8) && (StrBegin(&s)[0] == 'f') && (StrBegin(&s)[StrLen(&s) - 1] == '2');
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInit(&alloc);
        StrAppendFmt(&s, "AAAAAAAA");
        size before_length = StrLen(&s);
        bool ok            = StrPatchFmt(&s, 2, "{}", LVAL(1234));
    
        BufPushBytes(&b, (const u8 *)"hello", 5);
        StrAppendFmt(&output, "{}", b); // Buf is a first-class {} argument now
    
        bool ok = ZstrCompare(StrBegin(&output), "hello") == 0;
        // 0x1B (ESC) is non-printable -> "\x1b".
        u8 esc = 0x1B;
        StrAppendFmt(&out, "{c}", esc);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x1b") == 0);
        StrClear(&out);
        // 0x07 (BEL) -> "\x07".
        u8 bel = 0x07;
        StrAppendFmt(&out, "{c}", bel);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x07") == 0);
        StrClear(&out);
        // 0xFF with caps spec -> uppercase hex digits "\xFF".
        u8 hi = 0xFF;
        StrAppendFmt(&out, "{A}", hi);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\xFF") == 0);
        StrClear(&out);
        // 0xAB lowercase by default for {c}.
        u8 ab = 0xAB;
        StrAppendFmt(&out, "{c}", ab);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\xab") == 0);
        DateTime utc = DateTimeFromUnixNs(base, 0);
        StrClear(&s);
        StrAppendFmt(&s, "{}", utc);
        ok = ok && ZstrCompare(StrBegin(&s), "2021-01-01T00:00:00Z") == 0;
        DateTime ist = DateTimeFromUnixNs(base, 19800); // +05:30
        StrClear(&s);
        StrAppendFmt(&s, "{}", ist);
        ok = ok && ZstrCompare(StrBegin(&s), "2021-01-01T05:30:00+05:30") == 0;
        DateTime neg = DateTimeFromUnixNs(base, -34200); // -09:30
        StrClear(&s);
        StrAppendFmt(&s, "{}", neg);
        ok = ok && ZstrCompare(StrBegin(&s), "2020-12-31T14:30:00-09:30") == 0;
        DateTime frac = DateTimeFromUnixNs(base + 123456789ull, 0);
        StrClear(&s);
        StrAppendFmt(&s, "{}", frac);
        ok = ok && ZstrCompare(StrBegin(&s), "2021-01-01T00:00:00.123456789Z") == 0;
        Str s = StrInitFromZstr("xyz", &alloc);
    
        StrAppendFmt(&output, "AB");
        // width 6, right-aligned: "xyz" (content_len 3) gets 3 pad spaces.
        StrAppendFmt(&output, "{>6}", s);
        StrAppendFmt(&output, "AB");
        // width 6, right-aligned: "xyz" (content_len 3) gets 3 pad spaces.
        StrAppendFmt(&output, "{>6}", s);
        // start_len was 2 ("AB"), content_len = StrLen(o) - start_len = 3, pad to
        // width 6 => 3 spaces. StrPad with ALIGN_RIGHT pushes the pad to the FRONT
    
        Str s = StrInitFromZstr("hi", &alloc);
        StrAppendFmt(&output, "{<5}", s);
        ok = ok && (ZstrCompare(StrBegin(&output), "hi   ") == 0);
        StrPushBackR(&s, (char)0x0F); // single byte 0x0F -> "f"
    
        StrAppendFmt(&output, "{X}", s);
        ok = ok && (ZstrCompare(StrBegin(&output), "0x0F") == 0);
        StrPushBackR(&s, (char)0x0F);
    
        StrAppendFmt(&output, "{x}", s);
        ok = ok && (ZstrCompare(StrBegin(&output), "0x0f") == 0);
        StrPushBackR(&s, (char)0x02);
    
        StrAppendFmt(&output, "{x}", s);
        // gt_to_ge => " 0x01 0x02" (leading space); gt_to_le => "0x010x02".
        ok = ok && (ZstrCompare(StrBegin(&output), "0x01 0x02") == 0);
        StrPushBackR(&s, (char)0x05);
    
        StrAppendFmt(&output, "{x}", s);
        ok = ok && (ZstrCompare(StrBegin(&output), "0x05") == 0);
    
        Str s = StrInitFromZstr("hello", &alloc);
        StrAppendFmt(&output, "{.3}", s);
        ok = ok && (ZstrCompare(StrBegin(&output), "hel") == 0);
    
        Str s = StrInitFromZstr("hello", &alloc);
        StrAppendFmt(&output, "{.0}", s);
        ok = ok && (StrLen(&output) == 0);
    
        Zstr s = "AB"; // 0x41 0x42
        StrAppendFmt(&output, "{x}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "0x41 0x42") == 0);
    
        Zstr s = "Z"; // 0x5a
        StrAppendFmt(&output, "{x}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "0x5a") == 0);
    
        Zstr s = "\xab"; // single byte 0xab
        StrAppendFmt(&output, "{x}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "0xab") == 0);
    
        Zstr s = "\xab";
        StrAppendFmt(&output, "{X}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "0xAB") == 0);
    
        Zstr s = "\x05"; // single byte 0x05
        StrAppendFmt(&output, "{x}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "0x05") == 0);
    
        Zstr s = "\xfe";
        StrAppendFmt(&output, "{x}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "0xfe") == 0);
    
        Zstr s = "Hello";
        StrAppendFmt(&output, "{.3}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "Hel") == 0);
    
        Zstr s = "Hi";
        StrAppendFmt(&output, "{.10}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "Hi") == 0);
    
        Zstr s = "Hello";
        StrAppendFmt(&output, "{.0}", s);
        bool ok = (StrLen(&output) == 0);
    
        Zstr s = "Hello";
        StrAppendFmt(&output, "{.1}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "H") == 0);
    
        Zstr s = "Hi";
        StrAppendFmt(&output, "abc{>10}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "        abcHi") == 0);
    
        Zstr s = "Hi";
        StrAppendFmt(&output, "xy{<10}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "xyHi        ") == 0);
    
        Zstr s = "";
        StrAppendFmt(&output, "{>5}", s);
        bool ok = (ZstrCompare(StrBegin(&output), "     ") == 0);
        Float v      = FloatFromStr("1.0", alloc_base);
    
        StrAppendFmt(&output, "{e}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "1e+00") == 0);
        Float v      = FloatFromStr("0.0", alloc_base);
    
        StrAppendFmt(&output, "{.0e}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "0e+00") == 0);
        Float v      = FloatFromStr("0.0", alloc_base);
    
        StrAppendFmt(&output, "{.3e}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "0.000e+00") == 0);
        Float v      = FloatFromStr("12.0", alloc_base);
    
        StrAppendFmt(&output, "{.2e}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "1.20e+01") == 0);
        Float v      = FloatFromStr("12345.67", alloc_base);
    
        StrAppendFmt(&output, "{e}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "1.234567e+04") == 0);
        StrClear(&output);
        StrClear(&output);
    
        StrAppendFmt(&output, "{E}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "1.234567E+04") == 0);
        bool             ok    = true;
    
        StrAppendFmt(&out, "{05}", (u32)42);
        ok = ok && (ZstrCompare(StrBegin(&out), "00042") == 0);
        ok = ok && (StrLen(&out) == 5);
        bool             ok    = true;
    
        StrAppendFmt(&out, "{05}", (u32)12345);
        ok = ok && (ZstrCompare(StrBegin(&out), "12345") == 0);
        ok = ok && (StrLen(&out) == 5);
        bool             ok    = true;
    
        StrAppendFmt(&out, "{06}", (i32)-42);
        ok = ok && (ZstrCompare(StrBegin(&out), "-00042") == 0);
        ok = ok && (StrLen(&out) == 6);
        bool             ok    = true;
    
        StrAppendFmt(&out, "{05}", (i32)42);
        ok = ok && (ZstrCompare(StrBegin(&out), "00042") == 0);
        bool             ok    = true;
    
        StrAppendFmt(&out, "{08}", (i32)-7);
        ok = ok && (ZstrCompare(StrBegin(&out), "-0000007") == 0);
        ok = ok && (StrLen(&out) == 8);
        bool             ok    = true;
    
        StrAppendFmt(&out, "{016x}", (u32)0xFF);
        ok = ok && (ZstrCompare(StrBegin(&out), "00000000000000ff") == 0);
        ok = ok && (StrLen(&out) == 16);
    
        f64 v = 1.5;
        StrAppendFmt(&output, "{}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "1.500000") == 0);
    
        f64 v = 1.5;
        StrAppendFmt(&output, "{e}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "1.500000e+00") == 0);
    
        f64 v = 1.5;
        StrAppendFmt(&output, "{E}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "1.500000E+00") == 0);
    
        f64 v = 1.5;
        StrAppendFmt(&output, "{12}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "    1.500000") == 0);
        ok = ok && (StrLen(&output) == 12);
    
        f64 v = 1.5;
        StrAppendFmt(&output, "{12}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "    XYZ1.500000") == 0);
    
        f64 v = 1.5;
        StrAppendFmt(&output, "{12}", v);
        ok = ok && (StrLen(&output) == 12);
        Float v   = FloatFromStr("1.0", &alloc.base);
    
        StrAppendFmt(&out, "{e}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1e+00") == 0);
        Float v   = FloatFromStr("0.001", &alloc.base);
    
        StrAppendFmt(&out, "{e}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1e-03") == 0);
        Float v   = FloatFromStr("1e12", &alloc.base);
    
        StrAppendFmt(&out, "{e}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1e+12") == 0);
    
        i64 v = 255; // 0xff
        StrAppendFmt(&out, "{x}", v);
        // Real: lowercase hex "0xff". base->42 mutant fails (not "0xff");
        // and_to_or mutant would force caps -> "0xFF".
    
        i64 v = 255;
        StrAppendFmt(&out, "{X}", v);
        // Real: uppercase hex "0xFF". ne_to_eq mutant inverts caps -> "0xff".
        bool ok = (ZstrCompare(StrBegin(&out), "0xFF") == 0);
    
        i64 v = 5; // 0b101
        StrAppendFmt(&out, "{b}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "0b101") == 0);
    
        i64 v = 8; // 0o10
        StrAppendFmt(&out, "{o}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "0o10") == 0);
    
        i64 v = -42;
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "-42") == 0);
    
        i64 v = 42;
        StrAppendFmt(&out, "X{5}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "   X42") == 0);
    
        i64 v = 42;
        StrAppendFmt(&out, "X{05}", v);
        // start_len=1, content "42" len 2, width 5 -> three '0' fills: "X00042".
        bool ok = (ZstrCompare(StrBegin(&out), "X00042") == 0);
        bool success = true;
    
        StrAppendFmt(&output, "{c}", a);
        success = (ZstrCompare(StrBegin(&output), "A") == 0);
        bool success = true;
    
        StrAppendFmt(&output, "{c}", a);
        success = (ZstrCompare(StrBegin(&output), "z") == 0);
        bool success = true;
    
        StrAppendFmt(&output, "ab");
        StrAppendFmt(&output, "{>6}", seven);
        success = (ZstrCompare(StrBegin(&output), "     ab7") == 0);
    
        StrAppendFmt(&output, "ab");
        StrAppendFmt(&output, "{>6}", seven);
        success = (ZstrCompare(StrBegin(&output), "     ab7") == 0);
    
        i32  v  = 5;
        bool ok = !StrAppendFmt(&out, "{", v); // unclosed -> must return false
    
        StrDeinit(&out);
    
        u8   v  = 200;
        bool ok = StrAppendFmt(&out, "{1r}", v);
        ok      = ok && (ZstrCompare(StrBegin(&out), "200") == 0);
    
        u16  v  = 258;
        bool ok = StrAppendFmt(&out, "{2r}", v);
        ok      = ok && (ZstrCompare(StrBegin(&out), "258") == 0);
    
        u32  v  = 65538;
        bool ok = StrAppendFmt(&out, "{4r}", v);
        ok      = ok && (ZstrCompare(StrBegin(&out), "65538") == 0);
    
        u64  v  = 4294967298ULL;
        bool ok = StrAppendFmt(&out, "{8r}", v);
        ok      = ok && (ZstrCompare(StrBegin(&out), "4294967298") == 0);
    
        i64  v  = 4294967298LL;
        bool ok = StrAppendFmt(&out, "{8r}", v);
        ok      = ok && (ZstrCompare(StrBegin(&out), "4294967298") == 0);
    
        f64  v  = 1.5;
        bool ok = StrAppendFmt(&out, "{8r}", v);
        ok      = ok && (StrLen(&out) > 0); // returns false under the mutant
    
        // Pre-existing content so start_len != 0 at the point _write_u64 runs.
        StrAppendFmt(&out, "X:");
        u64 v = 255;
        // content_len = StrLen(o) - start_len = 3 ("255"), width 8 -> 5 pad spaces.
        // StrPad ALIGN_RIGHT prepends the pad to the FRONT of the whole buffer, so
        // the "X:" prefix rides along after the spaces: "     X:255" (len 10).
        StrAppendFmt(&out, "{8}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "     X:255") == 0);
    
        u64 v = 7;
        StrAppendFmt(&out, "{5}", v); // "    7"
        ok = ok && (ZstrCompare(StrBegin(&out), "    7") == 0);
        StrClear(&out);
    
        // Left-align variant so a dropped pad is unambiguous.
        StrAppendFmt(&out, "{<5}", v); // "7    "
        ok = ok && (ZstrCompare(StrBegin(&out), "7    ") == 0);
        bool             ok    = true;
    
        StrAppendFmt(&out, "p");       // start_len = 1
        u64 v = 42;
        StrAppendFmt(&out, "{05}", v); // zero-pad width 5 -> "00042"
        StrAppendFmt(&out, "p");       // start_len = 1
        u64 v = 42;
        StrAppendFmt(&out, "{05}", v); // zero-pad width 5 -> "00042"
        ok = ok && (ZstrCompare(StrBegin(&out), "p00042") == 0);
        Float            v          = FloatFromStr("12345.67", alloc_base);
    
        StrAppendFmt(&out, "{e}", v);
        bool ok = ZstrCompare(StrBegin(&out), "1.234567e+04") == 0;
        Float            v          = FloatFromStr("12345.67", alloc_base);
    
        StrAppendFmt(&out, "{.2e}", v);
        bool ok = ZstrCompare(StrBegin(&out), "1.23e+04") == 0;
        Float            v          = FloatFromStr("1.2", alloc_base);
    
        StrAppendFmt(&out, "XX");
        StrAppendFmt(&out, "{>5}", v);
        bool ok = ZstrCompare(StrBegin(&out), "  XX1.2") == 0 && StrLen(&out) == 7;
    
        StrAppendFmt(&out, "XX");
        StrAppendFmt(&out, "{>5}", v);
        bool ok = ZstrCompare(StrBegin(&out), "  XX1.2") == 0 && StrLen(&out) == 7;
        BitVec bv  = BitVecFromStr("1", alloc_base);
    
        StrAppendFmt(&out, "XX{>5}", bv);
        bool ok = (StrLen(&out) == 7) && (ZstrCompare(StrBegin(&out), "    XX1") == 0);
    
        Zstr name = "hello";
        StrAppendFmt(&output, "{s}", ZstrIO(name, alloc_base));
    
        bool ok = (ZstrCompare(StrBegin(&output), "hello") == 0);
    
        i8 v = (i8)'Q';
        StrAppendFmt(&output, "{c}", v);
    
        bool ok = (StrLen(&output) == 1) && (StrBegin(&output)[0] == 'Q');
        MemCopy(&v, &bits, sizeof(v));
    
        StrAppendFmt(&output, "{c}", v);
    
        bool ok = (StrLen(&output) == 4) && (ZstrCompare(StrBegin(&output), "BCDE") == 0);
        Str src = StrInit(&alloc);
        StrPushBackR(&src, 0x1A);
        StrAppendFmt(&out, "{c}", src);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x1a") == 0);
        StrClear(&out);
        // of the boundary too => "\xa1".
        StrPushBackR(&src, 0xA1);
        StrAppendFmt(&out, "{c}", src);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\xa1") == 0);
        // "1.2" has 1 fractional digit; {.5} requests 5 -> 4 zeros are padded.
        Float f = FloatFromStr("1.2", alloc_base);
        StrAppendFmt(&out, "{.5}", f);
        ok = ok && (ZstrCompare(StrBegin(&out), "1.20000") == 0);
        StrClear(&out);
    
        Float g = FloatFromStr("3.14", alloc_base);
        StrAppendFmt(&out, "{.6}", g);
        ok = ok && (ZstrCompare(StrBegin(&out), "3.140000") == 0);
        // 255 is 3 digits; zero-pad to width 3 -> exactly fits, no leading zeros.
        u32 v = 255;
        StrAppendFmt(&out, "{03}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "255") == 0);
        StrClear(&out);
    
        // Sanity: width one wider really does pad (guards against a no-op pad).
        StrAppendFmt(&out, "{04}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "0255") == 0);
        // "abc" is 3 chars; field width 3 -> no padding at all.
        Zstr s = "abc";
        StrAppendFmt(&out, "{3}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "abc") == 0);
        StrClear(&out);
    
        // Left/center at the exact boundary likewise add nothing.
        StrAppendFmt(&out, "{<3}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "abc") == 0);
        StrClear(&out);
        StrClear(&out);
    
        StrAppendFmt(&out, "{^3}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "abc") == 0);
        StrClear(&out);
    
        // Sanity: width 4 does pad (one space, right-aligned default).
        StrAppendFmt(&out, "{4}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), " abc") == 0);
        bool             ok    = true;
    
        StrAppendFmt(&s, "AAAAAAAA");
        size before = StrLen(&s);
        ok          = ok && StrPatchFmt(&s, 2, "{}", LVAL(1234));
    
        Str s = StrInitFromZstr("hello", &alloc);
        StrAppendFmt(&out, "{}", s); // width defaults to 0 -> no padding
        ok = ok && (ZstrCompare(StrBegin(&out), "hello") == 0);
    
        f64 v = 3.14;
        StrAppendFmt(&out, "{}", v); // default precision 6
        ok = ok && (ZstrCompare(StrBegin(&out), "3.140000") == 0);
        StrClear(&out);
        StrClear(&out);
    
        StrAppendFmt(&out, "{.2}", v); // explicit precision 2
        ok = ok && (ZstrCompare(StrBegin(&out), "3.14") == 0);
        bool             ok    = true;
    
        StrAppendFmt(&out, "{05}", (u32)7);
        ok = ok && (ZstrCompare(StrBegin(&out), "00007") == 0);
        bool             ok    = true;
    
        StrAppendFmt(&out, "{007}", (u32)3);
        ok = ok && (ZstrCompare(StrBegin(&out), "0000003") == 0);
        bool             ok    = true;
    
        StrAppendFmt(&out, "{09}", (u32)3);
        ok = ok && (ZstrCompare(StrBegin(&out), "000000003") == 0);
        bool             ok    = true;
    
        StrAppendFmt(&out, "{9}", (u32)3);
        ok = ok && (ZstrCompare(StrBegin(&out), "        3") == 0);
        ok = ok && (StrLen(&out) == 9);
    
        Zstr s  = "Hello";
        bool rc = StrAppendFmt(&out, "{.9s}", s);
        ok      = ok && rc;
        ok      = ok && (ZstrCompare(StrBegin(&out), "Hello") == 0);
        Str              out   = StrInit(&alloc);
        u32              v     = 7;
        StrAppendFmt(&out, "{05}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "00007") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        Zstr             s     = "ab";
        StrAppendFmt(&out, "{4}", s);
        bool ok = (ZstrCompare(StrBegin(&out), "  ab") == 0);
        StrClear(&out);
        StrClear(&out);
        // Two-digit width exercises the width loop (151) more than once.
        StrAppendFmt(&out, "{12}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "          ab") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        f64              v     = 3.14159;
        StrAppendFmt(&out, "{.2}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "3.14") == 0);
        StrClear(&out);
        StrClear(&out);
        // multi-digit precision exercises the precision loop body (165).
        StrAppendFmt(&out, "{.4}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "3.1416") == 0);
        StrDeinit(&out);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
        StrAppendFmt(&out, "literal");
        i32  v  = 5;
        bool rc = StrAppendFmt(&out, "{.}", v);
        StrAppendFmt(&out, "literal");
        i32  v  = 5;
        bool rc = StrAppendFmt(&out, "{.}", v);
        // Real: false; output keeps only the prior "literal" (append stopped).
        bool ok = (rc == false) && (ZstrCompare(StrBegin(&out), "literal") == 0);
        Str              out   = StrInit(&alloc);
        i32              v     = -7;
        StrAppendFmt(&out, "{05}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "-0007") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        u32              v     = 123;
        StrAppendFmt(&out, "{03}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "123") == 0);
        StrClear(&out);
        bool ok = (ZstrCompare(StrBegin(&out), "123") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{05}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "00123") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        Zstr             s     = "abc";
        StrAppendFmt(&out, "{3}", s);
        bool ok = (ZstrCompare(StrBegin(&out), "abc") == 0);
        StrClear(&out);
        bool ok = (ZstrCompare(StrBegin(&out), "abc") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{5}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "  abc") == 0);
        StrDeinit(&out);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
        StrAppendFmt(&out, "a{​{b");
        bool ok = (ZstrCompare(StrBegin(&out), "a{b") == 0);
        StrDeinit(&out);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
        StrAppendFmt(&out, "a}}b");
        bool ok = (ZstrCompare(StrBegin(&out), "a}b") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        u32              v     = 0xAB;
        StrAppendFmt(&out, "{x}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "0xab") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        u16              v     = 0x1234; // 4660
        bool             rc    = StrAppendFmt(&out, "{<2r}", v);
        bool             ok    = rc && (ZstrCompare(StrBegin(&out), "4660") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        u64              v     = 0x0102030405060708ULL; // 72623859790382856
        bool             rc    = StrAppendFmt(&out, "{>8r}", v);
        bool             ok    = rc && (ZstrCompare(StrBegin(&out), "72623859790382856") == 0);
        StrDeinit(&out);
        StrPushBackR(&s, (char)0x01);
        StrPushBackR(&s, (char)0x4F); // multi-digit so StrLen(&hex)!=1 path
        StrAppendFmt(&out, "{x}", s);
        bool ok = (ZstrCompare(StrBegin(&out), "0x01 0x4f") == 0);
        StrDeinit(&s);
        Str s = StrInit(&dbg);
        StrPushBackR(&s, (char)0x05);
        StrAppendFmt(&out, "{x}", s);
        bool ok = (ZstrCompare(StrBegin(&out), "0x05") == 0);
        StrDeinit(&s);
        DBG_BEGIN(dbg, out);
        Zstr s = "\x01\x4f";
        StrAppendFmt(&out, "{x}", s);
        bool ok = (ZstrCompare(StrBegin(&out), "0x01 0x4f") == 0);
        return ok && dbg_no_leak(&dbg, &out);
        DBG_BEGIN(dbg, out);
        Zstr s = "\x05";
        StrAppendFmt(&out, "{x}", s);
        bool ok = (ZstrCompare(StrBegin(&out), "0x05") == 0);
        return ok && dbg_no_leak(&dbg, &out);
        Str              out   = StrInit(&alloc);
        Str              s     = StrInitFromZstr("hi", &alloc);
        StrAppendFmt(&out, "{}", s); // width 0 default
        bool ok = (ZstrCompare(StrBegin(&out), "hi") == 0);
        StrClear(&out);
        bool ok = (ZstrCompare(StrBegin(&out), "hi") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{5}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "   hi") == 0);
        StrDeinit(&s);
        Str              out   = StrInit(&alloc);
        Zstr             s     = "hi";
        StrAppendFmt(&out, "{}", s);
        bool ok = (ZstrCompare(StrBegin(&out), "hi") == 0);
        StrClear(&out);
        bool ok = (ZstrCompare(StrBegin(&out), "hi") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{5}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "   hi") == 0);
        StrDeinit(&out);
        DBG_BEGIN(dbg, out);
        u64 v = 1234567890ULL;
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1234567890") == 0);
        return ok && dbg_no_leak(&dbg, &out);
        DBG_BEGIN(dbg, out);
        i64 v = -1234567890LL;
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "-1234567890") == 0);
        return ok && dbg_no_leak(&dbg, &out);
        Str              out   = StrInit(&alloc);
        u64              v     = 42;
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "42") == 0);
        StrClear(&out);
        bool ok = (ZstrCompare(StrBegin(&out), "42") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "    42") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        i64              v     = -42;
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "-42") == 0);
        StrClear(&out);
        bool ok = (ZstrCompare(StrBegin(&out), "-42") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "   -42") == 0);
        StrDeinit(&out);
                   ((u64)'F' << 16) | ((u64)'G' << 8) | (u64)'H';
        MemCopy(&v, &bits, sizeof(v));
        StrAppendFmt(&out, "{c}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "ABCDEFGH") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        f64              ninf  = -F64_INFINITY;
        StrAppendFmt(&out, "{}", ninf);
        bool ok = (ZstrCompare(StrBegin(&out), "-inf") == 0);
        StrClear(&out);
        StrClear(&out);
        f64 pinf = F64_INFINITY;
        StrAppendFmt(&out, "{}", pinf);
        ok = ok && (ZstrCompare(StrBegin(&out), "inf") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        f64              v     = 1.5;
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1.500000") == 0);
        StrClear(&out);
        StrClear(&out);
        // explicit precision 2 to contrast.
        StrAppendFmt(&out, "{.2}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "1.50") == 0);
        StrDeinit(&out);
        DBG_BEGIN(dbg, out);
        f64 v = 2.71828;
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "2.718280") == 0);
        return ok && dbg_no_leak(&dbg, &out);
        Str              out   = StrInit(&alloc);
        f64              v     = 1.5;
        StrAppendFmt(&out, "{.1}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1.5") == 0);
        StrClear(&out);
        bool ok = (ZstrCompare(StrBegin(&out), "1.5") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6.1}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "   1.5") == 0);
        StrDeinit(&out);
        Str              out   = StrInit(&alloc);
        Float            v     = FloatFromStr("12345.67", ab);
        StrAppendFmt(&out, "{e}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1.234567e+04") == 0);
        StrClear(&out);
        StrClear(&out);
        Float w = FloatFromStr("0.001234", ab);
        StrAppendFmt(&out, "{e}", w);
        ok = ok && (ZstrCompare(StrBegin(&out), "1.234e-03") == 0);
        FloatDeinit(&v);
        Str              out   = StrInit(&alloc);
        Float            v     = FloatFromStr("1.0", ab);
        StrAppendFmt(&out, "{e}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1e+00") == 0);
        FloatDeinit(&v);
        Str              out   = StrInit(&alloc);
        Float            v     = FloatFromStr("2.0", ab);
        StrAppendFmt(&out, "{e}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "2e+00") == 0);
        FloatDeinit(&v);
        DBG_BEGIN(dbg, out);
        Float v = FloatFromStr("1234567890.012345", &dbg.base);
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1234567890.012345") == 0);
        FloatDeinit(&v);
        Str              out   = StrInit(&alloc);
        Float            v     = FloatFromStr("3.14159", ab);
        StrAppendFmt(&out, "{.2}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "3.14") == 0);
        StrClear(&out);
        StrClear(&out);
        // precision longer than frac pads with zeros.
        StrAppendFmt(&out, "{.8}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "3.14159000") == 0);
        FloatDeinit(&v);
        Str              out   = StrInit(&alloc);
        Float            v     = FloatFromStr("5", ab);
        StrAppendFmt(&out, "{.3}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "5.000") == 0);
        FloatDeinit(&v);
        DBG_BEGIN(dbg, out);
        Float v = FloatFromStr("12345.67", &dbg.base);
        StrAppendFmt(&out, "{e}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1.234567e+04") == 0);
        FloatDeinit(&v);
        Str              out   = StrInit(&alloc);
        Float            v     = FloatFromStr("1.5", ab);
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1.5") == 0);
        StrClear(&out);
        bool ok = (ZstrCompare(StrBegin(&out), "1.5") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "   1.5") == 0);
        FloatDeinit(&v);
        Str              out   = StrInit(&alloc);
        Float            v     = FloatFromStr("1.5", ab);
        StrAppendFmt(&out, "AB");
        StrAppendFmt(&out, "{>6}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "   AB1.5") == 0);
        Float            v     = FloatFromStr("1.5", ab);
        StrAppendFmt(&out, "AB");
        StrAppendFmt(&out, "{>6}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "   AB1.5") == 0);
        FloatDeinit(&v);
        DBG_BEGIN(dbg, out);
        BitVec bv = BitVecFromStr("10110", &dbg.base);
        StrAppendFmt(&out, "{}", bv);
        bool ok = (ZstrCompare(StrBegin(&out), "10110") == 0);
        BitVecDeinit(&bv);
        Str              out   = StrInit(&alloc);
        BitVec           bv    = BitVecFromStr("101", ab);
        StrAppendFmt(&out, "{}", bv);
        bool ok = (ZstrCompare(StrBegin(&out), "101") == 0);
        StrClear(&out);
        bool ok = (ZstrCompare(StrBegin(&out), "101") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6}", bv);
        ok = ok && (ZstrCompare(StrBegin(&out), "   101") == 0);
        BitVecDeinit(&bv);
        DBG_BEGIN(dbg, out);
        Int v = IntFromStr("123456789012345678901234567890", &dbg.base);
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "123456789012345678901234567890") == 0);
        IntDeinit(&v);
        Str              out   = StrInit(&alloc);
        Int              v     = IntFrom(42, ab);
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "42") == 0);
        StrClear(&out);
        bool ok = (ZstrCompare(StrBegin(&out), "42") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "    42") == 0);
        IntDeinit(&v);
        DebugAllocator dbg = DebugAllocatorInitWith(LEAK_CFG);
        Str            out = StrInit(ALLOCATOR_OF(&dbg));
        bool           ok  = StrAppendFmt(&out, "{x}", s) && (StrLen(&out) > 0);
        StrDeinit(&out);
        ok = ok && (DebugAllocatorLiveCount(&dbg) == 0) && (DebugAllocatorLiveBytes(&dbg) == 0);
    bool test_leak_write_f64_default_freed(void) {
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", 1e300) && (StrLen(&out) > 0);
        LEAK_WRITE_EPILOGUE();
    }
        Float         f  = FloatFromStr("12345678901234567890123456789012345678901234567890.5", ALLOCATOR_OF(&fa));
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", f) && (StrLen(&out) > 0);
        StrDeinit(&out);
        ok = ok && (DebugAllocatorLiveCount(&dbg) == 0) && (DebugAllocatorLiveBytes(&dbg) == 0);
    
        Str  out = StrInit(adbg);
        bool ok  = StrAppendFmt(&out, "{e}", v);
        ok       = ok && (StrLen(&out) > 0);
    
        Str  out = StrInit(adbg);
        bool ok  = StrAppendFmt(&out, "{.3e}", v);
        ok       = ok && (StrLen(&out) > 0);
    
        Str  out = StrInit(adbg);
        bool ok  = StrAppendFmt(&out, "{.2}", v);
        ok       = ok && (StrLen(&out) > 0);
    
        Str  out = StrInit(adbg);
        bool ok  = StrAppendFmt(&out, "{.2}", v);
        ok       = ok && (StrLen(&out) > 0);
        Zstr s   = "AB"; // multi-byte Zstr -> hex loop runs per byte
        Str  out = StrInit(adbg);
        bool ok  = StrAppendFmt(&out, "{x}", s);
        ok       = ok && (StrLen(&out) > 0);
        i32  v   = -123456;
        Str  out = StrInit(adbg);
        bool ok  = StrAppendFmt(&out, "{}", v);
        ok       = ok && (StrLen(&out) > 0);
    bool test_leak_write_u64_temp_freed(void) {
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", (u64)12345678901234ull) && (StrLen(&out) > 0);
        LEAK_WRITE_EPILOGUE();
    }
    bool test_leak_write_f64_temp_freed(void) {
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", (f64)3.14159) && (StrLen(&out) > 0);
        LEAK_WRITE_EPILOGUE();
    }
        HeapAllocator va = HeapAllocatorInit();
        Str           s  = StrInit(ALLOCATOR_OF(&va));
        StrAppendFmt(&s, "payload");
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", s) && (StrLen(&out) > 0);
        StrAppendFmt(&s, "payload");
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", s) && (StrLen(&out) > 0);
        StrDeinit(&out);
        ok = ok && (DebugAllocatorLiveCount(&dbg) == 0) && (DebugAllocatorLiveBytes(&dbg) == 0);
        Int           v  = IntFromStr("123456789012345678901234567890", ALLOCATOR_OF(&va));
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", v) && (StrLen(&out) > 0);
        StrDeinit(&out);
        ok = ok && (DebugAllocatorLiveCount(&dbg) == 0) && (DebugAllocatorLiveBytes(&dbg) == 0);
        Float         v  = FloatFromStr("2.718281828", ALLOCATOR_OF(&va));
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", v) && (StrLen(&out) > 0);
        StrDeinit(&out);
        ok = ok && (DebugAllocatorLiveCount(&dbg) == 0) && (DebugAllocatorLiveBytes(&dbg) == 0);
        BitVecResize(&v, 12);
        LEAK_WRITE_PRELUDE();
        ok = ok && StrAppendFmt(&out, "{}", v) && (StrLen(&out) > 0);
        StrDeinit(&out);
        ok = ok && (DebugAllocatorLiveCount(&dbg) == 0) && (DebugAllocatorLiveBytes(&dbg) == 0);
        Str              out   = StrInit(&alloc);
    
        bool ok = StrAppendFmt(&out, "}}");
        ok      = ok && (ZstrCompare(StrBegin(&out), "}") == 0);
        u16 v = (u16)0x1AE0; // big-endian bytes: 0x1A, 0xE0
    
        StrAppendFmt(&output, "{c}", v);
    
        bool ok = (ZstrCompare(StrBegin(&output), "\\x1a\\xe0") == 0);
    
        Zstr b = "\x12";
        StrAppendFmt(&out, "{c}", b);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x12") == 0);
    
        Zstr b = "\x90";
        StrAppendFmt(&out, "{c}", b);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x90") == 0);
    
        Zstr b = "\xab";
        StrAppendFmt(&out, "{c}", b);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\xab") == 0);
    
        Zstr b = "\xcd";
        StrAppendFmt(&out, "{c}", b);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\xcd") == 0);
    
        Zstr b = "\xab";
        StrAppendFmt(&out, "{A}", b);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\xAB") == 0);
    
        Zstr b = "\xcd";
        StrAppendFmt(&out, "{A}", b);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\xCD") == 0);
    
        Zstr lo_letter = "\x1b";
        StrAppendFmt(&out, "{c}", lo_letter);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x1b") == 0);
        StrClear(&out);
    
        Zstr hi_letter = "\xb1";
        StrAppendFmt(&out, "{c}", hi_letter);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\xb1") == 0);
    // Test StrAppendFmt function
    bool test_str_WriteFmt(void) {
        WriteFmt("Testing StrAppendFmt\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s = StrInit(&alloc);
        StrAppendFmt(&s, "Hello, {}!", &"World"[0]);
    
        // Validate the string
    // StrAppendf formatter.
    bool test_str_write_fmt_append(void) {
        WriteFmt("Testing StrAppendFmt append\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Append formatted suffix.
        StrAppendFmt(&s, " {} {}", (Zstr) "World", (u32)2023);
    
        // Check that the string was appended correctly
        Str result = StrInit(&alloc);
        StrForeachIdx(&s, chr, idx) {
            StrAppendFmt(&result, "{c}{}", chr, idx);
        }
        StrForeachReverseIdx(&s, chr, idx) {
            // Append the character and its index to the result string
            StrAppendFmt(&result, "{c}{}", chr, idx);
        }
        StrForeachPtrIdx(&s, chrptr, idx) {
            // Append the character (via pointer) and its index to the result string
            StrAppendFmt(&result, "{c}{}", *chrptr, idx);
    
            // Modify the original string by converting to uppercase
        StrForeachReversePtrIdx(&s, chrptr, idx) {
            // Append the character (via pointer) and its index to the result string
            StrAppendFmt(&result, "{c}{}", *chrptr, idx);
    
            // Modify the original string by converting to uppercase
        StrForeachInRangeIdx(&s, chr, idx, 6, 11) {
            // Append the character and its index to the result string
            StrAppendFmt(&result, "{c}{}", chr, idx);
        }
        StrForeachPtrInRangeIdx(&s, chrptr, idx, 6, 11) {
            // Append the character and its index to the result string
            StrAppendFmt(&result, "{c}{}", *chrptr, idx);
    
            // Modify the original string by converting to uppercase
    
        Str spec = StrInit(a);
        StrAppendFmt(&spec, "{}", (Zstr) "1.2.3.4:4660");
    
        SocketAddr addr;
        HttpResponse response = HttpResponseInit(alloc_base);
        Str          body     = StrInit(alloc_base);
        StrAppendFmt(&body, "<h1>hi</h1>");
        HttpRespondWithHtml(&response, HTTP_RESPONSE_CODE_OK, &body);
        StrDeinit(&body);
    
        Str raw = StrInit(alloc_base);
        StrAppendFmt(&raw, "GET / HTTP/1.1\r\n");
        // 101 headers -- one past the cap of 100.
        for (u64 i = 0; i < 101; i++) {
        // 101 headers -- one past the cap of 100.
        for (u64 i = 0; i < 101; i++) {
            StrAppendFmt(&raw, "X-H{}: v\r\n", i);
        }
        StrAppendFmt(&raw, "\r\n");
            StrAppendFmt(&raw, "X-H{}: v\r\n", i);
        }
        StrAppendFmt(&raw, "\r\n");
    
        HttpRequest req  = HttpRequestInit(alloc_base);
    
        Str raw = StrInit(alloc_base);
        StrAppendFmt(&raw, "GET / HTTP/1.1\r\n");
        for (u64 i = 0; i < 100; i++) {
            StrAppendFmt(&raw, "X-H{}: v\r\n", i);
        StrAppendFmt(&raw, "GET / HTTP/1.1\r\n");
        for (u64 i = 0; i < 100; i++) {
            StrAppendFmt(&raw, "X-H{}: v\r\n", i);
        }
        StrAppendFmt(&raw, "\r\n");
            StrAppendFmt(&raw, "X-H{}: v\r\n", i);
        }
        StrAppendFmt(&raw, "\r\n");
    
        HttpRequest req  = HttpRequestInit(alloc_base);
    
        HttpHeader hh = HttpHeaderInit(adbg);
        StrAppendFmt(&hh.key, "X-A-Reasonably-Long-Header-Key-Name");
        StrAppendFmt(&hh.value, "a-reasonably-long-header-value-string");
        HttpHeader hh = HttpHeaderInit(adbg);
        StrAppendFmt(&hh.key, "X-A-Reasonably-Long-Header-Key-Name");
        StrAppendFmt(&hh.value, "a-reasonably-long-header-value-string");
    
        bool ok = VecPushBackR(&headers, hh);
    
        Str first = StrInit(adbg);
        StrAppendFmt(&first, "<h1>first-body-long-enough-to-heap-allocate</h1>");
    
        HttpResponse response = HttpResponseInit(adbg);
    
        Str second = StrInit(adbg);
        StrAppendFmt(&second, "<h1>second-body-also-long-enough-to-heap</h1>");
        // Second call must free the first body before installing the second.
        HttpRespondWithHtml(&response, HTTP_RESPONSE_CODE_OK, &second);
        // Pre-fill body with a heap-backed string.
        Str first = StrInit(adbg);
        StrAppendFmt(&first, "<h1>old-body-long-enough-to-force-heap-here</h1>");
        HttpResponse response = HttpResponseInit(adbg);
        HttpRespondWithHtml(&response, HTTP_RESPONSE_CODE_OK, &first);
                Str source_key = StrInit(&alloc);
                u64 source_id  = VecLen(&response.data) > 0 ? VecAt(&response.data, 0).source_function_id : 0;
                StrAppendFmt(&source_key, "{}", source_id);
    
                JW_OBJ_KV(json, StrBegin(&source_key), {
                        AnnSymbol *s          = &VecAt(&response.data, 0);
                        Str        target_key = StrInit(&alloc);
                        StrAppendFmt(&target_key, "{}", s->target_function_id);
    
                        JW_OBJ_KV(json, StrBegin(&target_key), {
                VecForeach(&symbols, symbol) {
                    Str source_key = StrInit(&alloc);
                    StrAppendFmt(&source_key, "{}", symbol.source_function_id);
    
                    JW_OBJ_KV(json, StrBegin(&source_key), {
                    JW_OBJ_KV(json, StrBegin(&source_key), {
                Str target_key = StrInit(&alloc);
                StrAppendFmt(&target_key, "{}", symbol.target_function_id);
    
                JW_OBJ_KV(json, StrBegin(&target_key), {
Last updated on