Skip to content

StrClear

Description

Set the string length to 0 while keeping the allocated capacity, so subsequent writes can reuse the buffer without reallocation. See VecClear for the full SUCCESS/FAILURE contract.

Usage example (Cross-references)

Usage examples (Cross-references)
    Str *StrError(i32 eno, Str *err_str) {
        ValidateStr(err_str);
        StrClear(err_str);
        StrAppendFmt(err_str, "{} (errno {})", errno_description(eno), eno);
        return err_str;
            case ARG_KIND_STR : {
                Str *s = (Str *)target;
                StrClear(s);
                StrPushBackMany(s, value);
                return true;
            return false;
        }
        StrClear(o);
        return str_append_fmt(o, fmt, args, argc);
    }
        }
    
        StrClear(str);
    
        if (config->use_prefix) {
        }
    
        StrClear(str);
    
        if (isnan_f64(value)) {
        if (StrLen(key) == 0) {
            LOG_ERROR("Expected config key");
            StrClear(key);
            return saved_si;
        }
                    if (!StrIterPeek(&si, &c)) {
                        LOG_ERROR("Unexpected end of quoted config value");
                        StrClear(value);
                        return saved_si;
                    }
    
            LOG_ERROR("Missing closing quote in config value");
            StrClear(value);
            return saved_si;
        }
        if (!StrIterPeek(&si, &c) || (c != '=' && c != ':')) {
            LOG_ERROR("Expected '=' or ':' after config key");
            StrClear(key);
            StrClear(value);
            return saved_si;
            LOG_ERROR("Expected '=' or ':' after config key");
            StrClear(key);
            StrClear(value);
            return saved_si;
        }
    
        if (StrIterIndex(&si) == StrIterIndex(&saved_si)) {
            StrClear(key);
            StrClear(value);
            return saved_si;
        if (StrIterIndex(&si) == StrIterIndex(&saved_si)) {
            StrClear(key);
            StrClear(value);
            return saved_si;
        }
        } else if (c != '\n') {
            LOG_ERROR("Unexpected trailing characters after config value");
            StrClear(key);
            StrClear(value);
            return saved_si;
            LOG_ERROR("Unexpected trailing characters after config value");
            StrClear(key);
            StrClear(value);
            return saved_si;
        } else {
                        if (!StrIterPeek(&si, &c)) {
                            LOG_ERROR("Unexpected end of string.");
                            StrClear(str);
                            return saved_si;
                        }
                                // a well-formed \uXXXX.
                                LOG_ERROR("Unsupported \\uXXXX escape in JSON string; rejecting.");
                                StrClear(str);
                                return saved_si;
                            default :
                                LOG_ERROR("Invalid JSON object key string.");
                                StrClear(str);
                                return saved_si;
                        }
        Str s = StrInitFromZstr("hello", &local);
    
        StrClear(&s);
    
        bool result = (StrLen(&s) == 0) && (ZstrCompare(StrBegin(&s), "") == 0);
        Str s = StrInitFromZstr("x", &local);
    
        StrClear(&s);
    
        bool result = (StrLen(&s) == 0) && (ZstrCompare(StrBegin(&s), "") == 0);
        StrAppendFmt(&output, "");
        success = success && (StrLen(&output) == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "Hello, world!");
        StrAppendFmt(&output, "Hello, world!");
        success = success && (ZstrCompare(StrBegin(&output), "Hello, world!") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{​{Hello}}");
        StrAppendFmt(&output, "{​{Hello}}");
        success = success && (ZstrCompare(StrBegin(&output), "{Hello}") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{​{{​{");
        StrAppendFmt(&output, "{}", str);
        success = success && (ZstrCompare(StrBegin(&output), "Hello") == 0);
        StrClear(&output);
    
        Zstr empty = "";
        StrAppendFmt(&output, "{}", empty);
        success = success && (StrLen(&output) == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{>10}", str);
        StrAppendFmt(&output, "{>10}", str);
        success = success && (ZstrCompare(StrBegin(&output), "     Hello") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{<10}", str);
        StrAppendFmt(&output, "{<10}", str);
        success = success && (ZstrCompare(StrBegin(&output), "Hello     ") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{^10}", str);
        StrAppendFmt(&output, "{^10}", str);
        success = success && (ZstrCompare(StrBegin(&output), "  Hello   ") == 0);
        StrClear(&output);
    
        Str s = StrInitFromZstr("World", &alloc);
        StrAppendFmt(&output, "{}", i8_val);
        success = success && (ZstrCompare(StrBegin(&output), "-42") == 0);
        StrClear(&output);
    
        i16 i16_val = -1234;
        StrAppendFmt(&output, "{}", i16_val);
        success = success && (ZstrCompare(StrBegin(&output), "-1234") == 0);
        StrClear(&output);
    
        i32 i32_val = -123456;
        StrAppendFmt(&output, "{}", i32_val);
        success = success && (ZstrCompare(StrBegin(&output), "-123456") == 0);
        StrClear(&output);
    
        i64 i64_val = -1234567890LL;
        StrAppendFmt(&output, "{}", i64_val);
        success = success && (ZstrCompare(StrBegin(&output), "-1234567890") == 0);
        StrClear(&output);
    
        u8 u8_val = 42;
        StrAppendFmt(&output, "{}", u8_val);
        success = success && (ZstrCompare(StrBegin(&output), "42") == 0);
        StrClear(&output);
    
        u16 u16_val = 1234;
        StrAppendFmt(&output, "{}", u16_val);
        success = success && (ZstrCompare(StrBegin(&output), "1234") == 0);
        StrClear(&output);
    
        u32 u32_val = 123456;
        StrAppendFmt(&output, "{}", u32_val);
        success = success && (ZstrCompare(StrBegin(&output), "123456") == 0);
        StrClear(&output);
    
        u64 u64_val = 1234567890ULL;
        StrAppendFmt(&output, "{}", u64_val);
        success = success && (ZstrCompare(StrBegin(&output), "1234567890") == 0);
        StrClear(&output);
    
        i8 i8_max = 127;
        StrAppendFmt(&output, "{}", i8_max);
        success = success && (ZstrCompare(StrBegin(&output), "127") == 0);
        StrClear(&output);
    
        i8 i8_min = -128;
        StrAppendFmt(&output, "{}", i8_min);
        success = success && (ZstrCompare(StrBegin(&output), "-128") == 0);
        StrClear(&output);
    
        u8 u8_max = 255;
        StrAppendFmt(&output, "{}", u8_max);
        success = success && (ZstrCompare(StrBegin(&output), "255") == 0);
        StrClear(&output);
    
        u8 u8_min = 0;
        StrAppendFmt(&output, "{x}", val);
        success = success && (ZstrCompare(StrBegin(&output), "0xdeadbeef") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{X}", val);
        StrAppendFmt(&output, "{}", f32_val);
        success = success && (ZstrCompare(StrBegin(&output), "3.141590") == 0);
        StrClear(&output);
    
        f64 f64_val = 2.71828;
        StrAppendFmt(&output, "{.2}", val);
        success = success && (ZstrCompare(StrBegin(&output), "3.14") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{.0}", val);
        StrAppendFmt(&output, "{.0}", val);
        success = success && (ZstrCompare(StrBegin(&output), "3") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{.10}", val);
        StrAppendFmt(&output, "{}", pos_inf);
        success = success && (ZstrCompare(StrBegin(&output), "inf") == 0);
        StrClear(&output);
    
        f64 neg_inf = -F64_INFINITY;
        StrAppendFmt(&output, "{}", neg_inf);
        success = success && (ZstrCompare(StrBegin(&output), "-inf") == 0);
        StrClear(&output);
    
        f64 nan_val = F64_NAN;
        StrAppendFmt(&output, "{5}", val);
        success = success && (ZstrCompare(StrBegin(&output), "   42") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{<5}", val);
        StrAppendFmt(&output, "{<5}", val);
        success = success && (ZstrCompare(StrBegin(&output), "42   ") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{^5}", val);
        StrAppendFmt(&output, "{^5}", val);
        success = success && (ZstrCompare(StrBegin(&output), " 42  ") == 0);
        StrClear(&output);
    
        Zstr str = "abc";
        StrAppendFmt(&output, "{5}", str);
        success = success && (ZstrCompare(StrBegin(&output), "  abc") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{<5}", str);
        StrAppendFmt(&output, "{<5}", str);
        success = success && (ZstrCompare(StrBegin(&output), "abc  ") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{^5}", str);
        StrAppendFmt(&output, "{} {} {}", hello, num, pi);
        success = success && (ZstrCompare(StrBegin(&output), "Hello 42 3.140000") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{} {} {}", pi, hello, num);
        StrAppendFmt(&output, "{c}", mixed_case);
        success = success && (ZstrCompare(StrBegin(&output), "MiXeD CaSe") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", mixed_case);
        StrAppendFmt(&output, "{a}", mixed_case);
        success = success && (ZstrCompare(StrBegin(&output), "mixed case") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", mixed_case);
        StrAppendFmt(&output, "{A}", mixed_case);
        success = success && (ZstrCompare(StrBegin(&output), "MIXED CASE") == 0);
        StrClear(&output);
    
        Str s = StrInitFromZstr("MiXeD CaSe", &alloc);
        StrAppendFmt(&output, "{c}", s);
        success = success && (ZstrCompare(StrBegin(&output), "MiXeD CaSe") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", s);
        StrAppendFmt(&output, "{a}", s);
        success = success && (ZstrCompare(StrBegin(&output), "mixed case") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", s);
        StrAppendFmt(&output, "{A}", s);
        success = success && (ZstrCompare(StrBegin(&output), "MIXED CASE") == 0);
        StrClear(&output);
    
        u8 upper_char = 'M';
        StrAppendFmt(&output, "{c}", upper_char);
        success = success && (ZstrCompare(StrBegin(&output), "M") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", upper_char);
        StrAppendFmt(&output, "{a}", upper_char);
        success = success && (ZstrCompare(StrBegin(&output), "m") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", lower_char);
        StrAppendFmt(&output, "{A}", lower_char);
        success = success && (ZstrCompare(StrBegin(&output), "M") == 0);
        StrClear(&output);
    
        u16 u16_value = ('A' << 8) | 'B'; // big-endian "AB"
        StrAppendFmt(&output, "{c}", u16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'A' && StrBegin(&output)[1] == 'B');
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", u16_value);
        StrAppendFmt(&output, "{a}", u16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'a' && StrBegin(&output)[1] == 'b');
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", u16_value);
        StrAppendFmt(&output, "{A}", u16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'A' && StrBegin(&output)[1] == 'B');
        StrClear(&output);
    
        i16 i16_value = ('C' << 8) | 'd'; // big-endian "Cd"
        StrAppendFmt(&output, "{c}", i16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'C' && StrBegin(&output)[1] == 'd');
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", i16_value);
        StrAppendFmt(&output, "{a}", i16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'c' && StrBegin(&output)[1] == 'd');
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", i16_value);
        StrAppendFmt(&output, "{A}", i16_value);
        success = success && (StrLen(&output) == 2 && StrBegin(&output)[0] == 'C' && StrBegin(&output)[1] == 'D');
        StrClear(&output);
    
        u32 u32_value = ('E' << 24) | ('f' << 16) | ('G' << 8) | 'h'; // big-endian "EfGh"
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'E' && StrBegin(&output)[1] == 'f' &&
                              StrBegin(&output)[2] == 'G' && StrBegin(&output)[3] == 'h');
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", u32_value);
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'e' && StrBegin(&output)[1] == 'f' &&
                              StrBegin(&output)[2] == 'g' && StrBegin(&output)[3] == 'h');
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", u32_value);
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'E' && StrBegin(&output)[1] == 'F' &&
                              StrBegin(&output)[2] == 'G' && StrBegin(&output)[3] == 'H');
        StrClear(&output);
    
        i32 i32_value = ('I' << 24) | ('j' << 16) | ('K' << 8) | 'l'; // big-endian "IjKl"
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'I' && StrBegin(&output)[1] == 'j' &&
                              StrBegin(&output)[2] == 'K' && StrBegin(&output)[3] == 'l');
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", i32_value);
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'i' && StrBegin(&output)[1] == 'j' &&
                              StrBegin(&output)[2] == 'k' && StrBegin(&output)[3] == 'l');
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", i32_value);
        success = success && (StrLen(&output) == 4 && StrBegin(&output)[0] == 'I' && StrBegin(&output)[1] == 'J' &&
                              StrBegin(&output)[2] == 'K' && StrBegin(&output)[3] == 'L');
        StrClear(&output);
    
        u64 u64_value = ((u64)'M' << 56) | ((u64)'n' << 48) | ((u64)'O' << 40) | ((u64)'p' << 32) | ('Q' << 24) |
                              StrBegin(&output)[2] == 'O' && StrBegin(&output)[3] == 'p' && StrBegin(&output)[4] == 'Q' &&
                              StrBegin(&output)[5] == 'r' && StrBegin(&output)[6] == 'S' && StrBegin(&output)[7] == 't');
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", u64_value);
                              StrBegin(&output)[2] == 'o' && StrBegin(&output)[3] == 'p' && StrBegin(&output)[4] == 'q' &&
                              StrBegin(&output)[5] == 'r' && StrBegin(&output)[6] == 's' && StrBegin(&output)[7] == 't');
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", u64_value);
                              StrBegin(&output)[2] == 'O' && StrBegin(&output)[3] == 'P' && StrBegin(&output)[4] == 'Q' &&
                              StrBegin(&output)[5] == 'R' && StrBegin(&output)[6] == 'S' && StrBegin(&output)[7] == 'T');
        StrClear(&output);
    
        i64 i64_value = ((i64)'U' << 56) | ((i64)'v' << 48) | ((i64)'W' << 40) | ((i64)'x' << 32) | ('Y' << 24) |
                              StrBegin(&output)[2] == 'W' && StrBegin(&output)[3] == 'x' && StrBegin(&output)[4] == 'Y' &&
                              StrBegin(&output)[5] == 'z' && StrBegin(&output)[6] == '1' && StrBegin(&output)[7] == '2');
        StrClear(&output);
    
        StrAppendFmt(&output, "{a}", i64_value);
                              StrBegin(&output)[2] == 'w' && StrBegin(&output)[3] == 'x' && StrBegin(&output)[4] == 'y' &&
                              StrBegin(&output)[5] == 'z' && StrBegin(&output)[6] == '1' && StrBegin(&output)[7] == '2');
        StrClear(&output);
    
        StrAppendFmt(&output, "{A}", i64_value);
        StrAppendFmt(&output, "{}", bv1);
        success = success && (ZstrCompare(StrBegin(&output), "10110") == 0);
        StrClear(&output);
    
        BitVec bv_empty = BitVecInit(alloc_base);
        StrAppendFmt(&output, "{}", bv_empty);
        success = success && (StrLen(&output) == 0);
        StrClear(&output);
    
        BitVec bv2 = BitVecFromInteger(0xABCD, 16, alloc_base);
        StrAppendFmt(&output, "{x}", bv2);
        success = success && (ZstrCompare(StrBegin(&output), "0xabcd") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{X}", bv2);
        StrAppendFmt(&output, "{X}", bv2);
        success = success && (ZstrCompare(StrBegin(&output), "0xABCD") == 0);
        StrClear(&output);
    
        BitVec bv3 = BitVecFromInteger(0755, 10, alloc_base);
        StrAppendFmt(&output, "{o}", bv3);
        success = success && (ZstrCompare(StrBegin(&output), "0o755") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{>10}", bv1);
        StrAppendFmt(&output, "{>10}", bv1);
        success = success && (ZstrCompare(StrBegin(&output), "     10110") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{<10}", bv1);
        StrAppendFmt(&output, "{<10}", bv1);
        success = success && (ZstrCompare(StrBegin(&output), "10110     ") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{^10}", bv1);
        StrAppendFmt(&output, "{^10}", bv1);
        success = success && (ZstrCompare(StrBegin(&output), "  10110   ") == 0);
        StrClear(&output);
    
        BitVec bv_zero = BitVecFromInteger(0, 1, alloc_base);
        StrAppendFmt(&output, "{x}", bv_zero);
        success = success && (ZstrCompare(StrBegin(&output), "0x0") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{o}", bv_zero);
        StrAppendFmt(&output, "{o}", bv_zero);
        success = success && (ZstrCompare(StrBegin(&output), "0o0") == 0);
        StrClear(&output);
    
        BitVecDeinit(&bv1);
        StrAppendFmt(&output, "{}", big_dec);
        success = success && (ZstrCompare(StrBegin(&output), "123456789012345678901234567890") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{x}", hex_val);
        StrAppendFmt(&output, "{x}", hex_val);
        success = success && (ZstrCompare(StrBegin(&output), "deadbeefcafebabe1234") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{X}", hex_val);
        StrAppendFmt(&output, "{X}", hex_val);
        success = success && (ZstrCompare(StrBegin(&output), "DEADBEEFCAFEBABE1234") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{b}", bin_val);
        StrAppendFmt(&output, "{b}", bin_val);
        success = success && (ZstrCompare(StrBegin(&output), "10100011") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{o}", oct_val);
        StrAppendFmt(&output, "{o}", oct_val);
        success = success && (ZstrCompare(StrBegin(&output), "755") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{>34}", big_dec);
        StrAppendFmt(&output, "{}", exact);
        success = success && (ZstrCompare(StrBegin(&output), "1234567890.012345") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{e}", sci);
        StrAppendFmt(&output, "{e}", sci);
        success = success && (ZstrCompare(StrBegin(&output), "1.234567e+04") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{E}", sci);
        StrAppendFmt(&output, "{E}", sci);
        success = success && (ZstrCompare(StrBegin(&output), "1.234567E+04") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{.3}", short_v);
        StrAppendFmt(&output, "{.3}", short_v);
        success = success && (ZstrCompare(StrBegin(&output), "1.200") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{>18}", sci);
        StrAppendFmt(&out, "{c}", esc);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x1b") == 0);
        StrClear(&out);
    
        // 0x07 (BEL) -> "\x07".
        StrAppendFmt(&out, "{c}", bel);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x07") == 0);
        StrClear(&out);
    
        // 0xFF with caps spec -> uppercase hex digits "\xFF".
        StrAppendFmt(&out, "{A}", hi);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\xFF") == 0);
        StrClear(&out);
    
        // 0xAB lowercase by default for {c}.
        }
        // Same, but the trailing literal is an escaped '{'.
        StrClear((Str *)&b);
        ok = ok && BufWriteFmt(&b, "{>1r}{​{", (u8)'Q');
        {
    
        DateTime utc = DateTimeFromUnixNs(base, 0);
        StrClear(&s);
        StrAppendFmt(&s, "{}", utc);
        ok = ok && ZstrCompare(StrBegin(&s), "2021-01-01T00:00:00Z") == 0;
    
        DateTime ist = DateTimeFromUnixNs(base, 19800); // +05:30
        StrClear(&s);
        StrAppendFmt(&s, "{}", ist);
        ok = ok && ZstrCompare(StrBegin(&s), "2021-01-01T05:30:00+05:30") == 0;
    
        DateTime neg = DateTimeFromUnixNs(base, -34200); // -09:30
        StrClear(&s);
        StrAppendFmt(&s, "{}", neg);
        ok = ok && ZstrCompare(StrBegin(&s), "2020-12-31T14:30:00-09:30") == 0;
    
        DateTime frac = DateTimeFromUnixNs(base + 123456789ull, 0);
        StrClear(&s);
        StrAppendFmt(&s, "{}", frac);
        ok = ok && ZstrCompare(StrBegin(&s), "2021-01-01T00:00:00.123456789Z") == 0;
        StrAppendFmt(&output, "{e}", v);
        ok = ok && (ZstrCompare(StrBegin(&output), "1.234567e+04") == 0);
        StrClear(&output);
    
        StrAppendFmt(&output, "{E}", v);
        StrAppendFmt(&out, "{5}", v); // "    7"
        ok = ok && (ZstrCompare(StrBegin(&out), "    7") == 0);
        StrClear(&out);
    
        // Left-align variant so a dropped pad is unambiguous.
        StrAppendFmt(&out, "{c}", src);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x1a") == 0);
        StrClear(&out);
        StrClear(&src);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x1a") == 0);
        StrClear(&out);
        StrClear(&src);
    
        // 0xA1: low nibble 0x01 (<10 -> '1') -- pins the low digit on the <10 side
        StrAppendFmt(&out, "{.5}", f);
        ok = ok && (ZstrCompare(StrBegin(&out), "1.20000") == 0);
        StrClear(&out);
    
        Float g = FloatFromStr("3.14", alloc_base);
        StrAppendFmt(&out, "{03}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "255") == 0);
        StrClear(&out);
    
        // Sanity: width one wider really does pad (guards against a no-op pad).
        StrAppendFmt(&out, "{3}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "abc") == 0);
        StrClear(&out);
    
        // Left/center at the exact boundary likewise add nothing.
        StrAppendFmt(&out, "{<3}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "abc") == 0);
        StrClear(&out);
    
        StrAppendFmt(&out, "{^3}", s);
        StrAppendFmt(&out, "{^3}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "abc") == 0);
        StrClear(&out);
    
        // Sanity: width 4 does pad (one space, right-aligned default).
        StrAppendFmt(&out, "{}", v); // default precision 6
        ok = ok && (ZstrCompare(StrBegin(&out), "3.140000") == 0);
        StrClear(&out);
    
        StrAppendFmt(&out, "{.2}", v); // explicit precision 2
        StrAppendFmt(&out, "{4}", s);
        bool ok = (ZstrCompare(StrBegin(&out), "  ab") == 0);
        StrClear(&out);
        // Two-digit width exercises the width loop (151) more than once.
        StrAppendFmt(&out, "{12}", s);
        StrAppendFmt(&out, "{.2}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "3.14") == 0);
        StrClear(&out);
        // multi-digit precision exercises the precision loop body (165).
        StrAppendFmt(&out, "{.4}", v);
        StrAppendFmt(&out, "{03}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "123") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{05}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "00123") == 0);
        StrAppendFmt(&out, "{3}", s);
        bool ok = (ZstrCompare(StrBegin(&out), "abc") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{5}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "  abc") == 0);
        StrAppendFmt(&out, "{}", s); // width 0 default
        bool ok = (ZstrCompare(StrBegin(&out), "hi") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{5}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "   hi") == 0);
        StrAppendFmt(&out, "{}", s);
        bool ok = (ZstrCompare(StrBegin(&out), "hi") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{5}", s);
        ok = ok && (ZstrCompare(StrBegin(&out), "   hi") == 0);
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "42") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "    42") == 0);
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "-42") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "   -42") == 0);
        StrAppendFmt(&out, "{}", ninf);
        bool ok = (ZstrCompare(StrBegin(&out), "-inf") == 0);
        StrClear(&out);
        f64 pinf = F64_INFINITY;
        StrAppendFmt(&out, "{}", pinf);
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1.500000") == 0);
        StrClear(&out);
        // explicit precision 2 to contrast.
        StrAppendFmt(&out, "{.2}", v);
        StrAppendFmt(&out, "{.1}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1.5") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6.1}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "   1.5") == 0);
        StrAppendFmt(&out, "{e}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1.234567e+04") == 0);
        StrClear(&out);
        Float w = FloatFromStr("0.001234", ab);
        StrAppendFmt(&out, "{e}", w);
        StrAppendFmt(&out, "{.2}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "3.14") == 0);
        StrClear(&out);
        // precision longer than frac pads with zeros.
        StrAppendFmt(&out, "{.8}", v);
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "1.5") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "   1.5") == 0);
        StrAppendFmt(&out, "{}", bv);
        bool ok = (ZstrCompare(StrBegin(&out), "101") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6}", bv);
        ok = ok && (ZstrCompare(StrBegin(&out), "   101") == 0);
        StrAppendFmt(&out, "{}", v);
        bool ok = (ZstrCompare(StrBegin(&out), "42") == 0);
        StrClear(&out);
        StrAppendFmt(&out, "{6}", v);
        ok = ok && (ZstrCompare(StrBegin(&out), "    42") == 0);
        success      = success && (StrCmp(&s, &expected) == 0);
        StrDeinit(&expected);
        StrClear(&s);
    
        z = "\"Hello, World!\"";
        success      = success && (StrCmp(&name, &expected) == 0);
        StrDeinit(&expected);
        StrClear(&name);
    
        f64 val = 0.0;
        StrAppendFmt(&out, "{c}", lo_letter);
        ok = ok && (ZstrCompare(StrBegin(&out), "\\x1b") == 0);
        StrClear(&out);
    
        Zstr hi_letter = "\xb1";
    // Test StrClear function
    bool test_str_clear(void) {
        WriteFmt("Testing StrClear\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Clear the string
        StrClear(&s);
    
        // Length should now be 0, but capacity should remain
    
        // Test hexadecimal conversion (lowercase)
        StrClear(&s);
        config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
        StrFromU64(&s, 0xABCD, &config);
    
        // Test hexadecimal conversion (uppercase)
        StrClear(&s);
        config = (StrIntFormat) {.base = 16, .uppercase = true, .use_prefix = true};
        StrFromU64(&s, 0xABCD, &config);
    
        // Test binary conversion
        StrClear(&s);
        config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
        StrFromU64(&s, 42, &config);
    
        // Test octal conversion
        StrClear(&s);
        config = (StrIntFormat) {.base = 8, .uppercase = false, .use_prefix = true};
        StrFromU64(&s, 42, &config);
    
        // Test zero
        StrClear(&s);
        config = (StrIntFormat) {.base = 10, .uppercase = false};
        StrFromU64(&s, 0, &config);
    
        // Test negative decimal conversion (only decimal supports negative sign)
        StrClear(&s);
        config = (StrIntFormat) {.base = 10, .uppercase = false};
        StrFromI64(&s, -12345, &config);
    
        // Test hexadecimal conversion of negative number (negative non-decimal treated as unsigned)
        StrClear(&s);
        config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
        StrFromI64(&s, -0xABCD, &config);
    
        // Test zero
        StrClear(&s);
        config = (StrIntFormat) {.base = 10, .uppercase = false};
        StrFromI64(&s, 0, &config);
    
        // Test binary conversion
        StrClear(&s);
        config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
        StrFromI64(&s, 42, &config);
    
        // Test fractional conversion
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 123.456, &config);
    
        // Test negative number
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, -123.456, &config);
    
        // Test scientific notation (forced)
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = false};
        StrFromF64(&s, 123.456, &config);
    
        // Test scientific notation (uppercase)
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = true};
        StrFromF64(&s, 123.456, &config);
    
        // Test very small number (auto scientific notation)
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 0.0000123, &config);
    
        // Test very large number (auto scientific notation)
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 1234567890123.0, &config);
    
        // Test zero
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 0.0, &config);
    
        // Test infinity
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, F64_INFINITY, &config);
    
        // Test negative infinity
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, -F64_INFINITY, &config);
    
        // Test NaN
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, F64_NAN, &config);
    
            // Test hex round-trip
            StrClear(&s);
            config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
            StrFromU64(&s, u64_values[i], &config);
    
        // Test minimum i64 (avoid INT64_MIN due to negation UB)
        StrClear(&s);
        config = (StrIntFormat) {.base = 10, .uppercase = false};
        StrFromI64(&s, INT64_MIN + 1, &config);
    
        // Test very small floating point
        StrClear(&s);
        StrFloatFormat fconfig = {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 1e-300, &fconfig);
    
        // Test very large floating point
        StrClear(&s);
        fconfig = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 1e300, &fconfig);
    
            // Test uppercase E
            StrClear(&s);
            config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = true};
            StrFromF64(&s, sci_values[i], &config);
        result = result && !StrEmpty(&s);
    
        StrClear(&s);
        result = result && (StrLen(&s) == 0);
        result = result && StrEmpty(&s);
Last updated on