Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 15 Jan 2021 14:26:55 GMT
From:      Ed Maste <emaste@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: e7c49bf5d78c - stable/12 - clang-format libc string functions imported from musl
Message-ID:  <202101151426.10FEQtYF013686@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/12 has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=e7c49bf5d78cca59832d568c282a296a3f74529f

commit e7c49bf5d78cca59832d568c282a296a3f74529f
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2020-11-18 22:01:34 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2021-01-15 14:25:35 +0000

    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.
    
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit 4874ddfd373c582d9cd7fe915d2259295d75d1e5)
---
 lib/libc/string/memchr.c |  25 ++++----
 lib/libc/string/memmem.c | 147 ++++++++++++++++++++++++++++-------------------
 lib/libc/string/strstr.c | 147 +++++++++++++++++++++++++++++------------------
 3 files changed, 194 insertions(+), 125 deletions(-)

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 <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <string.h>
-#include <stdint.h>
 #include <limits.h>
+#include <stdint.h>
+#include <string.h>
 
 #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 <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <string.h>
 #include <stdint.h>
+#include <string.h>
 
-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<l; i++)
-		BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
+	for (i = 0; i < l; i++)
+		BITOP(byteset, n[i], |=), shift[n[i]] = i + 1;
 
 	/* Compute maximal suffix */
-	ip = -1; jp = 0; k = p = 1;
-	while (jp+k<l) {
-		if (n[ip+k] == n[jp+k]) {
+	ip = -1;
+	jp = 0;
+	k = p = 1;
+	while (jp + k < l) {
+		if (n[ip + k] == n[jp + k]) {
 			if (k == p) {
 				jp += p;
 				k = 1;
-			} else k++;
-		} else if (n[ip+k] > 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<l) {
-		if (n[ip+k] == n[jp+k]) {
+	ip = -1;
+	jp = 0;
+	k = p = 1;
+	while (jp + k < l) {
+		if (n[ip + k] == n[jp + k]) {
 			if (k == p) {
 				jp += p;
 				k = 1;
-			} else k++;
-		} else if (n[ip+k] < n[jp+k]) {
+			} else
+				k++;
+		} else if (n[ip + k] < n[jp + k]) {
 			jp += k;
 			k = 1;
 			p = jp - ip;
@@ -115,26 +130,31 @@ static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const
 			k = p = 1;
 		}
 	}
-	if (ip+1 > 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); k<l && n[k] == h[k]; k++);
+		for (k = MAX(ms + 1, mem); k < l && n[k] == h[k]; k++)
+			;
 		if (k < l) {
-			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;
 	}
 }
 
-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<l) return 0;
+	if (k < l)
+		return 0;
 
 	/* Use faster algorithms for short needles */
 	h = memchr(h0, *n, k);
-	if (!h || l==1) return (void *)h;
+	if (!h || l == 1)
+		return (void *)h;
 	k -= h - (const unsigned char *)h0;
-	if (k<l) return 0;
-	if (l==2) return twobyte_memmem(h, k, n);
-	if (l==3) return threebyte_memmem(h, k, n);
-	if (l==4) return fourbyte_memmem(h, k, n);
-
-	return twoway_memmem(h, h+k, n, l);
+	if (k < l)
+		return 0;
+	if (l == 2)
+		return twobyte_memmem(h, k, n);
+	if (l == 3)
+		return threebyte_memmem(h, k, n);
+	if (l == 4)
+		return fourbyte_memmem(h, k, n);
+
+	return twoway_memmem(h, h + k, n, l);
 }
diff --git a/lib/libc/string/strstr.c b/lib/libc/string/strstr.c
index c17ed8208f18..d726aff1f1c3 100644
--- a/lib/libc/string/strstr.c
+++ b/lib/libc/string/strstr.c
@@ -25,37 +25,44 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <string.h>
 #include <stdint.h>
+#include <string.h>
 
-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<l) {
-		if (n[ip+k] == n[jp+k]) {
+	ip = -1;
+	jp = 0;
+	k = p = 1;
+	while (jp + k < l) {
+		if (n[ip + k] == n[jp + k]) {
 			if (k == p) {
 				jp += p;
 				k = 1;
-			} else k++;
-		} else if (n[ip+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<l) {
-		if (n[ip+k] == n[jp+k]) {
+	ip = -1;
+	jp = 0;
+	k = p = 1;
+	while (jp + k < l) {
+		if (n[ip + k] == n[jp + k]) {
 			if (k == p) {
 				jp += p;
 				k = 1;
-			} else k++;
-		} else if (n[ip+k] < n[jp+k]) {
+			} else
+				k++;
+		} else if (n[ip + k] < n[jp + k]) {
 			jp += k;
 			k = 1;
 			p = jp - ip;
@@ -114,14 +129,17 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
 			k = p = 1;
 		}
 	}
-	if (ip+1 > 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);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202101151426.10FEQtYF013686>