From c5b40777dd47654f0a381327467c54bc31f03c85 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 24 Jun 2020 03:59:26 +0200 Subject: stdlib: add memcmp() and strncmp() --- src/pOS/arch/x86/libc/string/memcmp.cpp | 14 ++++++++++++++ src/pOS/arch/x86/libc/string/strncmp.cpp | 9 +++++++++ src/pOS/include/libc/string.h | 2 ++ 3 files changed, 25 insertions(+) create mode 100644 src/pOS/arch/x86/libc/string/memcmp.cpp create mode 100644 src/pOS/arch/x86/libc/string/strncmp.cpp (limited to 'src') diff --git a/src/pOS/arch/x86/libc/string/memcmp.cpp b/src/pOS/arch/x86/libc/string/memcmp.cpp new file mode 100644 index 0000000..ee5ee35 --- /dev/null +++ b/src/pOS/arch/x86/libc/string/memcmp.cpp @@ -0,0 +1,14 @@ +#include + +int memcmp(const void* ptr1, const void* ptr2, size_t size) +{ + int result = 0; + const uint8_t* p1 = static_cast(ptr1); + const uint8_t* p2 = static_cast(ptr2); + + for(size_t i = 0; i < size; i++) + if(*p1++ != *p2++) + return *--p1 - *--p2; + + return result; +} diff --git a/src/pOS/arch/x86/libc/string/strncmp.cpp b/src/pOS/arch/x86/libc/string/strncmp.cpp new file mode 100644 index 0000000..a1b8379 --- /dev/null +++ b/src/pOS/arch/x86/libc/string/strncmp.cpp @@ -0,0 +1,9 @@ +#include + +int strncmp(const char* str1, const char* str2, size_t len) +{ + size_t strln = strlen(str1); + strln = strln > strlen(str2) ? strlen(str2) : strln; + strln = strln > len ? len : strln; + return memcmp(str1, str2, strln); +} diff --git a/src/pOS/include/libc/string.h b/src/pOS/include/libc/string.h index 27b496c..66f93ed 100644 --- a/src/pOS/include/libc/string.h +++ b/src/pOS/include/libc/string.h @@ -7,7 +7,9 @@ size_t strlen(const char* str); void* memset(void* buf, int value, size_t size); void* memcpy(void* dst, const void* src, size_t size); +int memcmp(const void* ptr1, const void* ptr2, size_t size); char* strncpy(char* dst, const char* src, size_t len); char* strncat(char *dest, const char *src, size_t len); +int strncmp(const char* str1, const char* str2, size_t len); #endif -- cgit 1.4.1