forked from Memnarch/Delphinus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DN.VariableResolver.pas
57 lines (47 loc) · 1.3 KB
/
DN.VariableResolver.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
unit DN.VariableResolver;
interface
uses
DN.VariableResolver.Intf;
type
TVariableResolver = class(TInterfacedObject, IVariableResolver)
private
FVariables: TArray<string>;
FValues: TArray<string>;
public
constructor Create(const AVariables, AValues: array of string);
function Resolve(const AText: string): string;
end;
implementation
uses
Classes,
Types,
SysUtils;
{ TVariableResolver }
function OpenToDynamicArray(const AValues: array of string): TArray<string>;
var
i: Integer;
begin
SetLength(Result, Length(AValues));
for i := 0 to High(AValues) do
Result[i] := AValues[i];
end;
constructor TVariableResolver.Create(const AVariables, AValues: array of string);
begin
inherited Create();
FVariables := OpenToDynamicArray(AVariables);
FValues := OpenToDynamicArray(AValues);
if Length(FVariables) <> Length(FValues) then
raise EArgumentException.Create('Number of elements in Variables argument does not match number of elements in Values argument');
end;
function TVariableResolver.Resolve(const AText: string): string;
var
i: Integer;
const
CPrefix = '$(';
CPostFix = ')';
begin
Result := AText;
for i := 0 to High(FVariables) do
Result := StringReplace(Result, CPrefix + FVariables[i] + CPostFix, FValues[i], [rfReplaceAll, rfIgnoreCase]);
end;
end.