From owner-svn-src-head@freebsd.org Wed Nov 18 22:01:35 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 5CC54473808; Wed, 18 Nov 2020 22:01:35 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Cbxbg2F1Fz4kCh; Wed, 18 Nov 2020 22:01:35 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 357AA18DF6; Wed, 18 Nov 2020 22:01:35 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0AIM1ZTu089309; Wed, 18 Nov 2020 22:01:35 GMT (envelope-from emaste@FreeBSD.org) Received: (from emaste@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0AIM1Y9E089305; Wed, 18 Nov 2020 22:01:34 GMT (envelope-from emaste@FreeBSD.org) Message-Id: <202011182201.0AIM1Y9E089305@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: emaste set sender to emaste@FreeBSD.org using -f From: Ed Maste Date: Wed, 18 Nov 2020 22:01:34 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367821 - head/lib/libc/string X-SVN-Group: head X-SVN-Commit-Author: emaste X-SVN-Commit-Paths: head/lib/libc/string X-SVN-Commit-Revision: 367821 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Nov 2020 22:01:35 -0000 Author: emaste Date: Wed Nov 18 22:01:34 2020 New Revision: 367821 URL: https://svnweb.freebsd.org/changeset/base/367821 Log: clang-format libc string functions imported from musl We have adopted these and don't consider them 'contrib' code, so bring them closer to style(9). This is a followon to r315467 and r351700. MFC after: 1 week Sponsored by: The FreeBSD Foundation Modified: head/lib/libc/string/memchr.c head/lib/libc/string/memmem.c head/lib/libc/string/strstr.c Modified: head/lib/libc/string/memchr.c ============================================================================== --- head/lib/libc/string/memchr.c Wed Nov 18 21:26:14 2020 (r367820) +++ head/lib/libc/string/memchr.c Wed Nov 18 22:01:34 2020 (r367821) @@ -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; } Modified: head/lib/libc/string/memmem.c ============================================================================== --- head/lib/libc/string/memmem.c Wed Nov 18 21:26:14 2020 (r367820) +++ head/lib/libc/string/memmem.c Wed Nov 18 22:01:34 2020 (r367821) @@ -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, s * 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, con 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, con } /* 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, c * 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, con 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, con 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, con /* 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, con } /* 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); }