forked from TES5Edit/TES5Edit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wbBetterStringList.pas
327 lines (281 loc) · 8.26 KB
/
wbBetterStringList.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
{******************************************************************************
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
*******************************************************************************}
unit wbBetterStringList;
{$I wbDefines.inc}
interface
uses
System.Types,
System.Classes,
System.SysUtils;
type
TStringListHelper = class helper for TStringList
procedure RemoveDuplicates;
end;
implementation
uses
DDetours,
wbInterface,
wbSort;
type
TAssign = procedure(Source: TPersistent) of object;
TStringsPrivateHacker = class(TPersistent)
private
FEncoding: TEncoding;
FDefaultEncoding: TEncoding;
FLineBreak: string;
FAdapter: IStringsAdapter;
FUpdateCount: Integer;
FDelimiter: Char;
FQuoteChar: Char;
FNameValueSeparator: Char;
FOptions: TStringsOptions;
end;
TStringListPrivateHacker = class(TStringsPrivateHacker)
private
FList: TStringItemList;
FCount: Integer;
FCapacity: Integer;
FSorted: Boolean;
FDuplicates: TDuplicates;
FCaseSensitive: Boolean;
FOnChange: TNotifyEvent;
FOnChanging: TNotifyEvent;
FOwnsObject: Boolean;
end;
TStringListProtectedHacker = class(TStringList);
TCompareStrings = function(const S1, S2: string): Integer of object;
var
CodePointer_TStringList_Assign : TAssign;
Trampoline_TStringList_Assign : procedure(Self: TObject; Source: TPersistent);
procedure Detour_TStringList_Assign(Self: TStringListProtectedHacker; Source: TStringListPrivateHacker);
var
CompareSelf, CompareSource: TCompareStrings;
begin
if TObject(Source) is TStringList then begin
with TStringListPrivateHacker(Self), Self do begin
BeginUpdate;
try
Clear;
FCaseSensitive := Source.FCaseSensitive;
FDuplicates := Source.FDuplicates;
{ Don't sort during copying, will sort after if necessary }
FSorted := False;
DefaultEncoding := Source.FDefaultEncoding;
SetEncoding(Source.FEncoding);
FLineBreak := Source.FLineBreak;
FDelimiter := Source.FDelimiter;
FQuoteChar := Source.FQuoteChar;
FNameValueSeparator := Source.FNameValueSeparator;
FOptions := Source.FOptions;
if Self.ClassType = TStringList then begin
{ we know exactly what class it is, so can shortcut this }
FList := Copy(Source.FList);
FCount := Source.FCount;
FCapacity := Source.FCapacity;
end else
{ there may be overriden methods that need to be called }
AddStrings(TStrings(Source));
CompareSelf := Self.CompareStrings;
CompareSource := TStringListProtectedHacker(Source).CompareStrings;
if TMethod(CompareSelf).Code = TMethod(CompareSelf).Code then
{ source and self are using the same compare method, so if source
was already sorted, self is now already correctly sorted too }
FSorted := Source.FSorted
else
{ source and self are using different compare methods, we need to
actually sort the strings now }
Sorted := Source.FSorted;
finally
EndUpdate;
end;
end;
end else
Trampoline_TStringList_Assign(Self, Source);
end;
type
TSort = procedure of object;
var
CodePointer_TStringList_Sort : TSort;
CodePointer_TStringList_CompareStrings : TCompareStrings;
Trampoline_TStringList_Sort : procedure(Self: TStringListProtectedHacker);
function ListSortCompareStringItemPtr_AnsiCompareStr(Item1, Item2: TwbMergeSort<TwbTwoPtr>.TPtr): Integer;
{$IFDEF WIN32}
asm
mov eax,[eax]
mov edx,[edx]
jmp AnsiCompareStr
end;
{$ENDIF}
{$IFDEF WIN64}
asm
mov rcx,[rcx]
mov rdx,[rdx]
jmp AnsiCompareStr
end;
{$ENDIF}
function ListSortCompareStringItemPtr_AnsiCompareText(Item1, Item2: TwbMergeSort<TwbTwoPtr>.TPtr): Integer;
{$IFDEF WIN32}
asm
mov eax,[eax]
mov edx,[edx]
jmp AnsiCompareText
end;
{$ENDIF}
{$IFDEF WIN64}
asm
mov rcx,[rcx]
mov rdx,[rdx]
jmp AnsiCompareText
end;
{$ENDIF}
function ListSortCompareStringItemPtr_CompareStr(Item1, Item2: TwbMergeSort<TwbTwoPtr>.TPtr): Integer;
{$IFDEF WIN32}
asm
mov eax,[eax]
mov edx,[edx]
jmp CompareStr
end;
{$ENDIF}
{$IFDEF WIN64}
asm
mov rcx,[rcx]
mov rdx,[rdx]
jmp CompareStr
end;
{$ENDIF}
function ListSortCompareStringItemPtr_CompareText(Item1, Item2: TwbMergeSort<TwbTwoPtr>.TPtr): Integer;
{$IFDEF WIN32}
asm
mov eax,[eax]
mov edx,[edx]
jmp CompareText
end;
{$ENDIF}
{$IFDEF WIN64}
(**)
asm
mov rcx,[rcx]
mov rdx,[rdx]
jmp CompareText
end;
(** )
begin
Result := CompareText(String(Item1^.A), string(Item2^.A));
end;
(**)
{$ENDIF}
procedure Detour_TStringList_Sort(Self: TStringListProtectedHacker);
var
CanHandle : Boolean;
CompareSelf : TCompareStrings;
ListSortCompare : TwbMergeSort<TwbTwoPtr>.TListSortCompareTPtr;
//List: TStringItemList;
i: Integer;
begin
with TStringListPrivateHacker(Self), Self do begin
if FSorted or (FCount < 2) then
Exit;
CanHandle := True;
if ClassType <> TStringList then begin
CompareSelf := CompareStrings;
if TMethod(CompareSelf).Code <> TMethod(CodePointer_TStringList_CompareStrings).Code then
CanHandle := False;
end;
if CanHandle then begin
if UseLocale then
if CaseSensitive then
ListSortCompare := ListSortCompareStringItemPtr_AnsiCompareStr
else
ListSortCompare := ListSortCompareStringItemPtr_AnsiCompareText
else
if CaseSensitive then
ListSortCompare := ListSortCompareStringItemPtr_CompareStr
else
ListSortCompare := ListSortCompareStringItemPtr_CompareText;
TwbMergeSort<TwbTwoPtr>.Sort(@FList[0], FCount, ListSortCompare);
end else
Trampoline_TStringList_Sort(Self);
end;
end;
{ TStringListHelper }
procedure TStringListHelper.RemoveDuplicates;
var
CanHandle : Boolean;
CompareSelf : TCompareStrings;
ListSortCompare : function(const S1, S2: string): Integer;
i, j : Integer;
Obj : TObject;
begin
if Count < 2 then
Exit;
CanHandle := True;
if ClassType <> TStringList then begin
CompareSelf := CompareStrings;
if TMethod(CompareSelf).Code <> TMethod(CodePointer_TStringList_CompareStrings).Code then
CanHandle := False;
end;
if CanHandle then begin
if UseLocale then
if CaseSensitive then
ListSortCompare := AnsiCompareStr
else
ListSortCompare := AnsiCompareText
else
if CaseSensitive then
ListSortCompare := CompareStr
else
ListSortCompare := CompareText;
with TStringListPrivateHacker(Self) do begin
j := 1;
for i := 1 to Pred(FCount) do
if ListSortCompare(FList[i].FString, FList[Pred(j)].FString) <> 0 then begin
if i <> j then begin
FList[j] := FList[i];
end;
Inc(j);
end else with FList[i] do begin
if OwnsObjects then
Obj := FObject
else
Obj := nil;
FString := '';
FObject := nil;
Obj.Free;
end;
FCount := j;
end;
end else
raise Exception.Create('Can''t handle RemoveDuplicates for this list.');
end;
initialization
with TStringListProtectedHacker(TStringList.Create) do try
CodePointer_TStringList_Assign := Assign;
CodePointer_TStringList_Sort := Sort;
CodePointer_TStringList_CompareStrings := CompareStrings;
finally
Free;
end;
BeginHooks;
try
@Trampoline_TStringList_Assign := InterceptCreate(@CodePointer_TStringList_Assign, @Detour_TStringList_Assign);
@Trampoline_TStringList_Sort := InterceptCreate(@CodePointer_TStringList_Sort, @Detour_TStringList_Sort);
finally
EndHooks;
end;
finalization
BeginUnHooks;
try
InterceptRemove(@Trampoline_TStringList_Assign);
InterceptRemove(@Trampoline_TStringList_Sort);
finally
EndUnHooks;
end;
end.