diff --git a/lib/libc/string/memchr.c b/lib/libc/string/memchr.c index d89d09775272..4b5d6f6a4c8a 100644 --- a/lib/libc/string/memchr.c +++ b/lib/libc/string/memchr.c @@ -25,30 +25,35 @@ #include __FBSDID("$FreeBSD$"); -#include -#include #include +#include +#include #define SS (sizeof(size_t)) -#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) +#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) -void *memchr(const void *src, int c, size_t n) +void * +memchr(const void *src, int c, size_t n) { const unsigned char *s = src; c = (unsigned char)c; #ifdef __GNUC__ - for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--); + for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--) + ; if (n && *s != c) { typedef size_t __attribute__((__may_alias__)) word; const word *w; size_t k = ONES * c; - for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS); + for (w = (const void *)s; n >= SS && !HASZERO(*w ^ k); + w++, n -= SS) + ; s = (const void *)w; } #endif - for (; n && *s != c; s++, n--); + for (; n && *s != c; s++, n--) + ; return n ? (void *)s : 0; } diff --git a/lib/libc/string/memmem.c b/lib/libc/string/memmem.c index e0a65a6a3f91..27863b9db623 100644 --- a/lib/libc/string/memmem.c +++ b/lib/libc/string/memmem.c @@ -25,40 +25,47 @@ #include __FBSDID("$FreeBSD$"); -#include #include +#include -static char *twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) +static char * +twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) { - uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; - for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++) - if (hw == nw) return (char *)h-2; - return hw == nw ? (char *)h-2 : 0; + uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1]; + for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++) + if (hw == nw) + return (char *)h - 2; + return hw == nw ? (char *)h - 2 : 0; } -static char *threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) +static char * +threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) { - uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; - uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; - for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8) - if (hw == nw) return (char *)h-3; - return hw == nw ? (char *)h-3 : 0; + uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8; + uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8; + for (h += 3, k -= 3; k; k--, hw = (hw | *h++) << 8) + if (hw == nw) + return (char *)h - 3; + return hw == nw ? (char *)h - 3 : 0; } -static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) +static char * +fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) { - uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; - uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; - for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++) - if (hw == nw) return (char *)h-4; - return hw == nw ? (char *)h-4 : 0; + uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3]; + uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3]; + for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++) + if (hw == nw) + return (char *)h - 4; + return hw == nw ? (char *)h - 4 : 0; } -#define MAX(a,b) ((a)>(b)?(a):(b)) -#define MIN(a,b) ((a)<(b)?(a):(b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define BITOP(a,b,op) \ - ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) +#define BITOP(a, b, op) \ + ((a)[(size_t)(b) / (8 * sizeof *(a))] op \ + (size_t)1 << ((size_t)(b) % (8 * sizeof *(a)))) /* * Two Way string search algorithm, with a bad shift table applied to the last @@ -68,25 +75,30 @@ static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned ch * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching, * Journal of the ACM 38(3):651-675 */ -static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l) +static char * +twoway_memmem(const unsigned char *h, const unsigned char *z, + const unsigned char *n, size_t l) { size_t i, ip, jp, k, p, ms, p0, mem, mem0; size_t byteset[32 / sizeof(size_t)] = { 0 }; size_t shift[256]; /* Computing length of needle and fill shift table */ - for (i=0; i n[jp+k]) { + } else + k++; + } else if (n[ip + k] > n[jp + k]) { jp += k; k = 1; p = jp - ip; @@ -99,14 +111,17 @@ static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const p0 = p; /* And with the opposite comparison */ - ip = -1; jp = 0; k = p = 1; - while (jp+k ms+1) ms = ip; - else p = p0; + if (ip + 1 > ms + 1) + ms = ip; + else + p = p0; /* Periodic needle? */ - if (memcmp(n, n+p, ms+1)) { + if (memcmp(n, n + p, ms + 1)) { mem0 = 0; - p = MAX(ms, l-ms-1) + 1; - } else mem0 = l-p; + p = MAX(ms, l - ms - 1) + 1; + } else + mem0 = l - p; mem = 0; /* Search loop */ for (;;) { /* If remainder of haystack is shorter than needle, done */ - if (z-h < l) return 0; + if (z - h < l) + return 0; /* Check last byte first; advance by shift on mismatch */ - if (BITOP(byteset, h[l-1], &)) { - k = l-shift[h[l-1]]; + if (BITOP(byteset, h[l - 1], &)) { + k = l - shift[h[l - 1]]; if (k) { - if (mem0 && mem && k < p) k = l-p; + if (mem0 && mem && k < p) + k = l - p; h += k; mem = 0; continue; @@ -146,38 +166,49 @@ static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const } /* Compare right half */ - for (k=MAX(ms+1,mem); kmem && n[k-1] == h[k-1]; k--); - if (k <= mem) return (char *)h; + for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--) + ; + if (k <= mem) + return (char *)h; h += p; mem = mem0; } } -void *memmem(const void *h0, size_t k, const void *n0, size_t l) +void * +memmem(const void *h0, size_t k, const void *n0, size_t l) { const unsigned char *h = h0, *n = n0; /* Return immediately on empty needle */ - if (!l) return (void *)h; + if (!l) + return (void *)h; /* Return immediately when needle is longer than haystack */ - if (k __FBSDID("$FreeBSD$"); -#include #include +#include -static char *twobyte_strstr(const unsigned char *h, const unsigned char *n) +static char * +twobyte_strstr(const unsigned char *h, const unsigned char *n) { - uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; - for (h++; *h && hw != nw; hw = hw<<8 | *++h); - return *h ? (char *)h-1 : 0; + uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1]; + for (h++; *h && hw != nw; hw = hw << 8 | *++h) + ; + return *h ? (char *)h - 1 : 0; } -static char *threebyte_strstr(const unsigned char *h, const unsigned char *n) +static char * +threebyte_strstr(const unsigned char *h, const unsigned char *n) { - uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; - uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; - for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8); - return *h ? (char *)h-2 : 0; + uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8; + uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8; + for (h += 2; *h && hw != nw; hw = (hw | *++h) << 8) + ; + return *h ? (char *)h - 2 : 0; } -static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) +static char * +fourbyte_strstr(const unsigned char *h, const unsigned char *n) { - uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; - uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; - for (h+=3; *h && hw != nw; hw = hw<<8 | *++h); - return *h ? (char *)h-3 : 0; + uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3]; + uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3]; + for (h += 3; *h && hw != nw; hw = hw << 8 | *++h) + ; + return *h ? (char *)h - 3 : 0; } -#define MAX(a,b) ((a)>(b)?(a):(b)) -#define MIN(a,b) ((a)<(b)?(a):(b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define BITOP(a,b,op) \ - ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) +#define BITOP(a, b, op) \ + ((a)[(size_t)(b) / (8 * sizeof *(a))] op \ + (size_t)1 << ((size_t)(b) % (8 * sizeof *(a)))) /* * Two Way string search algorithm, with a bad shift table applied to the last @@ -65,7 +72,8 @@ static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching, * Journal of the ACM 38(3):651-675 */ -static char *twoway_strstr(const unsigned char *h, const unsigned char *n) +static char * +twoway_strstr(const unsigned char *h, const unsigned char *n) { const unsigned char *z; size_t l, ip, jp, k, p, ms, p0, mem, mem0; @@ -73,19 +81,23 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n) size_t shift[256]; /* Computing length of needle and fill shift table */ - for (l=0; n[l] && h[l]; l++) - BITOP(byteset, n[l], |=), shift[n[l]] = l+1; - if (n[l]) return 0; /* hit the end of h */ + for (l = 0; n[l] && h[l]; l++) + BITOP(byteset, n[l], |=), shift[n[l]] = l + 1; + if (n[l]) + return 0; /* hit the end of h */ /* Compute maximal suffix */ - ip = -1; jp = 0; k = p = 1; - while (jp+k n[jp+k]) { + } else + k++; + } else if (n[ip + k] > n[jp + k]) { jp += k; k = 1; p = jp - ip; @@ -98,14 +110,17 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n) p0 = p; /* And with the opposite comparison */ - ip = -1; jp = 0; k = p = 1; - while (jp+k ms+1) ms = ip; - else p = p0; + if (ip + 1 > ms + 1) + ms = ip; + else + p = p0; /* Periodic needle? */ - if (memcmp(n, n+p, ms+1)) { + if (memcmp(n, n + p, ms + 1)) { mem0 = 0; - p = MAX(ms, l-ms-1) + 1; - } else mem0 = l-p; + p = MAX(ms, l - ms - 1) + 1; + } else + mem0 = l - p; mem = 0; /* Initialize incremental end-of-haystack pointer */ @@ -130,21 +148,24 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n) /* Search loop */ for (;;) { /* Update incremental end-of-haystack pointer */ - if (z-h < l) { + if (z - h < l) { /* Fast estimate for MIN(l,63) */ size_t grow = l | 63; const unsigned char *z2 = memchr(z, 0, grow); if (z2) { z = z2; - if (z-h < l) return 0; - } else z += grow; + if (z - h < l) + return 0; + } else + z += grow; } /* Check last byte first; advance by shift on mismatch */ - if (BITOP(byteset, h[l-1], &)) { - k = l-shift[h[l-1]]; + if (BITOP(byteset, h[l - 1], &)) { + k = l - shift[h[l - 1]]; if (k) { - if (k < mem) k = mem; + if (k < mem) + k = mem; h += k; mem = 0; continue; @@ -156,34 +177,46 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n) } /* Compare right half */ - for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++); + for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++) + ; if (n[k]) { - h += k-ms; + h += k - ms; mem = 0; continue; } /* Compare left half */ - for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--); - if (k <= mem) return (char *)h; + for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--) + ; + if (k <= mem) + return (char *)h; h += p; mem = mem0; } } -char *strstr(const char *h, const char *n) +char * +strstr(const char *h, const char *n) { /* Return immediately on empty needle */ - if (!n[0]) return (char *)h; + if (!n[0]) + return (char *)h; /* Use faster algorithms for short needles */ h = strchr(h, *n); - if (!h || !n[1]) return (char *)h; - if (!h[1]) return 0; - if (!n[2]) return twobyte_strstr((void *)h, (void *)n); - if (!h[2]) return 0; - if (!n[3]) return threebyte_strstr((void *)h, (void *)n); - if (!h[3]) return 0; - if (!n[4]) return fourbyte_strstr((void *)h, (void *)n); + if (!h || !n[1]) + return (char *)h; + if (!h[1]) + return 0; + if (!n[2]) + return twobyte_strstr((void *)h, (void *)n); + if (!h[2]) + return 0; + if (!n[3]) + return threebyte_strstr((void *)h, (void *)n); + if (!h[3]) + return 0; + if (!n[4]) + return fourbyte_strstr((void *)h, (void *)n); return twoway_strstr((void *)h, (void *)n); }