* Correct ReadString to be delphi compatible: count is number of characters, not bytes

This commit is contained in:
Michaël Van Canneyt 2021-09-11 19:09:26 +02:00
parent 82a0b7328e
commit eaf7f8b282
5 changed files with 16 additions and 9 deletions

View File

@ -1554,16 +1554,18 @@ Var
B : TBytes; B : TBytes;
Buf : TJSArrayBuffer; Buf : TJSArrayBuffer;
BytesLeft : Integer; BytesLeft : Integer;
ByteCount : Integer;
begin begin
// Top off // Top off
ByteCount:=Count*2; // UTF-16
BytesLeft:=(Size-Position); BytesLeft:=(Size-Position);
if BytesLeft<Count then if BytesLeft<ByteCount then
Count:=BytesLeft; ByteCount:=BytesLeft;
SetLength(B,Count); SetLength(B,ByteCount);
ReadBuffer(B,0,Count); ReadBuffer(B,0,ByteCount);
Buf:=BytesToMemory(B); Buf:=BytesToMemory(B);
Result:=BufferToString(Buf,0,Count); Result:=BufferToString(Buf,0,ByteCount);
end; end;
procedure TStringStream.WriteString(const AString: string); procedure TStringStream.WriteString(const AString: string);

View File

@ -167,11 +167,16 @@ procedure TTestStringStream.TestReadString;
Var Var
S : String; S : String;
begin begin
S:='ABCDEFGH'; S:='ABCDEFGH';
DoCreate(S); DoCreate(S);
AssertEquals('2 characters','AB',Stream.ReadString(4)); AssertEquals('2 characters','AB',Stream.ReadString(2));
AssertEquals('Top off characters','CDEFGH',Stream.ReadString(22)); AssertEquals('Top off characters','CDEFGH',Stream.ReadString(11));
S:='Hello World';
FreeAndNil(FStream);
DoCreate(S);
AssertEquals('Correct string',S,Stream.ReadString(Length(S)));
end; end;
procedure TTestStringStream.TestWriteString; procedure TTestStringStream.TestWriteString;

View File

@ -38,7 +38,7 @@
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
</Unit> </Unit>
<Unit> <Unit>
<Filename Value="testrtl.html"/> <Filename Value="index.html"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<CustomData Count="1"> <CustomData Count="1">
<Item0 Name="PasJSIsProjectHTMLFile" Value="1"/> <Item0 Name="PasJSIsProjectHTMLFile" Value="1"/>

View File

@ -26,7 +26,7 @@ program testrtl;
uses uses
browserconsole, consoletestrunner, frmrtlrun, simplelinkedlist, browserconsole, consoletestrunner, frmrtlrun, simplelinkedlist,
// tcstream, tcstream,
// tccompstreaming, // tccompstreaming,
// tcsyshelpers, // tcsyshelpers,
// tcgenarrayhelper, // tcgenarrayhelper,