summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPanu Matilainen <pmatilai@redhat.com>2012-09-04 13:43:43 +0300
committerPanu Matilainen <pmatilai@redhat.com>2012-09-04 13:47:23 +0300
commitf974c457e3f9f57995fe35f13a55b3cd2d514794 (patch)
tree626240c009ac729003908cd7e7d8f8fe2d95d257 /lib
parent75d88c405b257752a0f7d71054643a954828556f (diff)
downloadrpm-f974c457e3f9f57995fe35f13a55b3cd2d514794.tar.gz
rpm-f974c457e3f9f57995fe35f13a55b3cd2d514794.tar.bz2
rpm-f974c457e3f9f57995fe35f13a55b3cd2d514794.zip
Minor optimizations to rpmvercmp()
- Avoid calculating string lengths if versions are equal - Avoid calculating string lengths twice in numeric comparison
Diffstat (limited to 'lib')
-rw-r--r--lib/rpmvercmp.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/lib/rpmvercmp.c b/lib/rpmvercmp.c
index a6b707583..de3beeab4 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 */