-
Notifications
You must be signed in to change notification settings - Fork 14
/
Versions.pas
164 lines (138 loc) · 4.93 KB
/
Versions.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
unit Versions;
interface
type
TVersion = record
public
Major: string;
Minor: string;
Release: string;
Build: string;
Other: string;
constructor Create(Version: string);
function ToString(): string;
function IsEmpty(): Boolean;
class function Empty: TVersion; static;
class operator Equal(const Left, Right: TVersion): Boolean;
class operator NotEqual(const Left, Right: TVersion): Boolean;
class operator LessThan(const Left, Right: TVersion): Boolean;
class operator LessThanOrEqual(const Left, Right: TVersion): Boolean;
class operator GreaterThan(const Left, Right: TVersion): Boolean;
class operator GreaterThanOrEqual(const Left, Right: TVersion): Boolean;
class operator Implicit(const Some: TVersion): string;
class operator Implicit(const Some: string): TVersion;
private
class function CompareNumericStr(const Left, Right: string): Integer; static;
class function TryCompareStr(const Left, Right: string): Integer; static;
end;
implementation
uses
System.SysUtils;
{ TVersion }
constructor TVersion.Create(Version: string);
var
StringList: TArray<string>;
Len: Integer;
begin
StringList := Version.Split(['.'], 5);
Len := Length(StringList);
if Len > 0 then Major := StringList[0] else Major := string.Empty;
if Len > 1 then Minor := StringList[1] else Minor := string.Empty;
if Len > 2 then Release := StringList[2] else Release := string.Empty;
if Len > 3 then Build := StringList[3] else Build := string.Empty;
if Len > 4 then Other := StringList[4] else Other := string.Empty;
end;
function TVersion.ToString: string;
var
StringList: TArray<string>;
begin
try
if not Major.IsEmpty then StringList := StringList + [Major];
if not Minor.IsEmpty then StringList := StringList + [Minor];
if not Release.IsEmpty then StringList := StringList + [Release];
if not Build.IsEmpty then StringList := StringList + [Build];
if not Other.IsEmpty then StringList := StringList + [Other];
Result := String.Join('.', StringList);
except
Result := string.Empty;
end;
end;
function TVersion.IsEmpty: Boolean;
begin
Result := Major.IsEmpty and Minor.IsEmpty and Release.IsEmpty and
Build.IsEmpty and Other.IsEmpty;
end;
class function TVersion.Empty: TVersion;
begin
Result := '0.0.0.0';
end;
class operator TVersion.Equal(const Left, Right: TVersion): Boolean;
begin
Result := (TryCompareStr(Left.Major, Right.Major) = 0) and
(TryCompareStr(Left.Minor, Right.Minor) = 0) and
(TryCompareStr(Left.Release, Right.Release) = 0) and
(TryCompareStr(Left.Build, Right.Build) = 0) and
(TryCompareStr(Left.Other, Right.Other) = 0);
end;
class operator TVersion.NotEqual(const Left, Right: TVersion): Boolean;
begin
Result := not (Left = Right);
end;
class operator TVersion.LessThan(const Left, Right: TVersion): Boolean;
var
CmpMajor, CmpMinor, CmpRelease, CmpBuild, CmpOther: Integer;
begin
CmpMajor := TryCompareStr(Left.Major, Right.Major);
CmpMinor := TryCompareStr(Left.Minor, Right.Minor);
CmpRelease := TryCompareStr(Left.Release, Right.Release);
CmpBuild := TryCompareStr(Left.Build, Right.Build);
CmpOther := TryCompareStr(Left.Other, Right.Other);
Result :=
(CmpMajor < 0) or
((CmpMajor <= 0) and (CmpMinor < 0)) or
((CmpMajor <= 0) and (CmpMinor <= 0) and (CmpRelease < 0)) or
((CmpMajor <= 0) and (CmpMinor <= 0) and (CmpRelease <= 0) and (CmpBuild < 0 )) or
((CmpMajor <= 0) and (CmpMinor <= 0) and (CmpRelease <= 0) and (CmpBuild <= 0) and (CmpOther < 0));
end;
class operator TVersion.LessThanOrEqual(const Left, Right: TVersion): Boolean;
begin
Result := (Left < Right) or (Left = Right);
end;
class operator TVersion.GreaterThan(const Left, Right: TVersion): Boolean;
begin
Result := not (Left <= Right);
end;
class operator TVersion.GreaterThanOrEqual(const Left, Right: TVersion): Boolean;
begin
Result := not (Left < Right);
end;
class operator TVersion.Implicit(const Some: TVersion): string;
begin
Result := Some.ToString;
end;
class operator TVersion.Implicit(const Some: string): TVersion;
begin
Result.Create(Some);
end;
class function TVersion.CompareNumericStr(const Left, Right: string): Integer;
var
LeftInt, RightInt: Integer;
function GetInt(Str: string): Integer;
begin
if Str.IsEmpty then Result := 0 else Result := Integer.Parse(Str);
end;
begin
LeftInt := GetInt(Left);
RightInt := GetInt(Right);
if LeftInt > RightInt then Result := 1
else if LeftInt < RightInt then Result := -1
else Result := 0;
end;
class function TVersion.TryCompareStr(const Left, Right: string): Integer;
begin
try
Result := CompareNumericStr(Left, Right);
except
Result := string.CompareOrdinal(Left, Right);
end;
end;
end.