Skip to content

Commit 9d4faf1

Browse files
authored
Adding unit file
Add to your uses clause and you're good to go.
1 parent 9902969 commit 9d4faf1

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

urlEncodeDecode

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

0 commit comments

Comments
 (0)