ZstrCompareN
- Function
- August 22, 2025
Table of Contents
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)
- In
Ops.h:42
:
/// RETURN : 0 if both are equal
///
#define StrCmpCstr(str, cstr, cstr_len) ZstrCompareN((str)->data, cstr, cstr_len)
///
- In
JSON.c:475
:
if (StrIterPeek(&si) == 't') {
const char* pos = StrIterPos(&si);
if (pos && ZstrCompareN(pos, "true", 4) == 0) {
StrIterMove(&si, 4);
*b = true;
- In
JSON.c:487
:
if (StrIterPeek(&si) == 'f') {
const char* pos = StrIterPos(&si);
if (pos && ZstrCompareN(pos, "false", 5) == 0) {
StrIterMove(&si, 5);
*b = false;
- In
JSON.c:525
:
if (StrIterPeek(&si) == 'n') {
const char* pos = StrIterPos(&si);
if (pos && ZstrCompareN(pos, "null", 4) == 0) {
StrIterMove(&si, 4);
*is_null = true;
- In
Memory.c:96
:
}
i32 ZstrCompareN(const char* s1, const char* s2, size n) {
if (!s1 || !s2) {
LOG_FATAL("Invalid arguments");
- In
Str.c:134
:
prev = next + keylen; // skip past delimiter
} else {
if (ZstrCompareN(prev, key, end - prev)) {
Str tmp = StrInitFromCstr(prev, end - prev);
VecPushBack(&sv, tmp); // remaining part
- In
MisraEnum.c:154
:
// 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);
- In
Str.Memory.c:77
:
// 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
- In
Str.Memory.c:84
:
// 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);
- In
Str.Init.c:50
:
// Check that it's initialized correctly
bool result = (s.length == len && ZstrCompareN(s.data, test_str, len) == 0 && s.data[len] == '\0');
StrDeinit(&s);
- In
Io.Read.c:422
:
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(
- In
Io.Read.c:436
:
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;
- In
Io.Read.c:443
:
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;
- In
Io.Read.c:450
:
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;
- In
Io.Read.c:457
:
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;
- In
Io.Read.c:464
:
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;
- In
Io.Read.c:491
:
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;
- In
Io.Read.c:497
:
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;
- In
Str.Ops.c:81
:
// Test StrFindCstr
const char* found5 = StrFindCstr(&haystack, "Wor", 3);
result = result && (found5 != NULL && ZstrCompareN(found5, "World", 3) == 0);
StrDeinit(&haystack);