-
Notifications
You must be signed in to change notification settings - Fork 8
/
DX.Classes.Strings.pas
105 lines (88 loc) · 2.05 KB
/
DX.Classes.Strings.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
unit DX.Classes.Strings;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections;
type
/// <summary>
/// StringList is a simple, managed string collection. No Create or Free
/// required.
/// </summary>
StringList = TArray<String>;
TStringListHelper = record helper for StringList
procedure Add(const AString: string);
procedure AddStrings(const AStrings: TStrings); overload;
procedure AddStrings(const AStrings: StringList); overload;
procedure AddDelimited(const AText: string; const ADelimiter: string);
procedure Clear;
function Contains(const AValue: string): boolean;
function IsEmpty: boolean;
procedure Sort;
function Text(const ADelimiter: string = #13#10): string;
end;
implementation
{ TStringListHelper }
procedure TStringListHelper.Add(const AString: string);
begin
SetLength(self, Length(self) + 1);
self[Length(self) - 1] := AString;
end;
procedure TStringListHelper.AddStrings(const AStrings: TStrings);
var
s: String;
begin
for s in AStrings do
begin
self.Add(s);
end;
end;
procedure TStringListHelper.AddDelimited(const AText, ADelimiter: string);
begin
self := AText.Split([ADelimiter], TStringSplitOptions.None);
end;
procedure TStringListHelper.AddStrings(const AStrings: StringList);
var
s: String;
begin
for s in AStrings do
begin
self.Add(s);
end;
end;
procedure TStringListHelper.Clear;
begin
SetLength(self, 0);
end;
function TStringListHelper.Contains(const AValue: string): boolean;
var
s: string;
begin
result := false;
for s in self do
begin
if s = AValue then
begin
result := true;
break
end;
end;
end;
function TStringListHelper.IsEmpty: boolean;
begin
result := Length(self) = 0;
end;
procedure TStringListHelper.Sort;
begin
TArray.Sort<string>(self);
end;
function TStringListHelper.Text(const ADelimiter: string = #13#10): string;
var
s: string;
begin
result := '';
for s in self do
begin
result := result + s + ADelimiter;
end;
result := result.TrimRight(ADelimiter.ToCharArray);
end;
end.