rpmvercmp: Use helper macros to match upstream code better

Upstream rpm has some convenience inline functions for strcmp and the
character class functions. Define some macros here to make our code look
more like upstream despite being the same functionally.

One difference noted while investigating the inline functions in
upstream is that the rpm character class functions are slightly
different from the standard functions. The rpm functions are independent
of locale, unlike the libc routines. pkg-config should probably do the
same and could easily use the g_ascii_is* macros from glib.
This commit is contained in:
Dan Nicholson 2012-10-11 05:30:34 -07:00
parent f78b014371
commit 1085ba7377

View file

@ -22,6 +22,12 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
/* macros to help code look more like upstream */
#define rstreq(a, b) (strcmp(a, b) == 0)
#define risalnum(c) isalnum((guchar)(c))
#define risdigit(c) isdigit((guchar)(c))
#define risalpha(c) isalpha((guchar)(c))
/* compare alpha and numeric segments of two versions */ /* compare alpha and numeric segments of two versions */
/* return 1: a is newer than b */ /* return 1: a is newer than b */
/* 0: a and b are the same version */ /* 0: a and b are the same version */
@ -35,7 +41,7 @@ int rpmvercmp(const char * a, const char * b)
int isnum; int isnum;
/* easy comparison to see if versions are identical */ /* easy comparison to see if versions are identical */
if (!strcmp(a, b)) return 0; if (rstreq(a, b)) return 0;
str1 = g_alloca(strlen(a) + 1); str1 = g_alloca(strlen(a) + 1);
str2 = g_alloca(strlen(b) + 1); str2 = g_alloca(strlen(b) + 1);
@ -48,8 +54,8 @@ int rpmvercmp(const char * a, const char * b)
/* loop through each version segment of str1 and str2 and compare them */ /* loop through each version segment of str1 and str2 and compare them */
while (*one && *two) { while (*one && *two) {
while (*one && !isalnum((guchar)*one)) one++; while (*one && !risalnum(*one)) one++;
while (*two && !isalnum((guchar)*two)) two++; while (*two && !risalnum(*two)) two++;
/* If we ran to the end of either, we are finished with the loop */ /* If we ran to the end of either, we are finished with the loop */
if (!(*one && *two)) break; if (!(*one && *two)) break;
@ -60,13 +66,13 @@ int rpmvercmp(const char * a, const char * b)
/* grab first completely alpha or completely numeric segment */ /* grab first completely alpha or completely numeric segment */
/* leave one and two pointing to the start of the alpha or numeric */ /* leave one and two pointing to the start of the alpha or numeric */
/* segment and walk str1 and str2 to end of segment */ /* segment and walk str1 and str2 to end of segment */
if (isdigit((guchar)*str1)) { if (risdigit(*str1)) {
while (*str1 && isdigit((guchar)*str1)) str1++; while (*str1 && risdigit(*str1)) str1++;
while (*str2 && isdigit((guchar)*str2)) str2++; while (*str2 && risdigit(*str2)) str2++;
isnum = 1; isnum = 1;
} else { } else {
while (*str1 && isalpha((guchar)*str1)) str1++; while (*str1 && risalpha(*str1)) str1++;
while (*str2 && isalpha((guchar)*str2)) str2++; while (*str2 && risalpha(*str2)) str2++;
isnum = 0; isnum = 0;
} }