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
| | #include <stdint.h>
#include <string.h>
#include "word.h"
/**
* strncmp - Word sized c standard strncmp.
* @c: Comparative
* @s: Source
* @n: Max size of @s
*/
int strncmp(const char *c, const char *s, size_t n)
{
const size_t *wc, *ws;
if ((uintptr_t)c % sizeof(size_t) != (uintptr_t)s % sizeof(size_t))
goto misaligned;
for (; (uintptr_t)c % sizeof(size_t); c++, s++, n--) {
if (*c != *s || !*c || !*s || !n)
return *(const unsigned char *)c
- *(const unsigned char *)s;
}
for (wc = (const size_t *)c, ws = (const size_t *)s
; !word_has_zero(*wc) || !word_has_zero(*ws)
; wc++, ws++);
c = (const char *)wc;
s = (const char *)ws;
misaligned:
for(; *c == *s && *c && *s && n; c++, s++, n--);
return *(const unsigned char *)c - *(const unsigned char *)s;
}
|