2011-02-12 00:22:29 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#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 *strchr(const char *s, int c)
|
|
|
|
{
|
2011-04-03 18:16:11 -04:00
|
|
|
size_t *w, k;
|
|
|
|
|
|
|
|
c = (unsigned char)c;
|
2011-02-12 00:22:29 -05:00
|
|
|
if (!c) return (char *)s + strlen(s);
|
2011-04-03 18:16:11 -04:00
|
|
|
|
2011-04-05 09:27:41 -04:00
|
|
|
for (; ((uintptr_t)s & ALIGN); s++)
|
2011-04-03 18:16:11 -04:00
|
|
|
if (*(unsigned char *)s == c) return (char *)s;
|
2011-04-05 09:27:41 -04:00
|
|
|
else if (!*s) return 0;
|
2011-04-03 18:16:11 -04:00
|
|
|
k = ONES * c;
|
|
|
|
for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
|
|
|
|
for (s = (void *)w; *s; s++)
|
|
|
|
if (*(unsigned char *)s == c) return (char *)s;
|
|
|
|
return 0;
|
2011-02-12 00:22:29 -05:00
|
|
|
}
|