source: trunk/Packages/Common/Generics.pas

Last change on this file was 2, checked in by chronos, 16 months ago
  • Added: Initial development version.
File size: 1.5 KB
Line 
1unit Generics;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections;
7
8type
9
10 { TListString }
11
12 TListString = class(TList<string>)
13 procedure Assign(Source: TListString);
14 procedure Explode(Separator: Char; Text: string; MaxCount: Integer = -1);
15 function Implode(Separator: Char): string;
16 end;
17
18 { TDictionaryStringString }
19
20 TDictionaryStringString = class(TDictionary<string, string>)
21 procedure Assign(Source: TDictionaryStringString);
22 end;
23
24
25implementation
26
27{ TListString }
28
29procedure TListString.Assign(Source: TListString);
30var
31 I: Integer;
32begin
33 Count := Source.Count;
34 for I := 0 to Source.Count - 1 do
35 Items[I] := Source[I];
36end;
37
38function TListString.Implode(Separator: Char): string;
39var
40 I: Integer;
41begin
42 Result := '';
43 for I := 0 to Count - 1 do begin
44 Result := Result + Items[I];
45 if I < Count - 1 then Result := Result + Separator;
46 end;
47end;
48
49procedure TListString.Explode(Separator: Char; Text: string; MaxCount: Integer = -1);
50var
51 Index: Integer;
52begin
53 Clear;
54 repeat
55 Index := Pos(Separator, Text);
56 if Index > 0 then begin
57 Add(Copy(Text, 1, Index - 1));
58 System.Delete(Text, 1, Index);
59 if (MaxCount <> -1) and (Count >= MaxCount) then Break;
60 end else Break;
61 until False;
62 if Text <> '' then begin
63 Add(Text);
64 end;
65end;
66
67{ TDictionaryStringString }
68
69procedure TDictionaryStringString.Assign(Source: TDictionaryStringString);
70var
71 Item: TPair<string, string>;
72begin
73 Clear;
74 for Item in Source do
75 Add(Item.Key, Item.Value);
76end;
77
78end.
Note: See TracBrowser for help on using the repository browser.