TIdURI.URLDecode can't handle Unicode characters

Giganews Newsgroups
Subject: TIdURI.URLDecode can't handle Unicode characters
Posted by:  Lee_Nover (Lee_Nover[nospam]@delphi-si.com)
Date: Fri, 19 Mar 2004

here's my solution:

function URLDecode(ASrc: string): string;
var
  i: integer;
  ESC: string[2];
  escw: string[4];
  CharCode: integer;
begin
  Result := '';    {Do not Localize}
  // S.G. 27/11/2002: Spaces is NOT to be encoded as "+".
  // S.G. 27/11/2002: "+" is a field separator in query parameter, space
is...
  // S.G. 27/11/2002: well, a space
  // ASrc := StringReplace(ASrc, '+', ' ', [rfReplaceAll]);  {do not
localize}
  i := 1;
  while i <= Length(ASrc) do begin
    if ASrc[i] <> '%' then begin  {do not localize}
      Result := Result + ASrc[i]
    end else begin
      Inc(i); // skip the % char
      // handle unicode characters !
      if ASrc[i] in ['U', 'u'] then
      begin
        Inc(i);
        ESCw := Copy(ASrc, i, 4); // Copy the escape code
        Inc(i, 3);
        CharCode := StrToInt('$' + ESCw);  {do not localize}
        Result:=Result + WideChar(CharCode);
      end
      else
      begin
        ESC := Copy(ASrc, i, 2); // Copy the escape code
        Inc(i, 1); // Then skip it.
        try
          CharCode := StrToInt('$' + ESC);  {do not localize}
          if (CharCode > 0) and (CharCode < 256) then begin
            Result := Result + Char(CharCode);
          end;
        except end;
      end;
    end;
    Inc(i);
  end;
end;

Replies