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
| | #include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "word.h"
/**
* memchr - Word sized c standard memchr.
* @s: Source
* @c: Character
* @n: Max size of @s
*/
void *memchr(const void *s, int c, size_t n)
{
const unsigned char *cs = (const unsigned char *)s;
const size_t *w;
c = (unsigned char)c;
for (; (uintptr_t)cs % sizeof(size_t); cs++, n--) {
if (!n) return NULL;
if (*cs == c) return (void *)cs;
}
for (w = (const size_t *)cs; !word_has_char(*w, c); w++, n--)
if (!n) return NULL;
for (cs = (const unsigned char *)w; *cs != c; cs++, n--)
if (!n) return NULL;
return (void *)cs;
}
|