FIX: IntToStrTS when ThousandSeparator is NUL (#1738)

also simplify IntToStrTS (smaller, less variables)
This commit is contained in:
xBZZZZ 2024-07-11 20:31:06 +03:00 committed by Alexander Koblov
commit e73b33791b

View file

@ -388,33 +388,32 @@ begin
end;
function IntToStrTS(const APositiveValue: Int64): String;
var i, vSrcLen, vSrcI, vSrcNumberNo, vResLen: byte;
var
// 25 is length of '9,223,372,036,854,775,807' (max Int64 with thousand separators)
ShortStringResult: String[25];
SmallI, BigI: Byte;
begin
if APositiveValue < 0 then
Exit(IntToStr(APositiveValue));
Str(APositiveValue, Result);
vSrcLen := Result.Length;
vResLen := vSrcLen + ((vSrcLen - 1) div 3);
if vSrcLen = vResLen then
Exit;
SetLength(Result, vResLen);
vSrcI := vResLen;
vSrcNumberNo := 1;
for i:= vSrcLen downto 1 do
begin
Result[vSrcI] := Result[i];
Dec(vSrcI);
if(vSrcNumberNo <> vSrcLen) and (vSrcNumberNo mod 3 = 0) then
begin
Result[vSrcI] := FormatSettings.ThousandSeparator;
Dec(vSrcI);
end;
Inc(vSrcNumberNo);
end;
Str(APositiveValue, ShortStringResult);
if (APositiveValue > 999) and (DefaultFormatSettings.ThousandSeparator <> #0) then
begin
SmallI := Length(ShortStringResult);
BigI := SmallI + (SmallI - 1) div 3;
SetLength(ShortStringResult, BigI);
repeat
ShortStringResult[BigI] := ShortStringResult[SmallI];
Dec(BigI);
Dec(SmallI);
ShortStringResult[BigI] := ShortStringResult[SmallI];
Dec(BigI);
Dec(SmallI);
ShortStringResult[BigI] := ShortStringResult[SmallI];
Dec(BigI);
Dec(SmallI);
ShortStringResult[BigI] := DefaultFormatSettings.ThousandSeparator;
Dec(BigI);
until BigI = SmallI;
end;
Result := ShortStringResult;
end;
function cnvFormatFileSize(const iSize: int64; FSF: TFileSizeFormat; const Number: integer): string;