From e8a7e1c358caec60751460d337f634ff6957ff9d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 19 Nov 2018 21:39:11 +0100 Subject: Add a bunch of function and macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- memreplaceelem.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 memreplaceelem.c (limited to 'memreplaceelem.c') diff --git a/memreplaceelem.c b/memreplaceelem.c new file mode 100644 index 0000000..1620d5c --- /dev/null +++ b/memreplaceelem.c @@ -0,0 +1,83 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +void * +libsimple_memreplaceelem(void *restrict s_, const void *old_, const void *new_, size_t n, size_t width) /* TODO test, man */ +{ + switch (width) { + case 0: + return s_; + case 1: + { + uint8_t *restrict s = s_; + uint8_t old = *(const uint8_t *)old_; + uint8_t new = *(const uint8_t *)new_; + for (; n; s++, n--) + if (*s == old) + *s = new; + return s; + } + case 2: + { + uint16_t *restrict s = s_; + uint16_t old = *(const uint16_t *)old_; + uint16_t new = *(const uint16_t *)new_; + for (; n; s++, n--) + if (*s == old) + *s = new; + return s; + } + case 4: + { + uint32_t *restrict s = s_; + uint32_t old = *(const uint32_t *)old_; + uint32_t new = *(const uint32_t *)new_; + for (; n; s++, n--) + if (*s == old) + *s = new; + return s; + } + case 8: + { + uint64_t *restrict s = s_; + uint64_t old = *(const uint64_t *)old_; + uint64_t new = *(const uint64_t *)new_; + for (; n; s++, n--) + if (*s == old) + *s = new; + return s; + } + default: + { + char *restrict s = s_; + const char *old = old_; + const char *new = new_; + size_t i; + for (; n; s += width, n--) { + if (*s == *old) { + for (i = 0; i < width; i++) + if (s[i] != old[i]) + goto next; + for (i = 0; i < width; i++) + s[i] = new[i]; + } + next:; + } + return s; + } + } +} + + +#else +#include "test.h" + +int +main(void) +{ + return 0; +} + +#endif -- cgit v1.2.3-70-g09d2