-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.CommaTextPayload.pas
More file actions
153 lines (127 loc) · 3.67 KB
/
Copy pathMaxLogic.CommaTextPayload.pas
File metadata and controls
153 lines (127 loc) · 3.67 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
unit MaxLogic.CommaTextPayload;
interface
uses
System.SysUtils, System.Classes, System.Generics.Collections, system.Generics.Defaults;
type
/// <summary>
/// Managed record for fast and clean handling of key=value comma-delimited text using TDictionary.
/// ATTENTION: Case-insensitive keys.
/// </summary>
TCommaTextPayload = record
private
type
iDictWrapper = interface
['{34265137-1F93-4509-8A52-CAB468DA4998}']
function GetDict: TDictionary<string, string>;
end;
TDictWrapper = class(TInterfacedObject, iDictWrapper)
public
Dict: TDictionary<string, string>;
constructor Create;
destructor Destroy; override;
function GetDict: TDictionary<string, string>;
end;
private
fDictRef: iDictWrapper;
function GetDict: TDictionary<string, string>; inline;
function GetValue(const aKey: string): string; inline; inline;
procedure SetValue(const aKey, aValue: string); inline; inline;
function GetCount: Integer; inline; inline;
function GetCommaText: string; inline;
procedure SetCommaText(const aText: string); inline;
public
class function Create: TCommaTextPayload; static;
function Clone: TCommaTextPayload;
procedure Clear; inline;
function TryGetValue(const aKey: string; out aValue: string): Boolean; inline; inline;
property Values[const aKey: string]: string read GetValue write SetValue; default;
property Count: Integer read GetCount;
property CommaText: string read GetCommaText write SetCommaText;
end;
implementation
{ TCommaTextPayload.TDictWrapper }
constructor TCommaTextPayload.TDictWrapper.Create;
begin
inherited Create;
Dict := TDictionary<string, string>.Create(TIStringComparer.Ordinal);
end;
destructor TCommaTextPayload.TDictWrapper.Destroy;
begin
Dict.Free;
inherited;
end;
function TCommaTextPayload.TDictWrapper.GetDict: TDictionary<string, string>;
begin
Result:= Dict;
end;
{ TCommaTextPayload }
class function TCommaTextPayload.Create: TCommaTextPayload;
begin
Result.fDictRef := TDictWrapper.Create;
end;
function TCommaTextPayload.Clone: TCommaTextPayload;
var
lPair: TPair<string, string>;
begin
Result := TCommaTextPayload.Create;
for lPair in GetDict do
Result.GetDict.AddOrSetValue(lPair.Key, lPair.Value);
end;
procedure TCommaTextPayload.Clear;
begin
GetDict.Clear;
end;
function TCommaTextPayload.GetDict: TDictionary<string, string>;
begin
if not Assigned(fDictRef) then
fDictRef := TDictWrapper.Create;
Result := fDictRef.GetDict;
end;
function TCommaTextPayload.GetValue(const aKey: string): string;
begin
GetDict.TryGetValue(aKey, Result);
end;
procedure TCommaTextPayload.SetValue(const aKey, aValue: string);
begin
GetDict.AddOrSetValue(aKey, aValue);
end;
function TCommaTextPayload.GetCount: Integer;
begin
Result := GetDict.Count;
end;
function TCommaTextPayload.TryGetValue(const aKey: string; out aValue: string): Boolean;
begin
Result := GetDict.TryGetValue(aKey, aValue);
end;
function TCommaTextPayload.GetCommaText: string;
var
lList: TStringList;
lPair: TPair<string, string>;
begin
lList := TStringList.Create;
try
lList.StrictDelimiter := True;
for lPair in GetDict do
lList.Values[lPair.Key] := lPair.Value;
Result := lList.CommaText;
finally
lList.Free;
end;
end;
procedure TCommaTextPayload.SetCommaText(const aText: string);
var
lList: TStringList;
x: Integer;
begin
GetDict.Clear;
lList := TStringList.Create;
try
lList.StrictDelimiter := True;
lList.CommaText := aText;
for x := 0 to lList.Count - 1 do
GetDict.AddOrSetValue(lList.Names[x], lList.ValueFromIndex[x]);
finally
lList.Free;
end;
end;
end.