From d828d75fed0cfe3c0d4277b1dc2bbfc836bab24d Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Wed, 6 Jul 2022 12:36:27 +0200 Subject: [PATCH] bee_version_compare: Rewrite compare_version_strings() The current algorithm is complicated and broken. First it removes common prefixes, so that, for example, '1.5.8' and '1.5.9' are compared numerically as 8 and 9. After that, '0's are skipped, to that, for example, '1.5.09' can compare equal to '1.5.9'. In a next step, removed digits are restored, so that that '1.5.1134' and '1.5.1211' are compared as 1134 and 1211 and not as 34 and 11. However, the removal of '0's is done independently for both values, so that a different number of '0's might be removed, while the undo is done in sync, so that the same number of digits is 'restored'. As a result, '1.101' compares less that '1.11': a b 1.101 1.11 # original 01 1 # after removal of common prefix 1 1 # after removal of '0's 01 11 # after restoration of digits Rewrite algorithm from scratch. The new algorithm advances through both strings comparing them at their beging: - if one string ends, the other on is bigger - if both strings start with a digit - compare nummerically - if equal, skip over digits and loop - if the first character is different, compare character values - if equal, go to next character and loop To make the code more readable, the character pointers are dereferenced multiple times and some conditionals are evaluaated multiple times. We can trust the compiler to optimize this away. Comparing 'bee list --available' with the old and the new algorithm produces the following differences: firefox-9.0.1-0.x86_64 firefox-9.0.1-0.x86_64 firefox-10.0.2-0.x86_64 firefox-10.0.2-0.x86_64 firefox-102.0-0.x86_64 < firefox-11.0-0.x86_64 firefox-11.0-0.x86_64 firefox-12.0-0.x86_64 firefox-12.0-0.x86_64 firefox-94.0.1-0.x86_64 firefox-94.0.1-0.x86_64 firefox-94.0.2-0.x86_64 firefox-94.0.2-0.x86_64 > firefox-102.0-0.x86_64 firefox_current-4-0.x86_64 firefox_current-4-0.x86_64 firefox_current-5-0.x86_64 firefox_current-5-0.x86_64 java-1.7.0_13-0.x86_64 java-1.7.0_13-0.x86_64 java-1.7.0_17-0.x86_64 java-1.7.0_17-0.x86_64 > java-1.8.0_11-0.x86_64 > java-1.8.0_45-0.x86_64 java-1.8.0_101-0.x86_64 java-1.8.0_101-0.x86_64 java-1.8.0_102-0.x86_64 java-1.8.0_102-0.x86_64 ava-1.8.0_102-1.x86_64 java-1.8.0_102-1.x86_64 java-1.8.0_11-0.x86_64 < java-1.8.0_45-0.x86_64 < java-1.8.0_121-0.x86_64 java-1.8.0_121-0.x86_64 java-1.8.0_131-0.x86_64 java-1.8.0_131-0.x86_64 mxq-0.0_p106_8a0ad87-0.x86_6 mxq-0.0_p106_8a0ad87-0.x86_6 mxq-0.0_p107_eaf8146-0.x86_6 mxq-0.0_p107_eaf8146-0.x86_6 mxq-0.0_p108_f56522b-0.x86_6 < mxq-0.0_p108_0b7afc1-0.x86_6 mxq-0.0_p108_0b7afc1-0.x86_6 mxq-0.0_p108_4e83c8d-0.x86_6 mxq-0.0_p108_4e83c8d-0.x86_6 mxq-0.0_p109_ed52e7f-0.x86_6 | mxq-0.0_p108_f56522b-0.x86_6 mxq-0.0_p109_621fea8-0.x86_6 mxq-0.0_p109_621fea8-0.x86_6 > mxq-0.0_p109_ed52e7f-0.x86_6 mxq-0.0_p110_baba367-0.x86_6 mxq-0.0_p110_baba367-0.x86_6 mxq-0.0_p111_38cc7db-0.x86_6 mxq-0.0_p111_38cc7db-0.x86_6 mxq-0.1.1_p1_4840161-0.x86_6 mxq-0.1.1_p1_4840161-0.x86_6 mxq-0.1.2_p0_d21c6ae-0.x86_6 mxq-0.1.2_p0_d21c6ae-0.x86_6 mxq-0.1.2_p1_efa8ec4-0.x86_6 < mxq-0.1.2_p1_9ccc12f-0.x86_6 mxq-0.1.2_p1_9ccc12f-0.x86_6 > mxq-0.1.2_p1_efa8ec4-0.x86_6 mxq-0.1.3_p0_0c3032c-0.x86_6 mxq-0.1.3_p0_0c3032c-0.x86_6 mxq-0.1.4_p0_c936ba0-0.x86_6 mxq-0.1.4_p0_c936ba0-0.x86_6 mxstartup-2.9_p1_0e23fa8-0.x mxstartup-2.9_p1_0e23fa8-0.x mxstartup-2.9_p2_e915bc3-0.x mxstartup-2.9_p2_e915bc3-0.x mxstartup-2.9_p3_d919716-0.x < mxstartup-2.9_p3_21347ab-0.x mxstartup-2.9_p3_21347ab-0.x > mxstartup-2.9_p3_d919716-0.x mxstartup-2.10_p0_d919716-0. mxstartup-2.10_p0_d919716-0. mxstartup-2.11_p0_4ee8fed-0. mxstartup-2.11_p0_4ee8fed-0. --- src/bee_version_compare.c | 106 +++++++++++--------------------------- 1 file changed, 31 insertions(+), 75 deletions(-) diff --git a/src/bee_version_compare.c b/src/bee_version_compare.c index 43824b8..2ddb49c 100644 --- a/src/bee_version_compare.c +++ b/src/bee_version_compare.c @@ -28,86 +28,42 @@ #include "bee_version.h" -int compare_version_strings(char *v1, char *v2) { - char *a, *b; - long long i,j; - assert(v1); - assert(v2); - - a = v1; - b = v2; - - while(*a && *b && *a == *b) { - a++; - b++; - - if (*a == *b) +// '2.23.2' > '2.23.0' +//' 2.23.10' > '2.23.8' +// '2.23.001' == '2.32.1' +// '2.23.1001' > '2.23.101' + +int compare_version_strings(char *a, char *b) { + while (1) { + if (*a == '\0' && *b == '\0') + return 0; + if (*a == '\0') + return -1; + if (*b == '\0') + return 1; + + if (isdigit(*a) && isdigit (*b)) { + long i_a = atoll(a); + long i_b = atoll(b); + if (i_a < i_b) + return -1; + if (i_a > i_b) + return 1; + while(isdigit(*a)) + a++; + while(isdigit(*b)) + b++; continue; - - /* skip leading zeros of numbers != 0 */ - if (isdigit(*a) && isdigit(*b)) { - char *c; - - for (c=a; *c == '0'; c++) - ; - if (isdigit(*c)) - a = c; - - for (c=b; *c == '0'; c++) - ; - if (isdigit(*c)) - b = c; - } - } - - /* strings are equal ; *a==*b==0*/ - if(*a == *b) - return(0); - - if(isdigit(*a)) { - if(isdigit(*b)) { - /* rewind string to first digit */ - /* e.g. to compare 12 vs 100 and not 2 vs 00 */ - while(a > v1 && isdigit(*(a-1)) && - b > v2 && isdigit(*(b-1))) { - a--; - b--; - } - i = atoll(a); - j = atoll(b); - - if(ij) - return(1); - - /* numbers are equal but strings are not? */ - /* yes -> leading zeros: atoll("01") == atoll("1") */ - return(0); } - /* a > ('.',alpha, 0) */ - return(1); - } - if(isalpha(*a)) { - - /* alpha < digit */ - if(isdigit(*b)) - return(-1); - - if(isalpha(*b)) { - if(*a < *b) - return(-1); - return(1); - } - return(1); + if (*a < *b) + return -1; + if (*a > *b) + return 1; + a++; + b++; } - - if(! *b) - return(1); - - return(-1); } int compare_beepackage_names(struct beeversion *v1, struct beeversion *v2) {