diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2025-12-05 18:48:44 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2025-12-05 18:48:44 +0100 |
| commit | fc6d44be383213b7609b5e3c4778e235fb6b00c3 (patch) | |
| tree | 9806078c7fdae4c79711bfc0a7aac574e036c214 | |
| parent | 178d566278f580ed5625d2d34831b4d263ee09af (diff) | |
| download | teachos-fc6d44be383213b7609b5e3c4778e235fb6b00c3.tar.xz teachos-fc6d44be383213b7609b5e3c4778e235fb6b00c3.zip | |
kstd: implement memcmp
| -rw-r--r-- | libs/kstd/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | libs/kstd/src/libc/string.cpp | 15 |
2 files changed, 16 insertions, 0 deletions
diff --git a/libs/kstd/CMakeLists.txt b/libs/kstd/CMakeLists.txt index 75c2315..e0c551c 100644 --- a/libs/kstd/CMakeLists.txt +++ b/libs/kstd/CMakeLists.txt @@ -4,6 +4,7 @@ add_library("libs::kstd" ALIAS "kstd") set(KSTD_LIBC_SYMBOLS "abort" "strlen" + "memcmp" ) target_sources("kstd" PRIVATE diff --git a/libs/kstd/src/libc/string.cpp b/libs/kstd/src/libc/string.cpp index a42aedc..a9b85fc 100644 --- a/libs/kstd/src/libc/string.cpp +++ b/libs/kstd/src/libc/string.cpp @@ -1,5 +1,6 @@ #include <algorithm> #include <cstddef> +#include <span> namespace kstd::libc { @@ -10,6 +11,20 @@ namespace kstd::libc { return std::distance(string, std::ranges::find(string, nullptr, '\0')); } + + auto memcmp(void const * lhs, void const * rhs, std::size_t size) -> std::size_t + { + auto left_span = std::span{static_cast<std::byte const *>(lhs), size}; + auto right_span = std::span{static_cast<std::byte const *>(rhs), size}; + auto mismatched = std::ranges::mismatch(left_span, right_span); + + if (mismatched.in1 == left_span.end()) + { + return 0; + } + + return std::bit_cast<char>(*mismatched.in1) - std::bit_cast<char>(*mismatched.in2); + } } } // namespace kstd::libc
\ No newline at end of file |
