commit 437e32bac531455249a2ad20e2756da782fc2167
parent 9fdc79d8cddf62db618edfa7dd30e2413dc1ecb9
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Thu, 7 Dec 2017 00:36:48 +0100
[lib/c] Simplify strncmp()
This functio tried to write too many things in a line, and
this new version maybe has a better performance.
Diffstat:
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git 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);
}