From 97575a239b8f4d87a8f7ad5bb324c8e71c02d4d9 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 26 Mar 2021 10:26:10 +0200 Subject: clang-tidy fixes --- libk/string/memory.cc | 37 +++++++++++++++++++++++++++++++++++++ libk/string/string.cc | 26 -------------------------- 2 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 libk/string/memory.cc delete mode 100644 libk/string/string.cc (limited to 'libk/string') diff --git a/libk/string/memory.cc b/libk/string/memory.cc new file mode 100644 index 0000000..5b8a1f8 --- /dev/null +++ b/libk/string/memory.cc @@ -0,0 +1,37 @@ +#include + +extern "C" void* memcpy(void* dstptr, const void* srcptr, const size_t n) { + auto* dst = static_cast(dstptr); + const auto* src = static_cast(srcptr); + for (size_t i = 0; i < n; ++i) dst[i] = src[i]; + return dstptr; +} + +extern "C" void* memset(void* bufptr, const int value, const size_t n) { + auto* buf = static_cast(bufptr); + for (size_t i = 0; i < n; ++i) buf[i] = static_cast(value); + return bufptr; +} + +extern "C" void* memmove(void* dstptr, const void* srcptr, const size_t n) { + auto* dst = static_cast(dstptr); + const auto* src = static_cast(srcptr); + if (dst < src) { + for (size_t i = 0; i < n; ++i) dst[i] = src[i]; + } else { + for (size_t i = n; i != 0; i--) dst[i - 1] = src[i - 1]; + } + return dstptr; +} + +extern "C" int memcmp(const void* aptr, const void* bptr, const size_t n) { + const auto* a = static_cast(aptr); + const auto* b = static_cast(bptr); + for (size_t i = 0; i < n; ++i) { + if (a[i] < b[i]) + return -1; + else if (b[i] < a[i]) + return 1; + } + return 0; +} diff --git a/libk/string/string.cc b/libk/string/string.cc deleted file mode 100644 index c75feb1..0000000 --- a/libk/string/string.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include "string.h" - -// TODO - -// int strcpy(char *dst, const char *stc); -// void strcat(void *dst, const void *src); -// char* strncpy(char *dest, const char *src, int length); -// int strncmp(const char *s1, const char *s2, int c); - -/* strcmp - compare two C-strings */ -constexpr int strcmp(const char* s1, const char* s2) { - const auto s1_len = strlen(s1); - for (size_t i = 0; i < s1_len; ++i) { - if (s1[i] == s2[i]) - continue; - if (s1[i] > s2[i]) - return 1; - if (s1[i] < s2[i]) - return -1; - } - return 0; -} - -static_assert(strcmp("one", "one") == 0); -static_assert(strcmp("one", "two") < 0); -static_assert(strcmp("foo", "bar") > 0); -- cgit v1.2.1