From: Hallvard B Furuseth Date: Wed, 29 Jun 2011 17:17:31 +0000 (+0200) Subject: Compact memnrcmp, just for fun X-Git-Tag: OPENLDAP_REL_ENG_2_4_27~148^2~170 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=aed36f5780648b4b4f62745be30480fae88fce0e;p=openldap Compact memnrcmp, just for fun --- diff --git a/libraries/libmdb/mdb.c b/libraries/libmdb/mdb.c index ad08e9db3e..f89f46b753 100644 --- a/libraries/libmdb/mdb.c +++ b/libraries/libmdb/mdb.c @@ -346,25 +346,19 @@ memncmp(const void *s1, size_t n1, const void *s2, size_t n2) static int memnrcmp(const void *s1, size_t n1, const void *s2, size_t n2) { - const unsigned char *p1; - const unsigned char *p2; - - if (n1 == 0) - return n2 == 0 ? 0 : -1; + const unsigned char *p1, *p2, *p1_lim; if (n2 == 0) - return n1 == 0 ? 0 : 1; + return n1 != 0; + if (n1 == 0) + return -1; p1 = (const unsigned char *)s1 + n1 - 1; p2 = (const unsigned char *)s2 + n2 - 1; - while (*p1 == *p2) { - if (p1 == s1) - return (p2 == s2) ? 0 : -1; - if (p2 == s2) - return (p1 == p2) ? 0 : 1; - p1--; - p2--; + for (p1_lim = (n1 <= n2 ? s1 : s2); *p1 == *p2; p1--, p2--) { + if (p1 == p1_lim) + return (p1 != s1) ? (p1 != p2) : (p2 != s2) ? -1 : 0; } return *p1 - *p2; }