ref: 22f5e45998b00a0e48eb69282ab2c9a247defd13
parent: 232f814cbdfd8ab842db3419844f1988cbf8d367
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Dec 6 19:36:48 EST 2017
[lib/c] Simplify strncmp() This functio tried to write too many things in a line, and this new version maybe has a better performance.
--- a/lib/c/src/strncmp.c
+++ b/lib/c/src/strncmp.c
@@ -4,7 +4,14 @@
int
strncmp(const char *s1, const char *s2, size_t n)
{
- for (; n && *s1 && *s2 && *s1 == *s2; --n, ++s1, ++s2);
- ;
- return n ? (*(unsigned char *)s1 - *(unsigned char *)s2) : 0;
+ int c;
+
+ if (n == 0)
+ return 0;
+ while ((c = *s1) != '\0' && c != *s2) {
+ if (--n == 0)
+ return 0;
+ ++s1, ++s2;
+ }
+ return (*(unsigned char *) s1 - *(unsigned char *) s2);
}