MemCompare

Table of Contents

MemCompare

Description

Compare memory regions.

Parameters

NameDirectionDescription
p1inFirst memory region.
p2inSecond memory region.
ninNumber of bytes to compare.

Success

Returns 0 if equal, <0 if p1<p2, >0 if p1>p2.

Failure

Function cannot fail - always returns comparison result.

Usage example (Cross-references)

    #include <Misra/Std/Log.h>
    
    i32 MemCompare(const void *p1, const void *p2, size n) {
    if (!p1 || !p2) {
    LOG_FATAL("Invalid arguments");
    
    // Compare the substring
    if (MemCompare(haystack + pos, needle, needle_len) == 0) {
    return (char *)(haystack + pos);
    }
    
    static inline bool starts_with(const char *data, size data_len, const char *prefix, size prefix_len) {
    return data_len >= prefix_len && MemCompare(data, prefix, prefix_len) == 0;
    }
    
    static inline bool ends_with(const char *data, size data_len, const char *suffix, size suffix_len) {
    return data_len >= suffix_len && MemCompare(data + data_len - suffix_len, suffix, suffix_len) == 0;
    }
    
    while (i + match_len <= s->length && replaced < count) {
    if (MemCompare(s->data + i, match, match_len) == 0) {
    StrDeleteRange(s, i, match_len);
    StrInsertCstr(s, replacement, i, replacement_len);

Share :

Related Posts

ALIGN_UP

ALIGN_UP Description Aligns the given value up to the nearest multiple of alignment.

Read More

ALIGN_DOWN_POW2

ALIGN_DOWN_POW2 Description Aligns the given value down to the nearest power-of-two multiple.

Read More

LVAL

LVAL Description Creates a temporary, addressable l-value from a given expression x.

Read More