SynEdit: Initialize "Result" var of managed type (avoid mem re-alloc in case caller passes non-nil). Issue #41461 (see notes).

This commit is contained in:
Martin 2025-03-01 11:26:17 +01:00
parent 81493e2a7d
commit 69d65e704d
15 changed files with 42 additions and 39 deletions

View File

@ -1156,9 +1156,10 @@ end;
function TSynEditStrings.GetPhysicalCharWidths(Line: PChar; LineLen,
Index: Integer): TPhysicalCharWidths;
begin
SetLength(Result{%H-}, LineLen);
Result := nil;
if LineLen = 0 then
exit;
SetLength(Result{%H-}, LineLen);
DoGetPhysicalCharWidths(Line, LineLen, Index, @Result[0]);
end;

View File

@ -459,6 +459,7 @@ end;
{$IFDEF SYN_MBCSSUPPORT}
function TSynCustomExporter.ReplaceMBCS(Char1, Char2: char): string;
begin
Result := '';
SetLength(Result, 2);
Result[1] := Char1;
Result[2] := Char2;
@ -474,11 +475,12 @@ var
begin
IsSpace := TRUE;
if AToken <> '' then begin
Result := '';
SrcLen := Length(AToken);
ISrc := 1;
DestLen := SrcLen;
IDest := 1;
SetLength(Result{%H-}, DestLen);
SetLength(Result, DestLen);
while ISrc <= SrcLen do begin
c := AToken[ISrc];
IsSpace := IsSpace and (c = ' ');

View File

@ -1266,8 +1266,9 @@ end;
function TSynEditFoldExportStream.PeakString(ALen: Integer): String;
begin
Result := '';
If not(FPos+ ALen <= FLen) then
exit('');
exit;
SetLength(Result, ALen);
if ALen > 0 then
System.Move((FMem + FPos)^, Result[1], ALen);
@ -1284,8 +1285,9 @@ end;
function TSynEditFoldExportStream.ReadString(ALen: Integer): String;
begin
Result := '';
If not(FPos+ ALen <= FLen) then
exit('');
exit;
SetLength(Result, ALen);
if ALen > 0 then
System.Move((FMem + FPos)^, Result[1], ALen);
@ -3126,7 +3128,8 @@ var
i: Integer;
begin
i := FoldOpenCount(ALine);
SetLength(Result{%H-}, i);
Result := nil;
SetLength(Result, i);
while i > 0 do begin
dec(i);
Result[i] := InfoForFoldAtTextIndex(ALine, i, False, NeedLen);
@ -4609,9 +4612,10 @@ var
end;
begin
Result := Default(TFoldViewNodeInfo);
hl := TSynCustomFoldHighlighter(HighLighter);
if not assigned(hl) then
exit; // ToDo: Initialize Result
exit;
nd.LogXStart := 0;
nd.LogXEnd := 0;

View File

@ -914,7 +914,8 @@ procedure TSynSearchDictionary.BuildDictionary;
memory consumption, since they have 4 continuation bytes (array size 64)
to bring down the average.
*)
SetLength(Result{%H-}, Length(ATerm));
Result := '';
SetLength(Result, Length(ATerm));
for i := 1 to Length(ATerm) do begin
c := ATerm[i];
if c < #128

View File

@ -1835,9 +1835,11 @@ function TSynEditSelection.GetSelText : string;
begin
SrcLen := Length(S);
DstLen := Index + Count;
if SrcLen >= DstLen then
Result := Copy(S, Index, Count)
if SrcLen >= DstLen then begin
Result := Copy(S, Index, Count);
end
else begin
Result := '';
SetLength(Result, DstLen);
P := PChar(Result);
StrPCopy(P, Copy(S, Index, Count));

View File

@ -652,10 +652,10 @@ var
//DebugLn(['GetTextRange StartPos=',dbgs(StartPos),' EndPos=',dbgs(EndPos)]);
//DebugLn(Lines.Text);
if EndPos.Y<StartPos.Y then begin
Result:='';
Result:='';
if EndPos.Y<StartPos.Y then
exit;
end;
CurLine:=Lines[StartPos.Y-1];
if StartPos.y=EndPos.Y then
Result:=CurLine

View File

@ -274,6 +274,7 @@ begin
l := 0;
for i := 0 to length(CharWidths)-1 do
l := l + (CharWidths[i] and PCWMask);
Result := '';
SetLength(Result, l);
l := 1;

View File

@ -559,6 +559,7 @@ begin
l := 0;
for i := 0 to length(CharWidths)-1 do
l := l + (CharWidths[i] and PCWMask);
Result := '';
SetLength(Result, l);
l := 1;

View File

@ -4553,13 +4553,8 @@ begin
end;
function TSynPasSyn.GetToken: string;
var
Len: LongInt;
begin
Len := Run - fTokenPos;
SetLength(Result{%H-},Len);
if Len>0 then
System.Move(fLine[fTokenPos],Result[1],Len);
SetString(Result, @fLine[fTokenPos], Run - fTokenPos);
end;
procedure TSynPasSyn.GetTokenEx(out TokenStart: PChar; out TokenLength: integer);

View File

@ -188,12 +188,8 @@ begin
end;
function TSynPositionHighlighter.GetToken: string;
var
Len: LongInt;
begin
Len := fTokenEnd - fTokenPos;
SetLength(Result{%H-},Len);
System.Move(fLine[fTokenPos],Result[1],Len);
SetString(Result, @fLine[fTokenPos], fTokenEnd - fTokenPos);
end;
procedure TSynPositionHighlighter.GetTokenEx(out TokenStart: PChar;

View File

@ -958,8 +958,7 @@ var
begin
TCustomSynEdit(FriendEdit).GetHighlighterAttriAtRowColEx(APos, Ctx, FLastContextLine = APos.Y);
FLastContextLine := APos.Y;
SetLength(Result{%H-}, SizeOf(Integer));
PInteger(@Result[1])^ := Ctx;
SetString(Result, PChar(@Ctx), SizeOf(Ctx));
end;
procedure TSynPluginSyncroEdit.SetGutterGlyph(const AValue: TBitmap);

View File

@ -577,6 +577,7 @@ var
i, j, k: Integer;
s: String;
begin
Result := nil;
SetLength(Result, length(Lines));
for i := low(Lines) to high(Lines) do
Result[i-low(Lines)] := Lines[i];

View File

@ -81,7 +81,7 @@ end;
function TTestBasicSynEdit.TestLines1: TStringArray;
begin
SetLength(Result, 16);
SetLength(Result{%H-}, 16);
// 1 6 11 14
Result[0] := 'Some text to test'; //1
// 1 5 9
@ -111,7 +111,7 @@ end;
function TTestBasicSynEdit.TestLines2: TStringArray;
begin
SetLength(Result, 7);
SetLength(Result{%H-}, 7);
// 1 6 11 14
Result[0] := 'abc def ghi'; // 1
Result[1] := 'ABC DEF GHI'; // 2
@ -307,7 +307,7 @@ var
function TestText1: TStringArray;
begin
SetLength(Result, 4);
SetLength(Result{%H-}, 4);
Result[0] := 'abc';
Result[1] := 'öbc';
Result[2] := #9'abc';
@ -2378,7 +2378,7 @@ end;
procedure TTestBasicSynEdit.TestSearchReplace;
function TestText1: TStringArray;
begin
SetLength(Result, 9);
SetLength(Result{%H-}, 9);
Result[0] := 'aaaa';
Result[1] := 'xx11';
Result[2] := 'cccc';

View File

@ -78,7 +78,7 @@ implementation
function CopyArray(a: array of Integer): TIntArray;
begin
SetLength(Result, Length(a));
SetLength(Result{%H-}, Length(a));
if Length(a) > 0 then
move(a[0], Result[0], Length(a) * SizeOf(a[0]));
end;
@ -279,7 +279,7 @@ end;
function TTestMarkupFoldColoring.TestText1: TStringArray;
begin
SetLength(Result, 26);
SetLength(Result{%H-}, 26);
Result[ 0] := 'program Foo;';
Result[ 1] := '';
Result[ 2] := 'procedure a;';
@ -310,7 +310,7 @@ end;
function TTestMarkupFoldColoring.TestText2: TStringArray;
begin
SetLength(Result, 20);
SetLength(Result{%H-}, 20);
Result[0] := 'program a;';
Result[1] := 'procedure TEditorFrame.NotifChanged(Sender: TObject);';
Result[2] := 'begin';
@ -337,7 +337,7 @@ function TTestMarkupFoldColoring.TestTextEditIfThen(out ExpLines,
var
i: Integer;
begin
SetLength(Result, 29);
SetLength(Result{%H-}, 29);
SetLength(ExpLines, 29);
SetLength(ExpLinesEdited, 18); // stop at bad line // HL may change....
Result[ 0] := 'program a;';
@ -405,7 +405,7 @@ end;
function TTestMarkupFoldColoring.TestTextMultiLineIfIndent: TStringArray;
begin
SetLength(Result, 29);
SetLength(Result{%H-}, 29);
Result[ 0] := 'program a;';
Result[ 1] := 'procedure foo;';
Result[ 2] := 'begin';
@ -439,7 +439,7 @@ end;
function TTestMarkupFoldColoring.TestTextInval1: TStringArray;
begin
SetLength(Result, 20);
SetLength(Result{%H-}, 20);
Result[0] := 'procedure';
Result[1] := 'begin';
Result[2] := '';
@ -466,7 +466,7 @@ function TTestMarkupFoldColoring.TestTextScroll1: TStringArray;
var
i: Integer;
begin
SetLength(Result, 112);
SetLength(Result{%H-}, 112);
Result[0] := 'unit TestUnit;';
Result[1] := '';
Result[2] := '{$mode objfpc}{$H+}';
@ -517,7 +517,7 @@ end;
function TTestMarkupFoldColoring.TestTextCaseScroll1(out
ExpLines: TTestLineMarkupResults): TStringArray;
begin
SetLength(Result, 44+30);
SetLength(Result{%H-}, 44+30);
SetLength(ExpLines, 44);
Result[ 0] := 'program a;';
Result[ 1] := 'begin';

View File

@ -190,7 +190,7 @@ function FillArray(AFrom, ATo: integer; AIncrease: Integer = 1): TIntArray;
var
i: Integer;
begin
SetLength(Result, ATo - AFrom + 1);
SetLength(Result{%H-}, ATo - AFrom + 1);
for i := 0 to high(Result) do
Result[i] := AFrom + i * AIncrease;
end;
@ -210,7 +210,7 @@ function ViewedExp(AFirstViewedIdx: TLineIdx;
var
i, j: Integer;
begin
SetLength(Result, Length(ALines));
SetLength(Result{%H-}, Length(ALines));
j := 0;
for i := 0 to Length(ALines) - 1 do begin
if (i > 0) and (ALines[i].SubIdx = 0) then begin