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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Uri;
interface
{$SCOPEDENUMS ON}
uses
Boolean,
Byte,
Character,
Exception,
Integer,
Integer._32,
Stream.Output,
Text;
type
EUriException = class(EException);
EUriParseException = class(EUriException);
TUriRootType = (Relative, Absolute);
TUriResourceType = (Directory, File_);
TUriPath = record
private
Elements: TArray<TText>;
function GetCount(): TInteger32;
function GetItems(Index: TInteger32): TText;
function GetFirst(): TText;
procedure SetFirst(const Value: TText);
function GetLast(): TText;
procedure SetLast(const Value: TText);
public
RootType: TUriRootType;
ResourceType: TUriResourceType;
procedure Add(const Element: TText); overload;
procedure Add(const Elements: array of TText); overload;
procedure Add(const Path: TUriPath); overload;
function RemoveFirst(): TText;
function RemoveLast(): TText;
function IsEmpty(): TBoolean;
function ToText(): TText;
property Count: TInteger32 read GetCount;
property First: TText read GetFirst write SetFirst;
property Last: TText read GetLast write SetLast;
property Items[Index: TInteger32]: TText read GetItems; default;
public
class function New(RootType: TUriRootType; ResourceType: TUriResourceType; const Elements: array of TText): TUriPath; static;
class function NewFromText(Value: TText): TUriPath; static;
class operator Equal(const Left: TUriPath; const Right: TUriPath): TBoolean;
class operator GreaterThan(const Left: TUriPath; const Right: TUriPath): TBoolean;
end;
TUriParameter = record
private
LiteralValue: TBoolean;
public
Name: TText;
Value: TText;
function ToText(): TText;
public
class function New(Parameter: TText): TUriParameter; overload; static;
class function New(const Name: TText; const Value: TText): TUriParameter; overload; static;
class function NewLiteralValue(Value: TText): TUriParameter; static;
class operator Equal(const Left: TUriParameter; const Right: TUriParameter): TBoolean;
end;
TUriParameterArray = array of TUriParameter;
TUriParameters = record
private
Parameters: TUriParameterArray;
function GetItems(Index: TInteger32): TUriParameter;
public
procedure Add(const Name: TText; const Value: TText); overload;
procedure Add(const Name: TText; const Value: TInteger); overload;
procedure Add(const Name: TText; const Value: TBoolean); overload;
procedure Add(Parameter: TUriParameter); overload;
function GetValue(const Name: TText): TText;
procedure RemoveAt(Index: TInteger32);
procedure Remove(const Name: TText);
function IsEmpty(): TBoolean;
function Count(): TInteger32;
function ToText(): TText;
property Items[Index: TInteger32]: TUriParameter read GetItems; default;
public
class function New(): TUriParameters; overload; static;
class function New(Value: TText): TUriParameters; overload; static;
class operator Equal(const Left: TUriParameters; const Right: TUriParameters): TBoolean;
end;
TUriAuthority = record
public
HostName: TText;
Port: TInteger32;
function IsEmpty(): TBoolean;
function ToText(): TText;
public
class function New(): TUriAuthority; overload; static;
class function New(const HostName: TText; Port: TInteger32 = 0): TUriAuthority; overload; static;
class function NewFromText(Value: TText): TUriAuthority; overload; static;
class operator Equal(const Left: TUriAuthority; const Right: TUriAuthority): TBoolean;
end;
TUri = record
private
class function IsLetter(Character: TCharacter): TBoolean; static;
class function IsSchemeCharacter(Character: TCharacter): TBoolean; static;
class function ParseScheme(var Value: TText): TText; static;
class function EncodeByte(Stream: TOutputStream; Value: TByte): TText; static;
public
Scheme: TText;
Authority: TUriAuthority;
Path: TUriPath;
Parameters: TUriParameters;
Fragment: TText;
function IsAbsolute(): TBoolean;
function IsEmpty(): TBoolean;
function ToText(): TText;
function Resolve(const Uri: TUri): TUri;
function MakeRelative(Destination: TUri): TUri;
function AddParameter(Name, Value: TText): TUri; overload;
function AddParameter(Name: TText; Value: TInteger32): TUri; overload;
public
class function New(): TUri; overload; static;
class function New(Value: TText): TUri; overload; static;
class function NewOpaque(const Scheme: TText; const SchemeSpecificPart: TText): TUri; static;
class function NewUniformResourceName(const Namespace: TText; const NamespaceSpecificPart: TText): TUri; static;
class function NewJavascript(const Javascript: TText): TUri; static;
class function Encode(const Value: TText): TText; static;
class function Decode(const Value: TText): TText; static;
class operator Implicit(const Path: TUriPath): TUri;
class operator Equal(const Left: TUri; const Right: TUri): TBoolean;
class operator NotEqual(const Left: TUri; const Right: TUri): TBoolean;
end;
implementation
uses
Byte.Nibble,
Character.Encoding,
Character.Set_.Unicode,
Integer._32.TextFormat,
Mathematics,
Memory.Block.Physical,
Stream.Input,
Text.Stream.Output;
{ TUri }
function TUri.AddParameter(Name, Value: TText): TUri;
begin
Result := Self;
Result.Parameters.Add(Name, Value);
end;
function TUri.AddParameter(Name: TText; Value: TInteger32): TUri;
begin
Result := Self;
Result.Parameters.Add(Name, Value);
end;
function TUri.ToText(): TText;
begin
Result := '';
if Scheme <> '' then
Result := Result + Scheme + ':';
if not Authority.IsEmpty() then
Result := Result + '//' + Authority.ToText();
Result := Result + Path.ToText();
if not Parameters.IsEmpty then
Result := Result + '?' + Parameters.ToText();
if Fragment <> '' then
Result := Result + '#' + TUri.Encode(Fragment);
end;
class function TUri.Decode(const Value: TText): TText;
var
CodePoint: TCharacterCodePoint;
FirstNibble: TNibble;
SecondNibble: TNibble;
Block: TPhysicalMemoryBlock;
Input: TTextCursor;
Output: TOutputStream;
begin
Input := Value.CreateCursor();
Block := TPhysicalMemoryBlock.Create();
Output := Block.CreateAppendStream();
while not Input.AtEnd do
begin
CodePoint := Input.Read();
if CodePoint = TUnicode.PERCENT_SIGN then
begin
FirstNibble := CharacterToNibble(Input.Read());
SecondNibble := CharacterToNibble(Input.Read());
Output.WriteLittleEndian8((FirstNibble shl 4) or SecondNibble);
end
else
Output.WriteLittleEndian8(CodePoint);
end;
Result := TText.New(TCharacterEncoding.Utf8, Block.Address, Block.Size);
Input.Free();
Output.Free();
Block.Free();
end;
class function TUri.Encode(const Value: TText): TText;
var
Byte: TByte;
Utf8: TText;
Input: TInputStream;
Output: TTextOutputStream;
begin
Utf8 := Value.Recode(TCharacterEncoding.Utf8);
Input := Utf8.CreateInputStream();
Output := TTextOutputStream.Create(TCharacterEncoding.Windows1252, Value.Size);
while not Input.AtEnd() do
begin
Byte := Input.ReadLittleEndian8();
if
Byte in [
TUnicode.DIGIT_ZERO..TUnicode.DIGIT_NINE,
TUnicode.LATIN_SMALL_LETTER_A..TUnicode.LATIN_SMALL_LETTER_Z,
TUnicode.LATIN_CAPITAL_LETTER_A..TUnicode.LATIN_CAPITAL_LETTER_Z,
TUnicode.LOW_LINE,
TUnicode.HYPHEN_MINUS,
TUnicode.FULL_STOP,
TUnicode.TILDE
]
then
Output.WriteLittleEndian8(Byte)
else
EncodeByte(Output, Byte);
end;
Result := Output.GetValue();
Output.Free();
Input.Free();
end;
class function TUri.EncodeByte(Stream: TOutputStream; Value: TByte): TText;
begin
Stream.WriteLittleEndian8(TUnicode.PERCENT_SIGN);
Stream.WriteLittleEndian8(NibbleToCharacter(Value and $F0 shr 4));
Stream.WriteLittleEndian8(NibbleToCharacter(Value and $0F));
end;
class operator TUri.Equal(const Left: TUri; const Right: TUri): TBoolean;
begin
Result :=
(Left.Scheme = Right.Scheme)
and (Left.Authority = Right.Authority)
and (Left.Path = Right.Path)
and (Left.Parameters = Right.Parameters)
and (Left.Fragment = Right.Fragment);
end;
class operator TUri.Implicit(const Path: TUriPath): TUri;
begin
Result := TUri.New();
Result.Path := Path;
end;
function TUri.IsAbsolute(): TBoolean;
begin
Result := Scheme <> '';
end;
function TUri.IsEmpty(): TBoolean;
begin
Result := Authority.IsEmpty() and Path.IsEmpty() and Parameters.IsEmpty() and (Fragment = '');
end;
class function TUri.IsLetter(Character: TCharacter): TBoolean;
begin
Result := Character in ['a'..'z'];
end;
class function TUri.IsSchemeCharacter(Character: TCharacter): TBoolean;
begin
Result := Character in ['a'..'z', '0'..'9', '+', '.', '-'];
end;
function TUri.MakeRelative(Destination: TUri): TUri;
var
i: TInteger32;
RelativePath: TUriPath;
begin
Result := Destination;
// TODO: We probably have to take opaque URIs into account here
if (Destination.Scheme = Scheme) and (Destination.Authority = Authority) and (Path.Count <= Destination.Path.Count) then
begin
i := 0;
while (i < Path.Count) and (Path[i] = Destination.Path[i]) do
Inc(i);
if i = Path.Count then
begin
RelativePath := TUriPath.New(TUriRootType.Relative, Destination.Path.ResourceType, []);
while i < Destination.Path.Count do
begin
RelativePath.Add(Destination.Path[i]);
Inc(i);
end;
Result := TUri.New();
Result.Path := RelativePath;
Result.Parameters := Parameters;
Result.Fragment := Fragment;
end;
end;
end;
class function TUri.New(): TUri;
begin
Result.Scheme := '';
Result.Authority := TUriAuthority.New();
Result.Path := TUriPath.New(TUriRootType.Relative, TUriResourceType.Directory, []);
Result.Parameters := TUriParameters.New();
Result.Fragment := '';
end;
class function TUri.New(Value: TText): TUri;
var
Path: TText;
Query: TText;
BeforeFragment: TText;
IndexOfSlash: TInteger32;
begin
Result := TUri.New();
if Value.Count > 0 then
begin
Result.Scheme := ParseScheme(Value);
if Value.StartsWith('//') then
begin
Value.RemoveFromStart(2);
IndexOfSlash := Value.IndexOf('/');
if IndexOfSlash >= 0 then
Result.Authority := TUriAuthority.NewFromText(Value.RemoveFromStart(IndexOfSlash))
else
begin
Result.Authority := TUriAuthority.NewFromText(Value);
Value := '';
end;
end;
BeforeFragment := Value.Parse('#');
Result.Fragment := TUri.Decode(Value);
Path := BeforeFragment.Parse('?');
Query := BeforeFragment;
Result.Path := TUriPath.NewFromText(Path);
Result.Parameters := TUriParameters.New(Query);
end;
end;
class function TUri.NewJavascript(const Javascript: TText): TUri;
begin
Result := NewOpaque('javascript', Javascript);
end;
class function TUri.NewOpaque(const Scheme: TText; const SchemeSpecificPart: TText): TUri;
begin
Result := TUri.New();
Result.Scheme := Scheme;
Result.Path := TUriPath.New(TUriRootType.Relative, TUriResourceType.File_, [SchemeSpecificPart]);
end;
class function TUri.NewUniformResourceName(const Namespace: TText; const NamespaceSpecificPart: TText): TUri;
begin
// TODO: URNs can be modelled better by not using the underlying hierarchy system as a basis but store the specific structure of a URN instead
Result := TUri.NewOpaque('urn:' + Namespace, NamespaceSpecificPart);
end;
class operator TUri.NotEqual(const Left: TUri; const Right: TUri): TBoolean;
begin
Result := not (Left = Right);
end;
class function TUri.ParseScheme(var Value: TText): TText;
var
i: TInteger32;
begin
Result := '';
if IsLetter(Value[0]) then
begin
i := 0;
while (i < Value.Count) and IsSchemeCharacter(Value[i]) do
Inc(i);
if (i < Value.Count) and (Value[i] = ':') then
begin
Result := Value.RemoveFromStart(i);
Value.RemoveFromStart(1);
end;
end;
end;
function TUri.Resolve(const Uri: TUri): TUri;
begin
if Uri.IsAbsolute() then
Result := Uri
else
begin
Result.Scheme := Scheme;
Result.Authority := Authority;
if Uri.Path.RootType = TUriRootType.Absolute then
Result.Path := Uri.Path
else
begin
Result.Path := Path;
Result.Path.Add(Uri.Path);
end;
Result.Path.ResourceType := Uri.Path.ResourceType;
Result.Parameters := Uri.Parameters;
Result.Fragment := Uri.Fragment;
end;
end;
{ TUriPath }
procedure TUriPath.Add(const Element: TText);
begin
SetLength(Elements, Length(Elements) + 1);
Elements[High(Elements)] := Element;
end;
procedure TUriPath.Add(const Elements: array of TText);
var
i: TInteger32;
begin
for i := 0 to High(Elements) do
Add(Elements[i]);
end;
procedure TUriPath.Add(const Path: TUriPath);
var
i: TInteger32;
begin
for i := 0 to High(Path.Elements) do
Add(Path.Elements[i]);
ResourceType := Path.ResourceType;
end;
function TUriPath.ToText(): TText;
var
i: TInteger32;
begin
if RootType = TUriRootType.Absolute then
Result := '/'
else
Result := '';
if Length(Elements) > 0 then
begin
Result := Result + TUri.Encode(Elements[0]);
for i := 1 to High(Elements) do
Result := Result + '/' + TUri.Encode(Elements[i]) ;
if ResourceType = TUriResourceType.Directory then
Result := Result + '/';
end;
end;
function TUriPath.GetCount(): TInteger32;
begin
Result := Length(Elements);
end;
class operator TUriPath.Equal(const Left: TUriPath; const Right: TUriPath): TBoolean;
var
i: TInteger32;
begin
Result := (Left.RootType = Right.RootType) and (Left.ResourceType = Right.ResourceType) and (Left.Count = Right.Count);
i := 0;
while Result and (i < Left.Count) do
begin
Result := Left[i] = Right[i];
Inc(i);
end;
end;
function TUriPath.GetFirst(): TText;
begin
Result := Elements[0];
end;
function TUriPath.GetItems(Index: TInteger32): TText;
begin
Result := Elements[Index];
end;
function TUriPath.GetLast(): TText;
begin
Result := Elements[High(Elements)];
end;
class operator TUriPath.GreaterThan(const Left, Right: TUriPath): TBoolean;
var
i: TInteger32;
PathLength: TInteger32;
Equal: TBoolean;
begin
if Left.RootType = Right.RootType then
begin
if Left.ResourceType = Right.ResourceType then
begin
Equal := True;
i := 0;
PathLength := Minimum(Left.Count, Right.Count);
while Equal and (i < PathLength) do
begin
Equal := Left[i] = Right[i];
if Equal then
Inc(i);
end;
if i < PathLength then
Result := Left[i] > Right[i]
else
Result := Left.Count > PathLength;
end
else
Result := Left.ResourceType > Right.ResourceType;
end
else
Result := Left.RootType > Right.RootType;
end;
function TUriPath.IsEmpty(): TBoolean;
begin
Result := (RootType <> TUriRootType.Absolute) and (Length(Elements) = 0);
end;
class function TUriPath.New(RootType: TUriRootType; ResourceType: TUriResourceType; const Elements: array of TText): TUriPath;
var
i: TInteger32;
begin
Result.RootType := RootType;
Result.ResourceType := ResourceType;
SetLength(Result.Elements, Length(Elements));
for i := 0 to High(Elements) do
Result.Elements[i] := Elements[i];
end;
class function TUriPath.NewFromText(Value: TText): TUriPath;
var
PathElement: TText;
begin
Result := TUriPath.New(TUriRootType.Relative, TUriResourceType.Directory, []);
if Value.StartsWith('/') then
begin
Value := Value.SubTextFrom(1);
Result.RootType := TUriRootType.Absolute;
end;
while Value.Count > 0 do
begin
PathElement := Value.ParseOptional('/');
if PathElement <> '' then
Result.Add(TUri.Decode(PathElement))
else
begin
Result.Add(TUri.Decode(Value));
Result.ResourceType := TUriResourceType.File_;
Value := '';
end;
end;
end;
function TUriPath.RemoveFirst(): TText;
begin
Result := Elements[0];
Elements := Copy(Elements, 1, Length(Elements) - 1);
end;
function TUriPath.RemoveLast(): TText;
begin
Result := Elements[Length(Elements) - 1];
Elements := Copy(Elements, 0, Length(Elements) - 1);
end;
procedure TUriPath.SetFirst(const Value: TText);
begin
Elements[0] := Value;
end;
procedure TUriPath.SetLast(const Value: TText);
begin
Elements[High(Elements)] := Value;
end;
{ TUriParameters }
procedure TUriParameters.Add(const Name: TText; const Value: TText);
begin
Add(TUriParameter.New(Name, Value));
end;
procedure TUriParameters.Add(const Name: TText; const Value: TInteger);
begin
Add(TUriParameter.New(Name, Value.ToText()));
end;
procedure TUriParameters.Add(const Name: TText; const Value: TBoolean);
var
BooleanValue: TText;
begin
if Value then
BooleanValue := 'true'
else
BooleanValue := 'false';
Add(TUriParameter.New(Name, BooleanValue));
end;
procedure TUriParameters.Add(Parameter: TUriParameter);
begin
SetLength(Parameters, Length(Parameters) + 1);
Parameters[High(Parameters)] := Parameter;
end;
function TUriParameters.ToText(): TText;
var
i: TInteger32;
begin
if Length(Parameters) > 0 then
begin
Result := Parameters[0].ToText();
for i := 1 to High(Parameters) do
Result := Result + '&' + Parameters[i].ToText();
end
else
Result := '';
end;
function TUriParameters.Count: TInteger32;
begin
Result := Length(Parameters);
end;
class operator TUriParameters.Equal(const Left: TUriParameters; const Right: TUriParameters): TBoolean;
var
i: TInteger32;
begin
Result := Left.Count = Right.Count;
i := 0;
while Result and (i < Left.Count) do
begin
Result := Left[i] = Right[i];
Inc(i);
end;
end;
function TUriParameters.GetValue(const Name: TText): TText;
var
Index: TInteger32;
i: TInteger32;
begin
Index := -1;
i := 0;
while (Index = -1) and (i < Count) do
begin
if Self[i].Name = Name then
Index := i
else
Inc(i);
end;
if Index <> -1 then
Result := Self[i].Value
else
Result := '';
end;
function TUriParameters.GetItems(Index: TInteger32): TUriParameter;
begin
Result := Parameters[Index];
end;
function TUriParameters.IsEmpty: TBoolean;
begin
Result := Length(Parameters) = 0;
end;
class function TUriParameters.New: TUriParameters;
begin
SetLength(Result.Parameters, 0);
end;
class function TUriParameters.New(Value: TText): TUriParameters;
begin
SetLength(Result.Parameters, 0);
while Value.Count > 0 do
begin
SetLength(Result.Parameters, Length(Result.Parameters) + 1);
Result.Parameters[High(Result.Parameters)] := TUriParameter.New(Value.Parse('&'));
end;
end;
procedure TUriParameters.Remove(const Name: TText);
var
Index: TInteger32;
i: TInteger32;
begin
Index := -1;
i := 0;
while (Index = -1) and (i < Count) do
begin
if Self[i].Name = Name then
Index := i
else
Inc(i);
end;
if Index <> -1 then
RemoveAt(Index);
end;
procedure TUriParameters.RemoveAt(Index: TInteger32);
var
i: TInteger32;
begin
for i := Index + 1 to Count - 1 do
Parameters[i - 1] := Parameters[i];
SetLength(Parameters, Length(Parameters) - 1);
end;
{ TUriParameter }
function TUriParameter.ToText(): TText;
begin
if LiteralValue then
Result := Value
else
Result := TUri.Encode(Name) + '=' + TUri.Encode(Value);
end;
class operator TUriParameter.Equal(const Left: TUriParameter; const Right: TUriParameter): TBoolean;
begin
Result := (Left.Name = Right.Name) and (Left.Value = Right.Value);
end;
class function TUriParameter.New(const Name: TText; const Value: TText): TUriParameter;
begin
Result.Name := Name;
Result.Value := Value;
Result.LiteralValue := False;
end;
class function TUriParameter.NewLiteralValue(Value: TText): TUriParameter;
begin
Result.Value := Value;
Result.LiteralValue := True;
end;
class function TUriParameter.New(Parameter: TText): TUriParameter;
var
Name: TText;
Value: TText;
begin
Name := Parameter.Parse('=');
Value := Parameter;
Name := Name.Replace('+', ' ');
Value := Value.Replace('+', ' ');
Result.Name := TUri.Decode(Name);
Result.Value := TUri.Decode(Value);
Result.LiteralValue := False;
end;
{ TUriAuthority }
function TUriAuthority.ToText(): TText;
begin
Result := HostName;
if Port > 0 then
Result := Result + ':' + Integer32ToText(Port);
end;
class operator TUriAuthority.Equal(const Left: TUriAuthority; const Right: TUriAuthority): TBoolean;
begin
Result := (Left.HostName = Right.HostName) and (Left.Port = Right.Port);
end;
function TUriAuthority.IsEmpty: TBoolean;
begin
Result := HostName = '';
end;
class function TUriAuthority.New: TUriAuthority;
begin
Result.HostName := '';
Result.Port := 0;
end;
class function TUriAuthority.New(const HostName: TText; Port: TInteger32): TUriAuthority;
begin
Result.HostName := HostName;
Result.Port := Port;
end;
class function TUriAuthority.NewFromText(Value: TText): TUriAuthority;
begin
Result := TUriAuthority.New();
Result.HostName := Value.Parse(':');
if Value <> '' then
Result.Port := TextToInteger32(Value);
end;
end.
|