Search code examples
inno-setuppascalscripttstringlist

Check if all Strings in a String List are the Same in Inno Setup


I found the following link, but it is for C#: c# Check if all Strings in a String List are the Same.

I wrote a working Code which can do the rest well but it only function correctly with Sorted String Lists.

Regards,

function StringListStrCheck(const S: String; StringList: TStringList): Boolean;
var
  CurrentString: Integer;
begin
if StringList = nil then
  RaiseException('The StringList specified does not exist');
if StringList.Count = 0 then
  RaiseException('The specified StringList is empty');
if StringList.Count = 1 then
  RaiseException('The specified StringList does not contain multiple Strings');
Result := False;
CurrentString := 1;
Repeat
if (CompareStr( S, StringList.Strings[CurrentString]) = -1) then begin
Result := False;
end;
if (CompareStr( S, StringList.Strings[CurrentString]) = 0) then begin
Result := True;
end;
CurrentString := CurrentString + 1;
Until CurrentString > (StringList.Count - 1 );
end;

This returns True if the Specified String is same as all other Strings in the specified String List.

Otherwise, it returns False.

But, the problem is that it can only do the Checking correctly if the given String List is sorted or its strings don't have spaces. If any of the strings or all strings in the given String List have spaces, it must need to be Sorted. Otherwise this returns True even there are non - equal strings like Your Aplication and Your ApplicationX.

EXAMPLE

This StringList doesn't have any spaces in its any of the Strings:

var
   TestingList1: TStringList;

TestingList1 := TStringList.Create;
TestingList1.Add('CheckNow');
TestingList1.Add('DontCheckIt');
if StringListStrCheck('CheckNow', TestingList1) = True
then
    Log('All Strings are the same');
else
    Log('All Strings are not the same.'); 

It returns False correctly and it can be seen in the Output Message in the Log.

This StringList have spaces in its Strings:

var
   TestingList2: TStringList;

TestingList2 := TStringList.Create;
TestingList2.Add('Check Now');
TestingList2.Add('Check Tomorrow');
TestingList2.Add('Dont Check It');
if StringListStrCheck('Check Now', TestingList1) = True
then
    Log('All Strings are the same');
else
    Log('All Strings are not the same.'); 

But, here it returns True even those Strings are not the same.

But, after I sort it like below, the function works properly and returns False as expected.

TestingList2.Sorted := True;
TestingList2.Duplicates := dupAccept;

I like to know why this function is failing if the given StringList's Strings have spaces or given StringList is not Sorted and also like to know how can I make this function not to fail if the given StringList have spaces and /or given StringList is not sorted.

Thanks in Advance for your Help.


Solution

  • Just set the Result to True before the loop.

    And in the loop set it to False once any string does not match.

    Result := True;
    CurrentString := 1;
    Repeat
      if (CompareStr( S, StringList.Strings[CurrentString]) <> 0) then begin
        Result := False;
        Break;
      end;
    
      CurrentString := CurrentString + 1;
    Until CurrentString > (StringList.Count - 1 );
    

    Use Trim if you want to ignore leading and trailing spaces.