#include "byte.h" /** @file byte.c @author djb @brief byte manipulation functions */ unsigned int byte_chr(char *s, unsigned int n, int c) { char ch = c; char *t = s; for (;;) { // clang-format off if (!n) { break; } if (*t == ch) { break; } ++t; --n; if (!n) { break; } if (*t == ch) { break; } ++t; --n; if (!n) { break; } if (*t == ch) { break; } ++t; --n; if (!n) { break; } if (*t == ch) { break; } ++t; --n; // clang-format on } return t - s; } void byte_copy(char *to, unsigned int n, char *from) { for (;;) { // clang-format off if (!n) { return; } *to++ = *from++; --n; if (!n) { return; } *to++ = *from++; --n; if (!n) { return; } *to++ = *from++; --n; if (!n) { return; } *to++ = *from++; --n; // clang-format on } } void byte_copyr(char *to, unsigned int n, char *from) { to += n; from += n; for (;;) { // clang-format off if (!n) { return; } *--to = *--from; --n; if (!n) { return; } *--to = *--from; --n; if (!n) { return; } *--to = *--from; --n; if (!n) { return; } *--to = *--from; --n; // clang-format on } } int byte_diff(char *s, unsigned int n, char *t) { for (;;) { // clang-format off if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n; if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n; if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n; if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n; // clang-format on } return ((int)(unsigned int)(unsigned char)*s) - ((int)(unsigned int)(unsigned char)*t); } unsigned int byte_rchr(char *s, unsigned int n, int c) { char ch = c; char *t = s; char *u = 0; for (;;) { // clang-format off if (!n) { break; } if (*t == ch) { u = t; } ++t; --n; if (!n) { break; } if (*t == ch) { u = t; } ++t; --n; if (!n) { break; } if (*t == ch) { u = t; } ++t; --n; if (!n) { break; } if (*t == ch) { u = t; } ++t; --n; // clang-format on } if (!u) u = t; return u - s; } void byte_zero(char *s, unsigned int n) { for (;;) { // clang-format off if (!n) { break; } *s++ = 0; --n; if (!n) { break; } *s++ = 0; --n; if (!n) { break; } *s++ = 0; --n; if (!n) { break; } *s++ = 0; --n; // clang-format on } } void byte_fill(char *s, unsigned int n, int c) { char ch = c; for (;;) { // clang-format off if (!n) { break; } *s++ = ch; --n; if (!n) { break; } *s++ = ch; --n; if (!n) { break; } *s++ = ch; --n; if (!n) { break; } *s++ = ch; --n; // clang-format on } }