Skip to content

StrCmp

StrCmp

Description

Compare two Str objects

Parameters

Name Direction Description
str in First string
ostr in Other string

Returns

+ve or -ve depending on above or below in lexical ordering 0 if both are equal

Usage example (Cross-references)

Usage examples (Cross-references)
                if (VecLen(str) > 0) {
                    Str temp   = generate_str_from_input(data, offset, size, 20);
                    int result = StrCmp(str, &temp);
                    (void)result; // Suppress unused variable warning
                    StrDeinit(&temp);
        }
    
        bool result = StrCmp(&output_clean, &expected_clean) == 0;
    
        if (!result) {
        }
    
        bool result = StrCmp(&output_clean, &expected_clean) == 0;
    
        if (!result) {
        }
    
        bool result = StrCmp(&output_clean, &expected_clean) == 0;
    
        if (!result) {
    // Helper function to compare persons
    bool compare_persons(const TestPerson *a, const TestPerson *b) {
        return a->id == b->id && StrCmp(&a->name, &b->name) == 0 && a->age == b->age && a->is_active == b->is_active &&
               a->salary == b->salary;
    }
    // Helper function to compare configs
    bool compare_configs(const TestConfig *a, const TestConfig *b) {
        if (a->debug_mode != b->debug_mode || a->timeout != b->timeout || StrCmp(&a->log_level, &b->log_level) != 0 ||
            VecLen(&a->features) != VecLen(&b->features)) {
            return false;
    
        for (size i = 0; i < VecLen(&a->features); i++) {
            if (StrCmp(&VecAt(&a->features, i), &VecAt(&b->features, i)) != 0) {
                return false;
            }
        // Compare values
        if (original.count == parsed.count && original.temperature == parsed.temperature &&
            original.enabled == parsed.enabled && StrCmp(&original.message, &parsed.message) == 0) {
            WriteFmtLn("[DEBUG] Simple round-trip test passed");
        } else {
    
        // Compare values
        if (parsed.empty.length == original.empty.length && StrCmp(&original.simple, &parsed.simple) == 0 &&
            StrCmp(&original.with_spaces, &parsed.with_spaces) == 0 &&
            StrCmp(&original.with_special, &parsed.with_special) == 0) {
        // Compare values
        if (parsed.empty.length == original.empty.length && StrCmp(&original.simple, &parsed.simple) == 0 &&
            StrCmp(&original.with_spaces, &parsed.with_spaces) == 0 &&
            StrCmp(&original.with_special, &parsed.with_special) == 0) {
            WriteFmtLn("[DEBUG] String round-trip test passed");
        if (parsed.empty.length == original.empty.length && StrCmp(&original.simple, &parsed.simple) == 0 &&
            StrCmp(&original.with_spaces, &parsed.with_spaces) == 0 &&
            StrCmp(&original.with_special, &parsed.with_special) == 0) {
            WriteFmtLn("[DEBUG] String round-trip test passed");
        } else {
                if (VecAt(&original_strings, i).length != VecAt(&parsed_strings, i).length ||
                    (VecAt(&original_strings, i).length &&
                     StrCmp(&VecAt(&original_strings, i), &VecAt(&parsed_strings, i)) != 0)) {
                    strings_match = false;
                    break;
    
        Str expected = StrInitFromZstr("Hello");
        success      = success && (StrCmp(&s, &expected) == 0);
        StrDeinit(&expected);
        StrClear(&s);
    
        expected = StrInitFromZstr("Hello, World!");
        success  = success && (StrCmp(&s, &expected) == 0);
        StrDeinit(&expected);
    
        Str expected = StrInitFromZstr("Alice");
        success      = success && (StrCmp(&name, &expected) == 0);
        StrDeinit(&expected);
        StrClear(&name);
    
        expected = StrInitFromZstr("Bob");
        success  = success && (StrCmp(&name, &expected) == 0);
        StrDeinit(&expected);
        StrReadFmt(z, "{c}", str_val);
        Str  expected = StrInitFromZstr("Hello");
        bool str_pass = (StrCmp(&str_val, &expected) == 0);
        WriteFmt("str_val test: comparing with 'Hello', pass = {}\n", str_pass ? "true" : "false");
        success = success && str_pass;
        StrReadFmt(z, "{cs}", str_val);
        expected             = StrInitFromZstr("World");
        bool quoted_str_pass = (StrCmp(&str_val, &expected) == 0);
        WriteFmt("quoted str_val test: comparing with 'World', pass = {}\n", quoted_str_pass ? "true" : "false");
        success = success && quoted_str_pass;
            // Should read "hello" (stops at first space)
            Str  expected   = StrInitFromZstr("hello world");
            bool test1_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'hello', Pass: {}\n\n", test1_pass ? "true" : "false");
            success = success && test1_pass;
            // Should read "hello" (stops at first space)
            Str  expected   = StrInitFromZstr("hello");
            bool test1_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'hello', Pass: {}\n\n", test1_pass ? "true" : "false");
            success = success && test1_pass;
            // Should read "HELLO" (stops at first space)
            Str  expected   = StrInitFromZstr("HELLO WORLD");
            bool test2_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'HELLO', Pass: {}\n\n", test2_pass ? "true" : "false");
            success = success && test2_pass;
            // Should read "mixed case" (converts the entire quoted string)
            Str  expected   = StrInitFromZstr("mixed case");
            bool test3_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'mixed case', Pass: {}\n\n", test3_pass ? "true" : "false");
            success = success && test3_pass;
            // Should read "ABC123XYZ" (only letters are converted, numbers unchanged)
            Str  expected   = StrInitFromZstr("ABC123XYZ");
            bool test4_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'ABC123XYZ', Pass: {}\n\n", test4_pass ? "true" : "false");
            success = success && test4_pass;
            // Should read "Hello" (stops at first space, no case conversion)
            Str  expected   = StrInitFromZstr("Hello World");
            bool test5_pass = (StrCmp(&result, &expected) == 0);
            WriteFmt("Expected: 'Hello World', Pass: {}\n\n", test5_pass ? "true" : "false");
            success = success && test5_pass;
    // Test string comparison functions
    bool test_str_cmp(void) {
        WriteFmt("Testing StrCmp and StrCmpCstr\n");
    
        Str s1 = StrInitFromZstr("Hello");
    
        // Test StrCmp with equal strings
        int  cmp1   = StrCmp(&s1, &s2);
        bool result = (cmp1 == 0);
    
        // Test StrCmp with different strings (H < W in ASCII)
        int cmp2 = StrCmp(&s1, &s3);
        result   = result && (cmp2 < 0);
    
        // Test StrCmp with string prefix - ZstrCompare compares the entire strings
        int cmp3 = StrCmp(&s1, &s4);
        result   = result && (cmp3 < 0); // "Hello" comes before "Hello World" lexicographically
Last updated on