xref: /aosp_15_r20/external/musl/src/string/stpcpy.c (revision c9945492fdd68bbe62686c5b452b4dc1be3f8453)
1*c9945492SAndroid Build Coastguard Worker #include <string.h>
2*c9945492SAndroid Build Coastguard Worker #include <stdint.h>
3*c9945492SAndroid Build Coastguard Worker #include <limits.h>
4*c9945492SAndroid Build Coastguard Worker 
5*c9945492SAndroid Build Coastguard Worker #define ALIGN (sizeof(size_t))
6*c9945492SAndroid Build Coastguard Worker #define ONES ((size_t)-1/UCHAR_MAX)
7*c9945492SAndroid Build Coastguard Worker #define HIGHS (ONES * (UCHAR_MAX/2+1))
8*c9945492SAndroid Build Coastguard Worker #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
9*c9945492SAndroid Build Coastguard Worker 
__stpcpy(char * restrict d,const char * restrict s)10*c9945492SAndroid Build Coastguard Worker char *__stpcpy(char *restrict d, const char *restrict s)
11*c9945492SAndroid Build Coastguard Worker {
12*c9945492SAndroid Build Coastguard Worker #ifdef __GNUC__
13*c9945492SAndroid Build Coastguard Worker 	typedef size_t __attribute__((__may_alias__)) word;
14*c9945492SAndroid Build Coastguard Worker 	word *wd;
15*c9945492SAndroid Build Coastguard Worker 	const word *ws;
16*c9945492SAndroid Build Coastguard Worker 	if ((uintptr_t)s % ALIGN == (uintptr_t)d % ALIGN) {
17*c9945492SAndroid Build Coastguard Worker 		for (; (uintptr_t)s % ALIGN; s++, d++)
18*c9945492SAndroid Build Coastguard Worker 			if (!(*d=*s)) return d;
19*c9945492SAndroid Build Coastguard Worker 		wd=(void *)d; ws=(const void *)s;
20*c9945492SAndroid Build Coastguard Worker 		for (; !HASZERO(*ws); *wd++ = *ws++);
21*c9945492SAndroid Build Coastguard Worker 		d=(void *)wd; s=(const void *)ws;
22*c9945492SAndroid Build Coastguard Worker 	}
23*c9945492SAndroid Build Coastguard Worker #endif
24*c9945492SAndroid Build Coastguard Worker 	for (; (*d=*s); s++, d++);
25*c9945492SAndroid Build Coastguard Worker 
26*c9945492SAndroid Build Coastguard Worker 	return d;
27*c9945492SAndroid Build Coastguard Worker }
28*c9945492SAndroid Build Coastguard Worker 
29*c9945492SAndroid Build Coastguard Worker weak_alias(__stpcpy, stpcpy);
30