ZstrCompare

Table of Contents

ZstrCompare

Description

Compare two strings lexicographically.

Parameters

NameDirectionDescription
s1inFirst string.
s2inSecond string.

Success

Returns 0 if equal, <0 if s1<s2, >0 if s1>s2.

Failure

Function cannot fail if strings are valid.

Usage example (Cross-references)

    /// RETURN : 0 if both are equal
    ///
    #define StrCmp(str, ostr) ZstrCompare((str)->data, (ostr)->data)
    
    ///
    /// RETURN : 0 if both are equal
    ///
    #define StrCmpZstr(str, zstr) ZstrCompare((str)->data, (zstr))
    
    //
    do {
    // Skip "." and ".." entries
    if (ZstrCompare(findFileData.cFileName, ".") == 0 || ZstrCompare(findFileData.cFileName, "..") == 0) {
    continue;
    }
    }
    
    i32 ZstrCompare(const char* s1, const char* s2) {
    if (!s1 || !s2) {
    LOG_FATAL("Invalid arguments");
    StrPushBack(&s, 'o');
    
    bool result = (s.length == 5 && ZstrCompare(s.data, "Hello") == 0);
    
    StrDeinit(&s);
    Str* str2 = &VecAt(&sv, 1);
    
    result = result && (ZstrCompare(str1->data, "Hello") == 0);
    result = result && (ZstrCompare(str2->data, "World") == 0);
    }
    
    result = result && (ZstrCompare(str1->data, "Hello") == 0);
    result = result && (ZstrCompare(str2->data, "World") == 0);
    }
    StrIntFormat config = {.base = 10, .uppercase = false};
    StrFromU64(&s, 12345, &config);
    bool result = (ZstrCompare(s.data, "12345") == 0);
    if (!result) {
    printf("    FAIL: Expected '12345', got '%s'\n", s.data);
    config = (StrIntFormat) {.base = 16, .uppercase = false, .use_prefix = true};
    StrFromU64(&s, 0xABCD, &config);
    result = result && (ZstrCompare(s.data, "0xabcd") == 0);
    if (!result) {
    printf("    FAIL: Expected '0xabcd', got '%s'\n", s.data);
    config = (StrIntFormat) {.base = 16, .uppercase = true, .use_prefix = true};
    StrFromU64(&s, 0xABCD, &config);
    result = result && (ZstrCompare(s.data, "0xABCD") == 0);
    if (!result) {
    printf("    FAIL: Expected '0xABCD', got '%s'\n", s.data);
    config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
    StrFromU64(&s, 42, &config);
    result = result && (ZstrCompare(s.data, "0b101010") == 0);
    if (!result) {
    printf("    FAIL: Expected '0b101010', got '%s'\n", s.data);
    config = (StrIntFormat) {.base = 8, .uppercase = false, .use_prefix = true};
    StrFromU64(&s, 42, &config);
    result = result && (ZstrCompare(s.data, "0o52") == 0);
    if (!result) {
    printf("    FAIL: Expected '0o52', got '%s'\n", s.data);
    config = (StrIntFormat) {.base = 10, .uppercase = false};
    StrFromU64(&s, 0, &config);
    result = result && (ZstrCompare(s.data, "0") == 0);
    if (!result) {
    printf("    FAIL: Expected '0', got '%s'\n", s.data);
    StrIntFormat config = {.base = 10, .uppercase = false};
    StrFromI64(&s, 12345, &config);
    bool result = (ZstrCompare(s.data, "12345") == 0);
    if (!result) {
    printf("    FAIL: Expected '12345', got '%s'\n", s.data);
    config = (StrIntFormat) {.base = 10, .uppercase = false};
    StrFromI64(&s, -12345, &config);
    result = result && (ZstrCompare(s.data, "-12345") == 0);
    if (!result) {
    printf("    FAIL: Expected '-12345', got '%s'\n", s.data);
    config = (StrIntFormat) {.base = 10, .uppercase = false};
    StrFromI64(&s, 0, &config);
    result = result && (ZstrCompare(s.data, "0") == 0);
    if (!result) {
    printf("    FAIL: Expected '0', got '%s'\n", s.data);
    config = (StrIntFormat) {.base = 2, .uppercase = false, .use_prefix = true};
    StrFromI64(&s, 42, &config);
    result = result && (ZstrCompare(s.data, "0b101010") == 0);
    if (!result) {
    printf("    FAIL: Expected '0b101010', got '%s'\n", s.data);
    StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
    StrFromF64(&s, 123.0, &config);
    bool result = (ZstrCompare(s.data, "123.00") == 0);
    if (!result) {
    printf("    FAIL: Expected '123.00', got '%s'\n", s.data);
    config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
    StrFromF64(&s, 123.456, &config);
    result = result && (ZstrCompare(s.data, "123.456") == 0);
    if (!result) {
    printf("    FAIL: Expected '123.456', got '%s'\n", s.data);
    config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
    StrFromF64(&s, -123.456, &config);
    result = result && (ZstrCompare(s.data, "-123.456") == 0);
    
    // Test scientific notation (forced)
    config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = false};
    StrFromF64(&s, 123.456, &config);
    result = result && (ZstrCompare(s.data, "1.235e+02") == 0);
    
    // Test scientific notation (uppercase)
    config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = true};
    StrFromF64(&s, 123.456, &config);
    result = result && (ZstrCompare(s.data, "1.235E+02") == 0);
    
    // Test very small number (auto scientific notation)
    config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
    StrFromF64(&s, 0.0000123, &config);
    result = result && (ZstrCompare(s.data, "1.230e-05") == 0);
    
    // Test very large number (auto scientific notation)
    config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
    StrFromF64(&s, 1234567890123.0, &config);
    result = result && (ZstrCompare(s.data, "1.23e+12") == 0);
    
    // Test zero
    config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
    StrFromF64(&s, 0.0, &config);
    result = result && (ZstrCompare(s.data, "0.00") == 0);
    
    // Test infinity
    config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
    StrFromF64(&s, INFINITY, &config);
    result = result && (ZstrCompare(s.data, "inf") == 0);
    
    // Test negative infinity
    config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
    StrFromF64(&s, -INFINITY, &config);
    result = result && (ZstrCompare(s.data, "-inf") == 0);
    
    // Test NaN
    config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
    StrFromF64(&s, NAN, &config);
    result = result && (ZstrCompare(s.data, "nan") == 0);
    
    StrDeinit(&s);
    
    // Check that the string was reversed
    bool result = (ZstrCompare(s.data, "olleH") == 0);
    
    // Test with an even-length string
    
    // Check that the string was reversed
    result = result && (ZstrCompare(s.data, "dcba") == 0);
    
    // Test with a single-character string
    
    // Check that the string is unchanged
    result = result && (ZstrCompare(s.data, "a") == 0);
    
    // Test with an empty string
    
    // Check that the character was inserted correctly
    bool result = (ZstrCompare(s.data, "He!llo") == 0);
    
    // Insert a character at the beginning
    
    // Check that the character was inserted correctly
    result = result && (ZstrCompare(s.data, "?He!llo") == 0);
    
    // Insert a character at the end
    
    // Check that the character was inserted correctly
    result = result && (ZstrCompare(s.data, "?He!llo.") == 0);
    
    StrDeinit(&s);
    
    // Check that the string was inserted correctly
    bool result = (ZstrCompare(s.data, "He Worldllo") == 0);
    
    StrDeinit(&s);
    
    // Check that the string was inserted correctly
    bool result = (ZstrCompare(s.data, "He Worldllo") == 0);
    
    StrDeinit(&s);
    
    // Check that the string was inserted correctly
    bool result = (ZstrCompare(s1.data, "He Worldllo") == 0);
    
    StrDeinit(&s1);
    
    // Check that the string was inserted correctly
    bool result = (ZstrCompare(s.data, "He Worldllo") == 0);
    
    StrDeinit(&s);
    
    // Check that the string was inserted correctly
    bool result = (ZstrCompare(s.data, "He Worldllo") == 0);
    
    StrDeinit(&s);
    
    // Check that the string was inserted correctly
    bool result = (ZstrCompare(s.data, "Hello World") == 0);
    
    StrDeinit(&s);
    
    // Check that the string was inserted correctly
    bool result = (ZstrCompare(s.data, "Hello World") == 0);
    
    StrDeinit(&s);
    
    // Check that the string was inserted correctly
    bool result = (ZstrCompare(s.data, "Hello World") == 0);
    
    StrDeinit(&s);
    
    // Check that the string was inserted correctly
    bool result = (ZstrCompare(s.data, "Hello World") == 0);
    
    StrDeinit(&s);
    
    // Check that the characters were inserted correctly
    bool result = (ZstrCompare(s.data, "Hello World") == 0);
    
    StrDeinit(&s);
    
    // Check that the characters were inserted correctly
    bool result = (ZstrCompare(s.data, "Hello World") == 0);
    
    StrDeinit(&s);
    
    // Check that the strings were merged correctly
    bool result = (ZstrCompare(s1.data, "Hello World") == 0);
    
    // Check that s2 was reset - data should be NULL, length should be 0
    
    // Check that the strings were merged correctly
    bool result = (ZstrCompare(s1.data, "Hello World") == 0);
    
    // Check that s2 was not reset
    
    // Check that s2 was not reset
    result = result && (s2.length == 6 && ZstrCompare(s2.data, " World") == 0);
    
    StrDeinit(&s1);
    
    // Check that the strings were merged correctly
    bool result = (ZstrCompare(s1.data, "Hello World") == 0);
    
    // Check that s2 was not reset (since StrMerge is an alias for StrMergeR)
    
    // Check that s2 was not reset (since StrMerge is an alias for StrMergeR)
    result = result && (s2.length == 6 && ZstrCompare(s2.data, " World") == 0);
    
    StrDeinit(&s1);
    
    // Check that the string was appended correctly
    bool result = (ZstrCompare(s.data, "Hello World 2023") == 0);
    
    StrDeinit(&s);
    
    // Check that the character was popped correctly
    bool result = (c == 'o' && ZstrCompare(s.data, "Hell") == 0);
    
    // Pop another character without storing it - avoid passing NULL directly
    
    // Check that the character was removed
    result = result && (ZstrCompare(s.data, "Hel") == 0);
    
    StrDeinit(&s);
    
    // Check that the character was popped correctly
    bool result = (c == 'H' && ZstrCompare(s.data, "ello") == 0);
    
    // Pop another character without storing it - avoid passing NULL directly
    
    // Check that the character was removed
    result = result && (ZstrCompare(s.data, "llo") == 0);
    
    StrDeinit(&s);
    
    // Check that the character was removed correctly
    bool result = (c == 'l' && ZstrCompare(s.data, "Helo") == 0);
    
    // Remove another character without storing it - avoid passing NULL directly
    
    // Check that the character was removed
    result = result && (ZstrCompare(s.data, "Hlo") == 0);
    
    StrDeinit(&s);
    
    // Check that the characters were removed correctly
    bool result = (ZstrCompare(buffer, " Worl") == 0 && ZstrCompare(s.data, "Hellod") == 0);
    
    // Remove another range without storing it - use a temporary buffer instead of NULL
    
    // Check that the characters were removed
    result = result && (ZstrCompare(s.data, "Hell") == 0);
    
    StrDeinit(&s);
    
    // Check that the character was deleted
    bool result = (ZstrCompare(s.data, "Hell") == 0);
    
    // Delete another character
    
    // Check that the character was deleted
    result = result && (ZstrCompare(s.data, "Hel") == 0);
    
    StrDeinit(&s);
    
    // Check that the character was deleted
    bool result = (ZstrCompare(s.data, "Helo") == 0);
    
    // Delete another character
    
    // Check that the character was deleted
    result = result && (ZstrCompare(s.data, "Hlo") == 0);
    
    StrDeinit(&s);
    
    // Check that the characters were deleted
    bool result = (ZstrCompare(s.data, "Hello") == 0);
    
    // Delete another range
    
    // Check that the characters were deleted
    result = result && (ZstrCompare(s.data, "Heo") == 0);
    
    StrDeinit(&s);
    // Test literal text
    StrWriteFmt(&output, "Hello, world!");
    success = success && (ZstrCompare(output.data, "Hello, world!") == 0);
    StrClear(&output);
    // Test escaped braces
    StrWriteFmt(&output, "{{Hello}}");
    success = success && (ZstrCompare(output.data, "{Hello}") == 0);
    StrClear(&output);
    // Test double escaped braces
    StrWriteFmt(&output, "{{{{");
    success = success && (ZstrCompare(output.data, "{{") == 0);
    
    StrDeinit(&output);
    const char* str = "Hello";
    StrWriteFmt(&output, "{}", str);
    success = success && (ZstrCompare(output.data, "Hello") == 0);
    StrClear(&output);
    // Test string with width and alignment
    StrWriteFmt(&output, "{>10}", str);
    success = success && (ZstrCompare(output.data, "     Hello") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{<10}", str);
    success = success && (ZstrCompare(output.data, "Hello     ") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{^10}", str);
    success = success && (ZstrCompare(output.data, "  Hello   ") == 0);
    StrClear(&output);
    Str s = StrInitFromZstr("World");
    StrWriteFmt(&output, "{}", s);
    success = success && (ZstrCompare(output.data, "World") == 0);
    StrDeinit(&s);
    i8 i8_val = -42;
    StrWriteFmt(&output, "{}", i8_val);
    success = success && (ZstrCompare(output.data, "-42") == 0);
    StrClear(&output);
    i16 i16_val = -1234;
    StrWriteFmt(&output, "{}", i16_val);
    success = success && (ZstrCompare(output.data, "-1234") == 0);
    StrClear(&output);
    i32 i32_val = -123456;
    StrWriteFmt(&output, "{}", i32_val);
    success = success && (ZstrCompare(output.data, "-123456") == 0);
    StrClear(&output);
    i64 i64_val = -1234567890LL;
    StrWriteFmt(&output, "{}", i64_val);
    success = success && (ZstrCompare(output.data, "-1234567890") == 0);
    StrClear(&output);
    u8 u8_val = 42;
    StrWriteFmt(&output, "{}", u8_val);
    success = success && (ZstrCompare(output.data, "42") == 0);
    StrClear(&output);
    u16 u16_val = 1234;
    StrWriteFmt(&output, "{}", u16_val);
    success = success && (ZstrCompare(output.data, "1234") == 0);
    StrClear(&output);
    u32 u32_val = 123456;
    StrWriteFmt(&output, "{}", u32_val);
    success = success && (ZstrCompare(output.data, "123456") == 0);
    StrClear(&output);
    u64 u64_val = 1234567890ULL;
    StrWriteFmt(&output, "{}", u64_val);
    success = success && (ZstrCompare(output.data, "1234567890") == 0);
    StrClear(&output);
    i8 i8_max = 127;
    StrWriteFmt(&output, "{}", i8_max);
    success = success && (ZstrCompare(output.data, "127") == 0);
    StrClear(&output);
    i8 i8_min = -128;
    StrWriteFmt(&output, "{}", i8_min);
    success = success && (ZstrCompare(output.data, "-128") == 0);
    StrClear(&output);
    u8 u8_max = 255;
    StrWriteFmt(&output, "{}", u8_max);
    success = success && (ZstrCompare(output.data, "255") == 0);
    StrClear(&output);
    u8 u8_min = 0;
    StrWriteFmt(&output, "{}", u8_min);
    success = success && (ZstrCompare(output.data, "0") == 0);
    
    StrDeinit(&output);
    u32 val = 0xDEADBEEF;
    StrWriteFmt(&output, "{x}", val);
    success = success && (ZstrCompare(output.data, "0xdeadbeef") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{X}", val);
    success = success && (ZstrCompare(output.data, "0xDEADBEEF") == 0);
    
    StrDeinit(&output);
    u8 val = 0xA5; // 10100101 in binary
    StrWriteFmt(&output, "{b}", val);
    success = success && (ZstrCompare(output.data, "0b10100101") == 0);
    
    StrDeinit(&output);
    u16 val = 0777;
    StrWriteFmt(&output, "{o}", val);
    success = success && (ZstrCompare(output.data, "0o777") == 0);
    
    StrDeinit(&output);
    f32 f32_val = 3.14159f;
    StrWriteFmt(&output, "{}", f32_val);
    success = success && (ZstrCompare(output.data, "3.141590") == 0);
    StrClear(&output);
    f64 f64_val = 2.71828;
    StrWriteFmt(&output, "{}", f64_val);
    success = success && (ZstrCompare(output.data, "2.718280") == 0);
    
    StrDeinit(&output);
    // Test different precisions
    StrWriteFmt(&output, "{.2}", val);
    success = success && (ZstrCompare(output.data, "3.14") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{.0}", val);
    success = success && (ZstrCompare(output.data, "3") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{.10}", val);
    success = success && (ZstrCompare(output.data, "3.1415926536") == 0);
    
    StrDeinit(&output);
    f64 pos_inf = INFINITY;
    StrWriteFmt(&output, "{}", pos_inf);
    success = success && (ZstrCompare(output.data, "inf") == 0);
    StrClear(&output);
    f64 neg_inf = -INFINITY;
    StrWriteFmt(&output, "{}", neg_inf);
    success = success && (ZstrCompare(output.data, "-inf") == 0);
    StrClear(&output);
    f64 nan_val = NAN;
    StrWriteFmt(&output, "{}", nan_val);
    success = success && (ZstrCompare(output.data, "nan") == 0);
    
    StrDeinit(&output);
    i32 val = 42;
    StrWriteFmt(&output, "{5}", val);
    success = success && (ZstrCompare(output.data, "   42") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{<5}", val);
    success = success && (ZstrCompare(output.data, "42   ") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{^5}", val);
    success = success && (ZstrCompare(output.data, " 42  ") == 0);
    StrClear(&output);
    const char* str = "abc";
    StrWriteFmt(&output, "{5}", str);
    success = success && (ZstrCompare(output.data, "  abc") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{<5}", str);
    success = success && (ZstrCompare(output.data, "abc  ") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{^5}", str);
    success = success && (ZstrCompare(output.data, " abc ") == 0);
    
    StrDeinit(&output);
    
    StrWriteFmt(&output, "{} {} {}", hello, num, pi);
    success = success && (ZstrCompare(output.data, "Hello 42 3.140000") == 0);
    StrClear(&output);
    // Instead of using positional arguments, we'll just reorder the arguments themselves
    StrWriteFmt(&output, "{} {} {}", pi, hello, num);
    success = success && (ZstrCompare(output.data, "3.140000 Hello 42") == 0);
    
    StrDeinit(&output);
    const char* mixed_case = "MiXeD CaSe";
    StrWriteFmt(&output, "{c}", mixed_case);
    success = success && (ZstrCompare(output.data, "MiXeD CaSe") == 0);
    StrClear(&output);
    // Test mixed case string with :a (lowercase)
    StrWriteFmt(&output, "{a}", mixed_case);
    success = success && (ZstrCompare(output.data, "mixed case") == 0);
    StrClear(&output);
    // Test mixed case string with :A (uppercase)
    StrWriteFmt(&output, "{A}", mixed_case);
    success = success && (ZstrCompare(output.data, "MIXED CASE") == 0);
    StrClear(&output);
    // Test with :c (preserve case)
    StrWriteFmt(&output, "{c}", s);
    success = success && (ZstrCompare(output.data, "MiXeD CaSe") == 0);
    StrClear(&output);
    // Test with :a (lowercase)
    StrWriteFmt(&output, "{a}", s);
    success = success && (ZstrCompare(output.data, "mixed case") == 0);
    StrClear(&output);
    // Test with :A (uppercase)
    StrWriteFmt(&output, "{A}", s);
    success = success && (ZstrCompare(output.data, "MIXED CASE") == 0);
    StrClear(&output);
    // Test uppercase char with :c (preserve case)
    StrWriteFmt(&output, "{c}", upper_char);
    success = success && (ZstrCompare(output.data, "M") == 0);
    StrClear(&output);
    // Test uppercase char with :a (lowercase)
    StrWriteFmt(&output, "{a}", upper_char);
    success = success && (ZstrCompare(output.data, "m") == 0);
    StrClear(&output);
    // Test lowercase char with :A (uppercase)
    StrWriteFmt(&output, "{A}", lower_char);
    success = success && (ZstrCompare(output.data, "M") == 0);
    StrClear(&output);
    BitVec bv1 = BitVecFromStr("10110");
    StrWriteFmt(&output, "{}", bv1);
    success = success && (ZstrCompare(output.data, "10110") == 0);
    StrClear(&output);
    BitVec bv2 = BitVecFromInteger(0xABCD, 16);
    StrWriteFmt(&output, "{x}", bv2);
    success = success && (ZstrCompare(output.data, "0xabcd") == 0);
    StrClear(&output);
    // Test 4: Uppercase hex formatting
    StrWriteFmt(&output, "{X}", bv2);
    success = success && (ZstrCompare(output.data, "0xABCD") == 0);
    StrClear(&output);
    BitVec bv3 = BitVecFromInteger(0755, 10);
    StrWriteFmt(&output, "{o}", bv3);
    success = success && (ZstrCompare(output.data, "0o755") == 0);
    StrClear(&output);
    // Test 6: Width and alignment
    StrWriteFmt(&output, "{>10}", bv1);
    success = success && (ZstrCompare(output.data, "     10110") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{<10}", bv1);
    success = success && (ZstrCompare(output.data, "10110     ") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{^10}", bv1);
    success = success && (ZstrCompare(output.data, "  10110   ") == 0);
    StrClear(&output);
    BitVec bv_zero = BitVecFromInteger(0, 1);
    StrWriteFmt(&output, "{x}", bv_zero);
    success = success && (ZstrCompare(output.data, "0x0") == 0);
    StrClear(&output);
    
    StrWriteFmt(&output, "{o}", bv_zero);
    success = success && (ZstrCompare(output.data, "0o0") == 0);
    StrClear(&output);
    
    // The result should be "H0e1l2l3o4"
    bool success = (ZstrCompare(result.data, "H0e1l2l3o4") == 0);
    
    StrDeinit(&s);
    if (saw_index_zero) {
    // The test output shows index 0 is processed, but the order is different than expected
    success = (ZstrCompare(result.data, "o4l3l2e1H0") == 0);
    printf("  (Index 0 was processed)\n");
    } else {
    printf("  (Index 0 was processed)\n");
    } else {
    success = (ZstrCompare(result.data, "o4l3l2e1") == 0);
    printf("  (Index 0 was NOT processed - bug in macro)\n");
    }
    
    // The result should be "H0e1l2l3o4"
    bool success = (ZstrCompare(result.data, "H0e1l2l3o4") == 0);
    
    // The original string should now be "HELLO" (all uppercase)
    
    // The original string should now be "HELLO" (all uppercase)
    success = success && (ZstrCompare(s.data, "HELLO") == 0);
    
    StrDeinit(&s);
    if (saw_index_zero) {
    // The test output shows index 0 is processed, but the order is different than expected
    success = (ZstrCompare(result.data, "o4l3l2e1H0") == 0);
    success = success && (ZstrCompare(s.data, "HELLO") == 0); // All uppercase
    printf("  (Index 0 was processed)\n");
    // The test output shows index 0 is processed, but the order is different than expected
    success = (ZstrCompare(result.data, "o4l3l2e1H0") == 0);
    success = success && (ZstrCompare(s.data, "HELLO") == 0); // All uppercase
    printf("  (Index 0 was processed)\n");
    } else {
    printf("  (Index 0 was processed)\n");
    } else {
    success = (ZstrCompare(result.data, "o4l3l2e1") == 0);
    success = success && (ZstrCompare(s.data, "HELLo") == 0); // All uppercase except first char
    printf("  (Index 0 was NOT processed - bug in macro)\n");
    } else {
    success = (ZstrCompare(result.data, "o4l3l2e1") == 0);
    success = success && (ZstrCompare(s.data, "HELLo") == 0); // All uppercase except first char
    printf("  (Index 0 was NOT processed - bug in macro)\n");
    }
    
    // The result should be "Hello"
    bool success = (ZstrCompare(result.data, "Hello") == 0);
    
    StrDeinit(&s);
    bool success = false;
    if (char_count == s.length) {
    success = (ZstrCompare(result.data, "olleH") == 0);
    printf("  (All characters were processed)\n");
    } else {
    printf("  (All characters were processed)\n");
    } else {
    success = (ZstrCompare(result.data, "olle") == 0);
    printf("  (First character was NOT processed - bug in macro)\n");
    }
    
    // The result should be "Hello" (original values before modification)
    bool success = (ZstrCompare(result.data, "Hello") == 0);
    
    // The original string should now be "HELLO" (all uppercase)
    
    // The original string should now be "HELLO" (all uppercase)
    success = success && (ZstrCompare(s.data, "HELLO") == 0);
    
    StrDeinit(&s);
    bool success = false;
    if (char_count == s.length) {
    success = (ZstrCompare(result.data, "olleH") == 0);
    success = success && (ZstrCompare(s.data, "HELLO") == 0); // All uppercase
    printf("  (All characters were processed)\n");
    if (char_count == s.length) {
    success = (ZstrCompare(result.data, "olleH") == 0);
    success = success && (ZstrCompare(s.data, "HELLO") == 0); // All uppercase
    printf("  (All characters were processed)\n");
    } else {
    printf("  (All characters were processed)\n");
    } else {
    success = (ZstrCompare(result.data, "olle") == 0);
    success = success && (ZstrCompare(s.data, "HELLo") == 0); // All uppercase except first char
    printf("  (First character was NOT processed - bug in macro)\n");
    } else {
    success = (ZstrCompare(result.data, "olle") == 0);
    success = success && (ZstrCompare(s.data, "HELLo") == 0); // All uppercase except first char
    printf("  (First character was NOT processed - bug in macro)\n");
    }
    
    // The result should be "W6o7r8l9d10" (characters from index 6-10 with their indices)
    bool success = (ZstrCompare(result.data, "W6o7r8l9d10") == 0);
    
    // Test with empty range
    
    // The result should be "Hello" (first 5 characters)
    bool success = (ZstrCompare(result.data, "Hello") == 0);
    
    // Test with range at the end of the string
    
    // The end_result should be "World" (last 5 characters)
    success = success && (ZstrCompare(end_result.data, "World") == 0);
    
    StrDeinit(&s);
    
    // The result should be "W6o7r8l9d10" (characters from index 6-10 with their indices)
    bool success = (ZstrCompare(result.data, "W6o7r8l9d10") == 0);
    
    // The original string should now have "WORLD" in uppercase
    
    // The original string should now have "WORLD" in uppercase
    success = success && (ZstrCompare(s.data, "Hello WORLD") == 0);
    
    StrDeinit(&s);
    
    // The result should be "Hello" (first 5 characters)
    bool success = (ZstrCompare(result.data, "Hello") == 0);
    
    // The original string should now have "HELLO" in uppercase
    
    // The original string should now have "HELLO" in uppercase
    success = success && (ZstrCompare(s.data, "HELLO World") == 0);
    
    StrDeinit(&s);
    
    // Should get exact same string back
    result = result && (ZstrCompare(str.data, patterns[i]) == 0);
    
    StrDeinit(&str);
    // Test string conversion consistency
    Str str = BitVecToStr(&bv);
    result  = result && (ZstrCompare(str.data, test_cases[i].pattern) == 0);
    StrDeinit(&str);
    u64 value = BitVecToInteger(&bv);
    // We test that we get a consistent value (not necessarily the exact expected one)
    result = result && (value > 0 || ZstrCompare(test_cases[i].pattern, "00000000") == 0);
    
    // Test byte conversion
    
    // At least two of them should match (bit order might affect one)
    bool cross_match = (ZstrCompare(str1.data, str2.data) == 0) || (ZstrCompare(str1.data, str3.data) == 0) ||
    (ZstrCompare(str2.data, str3.data) == 0);
    result = result && cross_match;
    // At least two of them should match (bit order might affect one)
    bool cross_match = (ZstrCompare(str1.data, str2.data) == 0) || (ZstrCompare(str1.data, str3.data) == 0) ||
    (ZstrCompare(str2.data, str3.data) == 0);
    result = result && cross_match;
    
    // Check that it's initialized correctly
    bool result = (s.length == strlen(test_str) && ZstrCompare(s.data, test_str) == 0);
    
    StrDeinit(&s);
    
    // Check that dst is initialized correctly
    bool result = (dst.length == src.length && ZstrCompare(dst.data, src.data) == 0);
    
    StrDeinit(&src);
    
    // Check that dst is initialized correctly
    bool result = (dst.length == src.length && ZstrCompare(dst.data, src.data) == 0);
    
    StrDeinit(&src);
    
    // Check that it's initialized correctly
    bool result = (ZstrCompare(s.data, "Hello, World!") == 0);
    
    StrDeinit(&s);
    
    // Check that it works correctly
    if (ZstrCompare(stack_str.data, "Hello, Stack!") != 0) {
    result = false;
    }
    
    // Check that the copy was successful
    bool result = (success && dst.length == src.length && ZstrCompare(dst.data, src.data) == 0);
    
    StrDeinit(&src);
    StrReadFmt("10110", "{}", bv1);
    Str result1 = BitVecToStr(&bv1);
    success     = success && (ZstrCompare(result1.data, "10110") == 0);
    printf(
    "Test 1 - Binary: %.*s, Success: %s\n",
    (int)result1.length,
    result1.data,
    (ZstrCompare(result1.data, "10110") == 0) ? "true" : "false"
    );
    StrDeinit(&result1);
    StrReadFmt("   1101", "{}", bv4);
    Str result4 = BitVecToStr(&bv4);
    success     = success && (ZstrCompare(result4.data, "1101") == 0);
    printf(
    "Test 4 - Whitespace: %.*s, Success: %s\n",
    (int)result4.length,
    result4.data,
    (ZstrCompare(result4.data, "1101") == 0) ? "true" : "false"
    );
    StrDeinit(&result4);
    StrReadFmt("0", "{}", bv5);
    Str result5 = BitVecToStr(&bv5);
    success     = success && (ZstrCompare(result5.data, "0") == 0);
    printf(
    "Test 5 - Zero: %.*s, Success: %s\n",
    (int)result5.length,
    result5.data,
    (ZstrCompare(result5.data, "0") == 0) ? "true" : "false"
    );
    StrDeinit(&result5);
    // Test StrFindStr with match at end
    const char* found1 = StrFindStr(&haystack, &needle1);
    bool        result = (found1 != NULL && ZstrCompare(found1, "World") == 0);
    
    // Test StrFindStr with match at beginning
    // Test StrFindStr with match at beginning
    const char* found2 = StrFindStr(&haystack, &needle2);
    result             = result && (found2 != NULL && ZstrCompare(found2, "Hello World") == 0);
    
    // Test StrFindStr with no match
    // Test StrFindZstr
    const char* found4 = StrFindZstr(&haystack, "World");
    result             = result && (found4 != NULL && ZstrCompare(found4, "World") == 0);
    
    // Test StrFindCstr
    Str s1 = StrInitFromZstr("Hello World");
    StrReplaceZstr(&s1, "World", "Universe", 1);
    bool result = (ZstrCompare(s1.data, "Hello Universe") == 0);
    
    // Test multiple replacements
    s1 = StrInitFromZstr("Hello Hello Hello");
    StrReplaceZstr(&s1, "Hello", "Hi", 2);
    result = result && (ZstrCompare(s1.data, "Hi Hi Hello") == 0);
    
    // Test StrReplaceCstr - use the full "World" string instead of just "Wo"
    s1 = StrInitFromZstr("Hello World");
    StrReplaceCstr(&s1, "World", 5, "Universe", 8, 1);
    result = result && (ZstrCompare(s1.data, "Hello Universe") == 0);
    
    // Test StrReplace
    Str replace = StrInitFromZstr("Universe");
    StrReplace(&s1, &find, &replace, 1);
    result = result && (ZstrCompare(s1.data, "Hello Universe") == 0);
    
    StrDeinit(&s1);
    bool result = (split.length == 3);
    if (split.length >= 3) {
    result = result && (ZstrCompare(split.data[0].data, "Hello") == 0);
    result = result && (ZstrCompare(split.data[1].data, "World") == 0);
    result = result && (ZstrCompare(split.data[2].data, "Test") == 0);
    if (split.length >= 3) {
    result = result && (ZstrCompare(split.data[0].data, "Hello") == 0);
    result = result && (ZstrCompare(split.data[1].data, "World") == 0);
    result = result && (ZstrCompare(split.data[2].data, "Test") == 0);
    }
    result = result && (ZstrCompare(split.data[0].data, "Hello") == 0);
    result = result && (ZstrCompare(split.data[1].data, "World") == 0);
    result = result && (ZstrCompare(split.data[2].data, "Test") == 0);
    }
    char     buffer1[10] = {0};
    memcpy(buffer1, iter1->data, iter1->length);
    result = result && (ZstrCompare(buffer1, "Hello") == 0);
    
    // Check second iterator
    char     buffer2[10] = {0};
    memcpy(buffer2, iter2->data, iter2->length);
    result = result && (ZstrCompare(buffer2, "World") == 0);
    
    // Check third iterator
    char     buffer3[10] = {0};
    memcpy(buffer3, iter3->data, iter3->length);
    result = result && (ZstrCompare(buffer3, "Test") == 0);
    }
    Str  s1       = StrInitFromZstr("  Hello  ");
    Str  stripped = StrLStrip(&s1, NULL);
    bool result   = (ZstrCompare(stripped.data, "Hello  ") == 0);
    StrDeinit(&stripped);
    // Test StrRStrip
    stripped = StrRStrip(&s1, NULL);
    result   = result && (ZstrCompare(stripped.data, "  Hello") == 0);
    StrDeinit(&stripped);
    // Test StrStrip
    stripped = StrStrip(&s1, NULL);
    result   = result && (ZstrCompare(stripped.data, "Hello") == 0);
    StrDeinit(&stripped);
    
    stripped = StrLStrip(&s1, "*");
    result   = result && (ZstrCompare(stripped.data, "Hello***") == 0);
    StrDeinit(&stripped);
    
    stripped = StrRStrip(&s1, "*");
    result   = result && (ZstrCompare(stripped.data, "***Hello") == 0);
    StrDeinit(&stripped);
    
    stripped = StrStrip(&s1, "*");
    result   = result && (ZstrCompare(stripped.data, "Hello") == 0);
    StrDeinit(&stripped);

Share :

Related Posts

JR_STR_KV

JR_STR_KV Description Read a string key-value pair if key matches.

Read More

StrWriteFmtInternal

StrWriteFmtInternal Description Print out a formatted string with rust-style placeholders to given string o.

Read More

JW_STR

JW_STR Description Append a string value (quoted) to the JSON.

Read More