Skip to content

StrInit

Description

Initialize a Str. Inside a Scope block the allocator argument may be omitted; the internal MisraScope allocator is used. Otherwise pass a typed allocator handle or a raw Allocator *.

Usage example (Cross-references)

Usage examples (Cross-references)
        ValidateStr(err_str);
        Allocator *alloc = StrAllocator(err_str);
        Str        out   = StrInit(alloc);
        StrAppendFmt(&out, "{} (errno {})", errno_description(eno), eno);
        StrDeinit(err_str);
    
        HeapAllocator h    = HeapAllocatorInit();
        Str           full = StrInit(&h);
        StrAppendFmt(&full, "[{}] [{}:{}] {}\n", (Zstr)NAMES[type], (Zstr)tag, line, (Zstr)msg);
            StackFrame frames[32];
            size       n     = CaptureStackTrace(frames, 32, 1);
            Str        trace = StrInit(&h);
            // FormatStackTrace takes `Allocator *` -- legitimate erasure
            // boundary; pass at the call site, no intermediate variable.
        // sense. Library-internal scratch keeps the debug-build instrumentation.
        DefaultAllocator scratch = DefaultAllocatorInit();
        Str              tmp     = StrInit(&scratch);
        bool             ok      = str_append_fmt(&tmp, fmt, args, argc);
        if (ok) {
        }
    
        out = StrInit(&scratch);
        ok  = str_append_fmt(&out, fmtstr, argv, argc);
        // DefaultAllocator: rendered size is caller-controlled (open-ended).
        DefaultAllocator scratch = DefaultAllocatorInit();
        Str              tmp     = StrInit(&scratch);
        bool             ok      = render_binary_fmt(&tmp, fmtstr, argv, argc);
        if (ok) {
        }
    
        Str buffer = StrInit(&scratch);
        i32 fd     = FileFd(file);
        }
    
        *out = StrInit(alloc);
    
        if (!float_try_to_str(&canonical, value, alloc)) {
            u64  frac   = 0;
    
            result = StrInit(alloc);
    
            if (StrBegin(&canonical)[0] == '-') {
        }
    
        *out = StrInit(alloc);
    
        if (!int_try_to_str(&digits, &value->significand, alloc)) {
        }
    
        result = StrInit(alloc);
    
        if (FloatIsZero(value)) {
                        }
                    }
                    Str hex = StrInit(StrAllocator(o));
                    if (!StrFromU64(&hex, c, &config)) {
                        StrDeinit(&hex);
                        }
                    }
                    Str          hex    = StrInit(StrAllocator(o));
                    StrIntFormat config = {.base = 16, .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0};
                    if (!StrFromU64(&hex, (u8)xs[i], &config)) {
        // base-prefix / sign / padding logic from having to reach into
        // `o`'s prefix bytes after the fact.
        Str temp = StrInit(StrAllocator(o));
    
        u8 base = 10;
        // base-prefix / sign / padding logic from having to reach into
        // `o`'s prefix bytes after the fact.
        Str temp = StrInit(StrAllocator(o));
    
        u8 base = 10;
            // as the integer renderers: padding/sign accounting wants a
            // self-contained slice it can measure.
            Str temp = StrInit(StrAllocator(o));
    
            u8             precision = fmt_info->flags & FMT_FLAG_HAS_PRECISION ? fmt_info->precision : 6;
        allocator_ptr = arg->allocator;
        previous      = *out;
        temp          = StrInit(allocator_ptr);
    
        default_fmt        = fmt_info ? *fmt_info :
        }
    
        temp   = StrInit(FloatAllocator(value));
        parsed = FloatInit(FloatAllocator(value));
    
        // Usage line: "usage: name [OPTIONS] --req <REQ>... <POS>..."
        Str usage = StrInit(self->alloc);
        StrPushBackMany(&usage, "usage: ");
        StrPushBackMany(&usage, self->name);
            n_specs = 64;
        for (u64 i = 0; i < n_specs; ++i) {
            left_col[i] = StrInit(self->alloc);
            left_w[i]   = spec_format_left(VecPtrAt(&self->specs, i), &left_col[i]);
            if (left_w[i] > max_w)
            return;
        }
        Str rendered = StrInit(meta);
    #if !defined(LOG_NO_BACKTRACE) || !LOG_NO_BACKTRACE
        FormatStackTrace(&rendered, frames, count, meta);
        }
    
        *out = StrInit(alloc);
        if (bv->length == 0) {
            return true;
            if (!StrPushBackR(out, bit_char)) {
                StrDeinit(out);
                *out = StrInit(alloc);
                return false;
            }
    
        if (!bitvec_try_to_str(&result, bv, alloc)) {
            result = StrInit(alloc);
        }
        ValidateFloat(out);
        result = FloatInit(FloatAllocator(out));
        digits = StrInit(FloatAllocator(out));
    
        if (pos < length && (text[pos] == '+' || text[pos] == '-')) {
        }
    
        *out = StrInit(alloc);
    
        if (FloatIsZero(value)) {
        }
    
        result = StrInit(alloc);
    
        if (value->negative) {
    
        if (!float_try_to_str(&result, value, alloc)) {
            result = StrInit(alloc);
        }
        }
    
        *out = StrInit(alloc);
        if (len == 0) {
            return true;
    
    Str str_init_from_cstr(Zstr cstr, size len, Allocator *alloc) {
        Str result = StrInit(alloc);
    
        // Try-form leaves `result` empty-but-valid on OOM, so the caller
    
        MemSet(dst, 0, sizeof(Str));
        *dst             = StrInit(clone_allocator);
        dst->copy_init   = src->copy_init;
        dst->copy_deinit = src->copy_deinit;
        // Borrowed substring view: data points into the caller's bytes and
        // capacity stays 0 so StrToU64 never tries to grow it.
        Str temp_str      = StrInit(str->allocator);
        temp_str.data     = str->data + pos;
        temp_str.length   = str->length - pos;
    
        if (!int_try_to_str(&result, value, alloc)) {
            result = StrInit(alloc);
        }
        }
    
        *out = StrInit(alloc);
    
        if (!int_validate_radix(radix)) {
        }
    
        result = StrInit(alloc);
    
        while (!IntIsZero(&current)) {
    
        if (!int_try_to_str_radix(&result, value, radix, uppercase, alloc)) {
            result = StrInit(alloc);
        }
    
        // (1) exact CodeView path
        *out_path = StrInit(StrAllocator(out_path));
        StrPushBackMany(out_path, cv->pdb_path);
        if (sys_path_exists(StrBegin(out_path)))
            return true;
    
        Str pdb_path = StrInit(alloc);
        if (!find_pdb(&entry->pe, StrBegin(&entry->module_path), &pdb_path)) {
            StrDeinit(&pdb_path);
    // Returns true on success; `out` is populated with an opened Elf.
    static bool try_open_sidecar(Zstr main_path, const Elf *main, Elf *out, Allocator *alloc) {
        Str path = StrInit(alloc);
    
        // (1) Build-ID
    
    Str socket_addr_format(const SocketAddr *addr, Allocator *alloc) {
        Str out = StrInit(alloc);
        if (!addr || addr->length == 0) {
            return out;
        PROCESS_INFORMATION pi = {0};
    
        Str cmdline = StrInit(alloc);
        StrPushBackMany(&cmdline, filepath);
        for (char **arg = argv + 1; *arg; ++arg) {
        }
        MemSet(out, 0, sizeof(*out));
        out->raw     = StrInit(alloc);
        out->entries = VecInitT(out->entries, alloc);
        }
    
        Str path = StrInit(alloc);
        if (!compose_dsym_path(StrBegin(&e->module_path), &path)) {
            StrDeinit(&path);
    
    static void parse_hosts_table(HostsTable *table, Allocator *alloc) {
        Str buf = StrInit(alloc);
        if (!slurp_file(HOSTS_FILE_PATH, &buf)) {
            StrDeinit(&buf);
    
    static void parse_resolv_conf(DnsAddrs *out, Allocator *alloc) {
        Str buf = StrInit(alloc);
        if (!slurp_file(RESOLV_CONF_FILE_PATH, &buf)) {
            StrDeinit(&buf);
        const HttpHeader *src = (const HttpHeader *)src_ptr;
    
        dst->key   = StrInit((Allocator *)alloc);
        dst->value = StrInit((Allocator *)alloc);
    
        dst->key   = StrInit((Allocator *)alloc);
        dst->value = StrInit((Allocator *)alloc);
    
        if (!StrInitCopy(&dst->key, &src->key)) {
        Allocator *alloc   = req->allocator;
        Zstr       cursor  = in;
        Str        method  = StrInit(alloc);
        Str        version = StrInit(alloc);
        Zstr       cursor  = in;
        Str        method  = StrInit(alloc);
        Str        version = StrInit(alloc);
    
        StrReadFmt(cursor, "{} {} {}\r\n", method, req->url, version);
        response->content_type = content_type;
        StrDeinit(&response->body);
        response->body = StrInit(response->allocator);
        if (FileReadAndClose(filepath, &response->body) < 0) {
            LOG_ERROR("failed to read file: {}", filepath);
    
    Str http_response_serialize(const HttpResponse *response, Allocator *alloc) {
        Str out = StrInit(alloc);
    
        if (!response) {
                LOG_ERROR("HttpResponseSerialize: failed to append body");
                StrDeinit(&out);
                return StrInit(alloc);
            }
        }
        char c;
        while (StrIterPeek(&si, &c)) {
            Str     key   = StrInit(MapAllocator(cfg));
            Str     value = StrInit(MapAllocator(cfg));
            StrIter read_si;
        while (StrIterPeek(&si, &c)) {
            Str     key   = StrInit(MapAllocator(cfg));
            Str     value = StrInit(MapAllocator(cfg));
            StrIter read_si;
    
        if (!value) {
            return StrInit(MapAllocator(cfg));
        }
    
        if (!value) {
            return StrInit(MapAllocator(cfg));
        }
            }
    
            Str key = StrInit(&scratch);
    
            // key start
        // stack-backed buffer is unsafe -- DefaultAllocator is the right fit.
        DefaultAllocator scratch = DefaultAllocatorInit();
        Str              ns      = StrInit(&scratch);
    
        bool is_neg = false;
            // valid input. DefaultAllocator is the right fit here.
            DefaultAllocator scratch = DefaultAllocatorInit();
            Str              s       = StrInit(&scratch);
            si                       = JReadString(si, &s);
            StrDeinit(&s);
        out->allocator   = alloc;
        out->entries     = VecInitT(out->entries, alloc);
        out->string_pool = StrInit(alloc);
    
        const ElfSection *line_section = ElfFindSection(elf, ".debug_line");
        out->allocator   = alloc;
        out->entries     = VecInitT(out->entries, alloc);
        out->string_pool = StrInit(alloc);
    
        if (!info_bytes || info_size == 0)
        out->data      = taken;
        out->functions = VecInitT(out->functions, BufAllocator(&taken));
        out->name_pool = StrInit(BufAllocator(&taken));
    
        u32 num_dir_bytes  = 0;
    // Decode one resource record. Advances the iter past the record.
    static bool decode_record(BufIter *it, DnsRecord *rec, Allocator *alloc) {
        rec->name   = StrInit(alloc);
        rec->target = StrInit(alloc);
        rec->rdata  = VecInitT(rec->rdata, alloc);
    static bool decode_record(BufIter *it, DnsRecord *rec, Allocator *alloc) {
        rec->name   = StrInit(alloc);
        rec->target = StrInit(alloc);
        rec->rdata  = VecInitT(rec->rdata, alloc);
        MemSet(rec->ipv4, 0, sizeof(rec->ipv4));
        // Skip echoed question section: each question is qname + qtype(2) + qclass(2).
        for (u16 i = 0; i < qd; ++i) {
            Str dummy = StrInit(alloc);
            if (!decode_name(&it, &dummy)) {
                StrDeinit(&dummy);
    static void log_request_summary(Zstr client_addr, Zstr prefix_bytes, size prefix_len) {
        Scope(scope, DefaultAllocator) {
            Str raw = StrInit(scope);
            StrPushBackMany(&raw, prefix_bytes);
        // treats as a fatal arg violation. An empty source clones to an
        // empty fresh Str.
        Str copy = (StrLen(src) == 0) ? StrInit((Allocator *)alloc) :
                                        StrInitFromCstr(StrBegin(src), StrLen(src), (Allocator *)alloc);
        *(Str *)dst_ptr = copy;
    
        // Create Str with capacity
        Str str = StrInit(alloc);
    
        // Fill with data or generate simple pattern if not enough input
                    si = JSkipWhitespace(si);                                                                              \
                }                                                                                                          \
                Str key       = StrInit(&alloc);                                                                           \
                UNPL(read_si) = JReadString(si, &key);                                                                     \
                if (StrIterIndex(&UNPL(read_si)) == StrIterIndex(&si)) {                                                   \
    #define JR_STR(si, str)                                                                                                \
        do {                                                                                                               \
            Str UNPL(my_str) = StrInit(&alloc);                                                                            \
            si               = JReadString((si), &UNPL(my_str));                                                           \
            (str)            = UNPL(my_str);                                                                               \
        do {                                                                                                               \
            if (!StrCmp(&key, (k))) {                                                                                      \
                Str UNPL(my_str) = StrInit(&alloc);                                                                        \
                si               = JReadString((si), &UNPL(my_str));                                                       \
                (str)            = UNPL(my_str);                                                                           \
        JR_OBJ(si2, {
            JR_ARR_KV(si2, "data", {
                Str value = StrInit(&alloc);
                JR_STR(si2, value);
                VecPushBack(&data, value);
            Str name;
            Str description;
        } obj = {StrInit(&alloc), StrInit(&alloc)};
    
        JR_OBJ(si, {
            Str message;
            Str data;
        } obj = {StrInit(&alloc), StrInit(&alloc), StrInit(&alloc)};
    
        JR_OBJ(si, {
            Str newline;
            Str tab;
        } obj = {StrInit(&alloc), StrInit(&alloc), StrInit(&alloc), StrInit(&alloc)};
    
        JR_OBJ(si, {
            i32  value;
            bool flag;
        } obj = {StrInit(&alloc), 0, false};
    
        JR_OBJ(si, {
    bool compare_json_output(const Str *output, Zstr expected, DefaultAllocator *alloc) {
        Str expected_str   = StrInitFromZstr(expected, alloc);
        Str output_clean   = StrInit(alloc);
        Str expected_clean = StrInit(alloc);
        Str expected_str   = StrInitFromZstr(expected, alloc);
        Str output_clean   = StrInit(alloc);
        Str expected_clean = StrInit(alloc);
    
        // Remove whitespace from both strings for comparison
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        struct {
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        struct {
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        ApiResponse response = {
            JW_OBJ_KV(json, "data", {
                // Write dynamic key for source function ID
                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);
                    if (VecLen(&response.data) > 0) {
                        AnnSymbol *s          = &VecAt(&response.data, 0);
                        Str        target_key = StrInit(&alloc);
                        StrAppendFmt(&target_key, "{}", s->target_function_id);
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Vec(FunctionInfo) functions = VecInitWithDeepCopy(NULL, FunctionInfoDeinit, &alloc);
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        SearchResult result = {0};
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Vec(AnnSymbol) symbols = VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc);
            JW_OBJ_KV(json, "functions", {
                VecForeach(&symbols, symbol) {
                    Str source_key = StrInit(&alloc);
                    StrAppendFmt(&source_key, "{}", symbol.source_function_id);
    
                    JW_OBJ_KV(json, StrBegin(&source_key), {
                Str target_key = StrInit(&alloc);
                StrAppendFmt(&target_key, "{}", symbol.target_function_id);
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        // Create strings for the nested structure
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Vec(u32) numbers = VecInit(&alloc);
        // Create a copy of expected without spaces for comparison
        Str expected_str   = StrInitFromZstr(expected, alloc);
        Str output_clean   = StrInit(alloc);
        Str expected_clean = StrInit(alloc);
        Str expected_str   = StrInitFromZstr(expected, alloc);
        Str output_clean   = StrInit(alloc);
        Str expected_clean = StrInit(alloc);
    
        // Remove spaces and newlines from both strings for comparison
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Str name = StrInitFromZstr("Alice", &alloc);
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        u32 count = 42;
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        bool enabled = true;
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Person person = {1001, StrInitFromZstr("Bob", &alloc), 25, true, 50000.0};
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Config config = {false, 30, StrInitFromZstr("INFO", &alloc)};
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Vec(Str) languages = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        struct {
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        SimpleProduct product = {0};
        // Create a copy of expected without spaces for comparison
        Str expected_str   = StrInitFromZstr(expected, alloc);
        Str output_clean   = StrInit(alloc);
        Str expected_clean = StrInit(alloc);
        Str expected_str   = StrInitFromZstr(expected, alloc);
        Str output_clean   = StrInit(alloc);
        Str expected_clean = StrInit(alloc);
    
        // Remove spaces and newlines from both strings for comparison
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        // Write completely empty object
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Vec(i32) empty_numbers = VecInit(&alloc);
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Str empty_name = StrInit(&alloc);
        Str  json    = StrInit(&alloc);
    
        Str empty_name = StrInit(&alloc);
        Str empty_desc = StrInit(&alloc);
    
        Str empty_name = StrInit(&alloc);
        Str empty_desc = StrInit(&alloc);
    
        JW_OBJ(json, {
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        i32 temp    = -25;
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        i64 big_int     = 9223372036854775807LL;
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        i32  int_zero   = 0;
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        // Note: These are the actual characters, not escape sequences
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        // These contain actual special characters that should be escaped
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Vec(i32) empty_list = VecInit(&alloc);
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        Vec(i32) empty_arr  = VecInit(&alloc);
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        i64 max_int   = 2147483647LL;
    
        bool success = true;
        Str  json    = StrInit(&alloc);
    
        f64 tiny          = 0.000001;
    
        bool success = true;
        Str  json1   = StrInit(&alloc);
        Str  json2   = StrInit(&alloc);
        Str  json3   = StrInit(&alloc);
        bool success = true;
        Str  json1   = StrInit(&alloc);
        Str  json2   = StrInit(&alloc);
        Str  json3   = StrInit(&alloc);
        Str  json4   = StrInit(&alloc);
        Str  json1   = StrInit(&alloc);
        Str  json2   = StrInit(&alloc);
        Str  json3   = StrInit(&alloc);
        Str  json4   = StrInit(&alloc);
        Str  json2   = StrInit(&alloc);
        Str  json3   = StrInit(&alloc);
        Str  json4   = StrInit(&alloc);
    
        // Single integer
            bool active;
            f64  score;
        } data = {0, StrInit(&alloc), false, 0.0};
    
        JR_OBJ(si, {
            Str status;
        } data = {
            {0, {StrInit(&alloc), 0}},
            StrInit(&alloc)
        };
        } data = {
            {0, {StrInit(&alloc), 0}},
            StrInit(&alloc)
        };
            } company;
        } data = {
            {{{StrInit(&alloc), 0, 0.0}}, StrInit(&alloc)}
        };
                    // Properly initialize all Str fields
                    AnnSymbol sym             = {0};
                    sym.analysis_name         = StrInit(&alloc);
                    sym.function_name         = StrInit(&alloc);
                    sym.sha256                = StrInit(&alloc);
                    AnnSymbol sym             = {0};
                    sym.analysis_name         = StrInit(&alloc);
                    sym.function_name         = StrInit(&alloc);
                    sym.sha256                = StrInit(&alloc);
                    sym.function_mangled_name = StrInit(&alloc);
                    sym.analysis_name         = StrInit(&alloc);
                    sym.function_name         = StrInit(&alloc);
                    sym.sha256                = StrInit(&alloc);
                    sym.function_mangled_name = StrInit(&alloc);
                    sym.source_function_id    = source_function_id;
                    sym.function_name         = StrInit(&alloc);
                    sym.sha256                = StrInit(&alloc);
                    sym.function_mangled_name = StrInit(&alloc);
                    sym.source_function_id    = source_function_id;
                    sym.target_function_id    = target_function_id;
        StrIter si = StrIterFromStr(json);
    
        ApiResponse response = {false, StrInit(&alloc), VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)};
    
        JR_OBJ(si, {
                        // Properly initialize all Str fields
                        AnnSymbol sym             = {0};
                        sym.analysis_name         = StrInit(&alloc);
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        AnnSymbol sym             = {0};
                        sym.analysis_name         = StrInit(&alloc);
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        sym.function_mangled_name = StrInit(&alloc);
                        sym.analysis_name         = StrInit(&alloc);
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        sym.function_mangled_name = StrInit(&alloc);
                        sym.source_function_id    = source_function_id;
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        sym.function_mangled_name = StrInit(&alloc);
                        sym.source_function_id    = source_function_id;
                        sym.target_function_id    = (u64)ZstrToI64(StrBegin(&key), NULL);
    
        FunctionInfo info = {0};
        info.name         = StrInit(&alloc);
    
        JR_OBJ(si, {
    
        ModelInfo info = {0};
        info.name      = StrInit(&alloc);
    
        JR_OBJ(si, {
    
        SearchResult result = {0};
        result.binary_name  = StrInit(&alloc);
        result.sha256       = StrInit(&alloc);
        result.tags         = VecInitWithDeepCopyT(result.tags, NULL, StrDeinit, &alloc);
        SearchResult result = {0};
        result.binary_name  = StrInit(&alloc);
        result.sha256       = StrInit(&alloc);
        result.tags         = VecInitWithDeepCopyT(result.tags, NULL, StrDeinit, &alloc);
        result.created_at   = StrInit(&alloc);
        result.sha256       = StrInit(&alloc);
        result.tags         = VecInitWithDeepCopyT(result.tags, NULL, StrDeinit, &alloc);
        result.created_at   = StrInit(&alloc);
        result.model_name   = StrInit(&alloc);
        result.owned_by     = StrInit(&alloc);
        result.tags         = VecInitWithDeepCopyT(result.tags, NULL, StrDeinit, &alloc);
        result.created_at   = StrInit(&alloc);
        result.model_name   = StrInit(&alloc);
        result.owned_by     = StrInit(&alloc);
        result.created_at   = StrInit(&alloc);
        result.model_name   = StrInit(&alloc);
        result.owned_by     = StrInit(&alloc);
    
        JR_OBJ(si, {
        StrIter si = StrIterFromStr(json);
    
        ApiResponse response = {false, StrInit(&alloc), VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)};
    
        WriteFmt("[DEBUG] About to parse JSON...\n");
                        // Properly initialize all Str fields
                        AnnSymbol sym             = {0};
                        sym.analysis_name         = StrInit(&alloc);
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        AnnSymbol sym             = {0};
                        sym.analysis_name         = StrInit(&alloc);
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        sym.function_mangled_name = StrInit(&alloc);
                        sym.analysis_name         = StrInit(&alloc);
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        sym.function_mangled_name = StrInit(&alloc);
                        sym.source_function_id    = source_function_id;
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        sym.function_mangled_name = StrInit(&alloc);
                        sym.source_function_id    = source_function_id;
                        sym.target_function_id    = (u64)ZstrToI64(StrBegin(&key), NULL);
        StrIter si = StrIterFromStr(json);
    
        ApiResponse response = {false, StrInit(&alloc), VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)};
    
        WriteFmt("[DEBUG] About to parse JSON...\n");
                        // Properly initialize all Str fields
                        AnnSymbol sym             = {0};
                        sym.analysis_name         = StrInit(&alloc);
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        AnnSymbol sym             = {0};
                        sym.analysis_name         = StrInit(&alloc);
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        sym.function_mangled_name = StrInit(&alloc);
                        sym.analysis_name         = StrInit(&alloc);
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        sym.function_mangled_name = StrInit(&alloc);
                        sym.source_function_id    = source_function_id;
                        sym.function_name         = StrInit(&alloc);
                        sym.sha256                = StrInit(&alloc);
                        sym.function_mangled_name = StrInit(&alloc);
                        sym.source_function_id    = source_function_id;
                        sym.target_function_id    = (u64)ZstrToI64(StrBegin(&key), NULL);
        StrIter si      = StrIterFromStr(json);
    
        Str name = StrInit(&alloc);
        Str city = StrInit(&alloc);
    
        Str name = StrInit(&alloc);
        Str city = StrInit(&alloc);
    
        JR_OBJ(si, {
    
        Person person = {0};
        person.name   = StrInit(&alloc);
    
        JR_OBJ(si, {
    
        Config config    = {0};
        config.log_level = StrInit(&alloc);
    
        JR_OBJ(si, {
        JR_OBJ(si, {
            JR_ARR_KV(si, "languages", {
                Str lang = StrInit(&alloc);
                JR_STR(si, lang);
                VecPushBack(&languages, lang);
            bool active;
        } data = {
            {StrInit(&alloc), StrInit(&alloc)},
            false
        };
    
        SimpleProduct product = {0};
        product.name          = StrInit(&alloc);
        product.tags          = VecInitWithDeepCopyT(product.tags, NULL, StrDeinit, &alloc);
            JR_FLT_KV(si, "price", product.price);
            JR_ARR_KV(si, "tags", {
                Str tag = StrInit(&alloc);
                JR_STR(si, tag);
                VecPushBack(&product.tags, tag);
    
        // Write to JSON
        Str json = StrInit(&alloc);
        JW_OBJ(json, {
            JW_INT_KV(json, "count", original.count);
            bool enabled;
            Str  message;
        } parsed = {0, 0.0, false, StrInit(&alloc)};
    
        StrIter si = StrIterFromStr(json);
    
        // Write to JSON
        Str json = StrInit(&alloc);
        JW_OBJ(json, {
            JW_INT_KV(json, "big_int", original.big_int);
    
        // Write to JSON
        Str json = StrInit(&alloc);
        JW_OBJ(json, {
            JW_BOOL_KV(json, "flag1", original.flag1);
            Str with_special;
        } original = {
            StrInit(&alloc),
            StrInitFromZstr("hello", &alloc),
            StrInitFromZstr("hello world with spaces", &alloc),
    
        // Write to JSON
        Str json = StrInit(&alloc);
        JW_OBJ(json, {
            JW_STR_KV(json, "empty", original.empty);
            Str with_spaces;
            Str with_special;
        } parsed = {StrInit(&alloc), StrInit(&alloc), StrInit(&alloc), StrInit(&alloc)};
    
        StrIter si = StrIterFromStr(json);
    
        // Write to JSON
        Str json = StrInit(&alloc);
        JW_OBJ(json, {
            JW_ARR_KV(json, "numbers", original_numbers, num, { JW_INT(json, num); });
            });
            JR_ARR_KV(si, "strings", {
                Str str = StrInit(&alloc);
                JR_STR(si, str);
                VecPushBack(&parsed_strings, str);
    
        // Write to JSON
        Str json = StrInit(&alloc);
        JW_OBJ(json, {
            JW_OBJ_KV(json, "user", {
    
        // Read back from JSON
        TestPerson parsed_person = {0, StrInit(&alloc), 0, false, 0.0};
    
        StrIter si = StrIterFromStr(json);
    
        // Write to JSON
        Str json = StrInit(&alloc);
        JW_OBJ(json, {
            JW_OBJ_KV(json, "user", {
        // Read back from JSON
        ComplexData parsed       = {0};
        parsed.user              = (TestPerson) {0, StrInit(&alloc), 0, false, 0.0};
        parsed.config.debug_mode = true; // opposite of original
        parsed.config.timeout    = 0;
        parsed.config.debug_mode = true; // opposite of original
        parsed.config.timeout    = 0;
        parsed.config.log_level  = StrInit(&alloc);
        parsed.config.features   = VecInitWithDeepCopyT(parsed.config.features, NULL, StrDeinit, &alloc);
        parsed.numbers           = VecInitT(parsed.numbers, &alloc);
                JR_STR_KV(si, "log_level", parsed.config.log_level);
                JR_ARR_KV(si, "features", {
                    Str feature = StrInit(&alloc);
                    JR_STR(si, feature);
                    VecPushBack(&parsed.config.features, feature);
        Vec(i32) empty_numbers = VecInit(&alloc);
        Vec(Str) empty_strings = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
        Str empty_str          = StrInit(&alloc);
    
        // Write to JSON
    
        // Write to JSON
        Str json = StrInit(&alloc);
        JW_OBJ(json, {
            JW_STR_KV(json, "empty_string", empty_str);
        Vec(i32) parsed_numbers = VecInit(&alloc);
        Vec(Str) parsed_strings = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
        Str  parsed_str         = StrInit(&alloc);
        bool found_empty_object = false;
            });
            JR_ARR_KV(si, "empty_strings", {
                Str str = StrInit(&alloc);
                JR_STR(si, str);
                VecPushBack(&parsed_strings, str);
    
        // Write to JSON
        Str json = StrInit(&alloc);
        JW_OBJ(json, {
            JW_INT_KV(json, "max_int", original.max_int);
        Str              src         = StrInitFromZstr("host = localhost\n", &alloc);
        StrIter          input       = StrIterFromStr(src);
        Str              host_copy   = StrInit(&alloc);
        Str             *stored_host = NULL;
        bool             result      = true;
    
        HttpResponse response = HttpResponseInit(alloc_base);
        Str          body     = StrInit(alloc_base);
        StrAppendFmt(&body, "<h1>hi</h1>");
        HttpRespondWithHtml(&response, HTTP_RESPONSE_CODE_OK, &body);
    
    
        Str s = StrInit(&alloc);
    
        // Reserve more space than needed
    
    
        Str s = StrInit(&alloc);
    
        // Reserve more space
        // Test with an empty string
        StrDeinit(&s);
        s = StrInit(&alloc);
    
        // Reverse the string
        size       n = bt_capture_outer(frames, 32);
    
        Str rendered = StrInit(alloc_base);
        FormatStackTrace(&rendered, frames, n, alloc_base);
        ok                 = ok && VecLen(&frames) >= 2;
    
        Str rendered = StrInit(alloc_base);
        FormatStackTrace(&rendered, &frames, alloc_base);
        size       n = bt_capture_outer(frames, 16);
    
        Str out = StrInit(alloc_base);
        FormatStackTraceWith(&out, frames, n, &res);
        size       n = cfi_capture_outer(&res, frames, 32);
    
        Str rendered = StrInit(alloc_base);
        FormatStackTraceWith(&rendered, frames, n, &res);
    
    
        Str s = StrInit(&alloc);
    
        // Test decimal conversion
    
    
        Str s = StrInit(&alloc);
    
        // Test positive decimal conversion
    
    
        Str s = StrInit(&alloc);
    
        // Test integer conversion
    
        for (size i = 0; i < sizeof(u64_values) / sizeof(u64_values[0]); i++) {
            Str s = StrInit(&alloc);
    
            // Test decimal round-trip
    
        for (size i = 0; i < sizeof(i64_values) / sizeof(i64_values[0]); i++) {
            Str s = StrInit(&alloc);
    
            // Test decimal round-trip
        for (size i = 0; i < sizeof(f64_values) / sizeof(f64_values[0]); i++) {
            for (u8 precision = 1; precision <= 6; precision++) {
                Str s = StrInit(&alloc);
    
                StrFloatFormat config = {.precision = precision, .force_sci = false, .uppercase = false};
        // Test base boundary conditions
        for (u8 base = 2; base <= 36; base++) {
            Str s = StrInit(&alloc);
    
            // Test with base value itself
    
        // Test extreme values
        Str s = StrInit(&alloc);
    
        // Test maximum u64
    
        for (u8 precision = 1; precision <= 17; precision++) {
            Str s = StrInit(&alloc);
    
            StrFloatFormat config = {.precision = precision, .force_sci = false, .uppercase = false};
    
        for (size i = 0; i < sizeof(sci_values) / sizeof(sci_values[0]); i++) {
            Str s = StrInit(&alloc);
    
            // Force scientific notation
    
        for (u8 base = 2; base <= 36; base++) {
            Str s = StrInit(&alloc);
    
            StrIntFormat config = {.base = base, .uppercase = false};
        // Test each base from 2 to 36
        for (u8 base = 2; base <= 36; base++) {
            Str s = StrInit(&alloc);
    
            // Test StrFromU64
        // Test uppercase digits for bases that use letters (11-36)
        for (u8 base = 11; base <= 36; base++) {
            Str s = StrInit(&alloc);
    
            StrIntFormat config = {.base = base, .uppercase = true, .use_prefix = false};
        for (size i = 0; i < sizeof(test_values) / sizeof(test_values[0]); i++) {
            for (u8 base = 2; base <= 36; base++) {
                Str s = StrInit(&alloc);
    
                StrIntFormat config = {.base = base, .uppercase = false, .use_prefix = false};
            u64 test_value = i * 1000007; // Large prime multiplier
    
            Str          s      = StrInit(&alloc);
            StrIntFormat config = {.base = 10, .uppercase = false};
            StrFromU64(&s, test_value, &config);
                f64 test_value = mantissa * F64Pow((10.0), (i32)(exp));
    
                Str            s      = StrInit(&alloc);
                StrFloatFormat config = {.precision = 6, .force_sci = false, .uppercase = false};
                StrFromF64(&s, test_value, &config);
    
    
        Str s = StrInit(&alloc);
    
        bool result = (StrLen(&s) == 0);
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
    
        Point2D p  = {.x = 3, .y = 4};
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
    
        Point2D p     = {.x = -1, .y = 2};
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
    
        Point2D src = {.x = 100, .y = -200};
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
    
        Bounds b = {
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
    
        Bounds src = {
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
    
        Region r = {
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
    
        Region src = {
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
    
        Region region = {
        // counted as a leak by the very report we're about to generate.
        HeapAllocator scratch = HeapAllocatorInit();
        Str           out     = StrInit(&scratch);
        DebugAllocatorReportLeaks(&dbg, &out);
        bool success = true;
    
        Str s = StrInit(&alloc);
        z     = "Hello";
        StrReadFmt(z, "{}", s);
    
        i32 num  = 0;
        Str name = StrInit(&alloc);
        z        = "Count: 42, Name: Alice";
        StrReadFmt(z, "Count: {}, Name: {}", num, name);
        success = success && abc_pass;
    
        Str str_val = StrInit(&alloc);
        z           = "Hello";
        StrReadFmt(z, "{c}", str_val);
        StrDeinit(&str_val);
    
        str_val = StrInit(&alloc);
        z       = "\"World\"";
        StrReadFmt(z, "{cs}", str_val);
        // Test 1: :a (lowercase) conversion
        {
            Str  result = StrInit(&alloc);
            Zstr in     = "Hello World";
        // Test 1.1: :a (lowercase) conversion
        {
            Str  result = StrInit(&alloc);
            Zstr in     = "Hello World";
        // Test 2: :A (uppercase) conversion
        {
            Str  result = StrInit(&alloc);
            Zstr in     = "hello world";
        // Test 2.1: :A (uppercase) conversion
        {
            Str  result1 = StrInit(&alloc);
            Str  result2 = StrInit(&alloc);
            Zstr in      = "hello world";
        {
            Str  result1 = StrInit(&alloc);
            Str  result2 = StrInit(&alloc);
            Zstr in      = "hello world";
        // Test 2.2: :A (uppercase) conversion
        {
            Str  result1 = StrInit(&alloc);
            Str  result2 = StrInit(&alloc);
            Zstr in      = "hello world mighty misra";
        {
            Str  result1 = StrInit(&alloc);
            Str  result2 = StrInit(&alloc);
            Zstr in      = "hello world mighty misra";
        // Test 3: :a with quoted string
        {
            Str  result = StrInit(&alloc);
            Zstr in     = "\"MiXeD CaSe\"";
        // Test 4: :A with quoted string containing special characters
        {
            Str  result = StrInit(&alloc);
            Zstr in     = "\"abc123XYZ\"";
        // Test 5: Regular :c format (no case conversion) for comparison
        {
            Str  result = StrInit(&alloc);
            Zstr in     = "Hello World";
        Int oct = IntInit(alloc_base);
    
        Str dec_text = StrInit(&alloc);
        Str hex_text = StrInit(&alloc);
        Str bin_text = StrInit(&alloc);
    
        Str dec_text = StrInit(&alloc);
        Str hex_text = StrInit(&alloc);
        Str bin_text = StrInit(&alloc);
        Str oct_text = StrInit(&alloc);
        Str dec_text = StrInit(&alloc);
        Str hex_text = StrInit(&alloc);
        Str bin_text = StrInit(&alloc);
        Str oct_text = StrInit(&alloc);
        Str hex_text = StrInit(&alloc);
        Str bin_text = StrInit(&alloc);
        Str oct_text = StrInit(&alloc);
    
        z = "123456789012345678901234567890";
        Float neg = FloatInit(alloc_base);
    
        Str dec_text = StrInit(&alloc);
        Str sci_text = StrInit(&alloc);
        Str neg_text = StrInit(&alloc);
    
        Str dec_text = StrInit(&alloc);
        Str sci_text = StrInit(&alloc);
        Str neg_text = StrInit(&alloc);
        Str dec_text = StrInit(&alloc);
        Str sci_text = StrInit(&alloc);
        Str neg_text = StrInit(&alloc);
    
        z = "1234567890.012345";
        Float value        = FloatFromStr("1234500e-2", ALLOCATOR_OF(&alloc));
        Int   result_value = IntInit(ALLOCATOR_OF(&alloc));
        Str   text         = StrInit(ALLOCATOR_OF(&alloc));
    
        bool result = FloatToInt(&result_value, &value);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        Allocator       *alloc_base = ALLOCATOR_OF(&alloc);
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        Allocator       *alloc_base = ALLOCATOR_OF(&alloc);
    
        Str  output  = StrInit(&alloc);
        bool success = true;
        Allocator       *alloc_base = ALLOCATOR_OF(&alloc);
    
        Str   output  = StrInit(&alloc);
        bool  success = true;
        Float exact   = FloatFromStr("1234567890.012345", alloc_base);
    bool test_str_write_fmt_clears(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInit(&alloc);
        StrAppendFmt(&s, "old prefix ");
        StrWriteFmt(&s, "fresh {}", LVAL(42));
    bool test_str_patch_fmt(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInit(&alloc);
        StrAppendFmt(&s, "AAAAAAAA");
        size before_length = StrLen(&s);
    
        Float value = FloatFromStr("12.5", &alloc.base);
        Str   text  = StrInit(&alloc.base);
    
        FloatNegate(&value);
        Float b            = FloatFromStr("0.03", &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatAdd(&result_value, &a, &b);
        Float b            = FloatFromStr(FLOAT_TEST_VERY_LARGE_TWOS, &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatAdd(&result_value, &a, &b);
        Int   whole        = IntFrom(2, &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatAdd(&result_value, &a, &b);
        Float b            = FloatFromStr("2", &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatSub(&result_value, &a, &b);
        Float b            = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES, &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatSub(&result_value, &a, &b);
        Int   whole        = IntFrom(2, &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatSub(&result_value, &a, &b);
        Float b            = FloatFromStr("-0.2", &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatMul(&result_value, &a, &b);
        Float b            = FloatFromStr("2", &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatMul(&result_value, &a, &b);
        Int   whole        = IntFrom(2, &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatMul(&result_value, &a, &b);
        Float b            = FloatFromStr("8", &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatDiv(&result_value, &a, &b, 3);
        Float b            = FloatFromStr("2", &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatDiv(&result_value, &a, &b, 0);
        Int   whole        = IntFrom(3, &alloc.base);
        Float result_value = FloatInit(&alloc.base);
        Str   text         = StrInit(&alloc.base);
    
        FloatDiv(&result_value, &a, &b, 1);
        Int b            = IntFrom(1, &alloc.base);
        Int result_value = IntInit(&alloc.base);
        Str text         = StrInit(&alloc.base);
    
        IntAdd(&result_value, &a, &b);
        Int result_value = IntInit(&alloc.base);
        Int huge         = IntFromStr("123456789012345678901234567890", &alloc.base);
        Str text         = StrInit(&alloc.base);
    
        IntAdd(&result_value, &base, &rhs);
        Int preserved    = IntFrom(99, &alloc.base);
        Int huge         = IntFromStr("12345678901234567890", &alloc.base);
        Str text         = StrInit(&alloc.base);
    
        bool result = IntSub(&result_value, &base, &rhs);
        Int value        = IntFromStr("12345678901234567890", &alloc.base);
        Int result_value = IntInit(&alloc.base);
        Str text         = StrInit(&alloc.base);
    
        IntMul(&result_value, &value, 25u);
        Int exponent     = IntFrom(20, &alloc.base);
        Int result_value = IntInit(&alloc.base);
        Str text         = StrInit(&alloc.base);
    
        IntPow(&result_value, &base, 20u);
        Int quotient  = IntInit(&alloc.base);
        Int remainder = IntInit(&alloc.base);
        Str qtext     = StrInit(&alloc.base);
    
        IntDivMod(&quotient, &remainder, &dividend, 97u);
        Int dividend     = IntFromStr("12345678901234567890", &alloc.base);
        Int result_value = IntInit(&alloc.base);
        Str text         = StrInit(&alloc.base);
    
        bool result = IntDivExact(&result_value, &dividend, 90u);
        Int quotient  = IntInit(&alloc.base);
        Int remainder = IntInit(&alloc.base);
        Str text      = StrInit(&alloc.base);
    
        IntDivMod(&quotient, &remainder, &dividend, 97);
        Int value = IntFromStr("1000000000", &alloc.base);
        Int next  = IntInit(&alloc.base);
        Str text  = StrInit(&alloc.base);
    
        bool ok = IntNextPrime(&next, &value);
        }
    
        Str body = StrInit(alloc_base);
        i64 got  = FileRead(&f, &body);
        FileClose(&f);
        }
    
        Str body = StrInit(alloc_base);
        i64 got  = FileRead(&f, &body);
        FileClose(&f);
    
        // Create a Str object
        Str s = StrInit(&alloc);
    
        // Check that it behaves like a Vec of chars
    
        // Create a valid Str
        Str s = StrInit(&alloc);
    
        // This should not crash
    
        // Create an invalid Str by corrupting its fields
        Str s = StrInit(&alloc);
    
        // Corrupt the string to make it invalid
    // Test StrInit function
    bool test_str_init(void) {
        WriteFmt("Testing StrInit\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s = StrInit(&alloc);
    
        // Validate the string
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s = StrInit(&alloc);
        StrAppendFmt(&s, "Hello, {}!", &"World"[0]);
    
        Str src = StrInitFromZstr("Hello, World!", &alloc);
        Str dst = StrInit(&alloc);
    
        // Copy src to dst
    
        Str  dup    = StrInitFromStr(&src, &alloc);
        Str  dst    = StrInit(&alloc);
        bool copied = StrInitCopy(&dst, &src);
    
        // Build a new string by iterating through each character with its index
        Str result = StrInit(&alloc);
        StrForeachIdx(&s, chr, idx) {
            StrAppendFmt(&result, "{c}{}", chr, idx);
    
        // Build a new string by iterating through each character in reverse with its index
        Str result = StrInit(&alloc);
    
        StrForeachReverseIdx(&s, chr, idx) {
    
        // Build a new string by iterating through each character pointer with its index
        Str result = StrInit(&alloc);
        StrForeachPtrIdx(&s, chrptr, idx) {
            // Append the character (via pointer) and its index to the result string
    
        // Build a new string by iterating through each character pointer in reverse with its index
        Str result = StrInit(&alloc);
    
        StrForeachReversePtrIdx(&s, chrptr, idx) {
    
        // Build a new string by iterating through each character
        Str result = StrInit(&alloc);
        StrForeach(&s, chr) {
            // Append the character to the result string
    
        // Build a new string by iterating through each character in reverse
        Str  result     = StrInit(&alloc);
        size char_count = 0;
    
        // Build a new string by iterating through each character pointer
        Str result = StrInit(&alloc);
        StrForeachPtr(&s, chrptr) {
            // Append the character (via pointer) to the result string
    
        // Build a new string by iterating through each character pointer in reverse
        Str  result     = StrInit(&alloc);
        size char_count = 0;
    
        // Build a new string by iterating through a range of characters with indices
        Str result = StrInit(&alloc);
        StrForeachInRangeIdx(&s, chr, idx, 6, 11) {
            // Append the character and its index to the result string
    
        // Test with empty range
        Str empty_result = StrInit(&alloc);
        StrForeachInRangeIdx(&s, chr, idx, 3, 3) {
            // This block should not execute
    
        // Build a new string by iterating through a range of characters
        Str result = StrInit(&alloc);
        StrForeachInRange(&s, chr, 0, 5) {
            // Append the character to the result string
    
        // Test with range at the end of the string
        Str end_result = StrInit(&alloc);
        StrForeachInRange(&s, chr, 6, 11) {
            // Append the character to the result string
    
        // Build a new string by iterating through a range of character pointers with indices
        Str result = StrInit(&alloc);
        StrForeachPtrInRangeIdx(&s, chrptr, idx, 6, 11) {
            // Append the character and its index to the result string
    
        // Build a new string by iterating through a range of character pointers
        Str result = StrInit(&alloc);
        StrForeachPtrInRange(&s, chrptr, 0, 5) {
            // Append the character to the result string
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        Str name = StrInit(&a);
        ArgOptional(&p, NULL, "--name", &name, "n");
        do {                                                                                                               \
            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)));                                      \
        do {                                                                                                               \
            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)));                                      \
        do {                                                                                                               \
            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)));                                       \
            i32           UNPL(sys_eno)   = (i32)(eno);                                                                    \
            HeapAllocator UNPL(log_alloc) = HeapAllocatorInit();                                                           \
            Str           UNPL(m)         = StrInit(&UNPL(log_alloc));                                                     \
            StrAppendFmt(&UNPL(m), __VA_ARGS__);                                                                           \
            StrInitStack(UNPL(syserr), 256) {                                                                              \
            i32           UNPL(sys_eno)   = (i32)(eno);                                                                    \
            HeapAllocator UNPL(log_alloc) = HeapAllocatorInit();                                                           \
            Str           UNPL(m)         = StrInit(&UNPL(log_alloc));                                                     \
            StrAppendFmt(&UNPL(m), __VA_ARGS__);                                                                           \
            StrInitStack(UNPL(syserr), 256) {                                                                              \
            i32           UNPL(sys_eno)   = (i32)(eno);                                                                    \
            HeapAllocator UNPL(log_alloc) = HeapAllocatorInit();                                                           \
            Str           UNPL(m)         = StrInit(&UNPL(log_alloc));                                                     \
            StrAppendFmt(&UNPL(m), __VA_ARGS__);                                                                           \
            StrInitStack(UNPL(syserr), 256) {                                                                              \
    #define ProcReadFromStdoutFmt(p, ...)                                                                                  \
        do {                                                                                                               \
            Str UNPL(buf) = StrInit();                                                                                     \
            ProcReadFromStdout((p), &UNPL(buf));                                                                           \
            Zstr UNPL(in) = StrBegin(&UNPL(buf));                                                                          \
    #define ProcReadFromStderrFmt(p, ...)                                                                                  \
        do {                                                                                                               \
            Str UNPL(buf) = StrInit();                                                                                     \
            ProcReadFromStderr((p), &UNPL(buf));                                                                           \
            Zstr UNPL(in) = StrBegin(&UNPL(buf));                                                                          \
    #define ProcWriteToStdinFmt(p, ...)                                                                                    \
        do {                                                                                                               \
            Str UNPL(buf) = StrInit();                                                                                     \
            StrAppendFmt(&UNPL(buf), __VA_ARGS__);                                                                         \
            ProcWriteToStdin((p), &UNPL(buf));                                                                             \
    #define ProcWriteToStdinFmtLn(p, ...)                                                                                  \
        do {                                                                                                               \
            Str UNPL(buf) = StrInit();                                                                                     \
            StrAppendFmt(&UNPL(buf), __VA_ARGS__);                                                                         \
            StrPushBackR(&UNPL(buf), '\n');                                                                                 \
    #define JR_STR(si, str)                                                                                                \
        do {                                                                                                               \
            Str UNPL(my_str) = StrInit();                                                                                  \
            si               = JReadString((si), &UNPL(my_str));                                                           \
            (str)            = UNPL(my_str);                                                                               \
        do {                                                                                                               \
            if (!StrCmp(&key, (k))) {                                                                                      \
                Str UNPL(my_str) = StrInit();                                                                              \
                si               = JReadString((si), &UNPL(my_str));                                                       \
                (str)            = UNPL(my_str);                                                                           \
                                                                                                                           \
                                                                                                                           \
                Str key = StrInit();                                                                                       \
                                                                                                                           \
                /* key start */                                                                                            \
Last updated on