0

What is the best and most SIMPLE way to do this in modern Delphi (Sydney):

var ThisExtension: string;
if ThisExtension in ['.jpeg', '.jpg', '.jpe', '.jif', '.jfif'] then

The above code gives me this compiler error:

E2015 Operator not applicable to this operand type
user1580348
  • 5,721
  • 4
  • 43
  • 105

2 Answers2

2

The System.StrUtils unit has a simple function for this:

if System.StrUtils.MatchText(ThisExtension, ['.jpeg', '.jpg', '.jpe', '.jif', '.jfif']) then

I don't know whether this is the most modern approach. (Maybe Generics has a better approach?). But it is fast and simple.

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • 2
    Note that `MatchText()` is case-insensitive. If you want a case-sensitive comparison, use [`MatchStr()`](http://docwiki.embarcadero.com/Libraries/en/System.StrUtils.MatchStr) instead. – Remy Lebeau Jun 15 '21 at 23:57
  • Who needs case sensitivity when comparing file extensions? – user1580348 Jun 15 '21 at 23:59
  • 4
    Not all file systems are case-insensitive. – Remy Lebeau Jun 15 '21 at 23:59
  • 2
    Yes, this is the best modern way IMHO. You can also declare a constant `const PictureExts: TArray = ['.bmp', '.png', '.jpg', '.gif'];` and use that. – Andreas Rejbrand Jun 16 '21 at 08:52
  • The possibility of declaring an expression like `TArray = ['.bmp', '.png', '.jpg', '.gif']` as a constant is an ingenious simplification in `Generics`. – user1580348 Jun 17 '21 at 07:06
  • @AndreasRejbrand Could you please write an answer with an `if` condition using `TArray = ['.bmp', '.png', '.jpg', '.gif']` so I can accept it? – user1580348 Jun 17 '21 at 10:03
  • @user1580348: I could, but I wouldn't feel good about it because that answer would essentially be the same as your answer but with a tiny difference! (Essentially: `A(12, 13)` vs. `C := 13; A(12, C)`.) You can accept your own answer, and anyone can read my comment about the fairly new Delphi possibility to create dynamic array constants. – Andreas Rejbrand Jun 17 '21 at 10:15
  • @AndreasRejbrand I can accept my own answer only 10 hours from now. Meanwhile, some unknown person has closed my question (!) although I have clearly stated in a comment that the proposed alternative answer does not meet the criteria of my question! This is very strange! There are strange people here on SO... – user1580348 Jun 17 '21 at 12:30
  • @AndreasRejbrand When I try to declare a constant like in your example: `const PictureExts: TArray = ['.bmp', '.png', '.jpg', '.gif'];` then I get a compiler error: `E2086 Type 'TArray' is not yet completely defined` – user1580348 Jun 17 '21 at 20:20
  • @user1580348: Which Delphi version are you using? – Andreas Rejbrand Jun 17 '21 at 21:04
  • @AndreasRejbrand Delphi 10.4.2. However, when I declare `array of string` instead then it works. – user1580348 Jun 17 '21 at 22:53
  • @user1580348: I assume you are declaring the `const` as a member of a class or record type? Because the syntax works for (1) global constants, (2) local constants, and (3) inline-local constants, but not for member constants. I consider this a bug in the Delphi compiler. – Andreas Rejbrand Jun 18 '21 at 07:13
  • @AndreasRejbrand You are right: When I declare it as an inline/local constant then it works. However, meanwhile, I solved the problem by using `array of string` as a class member instead. – user1580348 Jun 18 '21 at 21:41
  • [System.StrUtils.IndexText] (http://docwiki.embarcadero.com/Libraries/Sydney/en/System.StrUtils.IndexText) `IndexText(ThisExtension, ['.jpeg', '.jpg', '.jpe', '.jif', '.jfif']) >= 0;` – USauter Jun 29 '21 at 18:28
0

Maybe not the simplest or most elegant, but you can use a TStringList, eg:

var
  Exts: TStringList;

procedure InitExtensions;
begin
  Exts := TStringList.Create;
  Exts.CaseSensitive := ...; // false by default
  Exts.Add('.jpeg');
  Exts.Add('.jpg');
  Exts.Add('.jpe');
  Exts.Add('.jif');
  Exts.Add('.jfif');
end;

...

var
  ThisExtension: string;
begin
  ...
  if Exts.IndexOf(ThisExtension) <> -1 then
  begin
    ...
  end;
  ...
end;

...

initialization
  InitExtensions;
finalization
  Exts.Free;
end.
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770