replace v6 with experimental v7 code
This commit is contained in:
7
src/MpvNet/ExtensionMethod/ObjectExtension.cs
Normal file
7
src/MpvNet/ExtensionMethod/ObjectExtension.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
namespace MpvNet.ExtensionMethod;
|
||||
|
||||
public static class ObjectExtension
|
||||
{
|
||||
public static string ToStringEx(this object instance) => instance?.ToString() ?? "";
|
||||
}
|
||||
67
src/MpvNet/ExtensionMethod/PathStringExtension.cs
Normal file
67
src/MpvNet/ExtensionMethod/PathStringExtension.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
namespace MpvNet.ExtensionMethod;
|
||||
|
||||
public static class PathStringExtension
|
||||
{
|
||||
public static string Ext(this string filepath) => filepath.Ext(false);
|
||||
|
||||
public static string Ext(this string filepath, bool includeDot)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filepath))
|
||||
return "";
|
||||
|
||||
char[] chars = filepath.ToCharArray();
|
||||
|
||||
for (int x = filepath.Length - 1; x >= 0; x--)
|
||||
{
|
||||
if (chars[x] == Path.DirectorySeparatorChar)
|
||||
return "";
|
||||
|
||||
if (chars[x] == '.')
|
||||
return filepath.Substring(x + (includeDot ? 0 : 1)).ToLowerInvariant();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static string FileName(this string instance)
|
||||
{
|
||||
if (string.IsNullOrEmpty(instance))
|
||||
return "";
|
||||
|
||||
int index = instance.LastIndexOf('\\');
|
||||
|
||||
if (index > -1)
|
||||
return instance.Substring(index + 1);
|
||||
|
||||
index = instance.LastIndexOf('/');
|
||||
|
||||
if (index > -1)
|
||||
return instance.Substring(index + 1);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static string ShortPath(this string instance, int maxLength)
|
||||
{
|
||||
if (string.IsNullOrEmpty(instance))
|
||||
return "";
|
||||
|
||||
if (instance.Length > maxLength && instance.Substring(1, 2) == ":\\")
|
||||
instance = instance[..3] + "...\\" + instance.FileName();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Ensure trailing directory separator char
|
||||
public static string AddSep(this string instance)
|
||||
{
|
||||
if (string.IsNullOrEmpty(instance))
|
||||
return "";
|
||||
|
||||
if (!instance.EndsWith(Path.DirectorySeparatorChar.ToString()))
|
||||
instance = instance + Path.DirectorySeparatorChar;
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
30
src/MpvNet/ExtensionMethod/StringExtension.cs
Normal file
30
src/MpvNet/ExtensionMethod/StringExtension.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
|
||||
using System.Globalization;
|
||||
|
||||
namespace MpvNet.ExtensionMethod;
|
||||
|
||||
public static class StringExtension
|
||||
{
|
||||
public static string ToUpperEx(this string instance) => (instance != null) ? instance.ToUpperInvariant() : "";
|
||||
|
||||
public static string ToLowerEx(this string instance) => (instance != null) ? instance.ToLowerInvariant() : "";
|
||||
|
||||
public static string TrimEx(this string? instance) => (instance == null) ? "" : instance.Trim();
|
||||
|
||||
public static int ToInt(this string instance, int defaultValue = 0)
|
||||
{
|
||||
if (int.TryParse(instance, out int result))
|
||||
return result;
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public static float ToFloat(this string instance) =>
|
||||
float.Parse(instance.Replace(",", "."), NumberStyles.Float, CultureInfo.InvariantCulture);
|
||||
|
||||
public static bool ContainsEx(this string? instance, string? value) =>
|
||||
!string.IsNullOrEmpty(instance) && !string.IsNullOrEmpty(value) && instance.Contains(value);
|
||||
|
||||
public static bool StartsWithEx(this string? instance, string? value) =>
|
||||
(instance != null && value != null) && instance.StartsWith(value);
|
||||
}
|
||||
Reference in New Issue
Block a user