From 8114c84436424618f10bbfdab3bf9635f2781196 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 16 Feb 2021 11:09:43 +0100 Subject: Tweaked. --- stc/cstr.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/stc/cstr.h b/stc/cstr.h index b1f2c11d..42c0648f 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -398,13 +398,14 @@ STC_DEF void* c_memccpy(void* dst, const void* src, int c, size_t n) { enum {w = sizeof(uintptr_t)}; #define _mc_z (~(uintptr_t)0/255) - const uint8_t* s = (const uint8_t *) src; uint8_t *d = (uint8_t *) dst, *e = d + n; - 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 + const uint8_t* s = (const uint8_t *) src; + uint8_t *d = (uint8_t *) dst, *end = d + n; + for (uintptr_t x; d + w <= end; s += w, d += w) { + memcpy(&x, s, w); /* check if x contains c: */ + if (((x - _mc_z) & ~x & _mc_z*128) ^ (_mc_z*(uint8_t) c)) break; memcpy(d, &x, w); } - while (d < e) if ((*d++ = *s++) == (uint8_t) c) return d; + while (d < end) if ((*d++ = *s++) == (uint8_t) c) return d; return NULL; } -- cgit v1.2.3