Skip to content

Commit 4e31a79

Browse files
Code Quality: Optimized FileExtensionHelpers.HasExtension (#18044)
1 parent 874a33d commit 4e31a79

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Files.Shared/Helpers/FileExtensionHelpers.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static class FileExtensionHelpers
3232
/// <param name="filePathToCheck">Path or name or extension of the file to check.</param>
3333
/// <param name="extensions">List of the extensions to check.</param>
3434
/// <returns><c>true</c> if the filePathToCheck has one of the specified extensions; otherwise, <c>false</c>.</returns>
35-
public static bool HasExtension(string? filePathToCheck, params string[] extensions)
35+
public static bool HasExtension(string? filePathToCheck, params ReadOnlySpan<string> extensions)
3636
{
3737
if (string.IsNullOrWhiteSpace(filePathToCheck))
3838
return false;
@@ -42,7 +42,12 @@ public static bool HasExtension(string? filePathToCheck, params string[] extensi
4242
if (Directory.Exists(filePathToCheck))
4343
return false;
4444

45-
return extensions.Any(ext => Path.GetExtension(filePathToCheck).Equals(ext, StringComparison.OrdinalIgnoreCase));
45+
string pathExtension = Path.GetExtension(filePathToCheck);
46+
foreach (string ext in extensions)
47+
if (pathExtension.Equals(ext, StringComparison.OrdinalIgnoreCase))
48+
return true;
49+
50+
return false;
4651
}
4752

4853
/// <summary>

0 commit comments

Comments
 (0)