Menu

[r725]: / branches / revised-webservices / Src / UURIParams.pas  Maximize  Restore  History

Download this file

292 lines (252 with data), 6.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
{
* UURIParams.pas
*
* Encapsulates URI query string parameters and generates the query string from
* the parameters.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* 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.
*
* The Original Code is UURIParams.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2010 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UURIParams;
interface
uses
SysUtils, Classes, Generics.Collections, Generics.Defaults;
type
// todo: Move this to own unit [UComparers] (with ElfHash function below)
{
TSameStringEqualityComparer:
Case insensitive string comparer.
}
TSameStringEqualityComparer = class(TEqualityComparer<string>,
IEqualityComparer<string>
)
function Equals(const Left, Right: string): Boolean; override;
function GetHashCode(const Value: string): Integer; override;
end;
TURIParam = record
Name, Value: string;
constructor Create(const AName, AValue: string);
end;
TURIParams = class(TObject)
strict private
fDict: TDictionary<string,string>;
function BuildQueryString(const Encode: TFunc<string,string>): string;
public
constructor Create; overload;
constructor Create(const Name, Value: string); overload;
constructor Create(const Param: TURIParam); overload;
constructor Create(const Params: array of TURIParam); overload;
constructor Create(const Params: TStrings); overload;
constructor Create(const Params: array of string); overload;
destructor Destroy; override;
procedure Add(const Name, Value: string); overload;
procedure Add(const Param: TURIParam); overload;
procedure Add(const Params: array of TURIParam); overload;
procedure Add(const Params: TStrings); overload;
procedure Add(const Params: array of string); overload;
function Exists(const Name: string): Boolean; overload;
function Exists(const Param: TURIParam): Boolean; overload;
procedure Update(const Name, Value: string); overload;
procedure Update(const Param: TURIParam); overload;
function QueryString: string;
function EncodedQueryString: string;
function IsEmpty: Boolean;
end;
implementation
uses
WINDOWS,
UURIEncode;
// Source: http://www.scalabium.com/faq/dct0136.htm
// TODO: Move to UUtils or UComparers or somewhere like that or to hashes unit
function ElfHash(const Value: string): Integer;
var
i, x: Integer;
begin
Result := 0;
for i := 1 to Length(Value) do
begin
Result := (Result shl 4) + Ord(Value[i]);
x := Result and $F0000000;
if (x <> 0) then
Result := Result xor (x shr 24);
Result := Result and (not x);
end;
end;
{ TURIParams }
procedure TURIParams.Add(const Param: TURIParam);
begin
Add(Param.Name, Param.Value);
end;
procedure TURIParams.Add(const Name, Value: string);
begin
fDict.Add(Name, Value);
end;
procedure TURIParams.Add(const Params: array of TURIParam);
var
Param: TURIParam;
begin
for Param in Params do
Add(Param);
end;
procedure TURIParams.Add(const Params: TStrings);
var
Idx: Integer;
begin
Assert(Assigned(Params), ClassName + '.Add(TStrings): Params is nil');
for Idx := 0 to Pred(Params.Count) do
Add(Params.Names[Idx], Params.ValueFromIndex[Idx]);
end;
procedure TURIParams.Add(const Params: array of string);
var
Param: string;
ParamList: TStrings;
begin
ParamList := TStringList.Create;
try
for Param in Params do
ParamList.Add(Param);
Add(ParamList);
finally
ParamList.Free;
end;
end;
function TURIParams.BuildQueryString(
const Encode: TFunc<string, string>): string;
var
AParam: TPair<string,string>;
SB: TStringBuilder;
begin
SB := TStringBuilder.Create;
try
for AParam in fDict do
begin
if SB.Length > 0 then
SB.Append('&');
SB.Append(Encode(AParam.Key));
if AParam.Value <> '' then
SB.AppendFormat('=%s', [Encode(AParam.Value)]);
end;
Result := SB.ToString;
finally
SB.Free;
end;
end;
constructor TURIParams.Create;
begin
inherited Create;
fDict := TDictionary<string,string>.Create(
24, TSameStringEqualityComparer.Create
);
end;
constructor TURIParams.Create(const Name, Value: string);
begin
Create;
Add(Name, Value);
end;
constructor TURIParams.Create(const Param: TURIParam);
begin
Create;
Add(Param);
end;
constructor TURIParams.Create(const Params: array of string);
begin
Create;
Add(Params);
end;
constructor TURIParams.Create(const Params: array of TURIParam);
begin
Create;
Add(Params);
end;
constructor TURIParams.Create(const Params: TStrings);
begin
Create;
Add(Params);
end;
destructor TURIParams.Destroy;
begin
fDict.Free;
inherited;
end;
function TURIParams.EncodedQueryString: string;
begin
Result := BuildQueryString(
function(S: string): string
begin
Result := URIEncodeQueryString(S);
end
);
end;
function TURIParams.Exists(const Name: string): Boolean;
begin
Result := fDict.ContainsKey(Name);
end;
function TURIParams.Exists(const Param: TURIParam): Boolean;
begin
Result := Exists(Param.Name);
end;
function TURIParams.IsEmpty: Boolean;
begin
Result := fDict.Count = 0;
end;
function TURIParams.QueryString: string;
begin
Result := BuildQueryString(
function(S: string): string
begin
Result := S;
end
);
end;
procedure TURIParams.Update(const Name, Value: string);
begin
fDict.Items[Name] := Value;
end;
procedure TURIParams.Update(const Param: TURIParam);
begin
Update(Param.Name, Param.Value);
end;
{ TURIParam }
constructor TURIParam.Create(const AName, AValue: string);
begin
Name := AName;
Value := AValue;
end;
{ TSameStringEqualityComparer }
function TSameStringEqualityComparer.Equals(const Left, Right: string): Boolean;
begin
Result := AnsiSameText(Left, Right);
end;
function TSameStringEqualityComparer.GetHashCode(const Value: string): Integer;
begin
// Comparison takes place only done if hashes are same => must ignore case in
// hash if two strings in different case are to be considered same => hash of
// 'aaaa' = hash of 'AAAA' => Equals gets called and returns true
Result := ElfHash(AnsiLowerCase(Value));
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.