且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Inno Setup中比较版本字符串

更新时间:2023-02-20 12:09:46

Inno Setup中没有CompareVersion功能.

There's no CompareVersion function in Inno Setup.

您需要定义自己的内容.

You need to define your own.

类似的东西:

function CompareVersion(V1, V2: string): Integer;
var
  P, N1, N2: Integer;
begin
  Result := 0;
  while (Result = 0) and ((V1 <> '') or (V2 <> '')) do
  begin
    P := Pos('.', V1);
    if P > 0 then
    begin
      N1 := StrToInt(Copy(V1, 1, P - 1));
      Delete(V1, 1, P);
    end
      else
    if V1 <> '' then
    begin
      N1 := StrToInt(V1);
      V1 := '';
    end
      else
    begin
      N1 := 0;
    end;

    P := Pos('.', V2);
    if P > 0 then
    begin
      N2 := StrToInt(Copy(V2, 1, P - 1));
      Delete(V2, 1, P);
    end
      else
    if V2 <> '' then
    begin
      N2 := StrToInt(V2);
      V2 := '';
    end
      else
    begin
      N2 := 0;
    end;

    if N1 < N2 then Result := -1
      else
    if N1 > N2 then Result := 1;
  end;
end;

  • 如果版本相同,则返回0.
  • 如果V1早于V2,则
  • 返回-1.
  • 如果V1V2更新,则返回1.
  • 1.12被认为比1.1更新.
  • 1.1被认为与1.1.0相同.
  • 当版本在语法上无效(仅允许数字和点)时,引发异常.
    • Returns 0, if the versions are equal.
    • Returns -1, if the V1 is older than the V2.
    • Returns 1, if the V1 is newer than the V2.
    • 1.12 is considered newer than 1.1.
    • 1.1 is considered the same as 1.1.0.
    • Throws an exception, when a version is syntactically invalid (only digits and dots are allowed).
    • 单元测试:

procedure CompareVersionTest(V1, V2: string; Expected: Integer);
var
  R: Integer;
  M: string;
begin
  M := Format('Comparing [%s] vs. [%s] - expecting %d - ', [V1, V2, Expected]);
  R := CompareVersion(V1, V2);
  if R <> Expected then
  begin
    RaiseException(M + Format('Failed - Got %d', [R]));
  end
    else
  begin
    Log(M + 'Passed');
  end;
end;

procedure CompareVersionTests;
begin
  CompareVersionTest('1', '1', 0);
  CompareVersionTest('1.2', '1.2', 0);
  CompareVersionTest('1.2.3', '1.2.3', 0);
  CompareVersionTest('1', '2', -1);
  CompareVersionTest('1', '3', -1);
  CompareVersionTest('2', '3', -1);
  CompareVersionTest('2', '1', 1);
  CompareVersionTest('3', '1', 1);
  CompareVersionTest('3', '2', 1);
  CompareVersionTest('1', '12', -1);
  CompareVersionTest('12', '2', 1);
  CompareVersionTest('12', '12', 0);
  CompareVersionTest('1.2', '1.3', -1);
  CompareVersionTest('1.3', '1.2', 1);
  CompareVersionTest('1.2', '11.2', -1);
  CompareVersionTest('11.2', '1.2', 1);
  CompareVersionTest('1.1', '1.12', -1);
  CompareVersionTest('1.11', '1.12', -1);
  CompareVersionTest('1.12', '1.1', 1);
  CompareVersionTest('1.12', '1.11', 1);
  CompareVersionTest('1', '1.0', 0);
  CompareVersionTest('1.0', '1', 0);
  CompareVersionTest('1', '1.1', -1);
  CompareVersionTest('1.1', '1', 1);
  CompareVersionTest('1.12.123', '1.12.124', -1);
  CompareVersionTest('1.12.124', '1.12.123', 1);
  CompareVersionTest('1.12.123', '1.13.1', -1);
  CompareVersionTest('1.13.1', '1.12.123', 1);
  CompareVersionTest('1.12.123', '1.13', -1);
  CompareVersionTest('1.13', '1.12.123', 1);
  CompareVersionTest('1.12', '1.13.1', -1);
  CompareVersionTest('1.13.1', '1.12', 1);
end;

如果要运行单元测试,只需在CompareVersionTests > InitializeSetup .

If you want to run the unit tests, just call CompareVersionTests in InitializeSetup.