From bae3c479b2f1ff807553d1b9ff3b561cc4444054 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 16 Feb 2021 09:15:51 +0100 Subject: Fix/simplify c_memccpy() --- stc/cstr.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/stc/cstr.h b/stc/cstr.h index 5408548e..b1f2c11d 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -49,7 +49,7 @@ STC_API size_t cstr_find(cstr_t s, const char* needle); STC_API size_t cstr_find_n(cstr_t s, const char* needle, size_t pos, size_t nlen); STC_API size_t cstr_ifind_n(cstr_t s, const char* needle, size_t pos, size_t nlen); -STC_API void* c_memccpy(void* restrict dst, const void* restrict src, int c, size_t n); +STC_API void* c_memccpy(void* dst, const void* src, int c, size_t n); STC_API int c_strncasecmp(const char* s1, const char* s2, size_t n); STC_API char* c_strnfind(const char* s, const char* needle, size_t nmax); STC_API char* c_istrnfind(const char* s, const char* needle, size_t nmax); @@ -395,15 +395,13 @@ cstr_ifind_n(cstr_t s, const char* needle, size_t pos, size_t nlen) { /* http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord */ STC_DEF void* -c_memccpy(void* restrict dst, const void* restrict src, int c, size_t n) { +c_memccpy(void* dst, const void* src, int c, size_t n) { enum {w = sizeof(uintptr_t)}; - #define __z (~(uintptr_t)0/255) + #define _mc_z (~(uintptr_t)0/255) const uint8_t* s = (const uint8_t *) src; uint8_t *d = (uint8_t *) dst, *e = d + n; - const uint8_t *align = (const uint8_t *)(((uintptr_t)s + w-1) & ~(w-1)); - while (s != align) if ((*d++ = *s++) == (uint8_t) c) return d; - for (; d + w <= e; s += w, d += w) { - const uintptr_t x = *(uintptr_t *) s; - if (((x - __z) & ~x & __z*128) ^ (__z * (uint8_t) c)) break; /* hasvalue */ + for (uintptr_t x; d + (w*2) <= e; s += w, d += w) { + memcpy(&x, s, w); + if (((x - _mc_z) & ~x & _mc_z*128) ^ (_mc_z*(uint8_t) c)) break; // hasvalue memcpy(d, &x, w); } while (d < e) if ((*d++ = *s++) == (uint8_t) c) return d; -- cgit v1.2.3