[twsocket] Translate entity function

Stephen Williams SWilliams@fm.optus.net.au
Tue, 20 Jun 2000 13:55:00 +1000


Half the fun is rolling your own function or component.  :)

Here is an off-the-top-of-my-head solution that I tested and it worked first
go.  Took about five minutes to write.

type
  TDecodeMode = (dmCopy, dmBuild);
  TEntity = record
    Name: String;
    Value: Char;
  end;

const
  EntityList: array [0..1] of TEntity =
    (
      (Name: 'copy'; Value: ''),
      (Name: 'quot'; Value: '"')
      { Add more here. }
    );

function ValueForEntity(Entity: String): Char;
var
  Index: Integer;
  Temp: Integer;
begin
  Result := ' ';
  if Entity[1] = '#' then
  begin
    Temp := StrToIntDef(PChar(@Entity[2]), -1);
    if Temp > -1 then
    begin
      Result := Chr(Temp);
    end;
  end
  else
  begin
    Index := 0;
    while Index <= High(EntityList) do
    begin
      if Entity = EntityList[Index].Name then
      begin
        Result := EntityList[Index].Value;
        Exit;
      end;
      Inc(Index);
    end;
  end;
end;

function DecodeHTML(Value: String): String;
var
  InputPos: PChar;
  DecodeMode: TDecodeMode;
  Entity: String;
begin
  InputPos := @Value[1];
  DecodeMode := dmCopy;
  Entity := '';
  while InputPos^ <> #0 do
  begin
    case DecodeMode of
      dmCopy:
        begin
          case InputPos^ of
            '&':
              begin
                DecodeMode := dmBuild;
                Entity := '';
              end;
            else
              begin
                Result := Result + InputPos^;
              end;
          end;
        end;
      dmBuild:
        begin
          case InputPos^ of
            ';':
              begin
                DecodeMode := dmCopy;
                Result := Result + ValueForEntity(Entity);
              end;
            else
              begin
                Entity := Entity + InputPos^;
              end;
          end;
        end;
    end;
    Inc(InputPos);
  end;
end;

An example of using it is

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit2.Text := DecodeHTML(Edit1.Text);
end;

Steve 'Sly' Williams
IT Development
Cable & Wireless Optus Outsourcing Services
swilliams@fm.optus.net.au
------------------------------------------------------------
"What's so hard about pulling a sword out of a stone?  The real work's
already been done." -Lord Rust (Men At Arms -Discworld)
------------------------------------------------------------

> Is there some function in ICS that that can translate 
> entities in strings?
> If not, does anyone know where I can get a component that 
> does this so I
> don't have to make my own?
> 
> For example, I have the string
> "mailto:&#115;&#117;&#112;&#112;&#111;&#114;&#116;&#64;&#104;&
#116;&#109;&#1
08;&#118;&#97;&#108;&#105;&#100;&#97;&#116;&#111;&#114;&#46;&#99;&#111;&#109
;?subject=NEW BETA Coming Soon"...

I want a function to change it to "support@htmlvalidator.com?subject=NEW
BETA Coming Soon".

I would want it to support the named entities as well, like &quot;.