Recent

Author Topic: Possible bug in TStringList  (Read 675 times)

egsuh

  • Hero Member
  • *****
  • Posts: 682
Possible bug in TStringList
« on: December 13, 2020, 12:55:48 pm »
I'm afraid setting TStringList.ValueFromIndex[n] := ''  removes the row altogether, even though name is defined.

I confirmed this in TValueListEditor.
 
          ValueListEditor1.Values[ValueListEditor1.Keys[n] := '';    / /works fine, but
          ValueListEditor1.Strings.ValueFromIndex[n-1] := '';    / /removes the whole row.

Also,
         StringList1.Values[StringList1.Names[n]] := '';   works fine.
« Last Edit: December 13, 2020, 12:58:58 pm by egsuh »

Blaazen

  • Hero Member
  • *****
  • Posts: 3034
  • POKE 54296,15
    • Eye-Candy Controls
Re: Possible bug in TStringList
« Reply #1 on: December 13, 2020, 03:05:02 pm »
Code: Pascal  [Select][+][-]
  1. Procedure TStrings.SetValueFromIndex(Index: Integer; const Value: string);
  2.  
  3. begin
  4.   If (Value='') then
  5.     Delete(Index)
  6.   else
  7.     begin
  8.     If (Index<0) then
  9.       Index:=Add('');
  10.     CheckSpecialChars;
  11.     Strings[Index]:=GetName(Index)+FNameValueSeparator+Value;
  12.     end;
  13. end;

Delphi compatibility, I guess.
Lazarus 2.1.0 r64546 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Sieben

  • Full Member
  • ***
  • Posts: 215
Re: Possible bug in TStringList
« Reply #2 on: December 13, 2020, 03:10:07 pm »
It is indeed a bit confusing and inconsistent. While SetValueFromIndex indeed deletes the entry when Value is blank, it even adds an entry with a blank Name when Index is < 0:

Code: Pascal  [Select][+][-]
  1. Procedure TStrings.SetValueFromIndex(Index: Integer; const Value: string);
  2. begin
  3.   If (Value='') then
  4.     Delete(Index)
  5.   else
  6.     begin
  7.     If (Index<0) then
  8.       Index:=Add('');
  9.     CheckSpecialChars;
  10.     Strings[Index]:=GetName(Index)+FNameValueSeparator+Value;
  11.     end;
  12. end;

SetValue on the other hand apparently allows blank values for Name and Value, there is no testing at all:

Code: Pascal  [Select][+][-]
  1. Procedure TStrings.SetValue(const Name, Value: string);
  2. Var L : longint;
  3. begin
  4.   CheckSpecialChars;
  5.   L:=IndexOfName(Name);
  6.   if L=-1 then
  7.    Add (Name+FNameValueSeparator+Value)
  8.   else
  9.    Strings[L]:=Name+FNameValueSeparator+value;
  10. end;

For a consistent behaviour one would also expect that SetValue calls SetValueFromIndex once the index of Name is retrieved, reducing both code and the probability of inconsistencies.

Added: If this were for Delphi compatibility it should at least be consistent.
« Last Edit: December 13, 2020, 03:16:19 pm by Sieben »
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

PascalDragon

  • Hero Member
  • *****
  • Posts: 2891
  • Compiler Developer
Re: Possible bug in TStringList
« Reply #3 on: December 13, 2020, 04:25:07 pm »
Added: If this were for Delphi compatibility it should at least be consistent.

Delphi indeed behaves the same for SetValueFromIndex but it also displays the same behavior for SetValue (thus it behaves consistently), that FPC behaves differently is thus a bug. Would you please report it? :)

Sieben

  • Full Member
  • ***
  • Posts: 215
Re: Possible bug in TStringList
« Reply #4 on: December 13, 2020, 05:11:01 pm »
Reported as bug 38214. Bit weird though that empty Names/Keys should be allowed and are even added while empty Values are not...

Edit: still don't know if suggestions are welcome in the bug tracker...

Code: Pascal  [Select][+][-]
  1. procedure TStrings.SetValue(const Name, Value: string);
  2. var Index: Integer;
  3. begin
  4.   Index := IndexOfName(Name);
  5.   if (Index < 0)
  6.   and (Value > '') then
  7.     Index := Add(Name);
  8.   SetValueFromIndex(Index,Value);
  9. end;
  10.  
  11. procedure TStrings.SetValueFromIndex(Index: Integer; const Value: string);
  12. begin
  13.   if (Value > '') then
  14.   begin
  15.     CheckSpecialChars;
  16.     if (Index < 0) then
  17.       Index := Add('');
  18.     Strings[Index] := GetName(Index) + FNameValueSeparator + Value;
  19.   end else
  20.     if (Index > -1) then
  21.       Delete(Index);
  22. end;
« Last Edit: December 13, 2020, 09:00:19 pm by Sieben »
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Sieben

  • Full Member
  • ***
  • Posts: 215
Re: Possible bug in TStringList
« Reply #5 on: December 17, 2020, 08:19:41 pm »
I still think it doesn't make any sense to allow for an empty Name here but not for an empty Value, let alone adding an empty Name with SetValueFromIndex. First I thought this is just another weird Emba stuff, but to my very astonishment this misconception as I would call it has been in there since Delphi 1.0... I really wonder if in all those years anyone has ever really thought about it and if it wouldn't be worth a try to have a word with Emba about that...?
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

winni

  • Hero Member
  • *****
  • Posts: 2280
Re: Possible bug in TStringList
« Reply #6 on: December 17, 2020, 08:54:29 pm »
Added: If this were for Delphi compatibility it should at least be consistent.

Delphi indeed behaves the same for SetValueFromIndex but it also displays the same behavior for SetValue (thus it behaves consistently), that FPC behaves differently is thus a bug. Would you please report it? :)

Hi

A fixed Delphi bug is a bug because it is not Delphi compatible.

Says the chief physician of the madhouse because he needs new patients.

Winni

 

TinyPortal © 2005-2018