From 03d0aeb87987134750fd24058191e0f8c557352b Mon Sep 17 00:00:00 2001 From: stax76 Date: Fri, 9 Jan 2026 07:29:19 +0100 Subject: [PATCH] Convert extension method to extension property --- src/MpvNet.Windows/GuiCommand.cs | 6 ++-- src/MpvNet.Windows/WinForms/MainForm.cs | 4 +-- ...ngExtension.cs => PathStringExtensions.cs} | 31 ++++++++++--------- src/MpvNet/FileTypes.cs | 2 +- src/MpvNet/Player.cs | 2 +- 5 files changed, 24 insertions(+), 21 deletions(-) rename src/MpvNet/ExtensionMethod/{PathStringExtension.cs => PathStringExtensions.cs} (67%) diff --git a/src/MpvNet.Windows/GuiCommand.cs b/src/MpvNet.Windows/GuiCommand.cs index ce51d50..fc69304 100644 --- a/src/MpvNet.Windows/GuiCommand.cs +++ b/src/MpvNet.Windows/GuiCommand.cs @@ -303,20 +303,20 @@ public class GuiCommand if (File.Exists(path) && osd) { - if (FileTypes.IsAudio(path.Ext())) + if (FileTypes.IsAudio(path.Ext)) { text = Player.GetPropertyOsdString("filtered-metadata"); Player.CommandV("show-text", text, "5000"); return; } - else if (FileTypes.IsImage(path.Ext())) + else if (FileTypes.IsImage(path.Ext)) { fileSize = new FileInfo(path).Length; text = "Width: " + Player.GetPropertyInt("width") + "\n" + "Height: " + Player.GetPropertyInt("height") + "\n" + "Size: " + Convert.ToInt32(fileSize / 1024.0) + " KB\n" + - "Type: " + path.Ext().ToUpper(); + "Type: " + path.Ext.ToUpper(); Player.CommandV("show-text", text, "5000"); return; diff --git a/src/MpvNet.Windows/WinForms/MainForm.cs b/src/MpvNet.Windows/WinForms/MainForm.cs index fe7bc60..dd19d55 100644 --- a/src/MpvNet.Windows/WinForms/MainForm.cs +++ b/src/MpvNet.Windows/WinForms/MainForm.cs @@ -556,12 +556,12 @@ public partial class MainForm : Form if (App.AutofitImage > 1) App.AutofitImage = 1; - bool isAudio = FileTypes.IsAudio(Player.Path.Ext()); + bool isAudio = FileTypes.IsAudio(Player.Path.Ext); if (isAudio) autoFitHeight = Convert.ToInt32(workingArea.Height * App.AutofitAudio); - if (FileTypes.IsImage(Player.Path.Ext())) + if (FileTypes.IsImage(Player.Path.Ext)) autoFitHeight = Convert.ToInt32(workingArea.Height * App.AutofitImage); if (Player.VideoSize.Height == 0 || Player.VideoSize.Width == 0) diff --git a/src/MpvNet/ExtensionMethod/PathStringExtension.cs b/src/MpvNet/ExtensionMethod/PathStringExtensions.cs similarity index 67% rename from src/MpvNet/ExtensionMethod/PathStringExtension.cs rename to src/MpvNet/ExtensionMethod/PathStringExtensions.cs index 9850514..048fa16 100644 --- a/src/MpvNet/ExtensionMethod/PathStringExtension.cs +++ b/src/MpvNet/ExtensionMethod/PathStringExtensions.cs @@ -1,27 +1,30 @@  namespace MpvNet.ExtensionMethod; -public static class PathStringExtension +public static class PathStringExtensions { - public static string Ext(this string filepath) => filepath.Ext(false); - - public static string Ext(this string filepath, bool includeDot) + extension(string filepath) { - if (string.IsNullOrEmpty(filepath)) - return ""; + public string Ext => GetExt(filepath, false); - char[] chars = filepath.ToCharArray(); - - for (int x = filepath.Length - 1; x >= 0; x--) + static string GetExt(string path, bool includeDot) { - if (chars[x] == Path.DirectorySeparatorChar) + if (string.IsNullOrEmpty(path)) return ""; - if (chars[x] == '.') - return filepath.Substring(x + (includeDot ? 0 : 1)).ToLowerInvariant(); - } + char[] chars = path.ToCharArray(); - return ""; + for (int x = path.Length - 1; x >= 0; x--) + { + if (chars[x] == Path.DirectorySeparatorChar) + return ""; + + if (chars[x] == '.') + return path[(x + (includeDot ? 0 : 1))..].ToLowerInvariant(); + } + + return ""; + } } public static string FileName(this string instance) diff --git a/src/MpvNet/FileTypes.cs b/src/MpvNet/FileTypes.cs index 0f7e378..cba55ff 100644 --- a/src/MpvNet/FileTypes.cs +++ b/src/MpvNet/FileTypes.cs @@ -49,5 +49,5 @@ public static class FileTypes IsVideo(exts, ext) || IsAudio(exts, ext) || IsImage(exts, ext); public static IEnumerable GetMediaFiles(string[] files) => - files.Where(i => IsMedia(files, i.Ext())); + files.Where(i => IsMedia(files, i.Ext)); } diff --git a/src/MpvNet/Player.cs b/src/MpvNet/Player.cs index 9b1deb3..1195c22 100644 --- a/src/MpvNet/Player.cs +++ b/src/MpvNet/Player.cs @@ -441,7 +441,7 @@ public class MainPlayer : MpvClient file = ConvertFilePath(file); - string ext = file.Ext(); + string ext = file.Ext; if (OperatingSystem.IsWindows()) {