shithub: scc

Download patch

ref: aa62db1b2f210ffaa3fbc4a9a2f30b116b0240ee
parent: 2699bac1e5869e2ee92f588d8c91b3a7d19fbaf0
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Sep 21 14:42:09 EDT 2018

[lib/c] Fix str?spn functions

--- a/lib/c/strcspn.c
+++ b/lib/c/strcspn.c
@@ -4,15 +4,17 @@
 size_t
 strcspn(const char *s1, const char *s2)
 {
-	char buf[__NUMCHARS];
-	unsigned char ch;
+	const unsigned char *s = s1;
+	const unsigned char *accept = s2;
+	unsigned ch;
 	size_t n;
+	char buf[__NUMCHARS];
 
 	memset(buf, 0, sizeof(buf));
-	while (ch = *s2++)
+	while (ch = *accept++)
 		buf[ch] = 1;
 
-	for (n = 0; (ch = *s1++) && !buf[ch]; ++n)
+	for (n = 0; (ch = *s++) && !buf[ch]; ++n)
 		;
 
 	return n;
--- a/lib/c/strpbrk.c
+++ b/lib/c/strpbrk.c
@@ -4,15 +4,17 @@
 char *
 strpbrk(const char *s1, const char *s2)
 {
-	char buf[__NUMCHARS];
+	const unsigned char *s = s1;
+	const unsigned char *accept = s2;
 	unsigned ch;
+	char buf[__NUMCHARS];
 
 	memset(buf, 0, sizeof(buf));
-	while (ch = *s2++)
+	while (ch = *accept++)
 		buf[ch] = 1;
 
-	while ((ch = *s1) && !buf[ch])
-		s1++;
+	while ((ch = *s) && !buf[ch])
+		s++;
 
-	return (ch == '\0') ? NULL : (char *) s1;
+	return (ch == '\0') ? NULL : (char *) s;
 }
--- a/lib/c/strspn.c
+++ b/lib/c/strspn.c
@@ -4,15 +4,17 @@
 size_t
 strspn(const char *s1, const char *s2)
 {
-	char buf[__NUMCHARS];
-	unsigned char ch;
+	const unsigned char *s = s1;
+	const unsigned char *accept = s2;
+	unsigned ch;
 	size_t n;
+	char buf[__NUMCHARS];
 
 	memset(buf, 0, sizeof(buf));
-	while (ch = *s2++)
+	while (ch = *accept++)
 		buf[ch] = 1;
 
-	for (n = 0; (ch = *s1++) && buf[ch]; ++n)
+	for (n = 0; (ch = *s++) && buf[ch]; ++n)
 		;
 
 	return n;
--- a/tests/libc/execute/0016-strcspn.c
+++ b/tests/libc/execute/0016-strcspn.c
@@ -14,7 +14,7 @@
 {
 	puts("testing");
 
-	assert(strcspn("012", "56789") == 3);
+	assert(strcspn("0125", "56789") == 3);
 	assert(strcspn("", "56789") == 0);
 	assert(strcspn("01234", "") == 5);
 	assert(strcspn("", "") == 0);
--- a/tests/libc/execute/0025-strspn.c
+++ b/tests/libc/execute/0025-strspn.c
@@ -13,8 +13,8 @@
 main()
 {
 	puts("testing");
-	assert(strspn("abcdef", "cba") == 3);
-	assert(strspn("abc", "cba0") == 3);
+	assert(strspn("abccdef", "cba") == 4);
+	assert(strspn("abcg", "cba0") == 3);
 	assert(strspn("", "abc") == 0);
 	assert(strspn("abc", "") == 0);
 	assert(strspn("", "") == 0);