From a25c8296dea721c321731727cbec9c6db418b3ff Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sat, 14 Nov 2026 22:28:36 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Pad=20fixed-length=20strings=20in?= =?UTF-8?q?=20one=20step?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WriteStringFixed grew the string one character at a time, which is quadratic in the target length. --- UEnhancedMemoryStream.pas | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/UEnhancedMemoryStream.pas b/UEnhancedMemoryStream.pas index 399ba9a..2237ddc 100644 --- a/UEnhancedMemoryStream.pas +++ b/UEnhancedMemoryStream.pas @@ -268,10 +268,14 @@ end; procedure TEnhancedMemoryStream.WriteStringFixed(AValue: string; ALength: Integer); var - i: Integer; + oldLength: Integer; begin - for i := Length(AValue) to ALength do - AValue := AValue + #0; + oldLength := Length(AValue); + if oldLength < ALength then + begin + SetLength(AValue, ALength); + FillChar((PChar(AValue) + oldLength)^, ALength - oldLength, 0); + end; Write(PChar(AValue)^, ALength); end;