Author: Jonas Bilinkevicius
I just need a routine that scans an ordinary string and the replaces all
occurrences of '<', '&' and all other illegal characters by the correct HTML symbol.
Answer:
In Delphi 7 (HTTPApp.pas) you have:
1 function HTMLEncode(const AStr: string): string;
2 const
3 Convert = ['&', '<', '>', '"'];
4 var
5 Sp, Rp: PChar;
6 begin
7 SetLength(Result, Length(AStr) * 10);
8 Sp := PChar(AStr);
9 Rp := PChar(Result);
10 while Sp^ <> #0 do
11 begin
12 case Sp^ of
13 '&':
14 begin
15 FormatBuf(Rp^, 5, '&', 5, []);
16 Inc(Rp, 4);
17 end;
18 '<', '>':
19 begin
20 if Sp^ = '<' then
21 FormatBuf(Rp^, 4, '<', 4, [])
22 else
23 FormatBuf(Rp^, 4, '>', 4, []);
24 Inc(Rp, 3);
25 end;
26 '"':
27 begin
28 FormatBuf(Rp^, 6, '"', 6, []);
29 Inc(Rp, 5);
30 end;
31 else
32 Rp^ := Sp^
33 end;
34 Inc(Rp);
35 Inc(Sp);
36 end;
37 SetLength(Result, Rp - PChar(Result));
38 end;
which is pretty good. It will use quite a bit of memory on long input strings,
though. For some reason it sets the result buffer to 10 times the length of the
input buffer when 6 times would have been enough (for the worst case, all quote
chars (").
I rolled my own before D7 was released (part of my WOS framework - found on the D7
Companion CD):
39 function HTMLEncode(const S: string): string;
40 const
41 ConversionSet = ['&', '<', '>', '"', '+']; {The '+' is because of a IE bug}
42 ConversionChars: PChar = '&<>"+';
43 Entities: array[1..5] of string = ('&', '<', '>', '"', '+');
44 var
45 Sp, Rp: PChar;
46 P: integer;
47 begin
48 SetLength(Result, Length(S) * 6); {Ouch... ( worst case is all "'s )}
49 Sp := PChar(S);
50 Rp := PChar(Result);
51 while Sp^ <> #0 do
52 begin
53 if not (Sp^ in ConversionSet) then
54 begin
55 Rp^ := Sp^;
56 Inc(Rp);
57 end
58 else
59 begin
60 P := StrScan(ConversionChars, Sp^) - ConversionChars + 1;
61 StrCopy(RP, PChar(Entities[P]));
62 Inc(Rp, Length(Entities[P]));
63 end;
64 Inc(Sp);
65 end;
66 SetLength(Result, Rp - PChar(Result));
67 end;
|