Function urldecode (const S: string): string;
VaR
Idx: integer; // loops thru chars in string
HEX: string; // string of hex characters
Code: integer; // hex character code (-1 on error)
Begin
// Intialise result and string Index
Result: = '';
Idx: = 1;
// Loop thru string decoding each character
While idx <= length (s) do
Begin
Case s [idx]
'% ':
Begin
// % Shocould be followed by two hex digits-exception otherwise
If idx <= length (S)-2 then
Begin
// There are sufficient digits-try to decode hex digits
HEX: = s [idx + 1] + s [idx + 2];
Code: = sysutils. strtointdef ('$' + hex,-1 );
INC (idx, 2 );
End
Else
// Insufficient digits-Error
Code: =-1;
// Check for error and raise exception if found
If code =-1 then
Raise sysutils. econverterror. Create (
'Invalid hex digit in url'
);
// Decoded OK-add character to result
Result: = Result + CHR (CODE );
End;
'+ ':
// + Is decoded as a space
Result: = Result +''
Else
// All other characters pass thru unchanged
Result: = Result + s [idx];
End;
INC (idx );
End;
End;
function urlencode (const S: string; const inquerystring: Boolean): string;
var
idx: integer; // loops thru characters in string
begin
result: = '';
for idx: = 1 to length (s) DO
begin
case s [idx] of
'A '.. 'Z', 'A '.. 'Z', '0 '.. '9 ','-','_','. ':
result: = Result + s [idx];
'':
If inquerystring then
result: = Result + '
else
result: = Result +' % 20';
else
result: = Result + '%' + sysutils. inttohex (ord (s [idx]), 2);
end;