I'm trying to adapt the following version of the stpcpy
function to use restrict
-qualified pointers as its arguments and internally, but I'm not sure if simply adding the qualifier would result introduce undefined behavior.
#define ALIGN (sizeof(size_t)-1)
#define ONES ((size_t)-1/UCHAR_MAX)
#define HIGHS (ONES * (UCHAR_MAX/2+1))
#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
char *__stpcpy(char *d, const char *s)
{
size_t *wd;
const size_t *ws;
if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
for (; (*d=*s) && ((uintptr_t)s & ALIGN); s++, d++);
if (!*s) return d;
wd=(void *)d; ws=(const void *)s;
for (; !HASZERO(*ws); *wd++ = *ws++);
d=(void *)wd; s=(const void *)ws;
}
for (; (*d=*s); s++, d++);
return d;
}
Assuming the rules in C99 6.7.3.1 about accessing an object pertain only to the individual object accessed and not the whole array, I think it may be fine, as the elements written are only accessed once, and only for writing. But I'm rather uncomfortable with using restrict
at this point and don't want to rely on just my own judgement.