forked from ahausladen/DDevExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelphiParserContainers.pas
More file actions
247 lines (204 loc) · 5.97 KB
/
Copy pathDelphiParserContainers.pas
File metadata and controls
247 lines (204 loc) · 5.97 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
{******************************************************************************}
{* *}
{* Container classes *}
{* *}
{* (C) 2005 Andreas Hausladen *}
{* *}
{******************************************************************************}
unit DelphiParserContainers;
{$I ..\jedi\jedi.inc}
{$IFDEF COMPILER10_UP}
{$DEFINE D9}
{$ENDIF}
interface
uses
SysUtils, Classes;
type
THashtableStringList = class(TStringList)
protected
function CompareStrings(const S1: string; const S2: string): Integer; override;
end;
THashtable = class(TObject)
private
FItems: THashtableStringList;
FOwnsObjects: Boolean;
function GetCount: Integer; {$IFDEF D9}inline;{$ENDIF}
function GetValue(const AKey: string): TObject; {$IFDEF D9}inline;{$ENDIF}
function GetItem(Index: Integer): TObject; {$IFDEF D9}inline;{$ENDIF}
public
constructor Create(CaseSensitive: Boolean; AOwnsObjects: Boolean = True);
destructor Destroy; override;
procedure Clear;
procedure Remove(const AKey: string);
procedure Add(const AKey: string; AValue: TObject); {$IFDEF D9}inline;{$ENDIF}
function Contains(const AKey: string): Boolean; {$IFDEF D9}inline;{$ENDIF}
property Values[const AKey: string]: TObject read GetValue; default;
property Items[Index: Integer]: TObject read GetItem;
property Count: Integer read GetCount;
end;
TIntegerList = class(TList)
private
function GetItem(Index: Integer): Integer; {$IFDEF D9}inline;{$ENDIF}
procedure SetItem(Index: Integer; const Value: Integer);{$IFDEF D9}inline;{$ENDIF}
public
function Add(Value: Integer): Integer; {$IFDEF D9}inline;{$ENDIF}
property Items[Index: Integer]: Integer read GetItem write SetItem; default;
end;
TStringDictionary = class(TObject)
private
FItems: THashtableStringList;
FValues: TStringList;
function GetCount: Integer;
function GetValue(const AKey: string): string;
public
constructor Create;
destructor Destroy; override;
procedure Add(const AKey: string; const AValue: string);
function Contains(const AKey: string): Boolean;
function Find(const AKey: string): string;
property Values[const AKey: string]: string read GetValue; default;
property Count: Integer read GetCount;
end;
TStringCollection = class(TStringList)
public
function Contains(const Value: string): Boolean;
procedure RemoveAt(Index: Integer);
end;
implementation
{ THashtable }
constructor THashtable.Create(CaseSensitive: Boolean; AOwnsObjects: Boolean);
begin
inherited Create;
FOwnsObjects := AOwnsObjects;
FItems := THashtableStringList.Create;
FItems.Sorted := True;
FItems.Duplicates := dupError;
{$IFDEF COMPILER6_UP}
FItems.CaseSensitive := CaseSensitive;
{$ELSE}
Assert(CaseSensitive = False, 'TStringList has no CaseSensitive property in Delphi 5');
{$ENDIF COMPILER6_UP}
end;
destructor THashtable.Destroy;
var
i: Integer;
begin
if FOwnsObjects then
for i := 0 to Count - 1 do
FItems.Objects[i].Free;
FItems.Free;
inherited Destroy;
end;
function THashtable.Contains(const AKey: string): Boolean;
begin
Result := FItems.IndexOf(AKey) >= 0;
end;
function THashtable.GetCount: Integer;
begin
Result := FItems.Count;
end;
procedure THashtable.Add(const AKey: string; AValue: TObject);
begin
FItems.AddObject(AKey, AValue);
end;
function THashtable.GetValue(const AKey: string): TObject;
var
Index: Integer;
begin
if FItems.Find(AKey, Index) then
Result := FItems.Objects[Index]
else
Result := nil;
end;
procedure THashtable.Remove(const AKey: string);
var
Index: Integer;
begin
if FItems.Find(AKey, Index) then
begin
if FOwnsObjects then
FItems.Objects[Index].Free;
FItems.Delete(Index);
end;
end;
procedure THashtable.Clear;
begin
FItems.Clear;
end;
function THashtable.GetItem(Index: Integer): TObject;
begin
Result := FItems.Objects[Index];
end;
{ TIntegerList }
function TIntegerList.GetItem(Index: Integer): Integer;
begin
Result := Integer(inherited Items[Index]);
end;
function TIntegerList.Add(Value: Integer): Integer;
begin
Result := inherited Add(Pointer(Value));
end;
procedure TIntegerList.SetItem(Index: Integer; const Value: Integer);
begin
inherited Items[Index] := Pointer(Value);
end;
{ TStringCollection }
function TStringCollection.Contains(const Value: string): Boolean;
begin
Result := IndexOf(Value) >= 0;
end;
procedure TStringCollection.RemoveAt(Index: Integer);
begin
Delete(Index);
end;
{ TStringDictionary }
constructor TStringDictionary.Create;
begin
inherited Create;
FItems := THashtableStringList.Create;
FValues := TStringList.Create;
FItems.Sorted := True;
FItems.Duplicates := dupError;
end;
destructor TStringDictionary.Destroy;
begin
FItems.Free;
FValues.Free;
inherited Destroy;
end;
function TStringDictionary.GetCount: Integer;
begin
Result := FItems.Count;
end;
function TStringDictionary.GetValue(const AKey: string): string;
var
Index: Integer;
begin
Result := '';
if FItems.Find(AKey, Index) then
if Integer(FItems.Objects[Index]) >= 0 then
Result := FValues[Integer(FItems.Objects[Index])];
end;
procedure TStringDictionary.Add(const AKey: string; const AValue: string);
begin
FItems.AddObject(AKey, Pointer(FValues.Add(AValue)));
end;
function TStringDictionary.Contains(const AKey: string): Boolean;
var
Index: Integer;
begin
Result := FItems.Find(AKey, Index);
end;
function TStringDictionary.Find(const AKey: string): string;
begin
Result := Values[AKey];
end;
{ THashtableStringList }
function THashtableStringList.CompareStrings(const S1, S2: string): Integer;
begin
if CaseSensitive then
Result := CompareStr(S1, S2)
else
Result := CompareText(S1, S2);
end;
end.