Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
unit StringEncoding;
interface
type
TEncodingRule = record
Original: String;
Replace: String;
end;
TEncodingRules = array of TEncodingRule;
function Encode(const S: String): String;
function Decode(const S: String): String;
implementation
var
_EncodingRules: TEncodingRules;
function Encode(const S: String): String;
var
X, Y: Integer;
R: TEncodingRule;
T: String;
begin
Result:= S;
for X := Length(Result) downto 1 do begin
for Y := 0 to Length(_EncodingRules) - 1 do begin
R:= _EncodingRules[Y];
T:= Copy(Result, X, Length(R.Original));
if T = R.Original then begin
Delete(Result, X, Length(R.Original));
Insert(R.Replace, Result, X);
end;
end;
end;
end;
function Decode(const S: String): String;
var
X, Y: Integer;
R: TEncodingRule;
T: String;
begin
Result:= S;
for X := Length(Result) downto 1 do begin
for Y := Length(_EncodingRules) - 1 downto 0 do begin
R:= _EncodingRules[Y];
T:= Copy(Result, X, Length(R.Replace));
if T = R.Replace then begin
Delete(Result, X, Length(R.Replace));
Insert(R.Original, Result, X);
end;
end;
end;
end;
procedure InitEncodingRules;
procedure A(const Orig, Repl: String);
begin
SetLength(_EncodingRules, Length(_EncodingRules) + 1);
_EncodingRules[Length(_EncodingRules)-1].Original:= Orig;
_EncodingRules[Length(_EncodingRules)-1].Replace:= Repl;
end;
begin
A('_', '_UND_');
A('%', '_PER_');
A('&', '_AMP_');
A(' ', '_SPC_');
A('=', '_EQU_');
A('/', '_FSL_');
A('\', '_BSL_');
A('(', '_OPR_');
A(')', '_CPR_');
end;
procedure UninitEncodingRules;
begin
SetLength(_EncodingRules, 0);
end;
initialization
InitEncodingRules;
finalization
UninitEncodingRules;
end.