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
| S := UTF8StringReplace(SourceString, '&', '&', [rfReplaceAll]);
PourCent := UTF8Pos('%', S);
while PourCent > 0 do begin
Hexa := '$' + UTF8Copy(S, PourCent+1, 2);
c := StrToInt(Hexa);
if c < $80 then
Len := 1 // débute par 0
else if (c and $E0) = $C0 then
Len := 2 // débute par 110
else if (c and $F0) = $E0 then
Len := 3; // débute par 1110
case Len of
1: CodePoint := c;
2: begin
CodePoint := (c and $1F) shl 6;
if UTF8Copy(S, PourCent+3, 1) = '%' then
Hexa := '$' + UTF8Copy(S, PourCent+4, 2)
else raise Exception.Create('Longueur CodePoint erronée !');
c := StrToInt(Hexa);
CodePoint := CodePoint + (c and $3F);
end;
3: begin
CodePoint := (c and $F) shl 12;
if UTF8Copy(S, PourCent+3, 1) = '%' then
Hexa := '$' + UTF8Copy(S, PourCent+4, 2)
else raise Exception.Create('Longueur CodePoint erronée !');
c := StrToInt(Hexa);
CodePoint := CodePoint + ( (c and $3F) shl 6);
if UTF8Copy(S, PourCent+6, 1) = '%' then
Hexa := '$' + UTF8Copy(S, PourCent+7, 2)
else raise Exception.Create('Longueur CodePoint erronée !');
c := StrToInt(Hexa);
CodePoint := CodePoint + (c and $3F);
end;
else
raise Exception.Create('Longueur CodePoint non gérée !');
end;
S := UTF8Copy(S, 1, PourCent-1) + UnicodeToUTF8(CodePoint) + UTF8Copy(S, PourCent+3*Len, UTF8Length(S)-PourCent+3*Len);
PourCent := UTF8Pos('%', S);
end; |
Partager