/
clang
/
musl
Обзор
Документация
Войти
/
clang
/
musl
Код
Вики
Пакеты
0
Релизы
0
Аналитика
master
src/string/strlen.c
22 строки
514 B
Rich Felker
fix aliasing-based undefined behavior in string functions
26 сен 2018, 21:39
26 сен 2018, 21:39
4d0a821
Код
Авторство
О чём код?
#include <string.h> #include <stdint.h> #include <limits.h> #define ALIGN (sizeof(size_t)) #define ONES ((size_t)-1/UCHAR_MAX) #define HIGHS (ONES * (UCHAR_MAX/2+1)) #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) size_t strlen(const char *s) { const char *a = s; #ifdef __GNUC__ typedef size_t __attribute__((__may_alias__)) word; const word *w; for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a; for (w = (const void *)s; !HASZERO(*w); w++); s = (const void *)w; #endif for (; *s; s++); return s-a; }