Registriert seit: 23. Nov 2007
Ort: Lautertal
34 Beiträge
|
Re: Versionsnummern vergleichen?
27. Nov 2007, 18:53
Seltsam, ähnliches Problem kam vor kurzer Zeit in einem anderen Forum auf, wo ich folgende Delphi Lösung vorstellte:
Delphi-Quellcode:
program Project1;
{$APPTYPE CONSOLE}
{$DEFINE DEBUG}
uses
SysUtils, Classes;
type
TCVSResult = (csEqual, csFirst, csSecond);
TStrArray = array of string;
function Explode(Border, S: string): TStrArray;
var
S2: string;
I: Integer;
a: TStrArray;
begin
I := 0;
S2 := S + Border;
repeat
SetLength(A, Length(A) + 1);
a[I] := Copy(S2, 0, Pos(Border, S2) - 1);
Delete(S2, 1, Length(a[I] + Border));
Inc(I);
until S2 = '';
Result := a;
end;
function StringReplaceCharacters(Str: String): String;
var
I: Integer;
begin
Str := StringReplace(Str, ' ', '', [rfIgnoreCase]);
Str := StringReplace(Str, 'alpha', '.-26', [rfIgnoreCase]);
Str := StringReplace(Str, 'beta', '.-25', [rfIgnoreCase]);
for I := 0 to 19 do
Str := StringReplace(Str, 'RC' + IntToStr(I), '.-' + IntToStr(20 - I),
[rfIgnoreCase]);
for I := 0 to 25 do
Str := StringReplace(Str, Chr(Ord('a') + I), '.-' + IntToStr(26 - I + 20),
[rfIgnoreCase]);
Result := Str;
end;
function CompareVersionStrings(Str1, Str2: String): TCVSResult;
var
S1, S2: String;
Stl: array[0..1] of TStrArray;
I: Integer;
X: Integer;
begin
{$IFDEF DEBUG}
WriteLn('Debug: comparing' + Str1 + ' vs ' + Str2);
{$ENDIF}
Result := csEqual;
S1 := StringReplaceCharacters(Str1);
S2 := StringReplaceCharacters(Str2);
if S1 = S2 then
Exit;
Stl[0] := Explode('.', S1);
Stl[1] := Explode('.', S2);
I := 0;
if Length(Stl[0]) > Length(Stl[1]) then
begin
X := 1;
Result := csFirst;
end
else if Length(Stl[0]) < Length(Stl[1]) then
begin
X := 0;
Result := csSecond;
end;
for I := 0 to Length(Stl[X]) -1 do
begin
{$IFDEF DEBUG}
WriteLn('Debug: '+Stl[0][I]+' vs '+Stl[1][I]);
{$ENDIF}
if StrToInt(Stl[0][I]) > StrToInt(Stl[1][I]) then
begin
Result := csFirst;
Exit;
end
else if StrToInt(Stl[0][I]) < StrToInt(Stl[1][I]) then
begin
Result := csSecond;
Exit;
end;
end;
end;
begin
case CompareVersionStrings('1.0RC1', '1.0a') of
csFirst: WriteLn('First one');
csSecond: WriteLn('Second one');
csEqual: WriteLn('Equal Strings');
end;
ReadLn;
end.
Die Aufgabenstellung hier war allerdings bei Strings wie:
Code:
1a
1b
1RC1
1RC2
1.1
1.1.1
1.1.2
1.2
1.2fix
1.2fix2
2a
...
den neusten Finden zu können.
|
|
Zitat
|