Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I implement my own mechanism to encode and decode data to my own standards?

How To

How do I implement my own mechanism to encode and decode data to my own standards?

by  djjd47130  Posted    (Edited  )
A small project of mine has lead me to point out how flexible this solution is. I originally needed to make a mechanism to encode/decode strings by using my own replacement values. This turned into a different solution where you can easily specify the replacement values to use. A major advantage to this solution is that it performs only one loop through the string, as opposed to other methods such as StringReplace, which would perform many loops. This allows you to easily register replacement values in one place, rather than code them 2 times for encoding and decoding.

Code:
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.

As you can see, I'm making use of the initialization and finalization sections of this unit. During the initialization procedure InitEncodingRules, I am adding the rules. This can be changed by simply implementing your own replacements. The few samples above are just so you can understand the general concept.

Once you have customized the replacement values (Rules), you can use Encode and Decode easily to use these rules.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top