Monday, October 27, 2014

Strip Invalid Identifier Characters

I don't do much Delphi anymore (any really) but this has been hanging around in my inbox for a while so I cleaned it up. This is a simple function that is a modification of IsValidIdent to strip invalid characters from a string.

function StripInvalidCharacters(const Ident: string): string;

  function Alpha(C: Char): Boolean; inline;
  begin
    Result := TCharacter.IsLetter(C) or (C = '_');
  end;

  function AlphaNumeric(C: Char): Boolean; inline;
  begin
    Result := TCharacter.IsLetterOrDigit(C) or (C = '_');
  end;

var
  I, L: Integer;
begin
  L := Length(Ident);
  if (L > 0) and Alpha(Ident[1]) then
    Result := Ident[1];

  for I := 2 to L do
    if AlphaNumeric(Ident[I]) then

  Result := Result + Ident[I];
end;

var
  LocalVal: string;
  LocalHash: Integer;
begin
  if IsValidIdent(Value) then
  LocalVal := StripInvalidCharacters(Value)
else

No comments:

Post a Comment