Skip to content

ZstrCompareN

ZstrCompareN

Description

Compare two strings lexicographically up to n characters.

Parameters

Name Direction Description
s1 in First string.
s2 in Second string.
n in Maximum number of characters to compare.

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)

Usage examples (Cross-references)
    }
    
    i32 ZstrCompareN(const char *s1, const char *s2, size n) {
        if (!s1 || !s2) {
            LOG_FATAL("Invalid arguments");
                    prev = next + keylen;  // skip past delimiter
                } else {
                    if (ZstrCompareN(prev, key, end - prev)) {
                        Str tmp = StrInitFromCstr(prev, end - prev);
                        VecPushBack(&sv, tmp); // remaining part
            if (StrIterPeek(&si) == 't') {
                const char *pos = StrIterPos(&si);
                if (pos && ZstrCompareN(pos, "true", 4) == 0) {
                    StrIterMove(&si, 4);
                    *b = true;
                if (StrIterPeek(&si) == 'f') {
                    const char *pos = StrIterPos(&si);
                    if (pos && ZstrCompareN(pos, "false", 5) == 0) {
                        StrIterMove(&si, 5);
                        *b = false;
            if (StrIterPeek(&si) == 'n') {
                const char *pos = StrIterPos(&si);
                if (pos && ZstrCompareN(pos, "null", 4) == 0) {
                    StrIterMove(&si, 4);
                    *is_null = true;
            // Use VecForeach for iterating over entries
            VecForeach(&entries, e) {
                const char *compareTemplate = "    if(ZstrCompareN(\"{}\", zstr, {}) == 0) {{return {};}}\n";
                // Store the length in a variable to avoid taking address of rvalue
                unsigned long long strLength = (unsigned long long)e.str.length;
        z       = "AB";
        StrReadFmt(z, "{c}", u16_val);
        bool u16_multi_pass = (ZstrCompareN((const char *)&u16_val, "AB", 2) == 0);
        WriteFmt("u16_val multi-char test: comparing memory with 'AB', pass = {}\n", u16_multi_pass ? "true" : "false");
        WriteFmt(
        z       = "CD";
        StrReadFmt(z, "{c}", i16_val);
        bool i16_multi_pass = (ZstrCompareN((const char *)&i16_val, "CD", 2) == 0);
        WriteFmt("i16_val multi-char test: comparing memory with 'CD', pass = {}\n", i16_multi_pass ? "true" : "false");
        success = success && i16_multi_pass;
        z       = "EFGH";
        StrReadFmt(z, "{c}", u32_val);
        bool u32_multi_pass = (ZstrCompareN((const char *)&u32_val, "EFGH", 4) == 0);
        WriteFmt("u32_val multi-char test: comparing memory with 'EFGH', pass = {}\n", u32_multi_pass ? "true" : "false");
        success = success && u32_multi_pass;
        z       = "IJKL";
        StrReadFmt(z, "{c}", i32_val);
        bool i32_multi_pass = (ZstrCompareN((const char *)&i32_val, "IJKL", 4) == 0);
        WriteFmt("i32_val multi-char test: comparing memory with 'IJKL', pass = {}\n", i32_multi_pass ? "true" : "false");
        success = success && i32_multi_pass;
        z       = "MNOPQRST";
        StrReadFmt(z, "{c}", u64_val);
        bool u64_multi_pass = (ZstrCompareN((const char *)&u64_val, "MNOPQRST", 8) == 0);
        WriteFmt(
            "u64_val multi-char test: comparing memory with 'MNOPQRST', pass = {}\n",
        z       = "UVWXYZab";
        StrReadFmt(z, "{c}", i64_val);
        bool i64_multi_pass = (ZstrCompareN((const char *)&i64_val, "UVWXYZab", 8) == 0);
        WriteFmt(
            "i64_val multi-char test: comparing memory with 'UVWXYZab', pass = {}\n",
        z       = "XY";
        StrReadFmt(z, "{c}", u32_val);
        bool xy_pass = (ZstrCompareN((const char *)&u32_val, "XY", 2) == 0);
        WriteFmt("u32_val partial test: comparing memory with 'XY', pass = {}\n", xy_pass ? "true" : "false");
        success = success && xy_pass;
        z       = "abc";
        StrReadFmt(z, "{c}", u64_val);
        bool abc_pass = (ZstrCompareN((const char *)&u64_val, "abc", 3) == 0);
        WriteFmt("u64_val partial test: comparing memory with 'abc', pass = {}\n", abc_pass ? "true" : "false");
        success = success && abc_pass;
        // For negative numbers in non-decimal bases, it uses unsigned representation
        // -0xABCD = -(43981) = large positive number when treated as unsigned
        result = result && (ZstrCompareN(s.data, "0x", 2) == 0);
        if (!result) {
            WriteFmt("    FAIL: Expected hex prefix '0x', got '{}'\n", s.data);
    
        // Length should now be 3 and content should be "Hel"
        result = result && (s.length == 3) && (ZstrCompareN(s.data, "Hel", 3) == 0);
    
        // Resize to a larger length
        // Length should now be 8, and the first 3 characters should still be "Hel"
        // The rest will be filled with zeros
        result = result && (s.length == 8) && (ZstrCompareN(s.data, "Hel", 3) == 0);
    
        StrDeinit(&s);
        // Test StrFindCstr
        const char *found5 = StrFindCstr(&haystack, "Wor", 3);
        result             = result && (found5 != NULL && ZstrCompareN(found5, "World", 3) == 0);
    
        StrDeinit(&haystack);
    
        // Check that it's initialized correctly
        bool result = (s.length == len && ZstrCompareN(s.data, test_str, len) == 0 && s.data[len] == '\0');
    
        StrDeinit(&s);
    /// RETURN : 0 if both are equal
    ///
    #define StrCmpCstr(str, cstr, cstr_len) ZstrCompareN((str)->data, cstr, cstr_len)
    
    ///
Last updated on