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

FREE

FREE Description Safely deallocates memory and nullifies pointer.

Read More

NEW

NEW Description Allocates zero-initialized memory for a type.

Read More

MAX2

MAX2 Description Returns the larger of two values x and y.

Read More