summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-02-16 09:15:51 +0100
committerTyge Løvset <[email protected]>2021-02-16 09:15:51 +0100
commitbae3c479b2f1ff807553d1b9ff3b561cc4444054 (patch)
treef91ac70b8bd75e0ff74dec1cf8e94ec5b39593d7
parentadc8cc5f6e6ae66864c0266656d284bd6de2dfec (diff)
downloadSTC-modified-bae3c479b2f1ff807553d1b9ff3b561cc4444054.tar.gz
STC-modified-bae3c479b2f1ff807553d1b9ff3b561cc4444054.zip
Fix/simplify c_memccpy()
-rw-r--r--stc/cstr.h14
1 files 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;