mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-24 20:49:16 +02:00
* Use memorystream as a kind of tstringbuilder for json escaping(StringToJSONString), fixes bug #39525
This commit is contained in:
parent
dcb3906741
commit
7631dfb563
@ -881,40 +881,57 @@ end;
|
|||||||
function StringToJSONString(const S: TJSONStringType; Strict : Boolean = False): TJSONStringType;
|
function StringToJSONString(const S: TJSONStringType; Strict : Boolean = False): TJSONStringType;
|
||||||
|
|
||||||
Var
|
Var
|
||||||
I,J,L : Integer;
|
I,J,L, cnt : Integer;
|
||||||
C : Char;
|
C : Char;
|
||||||
|
fs:TMemoryStream;
|
||||||
|
|
||||||
|
procedure W(const ss:string); inline;
|
||||||
|
begin
|
||||||
|
fs.Write(ss[1],length(ss));
|
||||||
|
end;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
I:=1;
|
fs:=TMemoryStream.Create;
|
||||||
J:=1;
|
try
|
||||||
Result:='';
|
I:=1;
|
||||||
L:=Length(S);
|
J:=1;
|
||||||
While I<=L do
|
fs.Size:=0;
|
||||||
begin
|
Result:='';
|
||||||
C:=S[I];
|
L:=Length(S);
|
||||||
if (C in ['"','/','\',#0..#31]) then
|
While I<=L do
|
||||||
begin
|
begin
|
||||||
Result:=Result+Copy(S,J,I-J);
|
C:=S[I];
|
||||||
Case C of
|
if (C in ['"','/','\',#0..#31]) then
|
||||||
'\' : Result:=Result+'\\';
|
begin
|
||||||
'/' : if Strict then
|
fs.Write(S[J],I-J); // Result:=Result+Copy(S,J,I-J);
|
||||||
Result:=Result+'\/'
|
Case C of
|
||||||
else
|
'\' : W('\\'); //Result:=Result+'\\';
|
||||||
Result:=Result+'/';
|
'/' : if Strict then
|
||||||
'"' : Result:=Result+'\"';
|
W('\/') //Result:=Result+'\/'
|
||||||
#8 : Result:=Result+'\b';
|
else
|
||||||
#9 : Result:=Result+'\t';
|
W('/');//Result:=Result+'/';
|
||||||
#10 : Result:=Result+'\n';
|
'"' : W('\"');//Result:=Result+'\"';
|
||||||
#12 : Result:=Result+'\f';
|
#8 : W('\b');//Result:=Result+'\b';
|
||||||
#13 : Result:=Result+'\r';
|
#9 : W('\t');//Result:=Result+'\t';
|
||||||
else
|
#10 : W('\n');//Result:=Result+'\n';
|
||||||
Result:=Result+'\u'+HexStr(Ord(C),4);
|
#12 : W('\f');//Result:=Result+'\f';
|
||||||
|
#13 : W('\r');//Result:=Result+'\r';
|
||||||
|
else
|
||||||
|
W('\u'+HexStr(Ord(C),4)); //Result:=Result+'\u'+HexStr(Ord(C),4);
|
||||||
|
end;
|
||||||
|
J:=I+1;
|
||||||
|
end;
|
||||||
|
Inc(I);
|
||||||
end;
|
end;
|
||||||
J:=I+1;
|
//Result:=Result+Copy(S,J,I-1);
|
||||||
end;
|
cnt:=L-J+1; //
|
||||||
Inc(I);
|
if cnt>0 then
|
||||||
end;
|
fs.Write(S[J],cnt);
|
||||||
Result:=Result+Copy(S,J,I-1);
|
fs.Position:=0;
|
||||||
|
setstring(Result,Pchar(fs.Memory),fs.Size);
|
||||||
|
finally
|
||||||
|
fs.Free;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function JSONStringToString(const S: TJSONStringType): TJSONStringType;
|
function JSONStringToString(const S: TJSONStringType): TJSONStringType;
|
||||||
|
Loading…
Reference in New Issue
Block a user