[PATCH 4/5] rpmvercmp: Use helper macros to match upstream code better

Dan Nicholson dbn.lists at gmail.com
Sat Apr 20 09:41:57 PDT 2013


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.
---
 rpmvercmp.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/rpmvercmp.c b/rpmvercmp.c
index 648026c..50d56ac 100644
--- a/rpmvercmp.c
+++ b/rpmvercmp.c
@@ -22,6 +22,12 @@
 #include <string.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 */
 /* return 1: a is newer than b */
 /*        0: a and b are the same version */
@@ -35,7 +41,7 @@ int rpmvercmp(const char * a, const char * b)
     int isnum;
 
     /* 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);
     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 */
     while (*one && *two) {
-	while (*one && !isalnum((guchar)*one)) one++;
-	while (*two && !isalnum((guchar)*two)) two++;
+	while (*one && !risalnum(*one)) one++;
+	while (*two && !risalnum(*two)) two++;
 
 	/* If we ran to the end of either, we are finished with the loop */
 	if (!(*one && *two)) break;
@@ -60,13 +66,13 @@ int rpmvercmp(const char * a, const char * b)
 	/* grab first completely alpha or completely numeric segment */
 	/* leave one and two pointing to the start of the alpha or numeric */
 	/* segment and walk str1 and str2 to end of segment */
-	if (isdigit((guchar)*str1)) {
-	    while (*str1 && isdigit((guchar)*str1)) str1++;
-	    while (*str2 && isdigit((guchar)*str2)) str2++;
+	if (risdigit(*str1)) {
+	    while (*str1 && risdigit(*str1)) str1++;
+	    while (*str2 && risdigit(*str2)) str2++;
 	    isnum = 1;
 	} else {
-	    while (*str1 && isalpha((guchar)*str1)) str1++;
-	    while (*str2 && isalpha((guchar)*str2)) str2++;
+	    while (*str1 && risalpha(*str1)) str1++;
+	    while (*str2 && risalpha(*str2)) str2++;
 	    isnum = 0;
 	}
 
-- 
1.8.1.4



More information about the pkg-config mailing list