Skip to content
StrInitFromZstr

StrInitFromZstr

Description

Initialise a Str by value from a NUL-terminated Zstr. Length is derived from ZstrLen(zstr), so zstr is walked once to find the terminator before the copy is performed. The 1-arg form uses the enclosing MisraScope allocator; the 2-arg form takes an explicit allocator.

Success

Returns a usable Str holding the bytes of zstr up to (but not including) the terminator.

Failure

Returns an empty Str on allocator OOM. LOG_FATAL if zstr is NULL (via ZstrLen / underlying Cstr backend).

Usage example (Cross-references)

Usage examples (Cross-references)
        typedef Graph(Str) StrGraph;
        StrGraph    graph = GraphInitWithDeepCopy(str_init_copy, str_deinit, &alloc);
        Str         name  = StrInitFromZstr("alpha", &alloc);
        GraphNodeId node_id;
        GraphNode   node;
        // hand its address through; the helpers + Map / Graph deep-copy
        // callbacks own the resulting clones.
        Str s_alpha   = StrInitFromZstr("Alpha", &alloc);
        Str s_beta    = StrInitFromZstr("Beta", &alloc);
        Str s_gamma   = StrInitFromZstr("Gamma", &alloc);
        // callbacks own the resulting clones.
        Str s_alpha   = StrInitFromZstr("Alpha", &alloc);
        Str s_beta    = StrInitFromZstr("Beta", &alloc);
        Str s_gamma   = StrInitFromZstr("Gamma", &alloc);
        Str s_delta   = StrInitFromZstr("Delta", &alloc);
        Str s_alpha   = StrInitFromZstr("Alpha", &alloc);
        Str s_beta    = StrInitFromZstr("Beta", &alloc);
        Str s_gamma   = StrInitFromZstr("Gamma", &alloc);
        Str s_delta   = StrInitFromZstr("Delta", &alloc);
        Str s_echo    = StrInitFromZstr("Echo", &alloc);
        Str s_beta    = StrInitFromZstr("Beta", &alloc);
        Str s_gamma   = StrInitFromZstr("Gamma", &alloc);
        Str s_delta   = StrInitFromZstr("Delta", &alloc);
        Str s_echo    = StrInitFromZstr("Echo", &alloc);
        Str s_unknown = StrInitFromZstr("Unknown", &alloc);
        Str s_gamma   = StrInitFromZstr("Gamma", &alloc);
        Str s_delta   = StrInitFromZstr("Delta", &alloc);
        Str s_echo    = StrInitFromZstr("Echo", &alloc);
        Str s_unknown = StrInitFromZstr("Unknown", &alloc);
        Str s_delta   = StrInitFromZstr("Delta", &alloc);
        Str s_echo    = StrInitFromZstr("Echo", &alloc);
        Str s_unknown = StrInitFromZstr("Unknown", &alloc);
    
        GraphNodeId alpha = city_add_intersection(&graph, &index, &s_alpha, &alloc);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str   text  = StrInitFromZstr("not-a-number", &alloc.base);
        Float value = FloatInit(&alloc.base);
        bool  ok    = (FloatTryFromStr(&value, &text) == false);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str   text  = StrInitFromZstr("3.14", &alloc.base);
        Float value = FloatInit(&alloc.base);
        bool  ok    = FloatTryFromStr(&value, &text);
        DefaultAllocator local = DefaultAllocatorInit();
    
        Str s = StrInitFromZstr("hello", &local);
    
        StrClear(&s);
        DefaultAllocator local = DefaultAllocatorInit();
    
        Str s = StrInitFromZstr("x", &local);
    
        StrClear(&s);
    bool test_blind_regex_match_str_validates(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              p     = StrInitFromZstr("1", &alloc);
        bitvec_regex_match_str(NULL, &p);
        return false;
    
        // "111" never appears in "101010" -> must be false.
        Str pat_no = StrInitFromZstr("111", &alloc);
        result     = result && !BitVecRegexMatch(&source, &pat_no);
        // Sanity: a genuine substring still matches (guards against a trivially
        // always-false implementation).
        Str pat_yes = StrInitFromZstr("010", &alloc);
        result      = result && BitVecRegexMatch(&source, &pat_yes);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str    s      = StrInitFromZstr("111000111", &alloc);
        BitVec bv     = bitvec_from_str_str(&s, ALLOCATOR_OF(&alloc));
        bool   result = (BitVecLen(&bv) == 9) && (BitVecCountOnes(&bv) == 6);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  text   = StrInitFromZstr("0b101", &alloc);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_binary_str(&out, &text);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  text   = StrInitFromZstr("0B101", &alloc);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_binary_str(&out, &text);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  text   = StrInitFromZstr("1101", &alloc);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_binary_str(&out, &text);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  text   = StrInitFromZstr("0o17", &alloc);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_oct_str_str(&out, &text);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  text   = StrInitFromZstr("0O17", &alloc);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_oct_str_str(&out, &text);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  text   = StrInitFromZstr("17", &alloc);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_oct_str_str(&out, &text);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  text   = StrInitFromZstr("123", &alloc);
        Int  out    = IntInit(&alloc.base);
        bool parsed = IntTryFromStr(&out, &text);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  text   = StrInitFromZstr("+5", &alloc);
        Int  out    = IntInit(&alloc.base);
        bool parsed = IntTryFromStr(&out, &text);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  digits = StrInitFromZstr("123", ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool ok     = IntTryFromStrRadix(&value, &digits, 10);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  digits = StrInitFromZstr("+5", ALLOCATOR_OF(&alloc));
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool ok     = IntTryFromStrRadix(&value, &digits, 10);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str digits = StrInitFromZstr("12345", &alloc.base);
        Int value  = IntFromStr(&digits, &alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str digits = StrInitFromZstr("ff", &alloc.base);
        Int value  = IntFromStrRadix(&digits, (u8)16u, &alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str hex   = StrInitFromZstr("ff", &alloc.base);
        Int value = IntInit(&alloc.base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str hex   = StrInitFromZstr("12g3", &alloc.base);
        Int value = IntInit(&alloc.base);
        StrClear(&output);
    
        Str s = StrInitFromZstr("World", &alloc);
        StrAppendFmt(&output, "{}", s);
        success = success && (ZstrCompare(StrBegin(&output), "World") == 0);
        StrClear(&output);
    
        Str s = StrInitFromZstr("MiXeD CaSe", &alloc);
    
        StrAppendFmt(&output, "{c}", s);
        bool ok     = true;
    
        Str s = StrInitFromZstr("xyz", &alloc);
    
        StrAppendFmt(&output, "AB");
        bool ok     = true;
    
        Str s = StrInitFromZstr("hi", &alloc);
        StrAppendFmt(&output, "{<5}", s);
        ok = ok && (ZstrCompare(StrBegin(&output), "hi   ") == 0);
        bool ok     = true;
    
        Str s = StrInitFromZstr("hello", &alloc);
        StrAppendFmt(&output, "{.3}", s);
        ok = ok && (ZstrCompare(StrBegin(&output), "hel") == 0);
        bool ok     = true;
    
        Str s = StrInitFromZstr("hello", &alloc);
        StrAppendFmt(&output, "{.0}", s);
        ok = ok && (StrLen(&output) == 0);
    static bool test_m33_patch_fmt_malformed_returns_false(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              o     = StrInitFromZstr("-----", &alloc);
    
        // "AB{" pushes "AB" into the scratch then hits an unclosed spec ->
    static bool test_m33_patch_fmt_empty_at_end(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              o     = StrInitFromZstr("hello", &alloc);
    
        bool r = StrPatchFmt(&o, StrLen(&o), ""); // offset == length, empty render
    static bool test_m33_patch_fmt_exact_fit(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              o     = StrInitFromZstr("-----", &alloc); // length 5
    
        bool r = StrPatchFmt(&o, 0, "12345");                      // renders exactly 5 chars
        bool             ok    = true;
    
        Str s = StrInitFromZstr("hello", &alloc);
        StrAppendFmt(&out, "{}", s); // width defaults to 0 -> no padding
        ok = ok && (ZstrCompare(StrBegin(&out), "hello") == 0);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              out   = StrInit(&alloc);
        Str              s     = StrInitFromZstr("hi", &alloc);
        StrAppendFmt(&out, "{}", s); // width 0 default
        bool ok = (ZstrCompare(StrBegin(&out), "hi") == 0);
        StrReadFmt(z, "{}", s);
    
        Str expected = StrInitFromZstr("Hello", &alloc);
        success      = success && (StrCmp(&s, &expected) == 0);
        StrDeinit(&expected);
        StrReadFmt(z, "{s}", s);
    
        expected = StrInitFromZstr("Hello, World!", &alloc);
        success  = success && (StrCmp(&s, &expected) == 0);
        StrDeinit(&expected);
        success = success && (num == 42);
    
        Str expected = StrInitFromZstr("Alice", &alloc);
        success      = success && (StrCmp(&name, &expected) == 0);
        StrDeinit(&expected);
        success = success && double_equals(val, 3.14);
    
        expected = StrInitFromZstr("Bob", &alloc);
        success  = success && (StrCmp(&name, &expected) == 0);
        StrDeinit(&expected);
        z           = "Hello";
        StrReadFmt(z, "{c}", str_val);
        Str  expected = StrInitFromZstr("Hello", &alloc);
        bool str_pass = (StrCmp(&str_val, &expected) == 0);
        WriteFmt("str_val test: comparing with 'Hello', pass = {}\n", str_pass ? "true" : "false");
        z       = "\"World\"";
        StrReadFmt(z, "{cs}", str_val);
        expected             = StrInitFromZstr("World", &alloc);
        bool quoted_str_pass = (StrCmp(&str_val, &expected) == 0);
        WriteFmt("quoted str_val test: comparing with 'World', pass = {}\n", quoted_str_pass ? "true" : "false");
    
            // Should read "hello" (stops at first space)
            Str  expected   = StrInitFromZstr("hello world", &alloc);
            bool test1_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'hello', Pass: {}\n\n", test1_pass ? "true" : "false");
    
            // Should read "hello" (stops at first space)
            Str  expected   = StrInitFromZstr("hello", &alloc);
            bool test1_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'hello', Pass: {}\n\n", test1_pass ? "true" : "false");
    
            // Should read "HELLO" (stops at first space)
            Str  expected   = StrInitFromZstr("HELLO WORLD", &alloc);
            bool test2_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'HELLO', Pass: {}\n\n", test2_pass ? "true" : "false");
    
            // Should read "mixed case" (converts the entire quoted string)
            Str  expected   = StrInitFromZstr("mixed case", &alloc);
            bool test3_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'mixed case', Pass: {}\n\n", test3_pass ? "true" : "false");
    
            // Should read "ABC123XYZ" (only letters are converted, numbers unchanged)
            Str  expected   = StrInitFromZstr("ABC123XYZ", &alloc);
            bool test4_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'ABC123XYZ', Pass: {}\n\n", test4_pass ? "true" : "false");
    
            // Should read "Hello" (stops at first space, no case conversion)
            Str  expected   = StrInitFromZstr("Hello World", &alloc);
            bool test5_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'Hello World', Pass: {}\n\n", test5_pass ? "true" : "false");
        StrReadFmt(z, "{}", s);
    
        Str  expected = StrInitFromZstr("ABC", &alloc);
        bool ok       = (StrLen(&s) == 3) && (StrCmp(&s, &expected) == 0);
        StrReadFmt(z, "{}", s);
    
        Str  expected = StrInitFromZstr("A", &alloc);
        bool ok       = (StrLen(&s) == 1) && (StrCmp(&s, &expected) == 0);
        StrReadFmt(z, "{A}", s);
    
        Str  expected = StrInitFromZstr("A", &alloc); // uppercased
        bool ok       = (StrLen(&s) == 1) && (StrCmp(&s, &expected) == 0);
        StrReadFmt(z, "{s}", s);
    
        Str  expected = StrInitFromZstr("ABC", &alloc);
        bool ok       = (StrLen(&s) == 3) && (StrCmp(&s, &expected) == 0);
        StrReadFmt(z, "{s}", s);
    
        Str  expected = StrInitFromZstr("A", &alloc);
        bool ok       = (StrLen(&s) == 1) && (StrCmp(&s, &expected) == 0);
        StrReadFmt(z, "{As}", s);
    
        Str  expected = StrInitFromZstr("A", &alloc); // uppercased
        bool ok       = (StrLen(&s) == 1) && (StrCmp(&s, &expected) == 0);
    
    
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr("Hello", &alloc);
        Str s3 = StrInitFromZstr("World", &alloc);
    
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr("Hello", &alloc);
        Str s3 = StrInitFromZstr("World", &alloc);
        Str s4 = StrInitFromZstr("Hello World", &alloc);
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr("Hello", &alloc);
        Str s3 = StrInitFromZstr("World", &alloc);
        Str s4 = StrInitFromZstr("Hello World", &alloc);
        Str s2 = StrInitFromZstr("Hello", &alloc);
        Str s3 = StrInitFromZstr("World", &alloc);
        Str s4 = StrInitFromZstr("Hello World", &alloc);
    
        // Test StrCmp with equal strings
    
    
        Str haystack = StrInitFromZstr("Hello World", &alloc);
        Str needle1  = StrInitFromZstr("World", &alloc);
        Str needle2  = StrInitFromZstr("Hello", &alloc);
    
        Str haystack = StrInitFromZstr("Hello World", &alloc);
        Str needle1  = StrInitFromZstr("World", &alloc);
        Str needle2  = StrInitFromZstr("Hello", &alloc);
        Str needle3  = StrInitFromZstr("NotFound", &alloc);
        Str haystack = StrInitFromZstr("Hello World", &alloc);
        Str needle1  = StrInitFromZstr("World", &alloc);
        Str needle2  = StrInitFromZstr("Hello", &alloc);
        Str needle3  = StrInitFromZstr("NotFound", &alloc);
        Str needle1  = StrInitFromZstr("World", &alloc);
        Str needle2  = StrInitFromZstr("Hello", &alloc);
        Str needle3  = StrInitFromZstr("NotFound", &alloc);
    
        // Test StrFind (Str * key) with match at end
    
    
        Str haystack = StrInitFromZstr("Hello World", &alloc);
        Str needle   = StrInitFromZstr("World", &alloc);
    
        Str haystack = StrInitFromZstr("Hello World", &alloc);
        Str needle   = StrInitFromZstr("World", &alloc);
    
        bool result = StrContains(&haystack, &needle);
    
    
        Str s      = StrInitFromZstr("Hello World", &alloc);
        Str prefix = StrInitFromZstr("Hello", &alloc);
        Str suffix = StrInitFromZstr("World", &alloc);
    
        Str s      = StrInitFromZstr("Hello World", &alloc);
        Str prefix = StrInitFromZstr("Hello", &alloc);
        Str suffix = StrInitFromZstr("World", &alloc);
        Str s      = StrInitFromZstr("Hello World", &alloc);
        Str prefix = StrInitFromZstr("Hello", &alloc);
        Str suffix = StrInitFromZstr("World", &alloc);
    
        // Test Str-form
    
        // Test Zstr-form (string literals)
        Str s1 = StrInitFromZstr("Hello World", &alloc);
        StrReplace(&s1, "World", "Universe", 1);
        bool result = (ZstrCompare(StrBegin(&s1), "Hello Universe") == 0);
        // Test multiple replacements
        StrDeinit(&s1);
        s1 = StrInitFromZstr("Hello Hello Hello", &alloc);
        StrReplace(&s1, "Hello", "Hi", 2);
        result = result && (ZstrCompare(StrBegin(&s1), "Hi Hi Hello") == 0);
        // Test Cstr-form (fixed-length views) - use the full "World" string instead of just "Wo"
        StrDeinit(&s1);
        s1 = StrInitFromZstr("Hello World", &alloc);
        StrReplace(&s1, "World", 5, "Universe", 8, 1);
        result = result && (ZstrCompare(StrBegin(&s1), "Hello Universe") == 0);
        // Test Str-form
        StrDeinit(&s1);
        s1          = StrInitFromZstr("Hello World", &alloc);
        Str find    = StrInitFromZstr("World", &alloc);
        Str replace = StrInitFromZstr("Universe", &alloc);
        StrDeinit(&s1);
        s1          = StrInitFromZstr("Hello World", &alloc);
        Str find    = StrInitFromZstr("World", &alloc);
        Str replace = StrInitFromZstr("Universe", &alloc);
        StrReplace(&s1, &find, &replace, 1);
        s1          = StrInitFromZstr("Hello World", &alloc);
        Str find    = StrInitFromZstr("World", &alloc);
        Str replace = StrInitFromZstr("Universe", &alloc);
        StrReplace(&s1, &find, &replace, 1);
        result = result && (ZstrCompare(StrBegin(&s1), "Hello Universe") == 0);
    
        // Test StrSplit
        Str  s     = StrInitFromZstr("Hello,World,Test", &alloc);
        Strs split = StrSplit(&s, ",");
    
        // Test StrLStrip
        Str  s1       = StrInitFromZstr("  Hello  ", &alloc);
        Str  stripped = StrLStrip(&s1, NULL);
        bool result   = (ZstrCompare(StrBegin(&stripped), "Hello  ") == 0);
        // Test with custom strip characters
        StrDeinit(&s1);
        s1 = StrInitFromZstr("***Hello***", &alloc);
    
        stripped = StrLStrip(&s1, "*");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str hello_lc = StrInitFromZstr("hello", &alloc);
        Str hello_uc = StrInitFromZstr("HELLO", &alloc);
        Str hello_mc = StrInitFromZstr("HeLLo", &alloc);
    
        Str hello_lc = StrInitFromZstr("hello", &alloc);
        Str hello_uc = StrInitFromZstr("HELLO", &alloc);
        Str hello_mc = StrInitFromZstr("HeLLo", &alloc);
        Str world    = StrInitFromZstr("world", &alloc);
        Str hello_lc = StrInitFromZstr("hello", &alloc);
        Str hello_uc = StrInitFromZstr("HELLO", &alloc);
        Str hello_mc = StrInitFromZstr("HeLLo", &alloc);
        Str world    = StrInitFromZstr("world", &alloc);
        Str hello_x  = StrInitFromZstr("HelloX", &alloc); // longer
        Str hello_uc = StrInitFromZstr("HELLO", &alloc);
        Str hello_mc = StrInitFromZstr("HeLLo", &alloc);
        Str world    = StrInitFromZstr("world", &alloc);
        Str hello_x  = StrInitFromZstr("HelloX", &alloc); // longer
        Str hello_mc = StrInitFromZstr("HeLLo", &alloc);
        Str world    = StrInitFromZstr("world", &alloc);
        Str hello_x  = StrInitFromZstr("HelloX", &alloc); // longer
    
        // Equal under ASCII case folding.
    
        // Non-ASCII bytes pass through verbatim (no Unicode folding).
        Str non_ascii_a = StrInitFromZstr("ABC\xC0", &alloc);
        Str non_ascii_b = StrInitFromZstr("abc\xC0", &alloc);
        ok              = ok && StrCmpIgnoreCase(&non_ascii_a, &non_ascii_b) == 0;
        // Non-ASCII bytes pass through verbatim (no Unicode folding).
        Str non_ascii_a = StrInitFromZstr("ABC\xC0", &alloc);
        Str non_ascii_b = StrInitFromZstr("abc\xC0", &alloc);
        ok              = ok && StrCmpIgnoreCase(&non_ascii_a, &non_ascii_b) == 0;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s = StrInitFromZstr("Hello World", &alloc);
        // A genuine suffix must report true...
        Str  suffix = StrInitFromZstr("World", &alloc);
        Str s = StrInitFromZstr("Hello World", &alloc);
        // A genuine suffix must report true...
        Str  suffix = StrInitFromZstr("World", &alloc);
        bool match  = StrEndsWith(&s, &suffix); // Str* -> str_ends_with_str
        // ...and a non-suffix must report false. The replace_scalar_call mutant
        // forces the return to the constant 42 (true), so this false case is what
        // actually pins the call's result.
        Str  nonsuffix = StrInitFromZstr("Hello", &alloc);
        bool no_match  = StrEndsWith(&s, &nonsuffix);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s        = StrInitFromZstr("***", &alloc);
        Str  stripped = StrLStrip(&s, "*");
        bool result   = (StrLen(&stripped) == 0);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s        = StrInitFromZstr("***", &alloc);
        Str  stripped = StrRStrip(&s, "*");
        bool result   = (StrLen(&stripped) == 0);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s        = StrInitFromZstr("x", &alloc);
        Str  stripped = StrStrip(&s, "*");
        bool result   = (StrLen(&stripped) == 1) && (ZstrCompare(StrBegin(&stripped), "x") == 0);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  haystack = StrInitFromZstr("Hello World", &alloc);
        Str  needle   = StrInitFromZstr("missing", &alloc);
        bool result   = (StrContains(&haystack, &needle) == false);
    
        Str  haystack = StrInitFromZstr("Hello World", &alloc);
        Str  needle   = StrInitFromZstr("missing", &alloc);
        bool result   = (StrContains(&haystack, &needle) == false);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  haystack = StrInitFromZstr("Hello World", &alloc);
        Str  present  = StrInitFromZstr("World", &alloc);
        Str  absent   = StrInitFromZstr("missing", &alloc);
    
        Str  haystack = StrInitFromZstr("Hello World", &alloc);
        Str  present  = StrInitFromZstr("World", &alloc);
        Str  absent   = StrInitFromZstr("missing", &alloc);
        bool result   = (StrContains(&haystack, &present) == true) && (StrContains(&haystack, &absent) == false);
        Str  haystack = StrInitFromZstr("Hello World", &alloc);
        Str  present  = StrInitFromZstr("World", &alloc);
        Str  absent   = StrInitFromZstr("missing", &alloc);
        bool result   = (StrContains(&haystack, &present) == true) && (StrContains(&haystack, &absent) == false);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  haystack = StrInitFromZstr("Hello World", &alloc);
        bool result   = (StrContains(&haystack, "missing") == false);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str      s     = StrInitFromZstr("a,b,", &alloc);
        StrIters iters = StrSplitToIters(&s, ",");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s     = StrInitFromZstr("ab", &alloc);
        Strs split = StrSplit(&s, "abc");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s     = StrInitFromZstr("ab", &alloc);
        Strs split = StrSplit(&s, "abc");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str hello = StrInitFromZstr("Hello", &alloc);
    
        bool result = (StrCmp(&hello, "Hello") == 0);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s = StrInitFromZstr("abc", &alloc);
    
        bool result = (StrIndexOf(&s, "abc", 3) == 0);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        bool result = (StrStartsWith(&s, "Hello") == true);
        StrBegin(&a)[3] = (char)0xFF;
    
        Str b = StrInitFromZstr("abYY", &alloc); // length 4
    
        // Call the mutated function directly: the public StrCmp wrapper
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s      = StrInitFromZstr("Hello World", &alloc);
        Str prefix = StrInitFromZstr("Hello", &alloc);
    
        Str s      = StrInitFromZstr("Hello World", &alloc);
        Str prefix = StrInitFromZstr("Hello", &alloc);
    
        bool result = (StrStartsWith(&s, &prefix) == true);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s = StrInitFromZstr("World", &alloc);
    
        bool result = (StrEndsWith(&s, "World") == true);
    static bool test_find_zstr_validates(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("hello", &alloc);
        s.__magic              = 0; // corrupt: validate_vec must LOG_FATAL.
        (void)str_find_zstr(&s, "x");
    static bool test_starts_with_zstr_validates(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("hello", &alloc);
        s.__magic              = 0; // corrupt: validate_vec must LOG_FATAL.
        (void)str_starts_with_zstr(&s, "he");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s   = StrInitFromZstr("haystack", &alloc);
        Str key = StrInitFromZstr("stack", &alloc);
    
        Str s   = StrInitFromZstr("haystack", &alloc);
        Str key = StrInitFromZstr("stack", &alloc);
    
        // intentional bypass: corrupt s only.
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s   = StrInitFromZstr("haystack", &alloc);
        Str key = StrInitFromZstr("stack", &alloc);
    
        Str s   = StrInitFromZstr("haystack", &alloc);
        Str key = StrInitFromZstr("stack", &alloc);
    
        // intentional bypass: corrupt key only.
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s      = StrInitFromZstr("Hello World", &alloc);
        Str suffix = StrInitFromZstr("World", &alloc);
    
        Str s      = StrInitFromZstr("Hello World", &alloc);
        Str suffix = StrInitFromZstr("World", &alloc);
    
        // intentional bypass: corrupt s only.
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // intentional bypass: corrupt s only.
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s   = StrInitFromZstr("haystack", &alloc);
        Str key = StrInitFromZstr("stack", &alloc);
    
        Str s   = StrInitFromZstr("haystack", &alloc);
        Str key = StrInitFromZstr("stack", &alloc);
    
        // intentional bypass: corrupt key only.
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s                     = StrInitFromZstr("  x  ", &alloc);
        GENERIC_VEC(&s)->__magic ^= 0x1;
        Str stripped              = StrStrip(&s, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str haystack                = StrInitFromZstr("Hello World", &alloc);
        Str key                     = StrInitFromZstr("World", &alloc);
        GENERIC_VEC(&key)->__magic ^= 0x1;
    
        Str haystack                = StrInitFromZstr("Hello World", &alloc);
        Str key                     = StrInitFromZstr("World", &alloc);
        GENERIC_VEC(&key)->__magic ^= 0x1;
        (void)StrContains(&haystack, &key);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s                     = StrInitFromZstr("a,b,c", &alloc);
        GENERIC_VEC(&s)->__magic ^= 0x1;
        StrIters iters            = StrSplitToIters(&s, ",");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s                     = StrInitFromZstr("hello", &alloc);
        GENERIC_VEC(&s)->__magic ^= 0x1;
        (void)StrCmpIgnoreCase(&s, "HELLO");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s                                    = StrInitFromZstr("a,b,c", &alloc);
        Strs strs                                 = StrSplit(&s, ",");
        GENERIC_VEC(VecPtrAt(&strs, 0))->__magic ^= 0x1;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s      = StrInitFromZstr("a,b", &alloc);
        s.__magic  = 0; // corrupt the magic so ValidateStr aborts
        Strs split = StrSplit(&s, ",");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s     = StrInitFromZstr("Hello", &alloc);
        s.__magic = 0;
        int cmp   = StrCmp(&s, "x");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s     = StrInitFromZstr("abc", &alloc);
        s.__magic = 0;
        size idx  = StrIndexOf(&s, "x", 1);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s     = StrInitFromZstr("Hello", &alloc);
        s.__magic = 0;
        int cmp   = StrCmpIgnoreCase(&s, "x", 1);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str a     = StrInitFromZstr("alpha", &alloc);
        Str b     = StrInitFromZstr("beta", &alloc);
        a.__magic = 0;             // corrupt: magic mismatch, data/length still valid
    
        Str a     = StrInitFromZstr("alpha", &alloc);
        Str b     = StrInitFromZstr("beta", &alloc);
        a.__magic = 0;             // corrupt: magic mismatch, data/length still valid
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str a     = StrInitFromZstr("alpha", &alloc);
        Str b     = StrInitFromZstr("beta", &alloc);
        b.__magic = 0;
    
        Str a     = StrInitFromZstr("alpha", &alloc);
        Str b     = StrInitFromZstr("beta", &alloc);
        b.__magic = 0;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s     = StrInitFromZstr("HELLO", &alloc);
        Str other = StrInitFromZstr("hello", &alloc);
        s.__magic = 0;
    
        Str s     = StrInitFromZstr("HELLO", &alloc);
        Str other = StrInitFromZstr("hello", &alloc);
        s.__magic = 0;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s         = StrInitFromZstr("HELLO", &alloc);
        Str other     = StrInitFromZstr("hello", &alloc);
        other.__magic = 0;
    
        Str s         = StrInitFromZstr("HELLO", &alloc);
        Str other     = StrInitFromZstr("hello", &alloc);
        other.__magic = 0;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s      = StrInitFromZstr("Hello World", &alloc);
        Str prefix = StrInitFromZstr("Hello", &alloc);
        s.__magic  = 0;
    
        Str s      = StrInitFromZstr("Hello World", &alloc);
        Str prefix = StrInitFromZstr("Hello", &alloc);
        s.__magic  = 0;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s     = StrInitFromZstr("haystack", &alloc);
        s.__magic = 0;
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Swap 'H' and 'o'
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Initial length should be 5
    
    
        Str s = StrInitFromZstr("Hello, World!", &alloc);
    
        // Initial length should be 13
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Reverse the string
        // Test with an even-length string
        StrDeinit(&s);
        s = StrInitFromZstr("abcd", &alloc);
    
        // Reverse the string
        // Test with a single-character string
        StrDeinit(&s);
        s = StrInitFromZstr("a", &alloc);
    
        // Reverse the string
    // Test StrInitFromZstr function
    bool test_str_init_from_zstr(void) {
        WriteFmt("Testing StrInitFromZstr\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Zstr test_str = "Hello, World!";
        Str  s        = StrInitFromZstr(test_str, &alloc);
    
        // Validate the string
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str src = StrInitFromZstr("Hello, World!", &alloc);
        Str dst = StrInitFromStr(&src, &alloc);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str src = StrInitFromZstr("Hello, World!", &alloc);
        Str dst = StrDup(&src, &alloc);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str src = StrInitFromZstr("Hello, World!", &alloc);
        Str dst = StrInit(&alloc);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s = StrInitFromZstr("Hello, World!", &alloc);
    
        // Validate the string before deinit
    
        // Each piece is long enough to force its own heap buffer.
        Str  s      = StrInitFromZstr("alphaaa,betaaaa,gammaaa,deltaaa", &dbg);
        size before = DebugAllocatorLiveCount(&dbg);
        Allocator     *adbg = ALLOCATOR_OF(&dbg);
    
        Str s = StrInitFromZstr("hello world", adbg);
        StrDeinit(&s);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str src     = StrInitFromZstr("source", &alloc);
        src.__magic = 0;
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Insert a character in the middle
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Insert a string in the middle: (cstr, cstr_len) adjacent, then idx
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Insert a string in the middle
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Push a string at position 2
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Push a string at position 2
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Push a string at the back
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Push a string at the back
    
    
        Str s = StrInitFromZstr("World", &alloc);
    
        // Push a string at the front
    
    
        Str s = StrInitFromZstr("World", &alloc);
    
        // Push a string at the front
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Push characters at the back
    
    
        Str s = StrInitFromZstr("World", &alloc);
    
        // Push characters at the front
    
    
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr(" World", &alloc);
    
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr(" World", &alloc);
    
        // Merge s2 into s1 (L-value semantics)
    
    
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr(" World", &alloc);
    
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr(" World", &alloc);
    
        // Merge s2 into s1 (R-value semantics)
    
    
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr(" World", &alloc);
    
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr(" World", &alloc);
    
        // Merge s2 into s1 (L-form; ownership of s2's storage transfers to s1)
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Append formatted suffix.
    
        // Add some strings
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr("World", &alloc);
        // Add some strings
        Str s1 = StrInitFromZstr("Hello", &alloc);
        Str s2 = StrInitFromZstr("World", &alloc);
    
        VecPushBack(&sv, s1);
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Pop a character from the back
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Pop a character from the front
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Remove a character from the middle
    
    
        Str s = StrInitFromZstr("Hello World", &alloc);
    
        // Create a buffer to store the removed characters
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Delete the last character
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Delete a character from the middle
    
    
        Str s = StrInitFromZstr("Hello World", &alloc);
    
        // Delete a range of characters
    
        // Test decimal conversion
        Str  s       = StrInitFromZstr("12345", &alloc);
        u64  value   = 0;
        bool success = StrToU64(&s, &value, NULL);
        // Test hexadecimal conversion with explicit base
        StrDeinit(&s);
        s                     = StrInitFromZstr("ABCD", &alloc); // No 0x prefix when base is explicitly 16
        StrParseConfig config = {.base = 16};
        success               = StrToU64(&s, &value, &config);
        // Test hexadecimal conversion (auto-detect base with 0)
        StrDeinit(&s);
        s       = StrInitFromZstr("0xABCD", &alloc);
        success = StrToU64(&s, &value, NULL);
        result  = result && (success && value == 0xABCD);
        // Test binary conversion
        StrDeinit(&s);
        s       = StrInitFromZstr("0b101010", &alloc);
        success = StrToU64(&s, &value, NULL);
        result  = result && (success && value == 42);
        // Test octal conversion
        StrDeinit(&s);
        s       = StrInitFromZstr("0o52", &alloc);
        success = StrToU64(&s, &value, NULL);
        result  = result && (success && value == 42);
        // Test zero
        StrDeinit(&s);
        s       = StrInitFromZstr("0", &alloc);
        success = StrToU64(&s, &value, NULL);
        result  = result && (success && value == 0);
        // Test invalid input
        StrDeinit(&s);
        s       = StrInitFromZstr("not a number", &alloc);
        success = StrToU64(&s, &value, NULL);
        result  = result && (!success);
        // Test negative number (should fail for unsigned)
        StrDeinit(&s);
        s       = StrInitFromZstr("-123", &alloc);
        success = StrToU64(&s, &value, NULL);
        result  = result && (!success);
    
        // Test positive decimal conversion
        Str  s       = StrInitFromZstr("12345", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        // Test negative decimal conversion
        StrDeinit(&s);
        s       = StrInitFromZstr("-12345", &alloc);
        success = StrToI64(&s, &value, NULL);
        result  = result && (success && value == -12345);
        // Test hexadecimal conversion
        StrDeinit(&s);
        s       = StrInitFromZstr("0xABCD", &alloc);
        success = StrToI64(&s, &value, NULL);
        result  = result && (success && value == 0xABCD);
        // Test binary conversion
        StrDeinit(&s);
        s       = StrInitFromZstr("0b101010", &alloc);
        success = StrToI64(&s, &value, NULL);
        result  = result && (success && value == 42);
        // Test zero
        StrDeinit(&s);
        s       = StrInitFromZstr("0", &alloc);
        success = StrToI64(&s, &value, NULL);
        result  = result && (success && value == 0);
        // Test invalid input
        StrDeinit(&s);
        s       = StrInitFromZstr("not a number", &alloc);
        success = StrToI64(&s, &value, NULL);
        result  = result && (!success);
    
        // Test integer conversion
        Str  s       = StrInitFromZstr("123", &alloc);
        f64  value   = 0.0;
        bool success = StrToF64(&s, &value, NULL);
        // Test fractional conversion
        StrDeinit(&s);
        s       = StrInitFromZstr("123.456", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64Abs(value - 123.456) < 0.0001);
        // Test negative number
        StrDeinit(&s);
        s       = StrInitFromZstr("-123.456", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64Abs(value - (-123.456)) < 0.0001);
        // Test scientific notation
        StrDeinit(&s);
        s       = StrInitFromZstr("1.23e2", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64Abs(value - 123.0) < 0.0001);
        // Test zero
        StrDeinit(&s);
        s       = StrInitFromZstr("0", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64Abs(value) < 0.0001);
        // Test infinity
        StrDeinit(&s);
        s       = StrInitFromZstr("inf", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64IsInf(value) && value > 0);
        // Test negative infinity
        StrDeinit(&s);
        s       = StrInitFromZstr("-inf", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64IsInf(value) && value < 0);
        // Test NaN
        StrDeinit(&s);
        s       = StrInitFromZstr("nan", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (success && F64IsNan(value));
        // Test invalid input
        StrDeinit(&s);
        s       = StrInitFromZstr("not a number", &alloc);
        success = StrToF64(&s, &value, NULL);
        result  = result && (!success);
    
        for (size i = 0; i < sizeof(prefix_tests) / sizeof(prefix_tests[0]); i++) {
            Str            test_str = StrInitFromZstr(prefix_tests[i].input, &alloc);
            u64            value    = 0;
            StrParseConfig config   = {.base = prefix_tests[i].base};
        MemCopy(long_number, "12345678901234567890123456789012345678901234567890", 51);
    
        Str  long_str   = StrInitFromZstr(long_number, &alloc);
        u64  long_value = 0;
        bool success    = StrToU64(&long_str, &long_value, NULL);
    static bool test_leading_space_skip_condition(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr(" 1.5", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    static bool test_leading_space_advances(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr(" 1.0", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    static bool test_nan_exact_accept_with_offset(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr(" nan", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    static bool test_inf_exact_accept_with_offset(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr(" inf", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    static bool test_minus_sign_makes_negative(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("-5", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    static bool test_plus_sign_advances(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("+5", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    static bool test_integer_digits_set_flag(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("42", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    static bool test_fractional_digits_set_flag(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr(".5", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    static bool test_exponent_gate_not_widened(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("5x", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL); // non-strict default
    static bool test_negative_exponent_sign(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("1e-2", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    static bool test_exponent_digits_set_flag(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("1e5", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    static bool test_trailing_space_skip_condition(void) {
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              s      = StrInitFromZstr("5 ", &alloc);
        f64              value  = 0.0;
        StrParseConfig   config = {.strict = true, .trim_space = true, .base = 0};
    static bool test_trailing_space_advances(void) {
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              s      = StrInitFromZstr("5 ", &alloc);
        f64              value  = 0.0;
        StrParseConfig   config = {.strict = true, .trim_space = true, .base = 0};
    static bool test_strict_trailing_check_ge(void) {
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              s      = StrInitFromZstr("5", &alloc);
        f64              value  = 0.0;
        StrParseConfig   config = {.strict = true, .trim_space = true, .base = 0};
    static bool test_strict_trailing_check_le(void) {
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              s      = StrInitFromZstr("42", &alloc);
        f64              value  = 0.0;
        StrParseConfig   config = {.strict = true, .trim_space = true, .base = 0};
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str            s      = StrInitFromZstr("5", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.base = 99};
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str            s      = StrInitFromZstr("ff", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.base = 16};
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s      = StrInitFromZstr(" 42", &alloc);
        u64  value  = 0;
        bool ok     = StrToU64(&s, &value, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s      = StrInitFromZstr(" 42", &alloc);
        u64  value  = 0;
        bool ok     = StrToU64(&s, &value, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s      = StrInitFromZstr("05", &alloc);
        u64  value  = 0;
        bool ok     = StrToU64(&s, &value, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s      = StrInitFromZstr("0x", &alloc);
        u64  value  = 0;
        bool ok     = StrToU64(&s, &value, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s      = StrInitFromZstr("18446744073709551616", &alloc);
        u64  value  = 0;
        bool ok     = StrToU64(&s, &value, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s      = StrInitFromZstr("42", &alloc);
        u64  value  = 0;
        bool ok     = StrToU64(&s, &value, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str            s      = StrInitFromZstr("42 ", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.strict = true};
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str            s      = StrInitFromZstr("42  ", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.strict = true};
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str            s      = StrInitFromZstr("42x", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.strict = true};
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str            s      = StrInitFromZstr("42", &alloc);
        u64            value  = 0;
        StrParseConfig config = {.strict = true};
    static bool test_skip_prefix_len_guard_hex(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0x1F", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
    static bool test_skip_prefix_char_init_hex(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0x5", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
    static bool test_skip_prefix_char_index_hex(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0x5", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
    static bool test_skip_prefix_bin_lower(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0b1", &alloc);
        StrParseConfig   cfg   = {.base = 2};
        u64              out   = 0;
    static bool test_skip_prefix_bin_return(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0b1", &alloc);
        StrParseConfig   cfg   = {.base = 2};
        u64              out   = 0;
    static bool test_skip_prefix_oct_lower(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0o7", &alloc);
        StrParseConfig   cfg   = {.base = 8};
        u64              out   = 0;
    static bool test_skip_prefix_oct_upper(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0O7", &alloc);
        StrParseConfig   cfg   = {.base = 8};
        u64              out   = 0;
    static bool test_skip_prefix_oct_return(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0o7", &alloc);
        StrParseConfig   cfg   = {.base = 8};
        u64              out   = 0;
    static bool test_skip_prefix_hex_lower(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0x1F", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
    static bool test_skip_prefix_hex_upper(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0X1F", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
    static bool test_skip_prefix_hex_return(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("0x1F", &alloc);
        StrParseConfig   cfg   = {.base = 16};
        u64              out   = 0;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s       = StrInitFromZstr("  -5", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s       = StrInitFromZstr("  5", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s       = StrInitFromZstr("-5", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s       = StrInitFromZstr("+5", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        // 43 '0' characters: a valid parse (value 0) whose length exceeds the
        // mutated constant capacity of 42.
        Str s = StrInitFromZstr("0000000000000000000000000000000000000000000", &alloc);
    
        i64  value   = 7; // sentinel that must be overwritten to 0
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str  s       = StrInitFromZstr("-9223372036854775808", &alloc);
        i64  value   = 0;
        bool success = StrToI64(&s, &value, NULL);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str            s       = StrInitFromZstr("2", &alloc);
        u64            value   = 999; // sentinel
        StrParseConfig config  = {.base = 2};
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str s = StrInitFromZstr("123", &alloc);
        // intentional bypass: plant an inconsistent (length, capacity) pair that
        // ValidateStr must reject; mark dirty so the body re-runs.
    
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              s      = StrInitFromZstr("0B101", &alloc);
        StrParseConfig   config = {.base = 2};
        u64              value  = 0;
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str              s     = StrInitFromZstr("1e", &alloc);
        f64              value = 0.0;
        bool             ok    = StrToF64(&s, &value, NULL);
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Build a new string by iterating through each character with its index
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Build a new string by iterating through each character in reverse with its index
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Build a new string by iterating through each character pointer with its index
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Build a new string by iterating through each character pointer in reverse with its index
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Build a new string by iterating through each character
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Build a new string by iterating through each character in reverse
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Build a new string by iterating through each character pointer
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Build a new string by iterating through each character pointer in reverse
    
    
        Str s = StrInitFromZstr("Hello World", &alloc);
    
        // Build a new string by iterating through a range of characters with indices
    
    
        Str s = StrInitFromZstr("Hello World", &alloc);
    
        // Build a new string by iterating through a range of characters
    
    
        Str s = StrInitFromZstr("Hello World", &alloc);
    
        // Build a new string by iterating through a range of character pointers with indices
    
    
        Str s = StrInitFromZstr("Hello World", &alloc);
    
        // Build a new string by iterating through a range of character pointers
    
    
        Str s = StrInitFromZstr("Hello World!", &alloc); // 12 characters
    
        // Use StrForeachInRangeIdx which captures the 'end' parameter at the start
    
    
        Str s = StrInitFromZstr("Programming", &alloc); // 11 characters
    
        // Use StrForeachInRangeIdx with a fixed range that will become invalid
    
    
        Str s = StrInitFromZstr("Beautiful Weather", &alloc); // 17 characters
    
        // StrForeachReverseIdx (VecForeachReverseIdx) has explicit bounds checking: if ((idx) >= (v)->length) LOG_FATAL(...)
    
    
        Str s = StrInitFromZstr("Programming Test", &alloc); // 16 characters
    
        // StrForeachPtrIdx (VecForeachPtrIdx) has explicit bounds checking: if ((idx) >= (v)->length) LOG_FATAL(...)
    
    
        Str s = StrInitFromZstr("Excellent Example", &alloc); // 17 characters
    
        // StrForeachReversePtrIdx (VecForeachPtrReverseIdx) has explicit bounds checking: if ((idx) >= (v)->length) LOG_FATAL(...)
    
    
        Str s = StrInitFromZstr("Comprehensive Testing Framework", &alloc); // 31 characters
    
        // Use StrForeachPtrInRangeIdx with a fixed range that becomes invalid when we modify the string
    
    
        Str s = StrInitFromZstr("Testing Basic", &alloc); // 13 characters
    
        // Basic StrForeachIdx (VecForeachIdx) now has explicit bounds checking: if ((idx) >= (v)->length) LOG_FATAL(...)
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Get the first character
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Get the last character
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Get a pointer to the first character using StrBegin
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Get a pointer to one past the last character using StrEnd
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Access characters at different indices
    
    
        Str s = StrInitFromZstr("Hello", &alloc);
    
        // Access character pointers at different indices
        resolver_init_crafted(&r, a);
    
        Str      name = StrInitFromZstr("multi", a);
        DnsAddrs out  = VecInitT(out, a);
        bool     got  = dns_resolve_5_str(&r, &name, 7000, SOCKET_KIND_TCP, &out);
        resolver_init_crafted(&r, a);
    
        Str      spec = StrInitFromZstr("multi:8080", a);
        DnsAddrs out  = VecInitT(out, a);
        bool     got  = dns_resolve_4_vec_str(&r, &spec, SOCKET_KIND_TCP, &out);
        resolver_init_crafted(&r, a);
    
        Str        spec = StrInitFromZstr("multi:80", a);
        SocketAddr one;
        bool       got = dns_resolve_4_one_str(&r, &spec, SOCKET_KIND_TCP, &one);
        Allocator       *a     = ALLOCATOR_OF(&alloc);
    
        Str        name = StrInitFromZstr("example.com", a);
        DnsWireBuf buf  = VecInitT(buf, a);
        bool       ok   = DnsBuildQuery(&buf, 0x1234, &name, DNS_TYPE_A);
    
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"name\": \"Alice\", \"city\": \"New York\"}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"count\": 42, \"score\": 95.5, \"year\": 2024}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"enabled\": true, \"visible\": false}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        bool success = true;
        Str  json    = StrInitFromZstr(
            "{\"id\": 1001, \"name\": \"Bob\", \"age\": 25, \"is_active\": true, \"salary\": 50000.0}",
            &alloc
    
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"debug_mode\": false, \"timeout\": 30, \"log_level\": \"INFO\"}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"languages\": [\"C\", \"Python\", \"Rust\"]}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        bool success = true;
        Str  json    = StrInitFromZstr(
            "{\"user\": {\"name\": \"Charlie\", \"email\": \"charlie@example.com\"}, \"active\": true}",
            &alloc
    
        bool success = true;
        Str  json    = StrInitFromZstr(
            "{\"id\": 12345, \"name\": \"Laptop\", \"price\": 999.99, \"tags\": [\"electronics\", \"computers\", "
                "\"portable\"]}",
    bool compare_json_output(const Str *output, Zstr expected, DefaultAllocator *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);
    
        // Note: These are the actual characters, not escape sequences
        Str path    = StrInitFromZstr("C:\\Program Files\\App", &alloc);
        Str message = StrInitFromZstr("Hello, \"World\"!", &alloc);
        Str data    = StrInitFromZstr("line1\nline2\ttab", &alloc);
        // Note: These are the actual characters, not escape sequences
        Str path    = StrInitFromZstr("C:\\Program Files\\App", &alloc);
        Str message = StrInitFromZstr("Hello, \"World\"!", &alloc);
        Str data    = StrInitFromZstr("line1\nline2\ttab", &alloc);
        Str path    = StrInitFromZstr("C:\\Program Files\\App", &alloc);
        Str message = StrInitFromZstr("Hello, \"World\"!", &alloc);
        Str data    = StrInitFromZstr("line1\nline2\ttab", &alloc);
    
        JW_OBJ(json, {
    
        // These contain actual special characters that should be escaped
        Str quotes    = StrInitFromZstr("\"quotes\"", &alloc);
        Str backslash = StrInitFromZstr("\\", &alloc);
        Str newline   = StrInitFromZstr("\n", &alloc);
        // These contain actual special characters that should be escaped
        Str quotes    = StrInitFromZstr("\"quotes\"", &alloc);
        Str backslash = StrInitFromZstr("\\", &alloc);
        Str newline   = StrInitFromZstr("\n", &alloc);
        Str tab       = StrInitFromZstr("\t", &alloc);
        Str quotes    = StrInitFromZstr("\"quotes\"", &alloc);
        Str backslash = StrInitFromZstr("\\", &alloc);
        Str newline   = StrInitFromZstr("\n", &alloc);
        Str tab       = StrInitFromZstr("\t", &alloc);
        Str backslash = StrInitFromZstr("\\", &alloc);
        Str newline   = StrInitFromZstr("\n", &alloc);
        Str tab       = StrInitFromZstr("\t", &alloc);
    
        JW_OBJ(json, {
    
        // Single string
        Str single_str = StrInitFromZstr("hello", &alloc);
        JW_OBJ(json2, { JW_STR_KV(json2, "text", single_str); });
    // Helper function to compare JSON output (removes whitespace for comparison)
    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 status;
        } data = {
            {123, {StrInitFromZstr("Alice", &alloc), 30}},
            StrInitFromZstr("active", &alloc)
        };
        } data = {
            {123, {StrInitFromZstr("Alice", &alloc), 30}},
            StrInitFromZstr("active", &alloc)
        };
            } company;
        } data = {
            {{{StrInitFromZstr("John", &alloc), 25, 150000.0}}, StrInitFromZstr("TechCorp", &alloc)}
        };
        ApiResponse response = {
            true,
            StrInitFromZstr("Success", &alloc),
            VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)
        };
        // Add sample data
        AnnSymbol sym             = {0};
        sym.analysis_name         = StrInitFromZstr("test_analysis", &alloc);
        sym.function_name         = StrInitFromZstr("main_func", &alloc);
        sym.sha256                = StrInitFromZstr("abc123", &alloc);
        AnnSymbol sym             = {0};
        sym.analysis_name         = StrInitFromZstr("test_analysis", &alloc);
        sym.function_name         = StrInitFromZstr("main_func", &alloc);
        sym.sha256                = StrInitFromZstr("abc123", &alloc);
        sym.function_mangled_name = StrInitFromZstr("_Z4main", &alloc);
        sym.analysis_name         = StrInitFromZstr("test_analysis", &alloc);
        sym.function_name         = StrInitFromZstr("main_func", &alloc);
        sym.sha256                = StrInitFromZstr("abc123", &alloc);
        sym.function_mangled_name = StrInitFromZstr("_Z4main", &alloc);
        sym.source_function_id    = 12345;
        sym.function_name         = StrInitFromZstr("main_func", &alloc);
        sym.sha256                = StrInitFromZstr("abc123", &alloc);
        sym.function_mangled_name = StrInitFromZstr("_Z4main", &alloc);
        sym.source_function_id    = 12345;
        sym.target_function_id    = 67890;
        Vec(FunctionInfo) functions = VecInitWithDeepCopy(NULL, FunctionInfoDeinit, &alloc);
    
        FunctionInfo func1 = {12345, StrInitFromZstr("test_func", &alloc), 1024, 4096};
        FunctionInfo func2 = {54321, StrInitFromZstr("helper_func", &alloc), 512, 8192};
        VecPushBack(&functions, func1);
    
        FunctionInfo func1 = {12345, StrInitFromZstr("test_func", &alloc), 1024, 4096};
        FunctionInfo func2 = {54321, StrInitFromZstr("helper_func", &alloc), 512, 8192};
        VecPushBack(&functions, func1);
        VecPushBack(&functions, func2);
        SearchResult result = {0};
        result.binary_id    = 888;
        result.binary_name  = StrInitFromZstr("test_binary", &alloc);
        result.analysis_id  = 999;
        result.sha256       = StrInitFromZstr("abc123", &alloc);
        result.binary_name  = StrInitFromZstr("test_binary", &alloc);
        result.analysis_id  = 999;
        result.sha256       = StrInitFromZstr("abc123", &alloc);
        result.tags         = VecInitWithDeepCopyT(result.tags, NULL, StrDeinit, &alloc);
    
        // Create strings and push them properly
        Str tag1 = StrInitFromZstr("malware", &alloc);
        Str tag2 = StrInitFromZstr("x86", &alloc);
        // Create strings and push them properly
        Str tag1 = StrInitFromZstr("malware", &alloc);
        Str tag2 = StrInitFromZstr("x86", &alloc);
    
        VecPushBack(&result.tags, tag1);
        VecPushBack(&result.tags, tag2);
    
        result.created_at = StrInitFromZstr("2024-04-01", &alloc);
        result.model_id   = 12345;
        result.model_name = StrInitFromZstr("test_model", &alloc);
        result.created_at = StrInitFromZstr("2024-04-01", &alloc);
        result.model_id   = 12345;
        result.model_name = StrInitFromZstr("test_model", &alloc);
        result.owned_by   = StrInitFromZstr("user1", &alloc);
        result.model_id   = 12345;
        result.model_name = StrInitFromZstr("test_model", &alloc);
        result.owned_by   = StrInitFromZstr("user1", &alloc);
    
        JW_OBJ(json, {
    
        AnnSymbol sym1             = {0};
        sym1.analysis_name         = StrInitFromZstr("analysis1", &alloc);
        sym1.function_name         = StrInitFromZstr("func1", &alloc);
        sym1.sha256                = StrInitFromZstr("hash1", &alloc);
        AnnSymbol sym1             = {0};
        sym1.analysis_name         = StrInitFromZstr("analysis1", &alloc);
        sym1.function_name         = StrInitFromZstr("func1", &alloc);
        sym1.sha256                = StrInitFromZstr("hash1", &alloc);
        sym1.function_mangled_name = StrInitFromZstr("_Z5func1", &alloc);
        sym1.analysis_name         = StrInitFromZstr("analysis1", &alloc);
        sym1.function_name         = StrInitFromZstr("func1", &alloc);
        sym1.sha256                = StrInitFromZstr("hash1", &alloc);
        sym1.function_mangled_name = StrInitFromZstr("_Z5func1", &alloc);
        sym1.source_function_id    = 111;
        sym1.function_name         = StrInitFromZstr("func1", &alloc);
        sym1.sha256                = StrInitFromZstr("hash1", &alloc);
        sym1.function_mangled_name = StrInitFromZstr("_Z5func1", &alloc);
        sym1.source_function_id    = 111;
        sym1.target_function_id    = 222;
    
        AnnSymbol sym2             = {0};
        sym2.analysis_name         = StrInitFromZstr("analysis2", &alloc);
        sym2.function_name         = StrInitFromZstr("func2", &alloc);
        sym2.sha256                = StrInitFromZstr("hash2", &alloc);
        AnnSymbol sym2             = {0};
        sym2.analysis_name         = StrInitFromZstr("analysis2", &alloc);
        sym2.function_name         = StrInitFromZstr("func2", &alloc);
        sym2.sha256                = StrInitFromZstr("hash2", &alloc);
        sym2.function_mangled_name = StrInitFromZstr("_Z5func2", &alloc);
        sym2.analysis_name         = StrInitFromZstr("analysis2", &alloc);
        sym2.function_name         = StrInitFromZstr("func2", &alloc);
        sym2.sha256                = StrInitFromZstr("hash2", &alloc);
        sym2.function_mangled_name = StrInitFromZstr("_Z5func2", &alloc);
        sym2.source_function_id    = 333;
        sym2.function_name         = StrInitFromZstr("func2", &alloc);
        sym2.sha256                = StrInitFromZstr("hash2", &alloc);
        sym2.function_mangled_name = StrInitFromZstr("_Z5func2", &alloc);
        sym2.source_function_id    = 333;
        sym2.target_function_id    = 444;
    
        // Create strings for the nested structure
        Str deep_message = StrInitFromZstr("deep", &alloc);
        Str test_name    = StrInitFromZstr("test", &alloc);
        // Create strings for the nested structure
        Str deep_message = StrInitFromZstr("deep", &alloc);
        Str test_name    = StrInitFromZstr("test", &alloc);
    
        JW_OBJ(json, {
    
        // Create strings and push them properly
        Str str1 = StrInitFromZstr("a", &alloc);
        Str str2 = StrInitFromZstr("b", &alloc);
        Str str3 = StrInitFromZstr("c", &alloc);
        // Create strings and push them properly
        Str str1 = StrInitFromZstr("a", &alloc);
        Str str2 = StrInitFromZstr("b", &alloc);
        Str str3 = StrInitFromZstr("c", &alloc);
        Str str1 = StrInitFromZstr("a", &alloc);
        Str str2 = StrInitFromZstr("b", &alloc);
        Str str3 = StrInitFromZstr("c", &alloc);
    
        VecPushBack(&strings, str1);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str     json = StrInitFromZstr("{\"test\": \"value\"}", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"id\": 12345, \"name\": \"test\", \"active\": true, \"score\": 98.5}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        bool success = true;
        Str  json    = StrInitFromZstr(
            "{\"user\": {\"id\": 123, \"profile\": {\"name\": \"Alice\", \"age\": 30}}, \"status\": \"active\"}",
            &alloc
    
        bool success = true;
        Str  json    = StrInitFromZstr(
            "{\"company\": {\"departments\": {\"engineering\": {\"head\": \"John\", \"count\": 25, \"budget\": 150000.0}}, "
                "\"name\": \"TechCorp\"}}",
    
        bool success = true;
        Str  json    = StrInitFromZstr(
            "{\"functions\": {\"12345\": {\"67890\": {\"distance\": 0.85, \"name\": \"main\"}}, \"54321\": {\"98765\": "
                "{\"distance\": 0.92, \"name\": \"helper\"}}}}",
    
        bool success = true;
        Str  json    = StrInitFromZstr(
            "{\"status\": true, \"message\": \"Success\", \"data\": {\"12345\": {\"67890\": {\"distance\": 0.85, "
                "\"nearest_neighbor_analysis_id\": 999, \"nearest_neighbor_binary_id\": 888, "
    
        bool    success = true;
        Str     json = StrInitFromZstr("{\"id\": 12345, \"name\": \"test_func\", \"size\": 1024, \"vaddr\": 4096}", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"id\": 54321, \"name\": \"test_model\"}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        bool success = true;
        Str  json    = StrInitFromZstr(
            "{\"binary_id\": 888, \"binary_name\": \"test_binary\", \"analysis_id\": 999, \"sha256\": \"abc123\", "
                "\"created_at\": \"2024-04-01\", \"model_id\": 12345, \"model_name\": \"test_model\", \"owned_by\": \"user1\"}",
    
        bool success = true;
        Str  json    = StrInitFromZstr(
            "{\"status\": true, \"message\": \"Success\", \"data\": {\"12345\": {\"67890\": {\"distance\": 0.85, "
                "\"nearest_neighbor_analysis_id\": 999, \"nearest_neighbor_binary_id\": 888, "
    
        bool success = true;
        Str  json    = StrInitFromZstr(
            "{\"status\": true, \"message\": \"Success\", \"data\": {\"12345\": {\"67890\": {\"distance\": 0.85, "
                "\"nearest_neighbor_analysis_id\": 999, \"nearest_neighbor_binary_id\": 888, "
    bool compare_json_output(const Str *output, Zstr expected, DefaultAllocator *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  json    = StrInit(&alloc);
    
        Str name = StrInitFromZstr("Alice", &alloc);
        Str city = StrInitFromZstr("New York", &alloc);
    
        Str name = StrInitFromZstr("Alice", &alloc);
        Str city = StrInitFromZstr("New York", &alloc);
    
        JW_OBJ(json, {
        Str  json    = StrInit(&alloc);
    
        Person person = {1001, StrInitFromZstr("Bob", &alloc), 25, true, 50000.0};
    
        JW_OBJ(json, {
        Str  json    = StrInit(&alloc);
    
        Config config = {false, 30, StrInitFromZstr("INFO", &alloc)};
    
        JW_OBJ(json, {
    
        // Create strings and push them properly
        Str lang1 = StrInitFromZstr("C", &alloc);
        Str lang2 = StrInitFromZstr("Python", &alloc);
        Str lang3 = StrInitFromZstr("Rust", &alloc);
        // Create strings and push them properly
        Str lang1 = StrInitFromZstr("C", &alloc);
        Str lang2 = StrInitFromZstr("Python", &alloc);
        Str lang3 = StrInitFromZstr("Rust", &alloc);
        Str lang1 = StrInitFromZstr("C", &alloc);
        Str lang2 = StrInitFromZstr("Python", &alloc);
        Str lang3 = StrInitFromZstr("Rust", &alloc);
    
        VecPushBack(&languages, lang1);
            bool active;
        } data = {
            {StrInitFromZstr("Charlie", &alloc), StrInitFromZstr("charlie@example.com", &alloc)},
            true
        };
        SimpleProduct product = {0};
        product.id            = 12345;
        product.name          = StrInitFromZstr("Laptop", &alloc);
        product.price         = 999.99;
        product.tags          = VecInitWithDeepCopyT(product.tags, NULL, StrDeinit, &alloc);
    
        // Create strings and push them properly
        Str tag1 = StrInitFromZstr("electronics", &alloc);
        Str tag2 = StrInitFromZstr("computers", &alloc);
        Str tag3 = StrInitFromZstr("portable", &alloc);
        // Create strings and push them properly
        Str tag1 = StrInitFromZstr("electronics", &alloc);
        Str tag2 = StrInitFromZstr("computers", &alloc);
        Str tag3 = StrInitFromZstr("portable", &alloc);
        Str tag1 = StrInitFromZstr("electronics", &alloc);
        Str tag2 = StrInitFromZstr("computers", &alloc);
        Str tag3 = StrInitFromZstr("portable", &alloc);
    
        VecPushBack(&product.tags, tag1);
    
        // Test completely empty object
        Str     json1 = StrInitFromZstr("{}", &alloc);
        StrIter si1   = StrIterFromStr(json1);
    
        // Test empty object with whitespace
        Str     json2 = StrInitFromZstr("  {   }  ", &alloc);
        StrIter si2   = StrIterFromStr(json2);
    
        // Test completely empty array
        Str     json1 = StrInitFromZstr("{\"items\":[]}", &alloc);
        StrIter si1   = StrIterFromStr(json1);
    
        // Test empty array with whitespace
        Str     json2 = StrInitFromZstr("{\"data\": [  ] }", &alloc);
        StrIter si2   = StrIterFromStr(json2);
        bool success = true;
    
        Str     json = StrInitFromZstr("{\"name\":\"\",\"description\":\"\"}", &alloc);
        StrIter si   = StrIterFromStr(json);
        bool success = true;
    
        Str     json = StrInitFromZstr("{\"temp\":-25,\"balance\":-1000.50,\"delta\":-0.001}", &alloc);
        StrIter si   = StrIterFromStr(json);
        bool success = true;
    
        Str json = StrInitFromZstr(
            "{\"big_int\":9223372036854775807,\"big_float\":1.7976931348623157e+308,\"small_float\":2.2250738585072014e-"
            "308}",
        bool success = true;
    
        Str     json = StrInitFromZstr("{\"int_zero\":0,\"float_zero\":0.0,\"bool_false\":false}", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        // Test with various special characters that might be problematic
        Str json = StrInitFromZstr(
            "{\"path\":\"C:\\\\Program Files\\\\App\",\"message\":\"Hello, "
            "\\\"World\\\"!\",\"data\":\"line1\\nline2\\ttab\"}",
        // In a C source literal each backslash is doubled, so e.g. the JSON token
        // `\n` is spelled "\\n" here.
        Str json = StrInitFromZstr(
            "{"
            "\"quote\":\"\\\"\","
    
        // Test with lots of different whitespace patterns
        Str     json = StrInitFromZstr("  {\n\t\"name\"  :  \"test\"  ,\n  \"value\": 42\t,\"flag\"\n:\ntrue\n}\t", &alloc);
        StrIter si   = StrIterFromStr(json);
        bool success = true;
    
        Str     json = StrInitFromZstr("{\"outer\":{},\"list\":[],\"deep\":{\"inner\":{}}}", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        Str json =
            StrInitFromZstr("{\"empty_obj\":{},\"filled_obj\":{\"x\":1},\"empty_arr\":[],\"filled_arr\":[1,2]}", &alloc);
        StrIter si = StrIterFromStr(json);
    
        // Using smaller values that are safer to work with
        Str json   = StrInitFromZstr("{\"max_int\":2147483647,\"min_int\":-2147483648,\"one\":1,\"minus_one\":-1}", &alloc);
        StrIter si = StrIterFromStr(json);
    
        Str json =
            StrInitFromZstr("{\"tiny\":0.000001,\"huge\":999999.999999,\"zero\":0.0,\"negative_tiny\":-0.000001}", &alloc);
        StrIter si = StrIterFromStr(json);
        // JReadBool on a token that starts like a bool but isn't.
        {
            Str     j   = StrInitFromZstr("tru3", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = true;
        }
        {
            Str     j   = StrInitFromZstr("fXlse", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = true;
        // Input too short to spell a bool -- must fail (the >= length guard).
        {
            Str     j   = StrInitFromZstr("tr", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = true;
        // JReadNull on a 'n...' token that isn't "null".
        {
            Str     j       = StrInitFromZstr("nuXX", &alloc);
            StrIter si      = StrIterFromStr(j);
            bool    is_null = true;
    
        {
            Str     j   = StrInitFromZstr("true rest", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = false;
        }
        {
            Str     j   = StrInitFromZstr("false rest", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = true;
        }
        {
            Str     j       = StrInitFromZstr("null rest", &alloc);
            StrIter si      = StrIterFromStr(j);
            bool    is_null = false;
        // boundary case for the minimum-length guard.
        {
            Str     j   = StrInitFromZstr("true", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = false;
        }
        {
            Str     j   = StrInitFromZstr("false", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = true;
        }
        {
            Str     j       = StrInitFromZstr("null", &alloc);
            StrIter si      = StrIterFromStr(j);
            bool    is_null = false;
        Zstr cases[] = {"\"\\u\"", "\"\\u12\"", "\"ab\\u\""};
        for (u64 i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
            Str     j   = StrInitFromZstr(cases[i], &alloc);
            StrIter si  = StrIterFromStr(j);
            Str     out = StrInit(&alloc);
        // reject -> iterator rewinds to start, output cleared.
        {
            Str     j   = StrInitFromZstr("\"a\\u00e9b\"", &alloc);
            StrIter si  = StrIterFromStr(j);
            Str     out = StrInit(&alloc);
        // is specific to \u, not a blanket failure.
        {
            Str     j   = StrInitFromZstr("\"plain\"", &alloc);
            StrIter si  = StrIterFromStr(j);
            Str     out = StrInit(&alloc);
        bool             success = true;
    
        Str json = StrInitFromZstr(
            "{"
            "\"u_str\":\"ignore\","
    
        for (u64 i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
            Str     json = StrInitFromZstr(cases[i], &alloc);
            StrIter si   = StrIterFromStr(json);
            i64     a    = 0;
    
        {
            Str     j   = StrInitFromZstr("-12345", &alloc);
            StrIter si  = StrIterFromStr(j);
            i64     v   = 0;
        {
            // Positive control: a sign-flip mutation would make this negative.
            Str     j   = StrInitFromZstr("12345", &alloc);
            StrIter si  = StrIterFromStr(j);
            i64     v   = 0;
        }
        {
            Str     j   = StrInitFromZstr("-2.5", &alloc);
            StrIter si  = StrIterFromStr(j);
            f64     v   = 0.0;
        bool             success = true;
    
        Str     j   = StrInitFromZstr("7", &alloc);
        StrIter si  = StrIterFromStr(j);
        f64     val = 0.0;
        bool             success = true;
    
        Str     j   = StrInitFromZstr("13", &alloc);
        StrIter si  = StrIterFromStr(j);
        f64     val = 0.0;
        // 'n' lead-in but not "null": ZstrCompareN fails -> *is_null stays the
        // value the unconditional clear set it to, which must be false.
        Str     j       = StrInitFromZstr("nuII", &alloc);
        StrIter si      = StrIterFromStr(j);
        bool    is_null = true; // poison: must be overwritten to false
        bool             success = true;
    
        Str     j       = StrInitFromZstr("null", &alloc);
        StrIter si      = StrIterFromStr(j);
        bool    is_null = false;
            bool enabled;
            Str  message;
        } original = {42, 25.5, true, StrInitFromZstr("hello world", &alloc)};
    
        // Write to JSON
        } original = {
            StrInit(&alloc),
            StrInitFromZstr("hello", &alloc),
            StrInitFromZstr("hello world with spaces", &alloc),
            StrInitFromZstr("special: !@#$%^&*()", &alloc)
            StrInit(&alloc),
            StrInitFromZstr("hello", &alloc),
            StrInitFromZstr("hello world with spaces", &alloc),
            StrInitFromZstr("special: !@#$%^&*()", &alloc)
        };
            StrInitFromZstr("hello", &alloc),
            StrInitFromZstr("hello world with spaces", &alloc),
            StrInitFromZstr("special: !@#$%^&*()", &alloc)
        };
    
        // Create strings and push them properly
        Str str1 = StrInitFromZstr("first", &alloc);
        Str str2 = StrInitFromZstr("second", &alloc);
        Str str3 = StrInitFromZstr("", &alloc);
        // Create strings and push them properly
        Str str1 = StrInitFromZstr("first", &alloc);
        Str str2 = StrInitFromZstr("second", &alloc);
        Str str3 = StrInitFromZstr("", &alloc);
        Str str4 = StrInitFromZstr("last", &alloc);
        Str str1 = StrInitFromZstr("first", &alloc);
        Str str2 = StrInitFromZstr("second", &alloc);
        Str str3 = StrInitFromZstr("", &alloc);
        Str str4 = StrInitFromZstr("last", &alloc);
        Str str2 = StrInitFromZstr("second", &alloc);
        Str str3 = StrInitFromZstr("", &alloc);
        Str str4 = StrInitFromZstr("last", &alloc);
    
        VecPushBack(&original_strings, str1);
    
        // Original data
        TestPerson original_person = {12345, StrInitFromZstr("John Doe", &alloc), 30, true, 75000.50};
    
        // Write to JSON
        ComplexData original    = {0};
        original.user.id        = 999;
        original.user.name      = StrInitFromZstr("Complex User", &alloc);
        original.user.age       = 25;
        original.user.is_active = true;
        original.config.debug_mode = false;
        original.config.timeout    = 30;
        original.config.log_level  = StrInitFromZstr("INFO", &alloc);
        original.config.features   = VecInitWithDeepCopyT(original.config.features, NULL, StrDeinit, &alloc);
    
        // Create strings and push them properly
        Str feature1 = StrInitFromZstr("auth", &alloc);
        Str feature2 = StrInitFromZstr("logging", &alloc);
        // Create strings and push them properly
        Str feature1 = StrInitFromZstr("auth", &alloc);
        Str feature2 = StrInitFromZstr("logging", &alloc);
    
        VecPushBack(&original.config.features, feature1);
        DefaultAllocator alloc = DefaultAllocatorInit();
        KvConfig         cfg   = KvConfigInit(&alloc);
        Str              text  = StrInitFromZstr(src, &alloc);
        StrIter          input = StrIterFromStr(text);
        Str             *got   = NULL;
        DefaultAllocator alloc  = DefaultAllocatorInit();
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("flag = yes\n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        bool             v      = false;
    static bool test_kv_skipline_consumes_lf(void) {
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              src    = StrInitFromZstr("xy\nZ", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigSkipLine(input);
    static bool test_kv_skipline_consumes_one_line_end(void) {
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              src    = StrInitFromZstr("a\n\nZ", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigSkipLine(input);
        DefaultAllocator alloc  = DefaultAllocatorInit();
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("a=1\r\nb=2\r\n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigParse(input, &cfg);
        DefaultAllocator alloc  = DefaultAllocatorInit();
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("k = # just a comment\n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        Str             *v      = NULL;
        // skipped (fine) but the branch also mis-handles the bare-newline
        // case. Pair with a trailing-comment line so both branches matter.
        Str     src    = StrInitFromZstr("a = 1 # c\nb = 2\n", &alloc);
        StrIter input  = StrIterFromStr(src);
        StrIter si     = KvConfigParse(input, &cfg);
        DefaultAllocator alloc  = DefaultAllocatorInit();
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("k = \"v\" junk\n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigParse(input, &cfg);
        DefaultAllocator alloc  = DefaultAllocatorInit();
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("   k = v\n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigParse(input, &cfg);
        KvConfig         cfg   = KvConfigInit(&alloc);
        // A line with only spaces, then a real pair.
        Str     src    = StrInitFromZstr("   \nk = v\n", &alloc);
        StrIter input  = StrIterFromStr(src);
        StrIter si     = KvConfigParse(input, &cfg);
        DefaultAllocator alloc  = DefaultAllocatorInit();
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("k = v\n   \n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigParse(input, &cfg);
        DefaultAllocator alloc = DefaultAllocatorInit();
        KvConfig         cfg   = KvConfigInit(&alloc);
        Str              src   = StrInitFromZstr(
            "host = localhost\n"
                           "port = 8080\n"
        DefaultAllocator alloc = DefaultAllocatorInit();
        KvConfig         cfg   = KvConfigInit(&alloc);
        Str              src   = StrInitFromZstr(
            "# comment line\n"
                           "path = \"/srv/my app\"   # keep spaces in quotes\n"
        DefaultAllocator alloc       = DefaultAllocatorInit();
        KvConfig         cfg         = KvConfigInit(&alloc);
        Str              src         = StrInitFromZstr("host = localhost\n", &alloc);
        StrIter          input       = StrIterFromStr(src);
        Str              host_copy   = StrInit(&alloc);
        DefaultAllocator alloc = DefaultAllocatorInit();
        KvConfig         cfg   = KvConfigInit(&alloc);
        Str              src   = StrInitFromZstr(
            "workers = 16\n"
                           "pi = 3.14159\n"
        DefaultAllocator alloc = DefaultAllocatorInit();
        KvConfig         cfg   = KvConfigInit(&alloc);
        Str              src   = StrInitFromZstr(
            "valid = yes\n"
                           "broken line\n"
        DefaultAllocator alloc = DefaultAllocatorInit();
        KvConfig         cfg   = KvConfigInit(&alloc);
        Str              src   = StrInitFromZstr(
            "t1 = true\n"
                           "t2 = YES\n"
        DefaultAllocator alloc = DefaultAllocatorInit();
        KvConfig         cfg   = KvConfigInit(&alloc);
        Str              src   = StrInitFromZstr(
            "name = misra\n"
                           "workers = 16\n"
        (void)KvConfigParse(input, &cfg);
    
        Str k_name    = StrInitFromZstr("name", &alloc);
        Str k_missing = StrInitFromZstr("missing", &alloc);
        Str k_workers = StrInitFromZstr("workers", &alloc);
    
        Str k_name    = StrInitFromZstr("name", &alloc);
        Str k_missing = StrInitFromZstr("missing", &alloc);
        Str k_workers = StrInitFromZstr("workers", &alloc);
        Str k_ratio   = StrInitFromZstr("ratio", &alloc);
        Str k_name    = StrInitFromZstr("name", &alloc);
        Str k_missing = StrInitFromZstr("missing", &alloc);
        Str k_workers = StrInitFromZstr("workers", &alloc);
        Str k_ratio   = StrInitFromZstr("ratio", &alloc);
        Str k_enabled = StrInitFromZstr("enabled", &alloc);
        Str k_missing = StrInitFromZstr("missing", &alloc);
        Str k_workers = StrInitFromZstr("workers", &alloc);
        Str k_ratio   = StrInitFromZstr("ratio", &alloc);
        Str k_enabled = StrInitFromZstr("enabled", &alloc);
        Str k_workers = StrInitFromZstr("workers", &alloc);
        Str k_ratio   = StrInitFromZstr("ratio", &alloc);
        Str k_enabled = StrInitFromZstr("enabled", &alloc);
    
        // contains via Str*
        DefaultAllocator alloc = DefaultAllocatorInit();
        KvConfig         cfg   = KvConfigInit(&alloc);
        Str              src   = StrInitFromZstr(
            "a = value one     # trailing comment\n"
                           "b = value two\t\t; tab-spaced comment\n"
        DefaultAllocator alloc = DefaultAllocatorInit();
        KvConfig         cfg   = KvConfigInit(&alloc);
        Str              src   = StrInitFromZstr(
            "host = localhost\r\n"
                           "port = 8080\r\n"
    
        KvConfig cfg   = KvConfigInit(base);
        Str      text  = StrInitFromZstr(src, base);
        StrIter  input = StrIterFromStr(text);
        // NEW buffer and the old one to be released at L215. A long-ish value
        // keeps both buffers off any small-string fast path.
        Str     text  = StrInitFromZstr("key =   spaced-out-value-here   \n", adbg);
        StrIter input = StrIterFromStr(text);
    
        KvConfig cfg   = KvConfigInit(adbg);
        Str      text  = StrInitFromZstr("a-reasonably-long-config-key = v\n", adbg);
        StrIter  input = StrIterFromStr(text);
    
        KvConfig cfg   = KvConfigInit(adbg);
        Str      text  = StrInitFromZstr("k = a-reasonably-long-config-value\n", adbg);
        StrIter  input = StrIterFromStr(text);
    
        KvConfig cfg   = KvConfigInit(adbg);
        Str      text  = StrInitFromZstr("present-config-key = value\n", adbg);
        StrIter  input = StrIterFromStr(text);
Last updated on