Closed shavluk closed 11 months ago
There is "if Right>0 then" on line 718.
@19Rustam80 There is "if Right>0 then" on line 718. Yes, but Right is decremented in the loop line 721 and could fall below 0....
Tested an had this result:
No spaces
'AAAA' -> Left : 1; Right: 4
'AAA' -> Left : 1; Right: 3
'AA' -> Left : 1; Right: 2
'A' -> Left : 1; Right: 1
2 spaces
' BBBB ' -> Left : 3; Right: 6
' BBB ' -> Left : 3; Right: 5
' BB ' -> Left : 3; Right: 4
' B ' -> Left : 3; Right: 3
1 space
' CCCC ' -> Left : 2; Right: 5
' CCC ' -> Left : 2; Right: 4
' CC ' -> Left : 2; Right: 3
' C ' -> Left : 2; Right: 2
Space right
'DDDD ' -> Left : 1; Right: 4
'DDD ' -> Left : 1; Right: 3
'DD ' -> Left : 1; Right: 2
'D ' -> Left : 1; Right: 1
Space left
' EEEE' -> Left : 2; Right: 5
' EEE' -> Left : 2; Right: 4
' EE' -> Left : 2; Right: 3
' E' -> Left : 2; Right: 2
Just space
' ' -> Left : 1; Right: 0
' ' -> Left : 1; Right: 0
' ' -> Left : 1; Right: 0
' ' -> Left : 1; Right: 0
'' -> Left : 0; Right: 0
Used this code:
procedure TFMMain.Button1Click(Sender: TObject);
procedure TrimPositions(const Str:string; var Left,Right:integer);
begin
Right:=Length(Str);
if Right>0 then
begin
Left :=1;
while (Right > 0) and (Str[Right] <= ' ') do Dec(Right);
while (Left <= Right) and (Str[Left] <= ' ') do Inc(Left);
end
else
Left:=0;
end;
var
s : string;
Left,Right : integer;
begin
inherited;
// MDebug is a TMemo
MDebug.Lines.Clear;
MDebug.Lines.Add('No spaces');
s := 'AAAA';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := 'AAA';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := 'AA';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := 'A';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
MDebug.Lines.Add('2 spaces');
s := ' BBBB ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' BBB ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' BB ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' B ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
MDebug.Lines.Add('1 space');
s := ' CCCC ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' CCC ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' CC ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' C ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
MDebug.Lines.Add('Space right');
s := 'DDDD ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := 'DDD ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := 'DD ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := 'D ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
MDebug.Lines.Add('Space left');
s := ' EEEE';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' EEE';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' EE';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' E';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
MDebug.Lines.Add('Just space');
s := ' ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := ' ';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
s := '';
TrimPositions(s, Left, Right);
MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
end;
@shavluk , thanks, applied the patch
Исправление в строке 721. Если передать на вход строку ' ', то процедура уйдет в отрицательные индексы по строке