ZstrCompareN

Table of Contents

ZstrCompareN

Description

Compare two strings lexicographically up to n characters.

Parameters

NameDirectionDescription
s1inFirst string.
s2inSecond string.
ninMaximum 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)

    /// RETURN : 0 if both are equal
    ///
    #define StrCmpCstr(str, cstr, cstr_len) ZstrCompareN((str)->data, cstr, cstr_len)
    
    ///
    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;
    }
    
    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
    // 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;
    // 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) {
    printf("    FAIL: Expected hex prefix '0x', got '%s'\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);
    
    // Check that it's initialized correctly
    bool result = (s.length == len && ZstrCompareN(s.data, test_str, len) == 0 && s.data[len] == '\0');
    
    StrDeinit(&s);
    u16_val = 0;
    StrReadFmt("AB", "{c}", u16_val);
    bool u16_multi_pass = (ZstrCompareN((const char*)&u16_val, "AB", 2) == 0);
    printf("u16_val multi-char test: comparing memory with 'AB', pass = %s\n", u16_multi_pass ? "true" : "false");
    printf(
    i16_val = 0;
    StrReadFmt("CD", "{c}", i16_val);
    bool i16_multi_pass = (ZstrCompareN((const char*)&i16_val, "CD", 2) == 0);
    printf("i16_val multi-char test: comparing memory with 'CD', pass = %s\n", i16_multi_pass ? "true" : "false");
    success = success && i16_multi_pass;
    u32_val = 0;
    StrReadFmt("EFGH", "{c}", u32_val);
    bool u32_multi_pass = (ZstrCompareN((const char*)&u32_val, "EFGH", 4) == 0);
    printf("u32_val multi-char test: comparing memory with 'EFGH', pass = %s\n", u32_multi_pass ? "true" : "false");
    success = success && u32_multi_pass;
    i32_val = 0;
    StrReadFmt("IJKL", "{c}", i32_val);
    bool i32_multi_pass = (ZstrCompareN((const char*)&i32_val, "IJKL", 4) == 0);
    printf("i32_val multi-char test: comparing memory with 'IJKL', pass = %s\n", i32_multi_pass ? "true" : "false");
    success = success && i32_multi_pass;
    u64_val = 0;
    StrReadFmt("MNOPQRST", "{c}", u64_val);
    bool u64_multi_pass = (ZstrCompareN((const char*)&u64_val, "MNOPQRST", 8) == 0);
    printf("u64_val multi-char test: comparing memory with 'MNOPQRST', pass = %s\n", u64_multi_pass ? "true" : "false");
    success = success && u64_multi_pass;
    i64_val = 0;
    StrReadFmt("UVWXYZab", "{c}", i64_val);
    bool i64_multi_pass = (ZstrCompareN((const char*)&i64_val, "UVWXYZab", 8) == 0);
    printf("i64_val multi-char test: comparing memory with 'UVWXYZab', pass = %s\n", i64_multi_pass ? "true" : "false");
    success = success && i64_multi_pass;
    u32_val = 0;
    StrReadFmt("XY", "{c}", u32_val);
    bool xy_pass = (ZstrCompareN((const char*)&u32_val, "XY", 2) == 0);
    printf("u32_val partial test: comparing memory with 'XY', pass = %s\n", xy_pass ? "true" : "false");
    success = success && xy_pass;
    u64_val = 0;
    StrReadFmt("abc", "{c}", u64_val);
    bool abc_pass = (ZstrCompareN((const char*)&u64_val, "abc", 3) == 0);
    printf("u64_val partial test: comparing memory with 'abc', pass = %s\n", abc_pass ? "true" : "false");
    success = success && abc_pass;
    // Test StrFindCstr
    const char* found5 = StrFindCstr(&haystack, "Wor", 3);
    result             = result && (found5 != NULL && ZstrCompareN(found5, "World", 3) == 0);
    
    StrDeinit(&haystack);

Share :

Related Posts

JR_STR_KV

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

Read More

JW_STR

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

Read More

StrWriteFmtInternal

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

Read More