Menu

[r2337]: / trunk / Src / UIStringList.pas  Maximize  Restore  History

Download this file

658 lines (609 with data), 22.8 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2007-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Defines interface to an object that stores and manipulates a list of strings,
* along with a class that implements the interface.
}
unit UIStringList;
interface
uses
// Delphi
Classes,
// Project
IntfCommon;
type
{
IStringList:
Interface to an object that stores and manipulates a list of strings.
}
IStringList = interface(IInterface)
['{D8A8E21F-FFE9-486D-8DA3-37CD08922023}']
function Add(const Str: string): Integer; overload;
{Adds a string to end of list.
@param Str [in] String to be added to list.
@return Index of new string in list.
}
procedure Add(const Strs: TStrings); overload;
{Adds all items from a string list to end of list.
@param Strs [in] String list to be added.
}
procedure Add(const Strs: IStringList); overload;
{Adds all items from another IStringList instance to end of list.
@param Strs [in] String list to be added.
}
procedure Add(const Strs: array of string); overload;
{Adds all strings from an array to end of list.
@param Strs [in] Dynamic array of strings to be added.
}
procedure Add(const Str: string; const Delim: string;
const AllowEmpty: Boolean; const Trim: Boolean = False); overload;
{Splits a string at delimiter and adds component parts of string to end of
list.
@param Str [in] String to be split.
@param Delim [in] String that delimits components of string.
@param AllowEmpty [in] Determines whether empty components are added to
list (True) or ignored (False).
@param Trim [in] Determines whether strings are trimmed of trailing and
leading spaces before adding to list.
}
procedure SetText(const Text: string; const Delim: string;
const AllowEmpty: Boolean; const Trim: Boolean = False);
{Sets list to component parts of a string, based on a delimiter.
@param Str [in] String to be split.
@param Delim [in] String that delimits components of string.
@param AllowEmpty [in] Determines whether empty components are stored in
list (True) or ignored (False).
@param Trim [in] Determines whether strings are trimmed of trailing and
leading spaces before adding to list.
}
function GetText(const Glue: string; const AllowEmpty: Boolean): string;
{Gets contents of list as single string with items joined by a string.
@param Glue [in] String used to glue list items together.
@param AllowEmpty [in] Determines whether empty items are to be included
in rendered text.
@return Required text string.
}
procedure Clear;
{Clears the list.
}
function IndexOf(const Str: string): Integer;
{Gets index of a string in list.
@param Str [in] String to be found.
@return Index of string in list or -1 if string not found.
}
function Contains(const Str: string): Boolean;
{Checks whether list contains a string.
@param Str [in] String to be found.
@return True if Str in list, False otherwise.
}
function Count: Integer;
{Gets number of items in list.
@return Required number of items.
}
procedure Delete(const Idx: Integer);
{Deletes a string from list.
@param Index of string to be deleted.
}
function GetItem(const Idx: Integer): string;
{Gets string at a given position in list.
@param Idx [in] Index of required string. Must be in range.
@return Required string.
}
procedure SetItem(const Idx: Integer; const Str: string);
{Sets string at a given position in list.
@param Idx [in] Index in list to receive strings. Must be in range.
@param Str [in] String to be stored in list.
}
property Items[const Idx: Integer]: string
read GetItem write SetItem; default;
{Indexed access to strings in list}
function GetCaseSensitive: Boolean;
{Checks whether searching in string list is to be case sensitive.
@return True if searching is case sensitive, False if not case
sensitive.
}
procedure SetCaseSensitive(const Flag: Boolean);
{Sets search case sensitivity.
@param Flag [in] True if case sensitive searching is required, False if
case insensitive searching is required.
}
property CaseSensitive: Boolean
read GetCaseSensitive write SetCaseSensitive; // default False;
{Determines whether searching is case sensitive or case insensitive}
function GetEnumerator: TStringsEnumerator;
{Creates an enumerator for the string list.
@return Enumerator instance.
}
procedure CopyTo(const SL: TStrings; const Overwrite: Boolean = False);
{Copies this object's string to a string list.
@param SL [in] String list to receive copy of this object's strings.
@param Overwrite [in] Flag indicating whether to clear SL before copying
(True) or to append strings to SL (False).
}
procedure CopyFrom(const SL: TStrings; const Overwrite: Boolean = False);
{Copies a string list's strings to this object.
@param SL [in] String list containing strings to be copies.
@param Overwrite [in] Flag indicating whether to clear this object
before copying (True) or to append the strings to this object (False).
}
function IsValidIndex(const Idx: Integer): Boolean;
{Checks if an index into the string list is valid, i.e. it is in range and
can be used as an index into the Items[] property without error.
@param Idx [in] Index to check.
@return True if index is valid and False if invalid.
}
function ToArray: TArray<string>;
{Copies strings from string list into an array of strings.
@return Array of strings.
}
end;
{
TIStringList:
Class that implements IStringList and provides various overloaded
constructors.
}
TIStringList = class(TInterfacedObject,
IStringList, IAssignable, IClonable
)
strict private
var
fStrings: TStringList;
{Stores string list}
protected
{ IStringList methods }
function Add(const Str: string): Integer; overload;
{Adds a string to end of list.
@param Str [in] String to be added to list.
@return Index of new string in list.
}
procedure Add(const Strs: TStrings); overload;
{Adds all items from a string list to end of list.
@param Strs [in] String list to be added.
}
procedure Add(const Strs: IStringList); overload;
{Adds all items from another IStringList instance to end of list.
@param Strs [in] String list to be added.
}
procedure Add(const Strs: array of string); overload;
{Adds all strings from an array to end of list.
@param Strs [in] Dynamic array of strings to be added.
}
procedure Add(const Str: string; const Delim: string;
const AllowEmpty: Boolean; const Trim: Boolean = False); overload;
{Splits a string at delimiter and adds component parts of string to end of
list.
@param Str [in] String to be split.
@param Delim [in] String that delimits components of string.
@param AllowEmpty [in] Determines whether empty components are added to
list (True) or ignored (False).
@param Trim [in] Determines whether strings are trimmed of trailing and
leading spaces before adding to list.
}
procedure SetText(const Text: string; const Delim: string;
const AllowEmpty: Boolean; const Trim: Boolean = False);
{Sets list to component parts of a string, based on a delimiter.
@param Str [in] String to be split.
@param Delim [in] String that delimits components of string.
@param AllowEmpty [in] Determines whether empty components are stored in
list (True) or ignored (False).
@param Trim [in] Determines whether strings are trimmed of trailing and
leading spaces before adding to list.
}
function GetText(const Glue: string; const AllowEmpty: Boolean): string;
{Gets contents of list as single string with items joined by a string.
@param Glue [in] String used to glue list items together.
@param AllowEmpty [in] Determines whether empty items are to be included
in rendered text.
@return Required text string.
}
procedure Clear;
{Clears the list.
}
function IndexOf(const Str: string): Integer;
{Gets index of a string in list.
@param Str [in] String to be found.
@return Index of string in list or -1 if string not found.
}
function Contains(const Str: string): Boolean;
{Checks whether list contains a string.
@param Str [in] String to be found.
@return True if Str in list, False otherwise.
}
function Count: Integer;
{Gets number of items in list.
@return Required number of items.
}
procedure Delete(const Idx: Integer);
{Deletes a string from list.
@param Index of string to be deleted.
}
function GetItem(const Idx: Integer): string;
{Gets string at a given position in list.
@param Idx [in] Index of required string. Must be in range.
@return Required string.
}
procedure SetItem(const Idx: Integer; const Str: string);
{Sets string at a given position in list.
@param Idx [in] Index in list to receive strings. Must be in range.
@param Str [in] String to be stored in list.
}
function GetCaseSensitive: Boolean;
{Checks whether searching in string list is to be case sensitive.
@return True if searching is case sensitive, False if not case
sensitive.
}
procedure SetCaseSensitive(const Flag: Boolean);
{Sets search case sensitivity.
@param Flag [in] True if case sensitive searching is required, False if
case insensitive searching is required.
}
function GetEnumerator: TStringsEnumerator;
{Creates an enumerator for the string list.
@return Enumerator instance.
}
procedure CopyTo(const SL: TStrings; const Overwrite: Boolean = False);
{Copies this object's string to a string list.
@param SL [in] String list to receive copy of this object's strings.
@param Overwrite [in] Flag indicating whether to clear SL before copying
(True) or to append strings to SL (False).
}
procedure CopyFrom(const SL: TStrings; const Overwrite: Boolean = False);
{Copies a string list's strings to this object.
@param SL [in] String list containing strings to be copies.
@param Overwrite [in] Flag indicating whether to clear this object
before copying (True) or to append the strings to this object (False).
}
function IsValidIndex(const Idx: Integer): Boolean;
{Checks if an index into the string list is valid, i.e. it is in range and
can be used as an index into the Items[] property without error.
@param Idx [in] Index to check.
@return True if index is valid and False if invalid.
}
function ToArray: TArray<string>;
{Copies strings from string list into an array of strings.
@return Array of strings.
}
{ IAssignable methods }
procedure Assign(const Src: IInterface);
{Sets list to a copy of another IStringList instance.
@param Src [in] String list to be copied. Must support IStringList.
@except EBug raised if Src does not support IStringList.
}
{ IClonable methods }
function Clone: IInterface;
{Creates a new instance of the object that is an extact copy of this
instance.
@return New object's IInterface interface.
}
public
constructor Create; overload;
{Class constructor. Creates new empty list.
}
constructor Create(const Strs: TStrings); overload;
{Class constructor. Creates new list containing specified strings.
@param Strs [in] List of strings to be stored in list.
}
constructor Create(const Strs: IStringList); overload;
{Class constructor. Creates new list containing strings from another
IStringList instance.
@param Strs [in] List of strings to be stored in list.
}
constructor Create(const Str: string); overload;
{Class constructor. Creates new list containing a single string.
@param Str [in] String to be included in list.
}
constructor Create(const Str, Delim: string; const AllowEmpty: Boolean;
const Trim: Boolean = False); overload;
{Class constructor. Creates new list containing strings split at a
delimiter.
@param Str [in] String to be split.
@param Delim [in] String that delimits components of string.
@param AllowEmpty [in] Determines whether empty components are stored in
list (True) or ignored (False).
@param Trim [in] Determines whether strings are trimmed of trailing and
leading spaces before adding to list.
}
constructor Create(const Strs: array of string); overload;
{Class constructor. Creates new list containing strings from array.
@param Strs [in] Array of strings to be included in list.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
end;
implementation
uses
// Delphi
SysUtils,
// Project
UExceptions, UStrUtils;
{ TIStringList }
procedure TIStringList.Add(const Strs: TStrings);
{Adds all items from a string list to end of list.
@param Strs [in] String list to be added.
}
begin
fStrings.AddStrings(Strs);
end;
function TIStringList.Add(const Str: string): Integer;
{Adds a string to end of list.
@param Str [in] String to be added to list.
@return Index of new string in list.
}
begin
Result := fStrings.Add(Str);
end;
procedure TIStringList.Add(const Strs: IStringList);
{Adds all items from another IStringList instance to end of list.
@param Strs [in] String list to be added.
}
var
Idx: Integer; // loops through strings in added list
begin
for Idx := 0 to Pred(Strs.Count) do
Add(Strs[Idx]);
end;
procedure TIStringList.Add(const Str: string; const Delim: string;
const AllowEmpty: Boolean; const Trim: Boolean);
{Splits a string at delimiter and adds component parts of string to end of
list.
@param Str [in] String to be split.
@param Delim [in] String that delimits components of string.
@param AllowEmpty [in] Determines whether empty components are added to list
(True) or ignored (False).
@param Trim [in] Determines whether strings are trimmed of trailing and
leading spaces before adding to list.
}
var
SL: TStringList; // string list to receive exploded string
begin
// Explode string an store in string list
SL := TStringList.Create;
try
StrExplode(Str, Delim, SL, AllowEmpty, Trim);
// Add strings to this list
Add(SL);
finally
SL.Free;
end;
end;
procedure TIStringList.Add(const Strs: array of string);
{Adds all strings from an array to end of list.
@param Strs [in] Dynamic array of strings to be added.
}
var
Idx: Integer; // loops thru elements of array
begin
for Idx := Low(Strs) to High(Strs) do
Add(Strs[Idx]);
end;
procedure TIStringList.Assign(const Src: IInterface);
{Sets list to a copy of another IStringList instance.
@param Src [in] String list to be copied. Must support IStringList.
@except EBug raised if Src does not support IStringList.
}
begin
if not Supports(Src, IStringList) then
raise EBug.Create(ClassName + '.Assign: Src must support IStringList');
Clear;
Add(Src as IStringList);
end;
procedure TIStringList.Clear;
{Clears the list.
}
begin
fStrings.Clear;
end;
function TIStringList.Clone: IInterface;
{Creates a new instance of the object that is an extact copy of this instance.
@return New object's IInterface interface.
}
begin
Result := TIStringList.Create(Self);
end;
function TIStringList.Contains(const Str: string): Boolean;
{Checks whether list contains a string.
@param Str [in] String to be found.
@return True if Str in list, False otherwise.
}
begin
Result := IndexOf(Str) >= 0;
end;
procedure TIStringList.CopyFrom(const SL: TStrings; const Overwrite: Boolean);
{Copies a string list's strings to this object.
@param SL [in] String list containing strings to be copies.
@param Overwrite [in] Flag indicating whether to clear this object before
copying (True) or to append the strings to this object (False).
}
begin
if OverWrite then
fStrings.Assign(SL)
else
Self.Add(SL);
end;
procedure TIStringList.CopyTo(const SL: TStrings; const Overwrite: Boolean);
{Copies this object's string to a string list.
@param SL [in] String list to receive copy of this object's strings.
@param Overwrite [in] Flag indicating whether to clear SL before copying
(True) or to append strings to SL (False).
}
begin
if Overwrite then
SL.Assign(fStrings)
else
SL.AddStrings(fStrings);
end;
function TIStringList.Count: Integer;
{Gets number of items in list.
@return Required number of items.
}
begin
Result := fStrings.Count;
end;
constructor TIStringList.Create;
{Class constructor. Creates new empty list.
}
begin
inherited Create;
fStrings := TStringList.Create;
end;
constructor TIStringList.Create(const Strs: TStrings);
{Class constructor. Creates new list containing specified strings.
@param Strs [in] List of strings to be stored in list.
}
begin
Create;
Add(Strs);
end;
constructor TIStringList.Create(const Str: string);
{Class constructor. Creates new list containing a single string.
@param Str [in] String to be included in list.
}
begin
Create;
Add(Str);
end;
constructor TIStringList.Create(const Strs: IStringList);
{Class constructor. Creates new list containing strings from another
IStringList instance.
@param Strs [in] List of strings to be stored in list.
}
begin
Create;
Add(Strs);
end;
constructor TIStringList.Create(const Str, Delim: string;
const AllowEmpty, Trim: Boolean);
{Class constructor. Creates new list containing strings split at a delimiter.
@param Str [in] String to be split.
@param Delim [in] String that delimits components of string.
@param AllowEmpty [in] Determines whether empty components are stored in
list (True) or ignored (False).
@param Trim [in] Determines whether strings are trimmed of trailing and
leading spaces before adding to list.
}
begin
Create;
Add(Str, Delim, AllowEmpty, Trim);
end;
constructor TIStringList.Create(const Strs: array of string);
{Class constructor. Creates new list containing strings from array.
@param Strs [in] Array of strings to be included in list.
}
begin
Create;
Add(Strs);
end;
procedure TIStringList.Delete(const Idx: Integer);
{Deletes a string from list.
@param Index of string to be deleted.
}
begin
fStrings.Delete(Idx);
end;
destructor TIStringList.Destroy;
{Class destructor. Tears down object.
}
begin
fStrings.Free;
inherited;
end;
function TIStringList.GetCaseSensitive: Boolean;
{Checks whether searching in string list is to be case sensitive.
@return True if searching is case sensitive, False if not case sensitive.
}
begin
Result := fStrings.CaseSensitive;
end;
function TIStringList.GetEnumerator: TStringsEnumerator;
{Creates an enumerator for the string list.
@return Enumerator instance.
}
begin
Result := fStrings.GetEnumerator;
end;
function TIStringList.GetItem(const Idx: Integer): string;
{Gets string at a given position in list.
@param Idx [in] Index of required string. Must be in range.
@return Required string.
}
begin
Result := fStrings[Idx];
end;
function TIStringList.GetText(const Glue: string;
const AllowEmpty: Boolean): string;
{Gets contents of list as single string with items joined by a string.
@param Glue [in] String used to glue list items together.
@param AllowEmpty [in] Determines whether empty items are to be included in
rendered text.
@return Required text string.
}
begin
Result := StrJoin(fStrings, Glue, AllowEmpty);
end;
function TIStringList.IndexOf(const Str: string): Integer;
{Gets index of a string in list.
@param Str [in] String to be found.
@return Index of string in list or -1 if string not found.
}
begin
Result := fStrings.IndexOf(Str);
end;
function TIStringList.IsValidIndex(const Idx: Integer): Boolean;
{Checks if an index into the string list is valid, i.e. it is in range and can
be used as an index into the Items[] property without error.
@param Idx [in] Index to check.
@return True if index is valid and False if invalid.
}
begin
Result := (Idx >= 0) and (Idx <= Pred(Count));
end;
procedure TIStringList.SetCaseSensitive(const Flag: Boolean);
{Sets search case sensitivity.
@param Flag [in] True if case sensitive searching is required, False if case
insensitive searching is required.
}
begin
fStrings.CaseSensitive := Flag;
end;
procedure TIStringList.SetItem(const Idx: Integer; const Str: string);
{Sets string at a given position in list.
@param Idx [in] Index in list to receive strings. Must be in range.
@param Str [in] String to be stored in list.
}
begin
fStrings[Idx] := Str;
end;
procedure TIStringList.SetText(const Text: string; const Delim: string;
const AllowEmpty: Boolean; const Trim: Boolean);
{Sets list to component parts of a string, based on a delimiter.
@param Str [in] String to be split.
@param Delim [in] String that delimits components of string.
@param AllowEmpty [in] Determines whether empty components are stored in
list (True) or ignored (False).
@param Trim [in] Determines whether strings are trimmed of trailing and
leading spaces before adding to list.
}
begin
Clear;
Add(Text, Delim, AllowEmpty, Trim);
end;
function TIStringList.ToArray: TArray<string>;
{Copies strings from string list into an array of strings.
@return Array of strings.
}
var
Idx: Integer; // loops through all strings
begin
SetLength(Result, Count);
for Idx := 0 to Pred(Count) do
Result[Idx] := GetItem(Idx);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.