Bläddra i källkod

Mark Robson noticed that our strcasecmp() behaviour was not
standards compliant. Brian Stafford then provided these new
implementations, which apprear to properly follow SUSv2.
-Erik

Eric Andersen 23 år sedan
förälder
incheckning
ce4b0fa7e8
2 ändrade filer med 23 tillägg och 22 borttagningar
  1. 11 11
      libc/string/strcasecmp.c
  2. 12 11
      libc/string/strncasecmp.c

+ 11 - 11
libc/string/strcasecmp.c

@@ -6,16 +6,16 @@
 #include <string.h>
 #include <ctype.h>
 
-int strcasecmp( const char *s, const char *d)
+int strcasecmp (const char *a, const char *b)
 {
-	for (;;) {
-		if (*s != *d) {
-			if (tolower(*s) != tolower(*d))
-				return *s - *d;
-		} else if (*s == '\0')
-			break;
-		s++;
-		d++;
-	}
-	return 0;
+    register int n;
+
+    while (*a == *b || (n = tolower (*a) - tolower (*b)) == 0)
+    {
+	if (*a == '\0')
+	    return 0;
+	a++, b++;
+    }
+    return n;
 }
+

+ 12 - 11
libc/string/strncasecmp.c

@@ -6,17 +6,18 @@
 #include <string.h>
 #include <ctype.h>
 
-int strncasecmp( const char *s, const char *d, size_t l)
+int strncasecmp (const char *a, const char *b, size_t len)
 {
-	while (l > 0) {
-		if (*s != *d) {
-			if (tolower(*s) != tolower(*d))
-				return *s - *d;
-		} else if (*s == '\0')
-			return 0;
-		s++;
-		d++;
-		l--;
-	}
+    register int n;
+
+    if (len < 1)
 	return 0;
+    while (*a == *b || (n = tolower (*a) - tolower (*b)) == 0)
+    {
+	if (*a == '\0' || --len < 1)
+	    return 0;
+	a++, b++;
+    }
+    return n;
 }
+