Archive: VersionCompare Recommendation


VersionCompare Recommendation
@Afro

I would like to recommend a minor modification to VersionCompare.

In WordFunc.nsh you have:

        WordFunc_VersionCompare_begin:

StrCpy $2 -1


I recommend updating that to the following:
        WordFunc_VersionCompare_begin:

WordFunc_VersionCompare_trimleadingzeros_ver1:
StrCpy $8 $0 1 0
StrCmp $8 '0' 0 WordFunc_VersionCompare_trimleadingzeros_ver2
StrCpy $0 $0 '' 1
goto WordFunc_VersionCompare_trimleadingzeros_ver1

WordFunc_VersionCompare_trimleadingzeros_ver2:
StrCpy $8 $1 1 0
StrCmp $8 '0' 0 WordFunc_VersionCompare_posttrim
StrCpy $1 $1 '' 1
goto WordFunc_VersionCompare_trimleadingzeros_ver2

WordFunc_VersionCompare_posttrim:

StrCpy $2 -1


I came about this when I was testing version numbers as in the following examples:
VersionCompare '5.01.12345' '05.01.12345' return=1 expected=0
VersionCompare '5.02.12345' '5.1.12345' return=2 expected=1
VersionCompare '5.01.12345' '5.1.12345' return=2 expected=0

Just has to do with the leading 0's for version # section.

If not, no worries, but this is how I would anticipate it run, IMO.

I had to make a small modification, this correction was not correctly accounting for ".0." scenarios in a version number. I have corrected it to the following code.

        WordFunc_VersionCompare_begin:

WordFunc_VersionCompare_trimleadingzeros_ver1:
;put ver1 first char in $8
StrCpy $8 $0 1 0
;test if its 0
StrCmp $8 '0' 0 WordFunc_VersionCompare_trimleadingzeros_ver2
;if it is grab first two char into $8
StrCpy $8 $0 2 0
;test for .0. situation
StrCmp $8 '0.' WordFunc_VersionCompare_trimleadingzeros_ver2 0
;strip leading 0
StrCpy $0 $0 '' 1
goto WordFunc_VersionCompare_trimleadingzeros_ver1

WordFunc_VersionCompare_trimleadingzeros_ver2:
;put ver2 first char in $8
StrCpy $8 $1 1 0
;test if its 0
StrCmp $8 '0' 0 WordFunc_VersionCompare_posttrim
StrCpy $8 $1 2 0
StrCmp $8 '0.' WordFunc_VersionCompare_posttrim 0
;strip leading 0
StrCpy $1 $1 '' 1
goto WordFunc_VersionCompare_trimleadingzeros_ver2

WordFunc_VersionCompare_posttrim:

StrCpy $2 -1

I would contact Instructor if you don't get a response from him. He wrote that function. I believe you are correct - 01 should be treated the same as 1. PHP's version_compare() does this.

Stu