From 24bb2eb1a49438b8e93b59a87e2b5e54f17c6581 Mon Sep 17 00:00:00 2001 From: Bart <9132501-flyingsheep@users.noreply.gitlab.com> Date: Sat, 22 Jan 2022 14:55:25 +0100 Subject: [PATCH] LazUtf8: small optimization of Utf8EscapeControlChars. --- components/lazutils/lazutf8.pas | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/components/lazutils/lazutf8.pas b/components/lazutils/lazutf8.pas index ae759dd36f..4bc0380493 100644 --- a/components/lazutils/lazutf8.pas +++ b/components/lazutils/lazutf8.pas @@ -2938,7 +2938,7 @@ const var Ch: Char; i,ResLen: Integer; - SubLen: SizeInt; + SLen, SubLen: SizeInt; const MaxGrowFactor: array[TEscapeMode] of integer = (3, 4, 5, 5, 5); begin @@ -2950,7 +2950,11 @@ begin SetLength(Result, Length(S)*MaxGrowFactor[EscapeMode]); ResLen := 0; //a byte < 127 cannot be part of a multi-byte codepoint, so this is safe - for i := 1 to Length(S) do + + //for i := 1 to Length(S) do + i := 1; + SLen := Length(S); + while (i <= SLen) do begin Inc(ResLen); Ch := S[i]; @@ -2985,10 +2989,17 @@ begin Inc(ResLen, SubLen-1); end; end;//case + Inc(i); end else begin - Result[ResLen] := Ch; + //Result[ResLen] := Ch; + SubLen := 1; + while (i + SubLen <= SLen) and (S[i+SubLen] > #31) do + Inc(SubLen); + Move(S[i], Result[ResLen], SubLen); + Inc(ResLen, SubLen-1); + Inc(i, SubLen); end; end; SetLength(Result, ResLen);