> A faster and more copmlete function UrlEncode should
> be made available. 

Here's my faster suggestion, only I'm not sure about the characters
allowed in a complete URL, may be someone has the idea?

function UrlCheckEncode(const S: String): String;
const
    UrlAllowedChars : TCharSet = ['a'..'z', 'A'..'Z', '0'..'9', ':', '/',
                                  '.', '?', ';',  '@', '&', '=', '+', '-'];

    function NeedsUrlEncoding : Integer;
    var
        I : Integer;
    begin
        for I := 1 to Length(S) do
            if not (S[I] in UrlAllowedChars) then begin
                Result := I;
                Exit;
            end;
        Result := 0;
    end;

var
    I : Integer;
    Y : Integer;
    X : String[2];
begin
    Y := NeedsUrlEncoding;
    if Y = 0 then begin
        Result := S;
        Exit;
    end;
    SetLength(Result, 3 * Length(S)); 
    if Y > 1 then
        Move(S[1], Result[1], Y - 1);
    for I := Y to Length(S) do begin
        if S[I] in UrlAllowedChars then begin
            Result[Y] := S[I];
            Inc(Y);
        end
        else begin
            X := IntToHex(Ord(S[I]), 2);
            Result[Y] := '%';
            Result[Y + 1] := X[1];
            Result[Y + 2] := X[2];
            Inc(Y, 3);
        end;
    end;
    SetLength(Result, Y - 1);
end;



{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function UrlDecode(S : String) : String;
var
    I  : Integer;
    Y  : Integer;
    Ch : Char;
begin
    I := 1;
    Y := 1;
    SetLength(Result, Length(S));
    while (I <= Length(S)) and (S[I] <> '&') do begin
        Ch := S[I];
        if Ch = '%' then begin
            Ch := chr(htoi2(@S[I + 1]));
            Inc(I, 2);
        end
        else if Ch = '+' then
            Ch := ' ';
        Result[Y] := Ch;
        Inc(I);
        Inc(Y);
    end;
    SetLength(Result, Y - 1);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
 
> 
> --
> Arno Garrels [TeamICS]
> http://www.overbyte.be/eng/overbyte/teamics.html
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to