ZstrCompareN
- Function
- October 8, 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
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:97
:
}
i32 ZstrCompareN(const char *s1, const char *s2, size n) {
if (!s1 || !s2) {
LOG_FATAL("Invalid arguments");
- In
Str.c:135
:
prev = next + keylen; // skip past delimiter
} else {
if (ZstrCompareN(prev, key, end - prev)) {
Str tmp = StrInitFromCstr(prev, end - prev);
VecPushBack(&sv, tmp); // remaining part
// 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);
- In
Io.Read.c:499
:
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(
- In
Io.Read.c:513
:
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;
- In
Io.Read.c:520
:
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;
- In
Io.Read.c:527
:
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;
- In
Io.Read.c:534
:
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",
- In
Io.Read.c:544
:
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",
- In
Io.Read.c:575
:
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;
- In
Io.Read.c:582
:
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;
- 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
Str.Ops.c:81
:
// Test StrFindCstr
const char *found5 = StrFindCstr(&haystack, "Wor", 3);
result = result && (found5 != NULL && ZstrCompareN(found5, "World", 3) == 0);
StrDeinit(&haystack);
- 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
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;
- In
Ops.h:42
:
/// RETURN : 0 if both are equal
///
#define StrCmpCstr(str, cstr, cstr_len) ZstrCompareN((str)->data, cstr, cstr_len)
///