我创建了一个记录,它将版本号的场景封装在整数数组中,并且可以作为字符串互换...
type
TVersion = record
Values: array of Integer;
function Count: Integer;
class operator implicit(aVersion: TVersion): String;
class operator implicit(aVersion: String): TVersion;
end;
class operator TVersion.implicit(aVersion: TVersion): String;
var
X: Integer;
begin
Result:= '';
for X := 0 to Length(aVersion.Values)-1 do begin
if X > 0 then Result:= Result + '.';
Result:= Result + IntToStr(aVersion.Values[X]);
end;
end;
class operator TVersion.implicit(aVersion: String): TVersion;
var
S, T: String;
I: Integer;
begin
S:= aVersion + '.';
SetLength(Result.Values, 0);
while Length(S) > 0 do begin
I:= Pos('.', S);
T:= Copy(S, 1, I-1);
Delete(S, 1, I);
SetLength(Result.Values, Length(Result.Values)+1);
Result.Values[Length(Result.Values)-1]:= StrToIntDef(T, 0);
end;
end;
function TVersion.Count: Integer;
begin
Result:= Length(Values);
end;
用法:
var
V: TVersion;
begin
V:= '1.2.3.4';
V.Values[3]:= 400;
ShowMessage(V);
SetLength(V.Values, 2);
V.Values[0]:= 7;
V.Values[1]:= 0;
ShowMessage(V);
end;
我想要做的是能够使用标准运算符比较这些版本,例如......
if V1 > V2 then ShowMessage('Version 1 is newer than version 2')
else if V1 < V2 then ShowMessage('Version 1 is older than version 2')
else if V1 = V2 then SHowMessage('Version 1 and version 2 are the same');
如果可能,我该怎么做?
答案 0 :(得分:2)
你必须实现一些比较运算符,如LessThan('&lt;'),Equal等。可重载运算符的完整列表is here。
Equal实现的简单示例(没有像1.2.0 = 1.2这样的情况)
class operator TVersion.Equal(a, b: TVersion): Boolean;
var
la, lb: Integer;
begin
la := Length(a.Values);
lb := Length(b.Values);
if la <> lb then
Result := False
else if la = 0 then
Result := True
else
Result := CompareMem(a.Values, b.Values, la * SizeOf(Integer));
end;
关于比较两种不同大小的数组(未测试):
class operator TVersion.LessThan(a, b: TVersion): Boolean;
var
la, lb, i: Integer;
begin
la := Length(a.Values);
lb := Length(b.Values);
for i := 0 to Min(la, lb) - 1 do
if a.Values[i] < b.Values[i] then // 1.2.xxx < 1.3.xxx
Exit(True)
else if a.Values[i] > b.Values[i] then //1.3.xxx > 1.2.xxx
Exit(False);
//common parts are equal, check a rest
//cases 1.2.3.xxx vs 1.2.3 or 1.2.3.xxx vs 1.2.3
//and empty cases too
Result := la < lb;
end;