From f974c457e3f9f57995fe35f13a55b3cd2d514794 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Tue, 4 Sep 2012 13:43:43 +0300 Subject: [PATCH] Minor optimizations to rpmvercmp() - Avoid calculating string lengths if versions are equal - Avoid calculating string lengths twice in numeric comparison --- lib/rpmvercmp.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/rpmvercmp.c b/lib/rpmvercmp.c index a6b7075..de3beea 100644 --- a/lib/rpmvercmp.c +++ b/lib/rpmvercmp.c @@ -15,6 +15,9 @@ /* -1: b is newer than a */ int rpmvercmp(const char * a, const char * b) { + /* easy comparison to see if versions are identical */ + if (rstreq(a, b)) return 0; + char oldch1, oldch2; char abuf[strlen(a)+1], bbuf[strlen(b)+1]; char *str1 = abuf, *str2 = bbuf; @@ -22,9 +25,6 @@ int rpmvercmp(const char * a, const char * b) int rc; int isnum; - /* easy comparison to see if versions are identical */ - if (rstreq(a, b)) return 0; - strcpy(str1, a); strcpy(str2, b); @@ -82,6 +82,7 @@ int rpmvercmp(const char * a, const char * b) if (two == str2) return (isnum ? 1 : -1); if (isnum) { + size_t onelen, twolen; /* this used to be done by converting the digit segments */ /* to ints using atoi() - it's changed because long */ /* digit segments can overflow an int - this should fix that. */ @@ -91,8 +92,10 @@ int rpmvercmp(const char * a, const char * b) while (*two == '0') two++; /* whichever number has more digits wins */ - if (strlen(one) > strlen(two)) return 1; - if (strlen(two) > strlen(one)) return -1; + onelen = strlen(one); + twolen = strlen(two); + if (onelen > twolen) return 1; + if (twolen > onelen) return -1; } /* strcmp will return which one is greater - even if the two */ -- 2.7.4