-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLightCore.StringList.pas
More file actions
340 lines (261 loc) · 8.92 KB
/
Copy pathLightCore.StringList.pas
File metadata and controls
340 lines (261 loc) · 8.92 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
UNIT LightCore.StringList;
{=============================================================================================================
2026.01.30
www.GabrielMoraru.com
Github.com/GabrielOnDelphi/Delphi-LightSaber/blob/main/System/Copyright.txt
==============================================================================================================
TStringList class helper
Remove duplicate items
Remove lines containing certain text
Trim
Shuffle
Reverse sort
HighestString
Yes, I know, we all hate class helpers because the last used class helper will overrite the other ones.
=============================================================================================================}
INTERFACE
USES
System.SysUtils, System.Classes;
TYPE
TTSL= class helper for TStringList
private
public
procedure RemoveDuplicateString(CONST s: string); { Removes ONE occurrence of the specified string (the last one, scanning from the end; case-insensitive) }
procedure RemoveDuplicateFile(CONST FileName: string); { Removes ONE occurrence of the specified filename (the last one, scanning from the end; case-insensitive) }
procedure RemoveDuplicates; { THIS WILL SORT THE LIST !!! }
procedure RemoveEmptyLines;
function RemoveLines (const BadWord: string): Integer;
function KeepLines (const KeepText: string): Integer;
procedure Trim; { Trim whitespace at both ends of each line. WARNING: also removes control chars (Tab, CR, LF) from INSIDE the line - do not use on tab-separated data! }
procedure Shuffle;
function FindLine(const Needle: string): Integer; { Find line that contains the specified text }
procedure SortReverse;
function HighestString: string;
function Concatenate(const Separator: string): String;
// Top
function GetTopLines(Count: Integer; IgnoreEmptyLines: Boolean= TRUE): string;
procedure RemoveTopLines(const aCount: Integer);
end;
// Work on a multi-line text
function String2TSL (CONST s: string): TStringList; { Converts a string to a TStringList. In other words it breaks the text to multiple lines. I need to call Free after this! }
function ExtractTopLines(CONST Text: string; Count: Integer; IgnoreEmptyLines: Boolean= TRUE): string; { Returns the top x lines from a text (multiple lines) }
function FindLine (CONST Needle, Haystack: string): string;
IMPLEMENTATION
USES LightCore;
{ Returns all lines concatenated in one single line.
Good to convert a list of items into a single string, where items are separated by the specified separator.
Note: Returns trailing separator after last item. }
function TTSL.Concatenate(const Separator: string): string;
begin
Result:= '';
for var s in Self do
Result:= Result+ s+ Separator;
end;
{ Finds the first line that contains the specified text (case-sensitive).
Returns the line index, or -1 if not found. }
function TTSL.FindLine(const Needle: string): Integer;
VAR i: Integer;
begin
Result:= -1;
for i:= 0 to Count-1 do
if Pos(Needle, Self[i]) > 0
then EXIT(i);
end;
{ Remove all lines that contain the specified text (case-insensitive).
Returns the number of lines removed. }
function TTSL.RemoveLines(const BadWord: string): Integer;
VAR i: Integer;
begin
Result:= 0;
for i:= Count-1 downto 0 do
if PosInsensitive(BadWord, Self[i]) > 0 then
begin
Delete(i);
Inc(Result);
end;
end;
{ Remove all lines that do NOT contain the specified text (case-insensitive).
Returns the number of lines removed. }
function TTSL.KeepLines(const KeepText: string): Integer;
VAR i: Integer;
begin
Result:= 0;
for i:= Count-1 downto 0 do
if PosInsensitive(KeepText, Self[i]) = 0 then
begin
Delete(i);
Inc(Result);
end;
end;
{ Removes all empty lines from the list. }
procedure TTSL.RemoveEmptyLines;
VAR i: Integer;
begin
for i:= Count-1 downto 0 do
if Self[i] = ''
then Self.Delete(i);
end;
{ Trim whitespace (spaces, tabs, line breaks) from both ends of each line.
WARNING: the RemoveFormatings pass also removes control chars (Tab, CR, LF) from INSIDE each line,
so this is NOT a pure trim - do not use on tab-separated data! }
procedure TTSL.Trim;
VAR i: Integer;
begin
for i:= Count-1 downto 0 do
Self[i]:= LightCore.RemoveFormatings(Self[i]);
for i:= Count-1 downto 0 do
Self[i]:= System.SysUtils.Trim(Self[i]);
end;
{ Returns the lexicographically highest string in the list.
For example: [ABC, ABCD] returns ABCD; [ABCD, B] returns B. }
function TTSL.HighestString: string;
VAR i: Integer;
begin
Result:= '';
for i:= 0 to Self.Count-1 do
if Self[i] > Result
then Result:= Self[i];
end;
{ Compare function for reverse sorting. }
function StringListSortCompare(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result:= AnsiCompareStr(List[Index2], List[Index1]);
end;
{ Sorts the list in reverse (descending) order. }
procedure TTSL.SortReverse;
begin
CustomSort(StringListSortCompare);
end;
{ Randomly shuffles the items in the list (Fisher-Yates algorithm). }
procedure TTSL.Shuffle;
VAR i: Integer;
begin
for i:= Count-1 downto 1 do
Exchange(i, Random(i + 1));
end;
{ Removes ONE occurrence of the specified string (case-insensitive).
Note: the list is scanned from the END, so if the string appears more than once, the LAST occurrence is removed. }
procedure TTSL.RemoveDuplicateString(const s: string);
VAR i: Integer;
begin
for i:= Count-1 downto 0 do
if SameText(s, Self[i]) then
begin
Delete(i);
Break;
end;
end;
{ Removes ONE occurrence of the specified filename (case-insensitive).
Note: the list is scanned from the END, so if the filename appears more than once, the LAST occurrence is removed. }
procedure TTSL.RemoveDuplicateFile(const FileName: string);
var
i: Integer;
begin
for i:= Count-1 downto 0 do
if SameText(FileName, Self[i]) then
begin
Delete(i);
Break;
end;
end;
{ Removes duplicate entries from the list (case-insensitive).
WARNING: This will SORT the list! }
procedure TTSL.RemoveDuplicates;
var
Buffer: TStringList;
i: Integer;
WasCaseSensitive: Boolean;
WasDuplicates: TDuplicates;
begin
{ TStringList.Assign below also clones Sorted/Duplicates/CaseSensitive from Buffer, clobbering ours. Save them }
WasCaseSensitive:= CaseSensitive;
WasDuplicates := Duplicates;
Buffer:= TStringList.Create;
try
Buffer.Sorted:= True;
Buffer.Duplicates:= dupIgnore;
Buffer.BeginUpdate;
for i:= 0 to Count - 1 do
Buffer.Add(Self[i]);
Buffer.EndUpdate;
Assign(Buffer);
finally
FreeAndNil(Buffer);
end;
{ Cancel Sorted flag to allow editing, otherwise "Operation not allowed on sorted list" }
Sorted:= FALSE;
CaseSensitive:= WasCaseSensitive;
Duplicates := WasDuplicates;
end;
{ TOP }
{ Remove the first x lines from the list. }
procedure TTSL.RemoveTopLines(CONST aCount: Integer);
var
i: Integer;
begin
if aCount > Count
then raise EArgumentOutOfRangeException.CreateFmt('RemoveTopLines: aCount (%d) exceeds list Count (%d)', [aCount, Count]);
for i:= aCount-1 downto 0 do
Self.Delete(i);
end;
{ Returns the first Count lines as a single string.
If IgnoreEmptyLines is True, empty lines are skipped (not counted). }
function TTSL.GetTopLines(Count: Integer; IgnoreEmptyLines: Boolean= TRUE): string;
var
s: string;
Total: Integer;
begin
Total:= 0;
Result:= '';
for s in Self do
begin
{ Skip empty lines if requested }
if IgnoreEmptyLines AND (s = '')
then Continue;
Inc(Total);
Result:= Result + s + CRLF;
if Total = Count
then Break;
end;
Result:= RemoveLastEnter(Result);
end;
{ Converts a multi-line string to a TStringList.
IMPORTANT: Caller must free the returned object! }
function String2TSL(const s: string): TStringList;
begin
Result:= TStringList.Create;
Result.Text:= s;
end;
{-----------------------------------------------------------------------------
EXTRACT
-----------------------------------------------------------------------------}
{ Returns the first Count lines from a multi-line text. }
function ExtractTopLines(const Text: string; Count: Integer; IgnoreEmptyLines: Boolean= TRUE): string;
var TSL: TStringList;
begin
TSL:= TStringList.Create;
try
TSL.Text:= Text;
Result:= TSL.GetTopLines(Count, IgnoreEmptyLines);
finally
FreeAndNil(TSL);
end;
end;
{ Searches for Needle (partial match) in a multi-line Haystack.
Returns the entire line containing the Needle, or empty string if not found. }
function FindLine(const Needle, Haystack: string): string;
var
TSL: TStringList;
s: string;
begin
Result:= '';
TSL:= String2TSL(Haystack);
try
for s in TSL do
if Pos(Needle, s) > 0
then EXIT(s);
finally
FreeAndNil(TSL);
end;
end;
end.