diff options
author | Mattias Andrée <maandree@kth.se> | 2018-10-28 16:40:44 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-10-28 16:40:44 +0100 |
commit | f66c02620d394819a29267b6fe4b682589201cbf (patch) | |
tree | 71f8942398e77ff3a20ec2a463d6a2c9ac88edc2 | |
parent | Add doc to aligned_memdup.h, memdup.h, strdup.h, and strndup.h (diff) | |
download | libsimple-f66c02620d394819a29267b6fe4b682589201cbf.tar.gz libsimple-f66c02620d394819a29267b6fe4b682589201cbf.tar.bz2 libsimple-f66c02620d394819a29267b6fe4b682589201cbf.tar.xz |
m
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | libsimple/mem.h | 2 | ||||
-rw-r--r-- | memelem.c | 11 | ||||
-rw-r--r-- | mempsetelem.c | 6 | ||||
-rw-r--r-- | memrelem.c | 8 |
4 files changed, 19 insertions, 8 deletions
diff --git a/libsimple/mem.h b/libsimple/mem.h index 7a7f7ce..57a1f2b 100644 --- a/libsimple/mem.h +++ b/libsimple/mem.h @@ -420,7 +420,7 @@ void *libsimple_mempsetelem(void *__buf, const void *__item, size_t __size, size * @return `buf` */ _LIBSIMPLE_GCC_ONLY(__attribute__((__warn_unused_result__))) -static inline void *libsimple_memsetelem(void *__buf, const void *__item, size_t __size, size_t __nitems) +static inline void *libsimple_memsetelem(void *__buf, const void *__item, size_t __size, size_t __nitems) /* TODO test */ { return libsimple_mempsetelem(__buf, __item, __size, __nitems), __buf; } #ifndef memsetelem # define memsetelem libsimple_memsetelem @@ -43,9 +43,14 @@ libsimple_memelem(const void *hay_, size_t hayn, const void *sub_, size_t subn) { char *hay = (void *)hay_; const char *sub = sub_; - for (; hayn--; hay += subn) - if (!memcmp(hay, sub, subn)) - return hay; + size_t i; + for (; hayn--; hay += subn) { + for (i = 0; i < subn; i++) + if (hay[i] != sub[i]) + goto next; + return hay; + next:; + } break; } diff --git a/mempsetelem.c b/mempsetelem.c index d431604..c8990ed 100644 --- a/mempsetelem.c +++ b/mempsetelem.c @@ -4,7 +4,7 @@ void * -libsimple_mempsetelem(void *buf_, const void *item, size_t size, size_t nitems) +libsimple_mempsetelem(void *buf_, const void *item, size_t size, size_t nitems) /* TODO test */ { switch (size) { case 0: @@ -38,8 +38,10 @@ libsimple_mempsetelem(void *buf_, const void *item, size_t size, size_t nitems) default: { char *buf = buf_; + size_t i; for (; nitems--; buf += size) - memcpy(buf, item, size); + for (i = 0; i < size; i++) + buf[i] = ((const char *)item)[i]; return buf; } } @@ -50,10 +50,14 @@ libsimple_memrelem(const void *hay_, size_t hayn, const void *sub_, size_t subn) { char *hay = (void *)hay_; const char *sub = sub_; + size_t i; for (hay += hayn * subn; hayn--;) { hay -= subn; - if (!memcmp(hay, sub, subn)) - return hay; + for (i = 0; i < subn; i++) + if (hay[i] != sub[i]) + goto next; + return hay; + next:; } break; |