diff options
author | eadler <eadler@FreeBSD.org> | 2011-11-22 00:07:53 +0000 |
---|---|---|
committer | eadler <eadler@FreeBSD.org> | 2011-11-22 00:07:53 +0000 |
commit | 7eb95f50e380e785a18aaee6734807ee6db226a6 (patch) | |
tree | abcaedad17a9a120ce91c83c2e7ca0fda8f47c2d | |
parent | 3be48b6d59eed7d8eadd7974014c5cf08eb38e9a (diff) | |
download | FreeBSD-src-7eb95f50e380e785a18aaee6734807ee6db226a6.zip FreeBSD-src-7eb95f50e380e785a18aaee6734807ee6db226a6.tar.gz |
- add check for pointer equality prior to performing the O(n) pass
- while here change 's' to 's1' in strcoll
Submitted by: eadler@
Reviewed by: theraven@
Approved by: brooks@
MFC after: 2 weeks
-rw-r--r-- | lib/libc/string/strcasecmp.c | 28 | ||||
-rw-r--r-- | lib/libc/string/strcmp.c | 3 | ||||
-rw-r--r-- | lib/libc/string/strcoll.c | 12 | ||||
-rw-r--r-- | lib/libc/string/strncmp.c | 3 |
4 files changed, 28 insertions, 18 deletions
diff --git a/lib/libc/string/strcasecmp.c b/lib/libc/string/strcasecmp.c index 65e042e..dae91d3 100644 --- a/lib/libc/string/strcasecmp.c +++ b/lib/libc/string/strcasecmp.c @@ -48,6 +48,9 @@ strcasecmp_l(const char *s1, const char *s2, locale_t locale) const u_char *us1 = (const u_char *)s1, *us2 = (const u_char *)s2; + if (s1 == s2) + return (0); + FIX_LOCALE(locale); while (tolower_l(*us1, locale) == tolower_l(*us2++, locale)) @@ -65,18 +68,21 @@ int strncasecmp_l(const char *s1, const char *s2, size_t n, locale_t locale) { FIX_LOCALE(locale); - if (n != 0) { - const u_char - *us1 = (const u_char *)s1, - *us2 = (const u_char *)s2; - do { - if (tolower_l(*us1, locale) != tolower_l(*us2++, locale)) - return (tolower_l(*us1, locale) - tolower_l(*--us2, locale)); - if (*us1++ == '\0') - break; - } while (--n != 0); - } + const u_char + *us1 = (const u_char *)s1, + *us2 = (const u_char *)s2; + + if (( s1 == s2) | (n == 0)) + return (0); + + + do { + if (tolower_l(*us1, locale) != tolower_l(*us2++, locale)) + return (tolower_l(*us1, locale) - tolower_l(*--us2, locale)); + if (*us1++ == '\0') + break; + } while (--n != 0); return (0); } diff --git a/lib/libc/string/strcmp.c b/lib/libc/string/strcmp.c index 95c778d..70fd22d 100644 --- a/lib/libc/string/strcmp.c +++ b/lib/libc/string/strcmp.c @@ -44,6 +44,9 @@ __FBSDID("$FreeBSD$"); int strcmp(const char *s1, const char *s2) { + if (s1 == s2) + return (0); + while (*s1 == *s2++) if (*s1++ == '\0') return (0); diff --git a/lib/libc/string/strcoll.c b/lib/libc/string/strcoll.c index a918fca..8add885 100644 --- a/lib/libc/string/strcoll.c +++ b/lib/libc/string/strcoll.c @@ -40,7 +40,7 @@ __FBSDID("$FreeBSD$"); #include <stdio.h> int -strcoll_l(const char *s, const char *s2, locale_t locale) +strcoll_l(const char *s1, const char *s2, locale_t locale) { int len, len2, prim, prim2, sec, sec2, ret, ret2; const char *t, *t2; @@ -50,16 +50,16 @@ strcoll_l(const char *s, const char *s2, locale_t locale) (struct xlocale_collate*)locale->components[XLC_COLLATE]; if (table->__collate_load_error) - return strcmp(s, s2); + return strcmp(s1, s2); len = len2 = 1; ret = ret2 = 0; if (table->__collate_substitute_nontrivial) { - t = tt = __collate_substitute(table, s); + t = tt = __collate_substitute(table, s1); t2 = tt2 = __collate_substitute(table, s2); } else { tt = tt2 = NULL; - t = s; + t = s1; t2 = s2; } while(*t && *t2) { @@ -95,8 +95,8 @@ strcoll_l(const char *s, const char *s2, locale_t locale) } int -strcoll(const char *s, const char *s2) +strcoll(const char *s1, const char *s2) { - return strcoll_l(s, s2, __get_locale()); + return strcoll_l(s1, s2, __get_locale()); } diff --git a/lib/libc/string/strncmp.c b/lib/libc/string/strncmp.c index 5bc3d5e..46db3f9 100644 --- a/lib/libc/string/strncmp.c +++ b/lib/libc/string/strncmp.c @@ -39,8 +39,9 @@ int strncmp(const char *s1, const char *s2, size_t n) { - if (n == 0) + if ((n == 0) | (s1 == s2)) return (0); + do { if (*s1 != *s2++) return (*(const unsigned char *)s1 - |