-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathurlEncodeDecode.pas
More file actions
67 lines (51 loc) · 1.23 KB
/
urlEncodeDecode.pas
File metadata and controls
67 lines (51 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
unit urlEncodeDecode;
{
URL Encoder/Decoder based on Rosetta Code challenge for encoding/decoding
https://rosettacode.org/wiki/URL_decoding
https://rosettacode.org/wiki/URL_encoding
Author - Marcus Fernstrom
License - Apache 2.0
Version - 0.1
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Dialogs, strutils;
function urlDecode(data: String):AnsiString;
function urlEncode(data: string):AnsiString;
implementation
function urlDecode(data: String): AnsiString;
var
ch: Char;
pos, skip: Integer;
begin
pos := 0;
skip := 0;
Result := '';
for ch in data do begin
if skip = 0 then begin
if (ch = '%') and (pos < data.length -2) then begin
skip := 2;
Result := Result + AnsiChar(Hex2Dec('$' + data[pos+2] + data[pos+3]));
end else begin
Result := Result + ch;
end;
end else begin
skip := skip - 1;
end;
pos := pos +1;
end;
end;
function urlEncode(data: string): AnsiString;
var
ch: AnsiChar;
begin
Result := '';
for ch in data do begin
if ((Ord(ch) < 65) or (Ord(ch) > 90)) and ((Ord(ch) < 97) or (Ord(ch) > 122)) then begin
Result := Result + '%' + IntToHex(Ord(ch), 2);
end else
Result := Result + ch;
end;
end;
end.