[fpc-pascal] Comparing version numbers

A.J. Venter aj at getopenlab.com
Sat Jun 3 03:22:24 CEST 2006

> Your code can't work with these ones, 4.1.15  and 4.12.1 because you
> only pad the 3rd (15 and 1), you need to pad each part of the
> version, my pseudocode out 4.1[space].15 and 4.12.1[space]. You can
> call your function with the 4,4; again with 12,1 utnil you get a
> result.... but you need a ternary boolean, as 4,4 generates an
> invalid result state.
Turns out there is a deeper flaw anyway :s
Neither version works with for example this one:
2.8.7 - 2.8.13
Even if you pad 2.8.7 you still end up with a failure.
The only way to prevent said failure is to pad it at the beginning:
2.8. 7 < 2.8.13
But THAT way around breaks a whole bunch of others for example in my case
there is an older package versioned 070utopia which needs to be seen as older 
than 071 (without the sub specifier) - THAT one only works if you pad at the 
So there is no way to predict which side you need to pad for the right result.

Considering that, I went back to the debian algorithm. True it's a very c-like 
approach to things, but it does seem to work and I guess if it works for 
debian that's a fairly major vote of confidence.

Here is my code (note that FirstNonDigit and FirstDigit are functions I wrote, 
which return the pos of the first digit/nondigit in the string - either 
returns -1 in case of no match).

Done := False;
Res := False;
If (FirstNonDigit(V1) = -1) and
   (FirstDigit(V1) = -1) then
    Done := True;
IF FirstNonDigit(V1) < FirstDigit(V1) then
 S1 := Copy(V1,1,FirstDigit(V1) -1);
 Delete(V1,1,firstDigit(v1) -1);
 S2 := Copy(V2,1,FirstDigit(V2) -1);
 Delete(V2,1,FirstDigit(v2) -1);
 If S1 <> S2 then
    Done := True;
    If S1 > S2 then
     Res := True;
IF (FirstNonDigit(V1) > FirstDigit(V1)) and (Not Done) then
 S1 := Copy(V1,1,FirstNonDigit(V1) -1);
 Delete(V1,1,firstNonDigit(v1) -1);
 S2 := Copy(V2,1,FirstNonDigit(V2) -1);
 Delete(V2,1,FirstNonDigit(v2) -1);
 If StrToInt(S1) <> StrToInt(S2) then
    Done := True;
 If StrToInt(S1) > StrToInt(S2) then
        Res := True;
until done;
CompareVersion := Res;

"there's nothing as inspirational for a hacker as a cat obscuring a bug 
by sitting in front of the monitor" - Boudewijn Rempt
A.J. Venter
Chief Software Architect
OpenLab International
+27 82 726 5103

More information about the fpc-pascal mailing list