1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| | #include <string.h>
#include <stdint.h>
#define HIGHS(x) ((x) - ((x)*0-1) / 255 * 128)
#define LOWS(x) (((x)*0-1) / 255)
#define has_char(x, c) ((x) - LOWS(x) & ~(x) & HIGHS(x) ^ LOWS(x) * (c))
void *memccpy(void *restrict d, const void *restrict s, int c, size_t n)
{
unsigned char *cd = (unsigned char *)d;
const unsigned char *cs = (const unsigned char *)s;
size_t *wd;
const size_t *ws;
c = (unsigned char)c;
if ((uintptr_t)s % sizeof(size_t) != (uintptr_t)d % sizeof(size_t)
|| n < sizeof(size_t))
goto bytewise;
for (; (uintptr_t)s % sizeof(size_t); *cd = *cs, cd++, cs++)
if (*cs == c) return cd + 1;
for (wd = (size_t *)d, ws = (const size_t *)s
; !has_char(*ws, c) && n >= sizeof(size_t)
; ws++, wd++, *wd = *ws);
cd = (unsigned char *)wd;
cs = (const unsigned char *)ws;
bytewise:
for (; *cs != c; *cd = *cs, cs++, cd++, n--)
if (!n) return NULL;
return cd + 1;
}
|