Structure because structure
This commit is contained in:
parent
01d1ea3573
commit
2b2a53466f
17 changed files with 693 additions and 674 deletions
925
Extensions/ConvertExtensions.cs
Normal file
925
Extensions/ConvertExtensions.cs
Normal file
|
|
@ -0,0 +1,925 @@
|
|||
namespace Xorog.UniversalExtensions.Converters;
|
||||
|
||||
public static class ConvertExtensions
|
||||
{
|
||||
public static int ToBase64CharArray(this byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut)
|
||||
=> Convert.ToBase64CharArray(inArray, offsetIn, length, outArray, offsetOut);
|
||||
|
||||
public static int ToBase64CharArray(this byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options)
|
||||
=> Convert.ToBase64CharArray(inArray, offsetIn, length, outArray, offsetOut, options);
|
||||
|
||||
public static string ToBase64String(this ReadOnlySpan<byte> bytes, Base64FormattingOptions options = Base64FormattingOptions.None)
|
||||
=> Convert.ToBase64String(bytes, options);
|
||||
|
||||
public static string ToBase64String(this byte[] inArray, Base64FormattingOptions options)
|
||||
=> Convert.ToBase64String(inArray, options);
|
||||
|
||||
public static string ToBase64String(this byte[] inArray, int offset, int length)
|
||||
=> Convert.ToBase64String(inArray, offset, length);
|
||||
|
||||
public static string ToBase64String(this byte[] inArray)
|
||||
=> Convert.ToBase64String(inArray);
|
||||
|
||||
public static string ToBase64String(this byte[] inArray, int offset, int length, Base64FormattingOptions options)
|
||||
=> Convert.ToBase64String(inArray, offset, length, options);
|
||||
|
||||
public static bool ToBoolean(this sbyte value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this ulong value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this uint value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this ushort value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToBoolean(value, provider);
|
||||
|
||||
public static bool ToBoolean(this string? value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this float value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToBoolean(value, provider);
|
||||
|
||||
public static bool ToBoolean(this double value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this long value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this object? value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this byte value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this char value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this DateTime value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this bool value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this short value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this int value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static bool ToBoolean(this decimal value)
|
||||
=> Convert.ToBoolean(value);
|
||||
|
||||
public static byte ToByte(this float value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this ulong value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this uint value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this ushort value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this string? value, int fromBase)
|
||||
=> Convert.ToByte(value, fromBase);
|
||||
|
||||
public static byte ToByte(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToByte(value, provider);
|
||||
|
||||
public static byte ToByte(this string? value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this sbyte value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this short value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this object? value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this char value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this DateTime value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this decimal value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this byte value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this bool value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this int value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this long value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this double value)
|
||||
=> Convert.ToByte(value);
|
||||
|
||||
public static byte ToByte(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToByte(value, provider);
|
||||
|
||||
public static char ToChar(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToChar(value, provider);
|
||||
|
||||
public static char ToChar(this ulong value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this uint value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this ushort value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this string value, IFormatProvider? provider)
|
||||
=> Convert.ToChar(value, provider);
|
||||
|
||||
public static char ToChar(this string value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this float value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this sbyte value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this object? value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this int value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this short value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this double value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this decimal value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this DateTime value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this char value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this byte value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this bool value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static char ToChar(this long value)
|
||||
=> Convert.ToChar(value);
|
||||
|
||||
public static DateTime ToDateTime(this float value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this string? value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToDateTime(value, provider);
|
||||
|
||||
public static DateTime ToDateTime(this sbyte value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this uint value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this ulong value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this ushort value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToDateTime(value, provider);
|
||||
|
||||
public static DateTime ToDateTime(this int value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this long value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this object? value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this char value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this DateTime value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this byte value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this double value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this short value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this bool value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static DateTime ToDateTime(this decimal value)
|
||||
=> Convert.ToDateTime(value);
|
||||
|
||||
public static decimal ToDecimal(this sbyte value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this ulong value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this uint value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this ushort value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToDecimal(value, provider);
|
||||
|
||||
public static decimal ToDecimal(this string? value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this float value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToDecimal(value, provider);
|
||||
|
||||
public static decimal ToDecimal(this object? value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this long value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this int value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this short value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this double value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this decimal value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this DateTime value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this char value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this byte value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static decimal ToDecimal(this bool value)
|
||||
=> Convert.ToDecimal(value);
|
||||
|
||||
public static double ToDouble(this ulong value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this uint value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this ushort value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToDouble(value, provider);
|
||||
|
||||
public static double ToDouble(this string? value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this sbyte value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToDouble(value, provider);
|
||||
|
||||
public static double ToDouble(this float value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this long value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this int value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this short value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this double value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this decimal value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this DateTime value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this char value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this byte value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this bool value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static double ToDouble(this object? value)
|
||||
=> Convert.ToDouble(value);
|
||||
|
||||
public static string ToHexString(this ReadOnlySpan<byte> bytes)
|
||||
=> Convert.ToHexString(bytes);
|
||||
|
||||
public static string ToHexString(this byte[] inArray)
|
||||
=> Convert.ToHexString(inArray);
|
||||
|
||||
public static string ToHexString(this byte[] inArray, int offset, int length)
|
||||
=> Convert.ToHexString(inArray, offset, length);
|
||||
|
||||
public static short ToInt16(this ulong value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this uint value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this ushort value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this string? value, int fromBase)
|
||||
=> Convert.ToInt16(value, fromBase);
|
||||
|
||||
public static short ToInt16(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToInt16(value, provider);
|
||||
|
||||
public static short ToInt16(this string? value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this float value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this sbyte value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToInt16(value, provider);
|
||||
|
||||
public static short ToInt16(this int value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this long value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this short value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this double value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this decimal value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this DateTime value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this char value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this byte value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this bool value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static short ToInt16(this object? value)
|
||||
=> Convert.ToInt16(value);
|
||||
|
||||
public static int ToInt32(this float value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this string? value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToInt32(value, provider);
|
||||
|
||||
public static int ToInt32(this sbyte value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this ushort value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this uint value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this ulong value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this string? value, int fromBase)
|
||||
=> Convert.ToInt32(value, fromBase);
|
||||
|
||||
public static int ToInt32(this object? value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToInt32(value, provider);
|
||||
|
||||
public static int ToInt32(this long value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this int value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this short value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this double value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this decimal value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this DateTime value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this char value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this byte value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static int ToInt32(this bool value)
|
||||
=> Convert.ToInt32(value);
|
||||
|
||||
public static long ToInt64(this float value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this string? value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToInt64(value, provider);
|
||||
|
||||
public static long ToInt64(this string? value, int fromBase)
|
||||
=> Convert.ToInt64(value, fromBase);
|
||||
|
||||
public static long ToInt64(this object? value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this uint value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this ulong value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this sbyte value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this ushort value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToInt64(value, provider);
|
||||
|
||||
public static long ToInt64(this short value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this long value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this int value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this double value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this decimal value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this DateTime value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this char value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this byte value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static long ToInt64(this bool value)
|
||||
=> Convert.ToInt64(value);
|
||||
|
||||
public static sbyte ToSByte(this string value, IFormatProvider? provider)
|
||||
=> Convert.ToSByte(value, provider);
|
||||
|
||||
public static sbyte ToSByte(this float value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this string? value, int fromBase)
|
||||
=> Convert.ToSByte(value, fromBase);
|
||||
|
||||
public static sbyte ToSByte(this string? value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this uint value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this ulong value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this sbyte value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this ushort value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToSByte(value, provider);
|
||||
|
||||
public static sbyte ToSByte(this int value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this long value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this object? value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this byte value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this char value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this DateTime value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this bool value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this double value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this short value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static sbyte ToSByte(this decimal value)
|
||||
=> Convert.ToSByte(value);
|
||||
|
||||
public static float ToSingle(this ulong value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this uint value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this ushort value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToSingle(value, provider);
|
||||
|
||||
public static float ToSingle(this string? value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this float value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this sbyte value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToSingle(value, provider);
|
||||
|
||||
public static float ToSingle(this object? value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this int value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this short value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this double value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this decimal value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this DateTime value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this char value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this byte value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this bool value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static float ToSingle(this long value)
|
||||
=> Convert.ToSingle(value);
|
||||
|
||||
public static string ToString(this float value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this long value, int toBase)
|
||||
=> Convert.ToString(value, toBase);
|
||||
|
||||
public static string? ToString(this object? value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string? ToString(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this sbyte value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this sbyte value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this float value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this short value, int toBase)
|
||||
=> Convert.ToString(value, toBase);
|
||||
|
||||
public static string? ToString(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this ushort value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this ushort value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this uint value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this uint value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this ulong value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this ulong value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this long value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string? ToString(this string? value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this long value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this short value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this int value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this bool value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this bool value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this byte value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this byte value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this char value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this char value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this DateTime value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this byte value, int toBase)
|
||||
=> Convert.ToString(value, toBase);
|
||||
|
||||
public static string ToString(this decimal value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this decimal value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this double value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this double value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static string ToString(this short value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this int value, int toBase)
|
||||
=> Convert.ToString(value, toBase);
|
||||
|
||||
public static string ToString(this int value)
|
||||
=> Convert.ToString(value);
|
||||
|
||||
public static string ToString(this DateTime value, IFormatProvider? provider)
|
||||
=> Convert.ToString(value, provider);
|
||||
|
||||
public static ushort ToUInt16(this float value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this string? value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToUInt16(value, provider);
|
||||
|
||||
public static ushort ToUInt16(this ulong value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this ushort value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this uint value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this sbyte value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this string? value, int fromBase)
|
||||
=> Convert.ToUInt16(value, fromBase);
|
||||
|
||||
public static ushort ToUInt16(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToUInt16(value, provider);
|
||||
|
||||
public static ushort ToUInt16(this double value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this long value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this object? value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this byte value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this char value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this DateTime value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this bool value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this short value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this int value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static ushort ToUInt16(this decimal value)
|
||||
=> Convert.ToUInt16(value);
|
||||
|
||||
public static uint ToUInt32(this sbyte value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this uint value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this ulong value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this ushort value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this string? value, int fromBase)
|
||||
=> Convert.ToUInt32(value, fromBase);
|
||||
|
||||
public static uint ToUInt32(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToUInt32(value, provider);
|
||||
|
||||
public static uint ToUInt32(this string? value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this float value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToUInt32(value, provider);
|
||||
|
||||
public static uint ToUInt32(this byte value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this long value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this int value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this short value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this double value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this decimal value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this DateTime value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this char value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this bool value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static uint ToUInt32(this object? value)
|
||||
=> Convert.ToUInt32(value);
|
||||
|
||||
public static ulong ToUInt64(this sbyte value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this float value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this string? value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this uint value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this string? value, int fromBase)
|
||||
=> Convert.ToUInt64(value, fromBase);
|
||||
|
||||
public static ulong ToUInt64(this ushort value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this object? value, IFormatProvider? provider)
|
||||
=> Convert.ToUInt64(value, provider);
|
||||
|
||||
public static ulong ToUInt64(this string? value, IFormatProvider? provider)
|
||||
=> Convert.ToUInt64(value, provider);
|
||||
|
||||
public static ulong ToUInt64(this object? value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this double value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this int value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this short value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this decimal value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this DateTime value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this char value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this byte value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this bool value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this ulong value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static ulong ToUInt64(this long value)
|
||||
=> Convert.ToUInt64(value);
|
||||
|
||||
public static string Replace(this string input, string old, object @new)
|
||||
=> input.Replace(old, @new.ToString());
|
||||
}
|
||||
5
Extensions/ExceptionExtensions.cs
Normal file
5
Extensions/ExceptionExtensions.cs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
namespace Xorog.UniversalExtensions;
|
||||
|
||||
public static class ExceptionExtensions
|
||||
{
|
||||
}
|
||||
98
Extensions/FileExtensions.cs
Normal file
98
Extensions/FileExtensions.cs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
namespace Xorog.UniversalExtensions;
|
||||
|
||||
public static class FileExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Copy a directory recursively
|
||||
/// </summary>
|
||||
/// <param name="sourceDirName"></param>
|
||||
/// <param name="destDir"></param>
|
||||
/// <param name="copySubDirs"></param>
|
||||
/// <exception cref="DirectoryNotFoundException"></exception>
|
||||
public static void CopyRecursively(this DirectoryInfo sourceDir, DirectoryInfo destDir, bool copySubDirs = true)
|
||||
{
|
||||
if (!sourceDir.Exists)
|
||||
{
|
||||
throw new DirectoryNotFoundException($"Source directory does not exist or could not be found: {sourceDir.FullName}")
|
||||
.AttachData("sourceDir", sourceDir)
|
||||
.AttachData("destDir", destDir)
|
||||
.AttachData("copySubDirs", copySubDirs);
|
||||
}
|
||||
|
||||
DirectoryInfo[] dirs = sourceDir.GetDirectories();
|
||||
|
||||
destDir.Create();
|
||||
|
||||
FileInfo[] files = sourceDir.GetFiles();
|
||||
foreach (FileInfo file in files)
|
||||
{
|
||||
var newFilePath = Path.Combine(destDir.FullName, file.Name);
|
||||
|
||||
_logger?.LogDebug("Copying '{file}' to '{dest}'", file.FullName, newFilePath);
|
||||
file.CopyTo(newFilePath, false);
|
||||
}
|
||||
|
||||
if (copySubDirs)
|
||||
foreach (DirectoryInfo subdir in dirs)
|
||||
CopyRecursively(subdir, new DirectoryInfo(Path.Combine(destDir.FullName, subdir.Name)), copySubDirs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try deleting the given files and directories until able to or reaching maximum retry count.
|
||||
/// </summary>
|
||||
/// <param name="directoryPaths">A list of directories to clean up</param>
|
||||
/// <param name="filePaths">A list of files to clean up</param>
|
||||
/// <param name="maxRetryCount">The maximum amount of retries</param>
|
||||
/// <returns></returns>
|
||||
public static async Task CleanupFilesAndDirectories(List<string>? directoryPaths, List<string>? filePaths, int maxRetryCount = 100)
|
||||
{
|
||||
var failCount = 0;
|
||||
|
||||
List<Exception> exceptions = new();
|
||||
|
||||
if (directoryPaths?.IsNotNullAndNotEmpty() ?? false)
|
||||
foreach (string DirectoryPath in directoryPaths)
|
||||
{
|
||||
while (Directory.Exists(DirectoryPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(DirectoryPath, true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exceptions.Add(ex);
|
||||
await Task.Delay(5000);
|
||||
failCount++;
|
||||
|
||||
if (failCount > maxRetryCount)
|
||||
throw new AggregateException("Failed to delete directory", exceptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
failCount = 0;
|
||||
exceptions.Clear();
|
||||
|
||||
if (filePaths?.IsNotNullAndNotEmpty() ?? false)
|
||||
foreach (string file in filePaths)
|
||||
{
|
||||
while (File.Exists(file))
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exceptions.Add(ex);
|
||||
await Task.Delay(5000);
|
||||
failCount++;
|
||||
|
||||
if (failCount > maxRetryCount)
|
||||
throw new AggregateException("Failed to delete file", exceptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Extensions/HashingExtensions.cs
Normal file
27
Extensions/HashingExtensions.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
namespace Xorog.UniversalExtensions;
|
||||
|
||||
public static class HashingExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Compute the SHA256-Hash for the given string
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string ComputeSHA256Hash(string str)
|
||||
{
|
||||
using SHA256 _SHA256 = SHA256.Create();
|
||||
return BitConverter.ToString(_SHA256.ComputeHash(Encoding.ASCII.GetBytes(str))).Replace("-", "").ToLowerInvariant();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute the SHA256-Hash for a given file
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
public static string ComputeSHA256Hash(FileInfo filePath)
|
||||
{
|
||||
using SHA256 _SHA256 = SHA256.Create();
|
||||
using FileStream fileStream = filePath.OpenRead();
|
||||
return BitConverter.ToString(_SHA256.ComputeHash(fileStream)).Replace("-", "").ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
40
Extensions/ListExtensions.cs
Normal file
40
Extensions/ListExtensions.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
namespace Xorog.UniversalExtensions;
|
||||
|
||||
public static class ListExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Select a random item from a list
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns>The randomly selected item</returns>
|
||||
/// <exception cref="ArgumentNullException">The list is null</exception>
|
||||
/// <exception cref="ArgumentException">The list is empty</exception>
|
||||
public static T SelectRandom<T>(this IEnumerable<T>? obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
if (!obj.Any())
|
||||
{
|
||||
throw new ArgumentException("The sequence is empty.");
|
||||
}
|
||||
|
||||
|
||||
int rng = new Random().Next(0, obj.Count());
|
||||
return obj.ElementAt(rng) ?? throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Check whether a list contains elements and is not null
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns>Whether the list contains elements and is not null</returns>
|
||||
public static bool IsNotNullAndNotEmpty<T>(this IEnumerable<T>? obj)
|
||||
=> obj is not null && obj.Any();
|
||||
}
|
||||
111
Extensions/ScheduledTaskExtensions.cs
Normal file
111
Extensions/ScheduledTaskExtensions.cs
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
namespace Xorog.UniversalExtensions;
|
||||
public static class ScheduledTaskExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a scheduled task
|
||||
/// </summary>
|
||||
/// <param name="task">The task to run</param>
|
||||
/// <param name="runTime">The time to run the task</param>
|
||||
/// <param name="customData">Any custom data you wish to provide.</param>
|
||||
/// <returns>An unique identifier of the task</returns>
|
||||
|
||||
public static string CreateScheduledTask(this Task task, DateTime runTime, object? customData = null)
|
||||
{
|
||||
if (task.Status != TaskStatus.WaitingForActivation)
|
||||
throw new InvalidOperationException("The task is already being executed or has been scheduled for execution.")
|
||||
.AttachData("Task", task)
|
||||
.AttachData("RunTime", runTime)
|
||||
.AttachData("CustomData", customData);
|
||||
|
||||
string UID = Guid.NewGuid().ToString();
|
||||
CancellationTokenSource CancellationToken = new CancellationTokenSource();
|
||||
|
||||
if (Math.Ceiling(runTime.GetTimespanUntil().TotalMilliseconds) < 0)
|
||||
runTime = DateTime.UtcNow.AddSeconds(1);
|
||||
|
||||
_ = LongDelay(runTime.GetTimespanUntil(), CancellationToken).ContinueWith(x =>
|
||||
{
|
||||
lock (RegisteredScheduledTasks)
|
||||
{
|
||||
RegisteredScheduledTasks.Remove(UID);
|
||||
}
|
||||
|
||||
_logger?.LogDebug("Running scheduled task with UID '{UID}'", UID, runTime.GetTimespanUntil().GetHumanReadable());
|
||||
|
||||
if (x.IsCompletedSuccessfully)
|
||||
task.Start();
|
||||
});
|
||||
|
||||
lock (RegisteredScheduledTasks)
|
||||
{
|
||||
_logger?.LogDebug("Creating scheduled task with UID '{UID}' running in {RunTime}", UID, runTime.GetTimespanUntil().GetHumanReadable());
|
||||
|
||||
RegisteredScheduledTasks.Add(UID, new ScheduledTask
|
||||
{
|
||||
Uid = UID,
|
||||
RunTime = runTime,
|
||||
TokenSource = CancellationToken,
|
||||
CustomData = customData,
|
||||
});
|
||||
}
|
||||
return UID;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a scheduled task
|
||||
/// </summary>
|
||||
/// <param name="UID">The task's unique identifier</param>
|
||||
/// <exception cref="KeyNotFoundException">Throws if the task hasn't been found or if an internal error occurred</exception>
|
||||
public static void DeleteScheduledTask(string UID)
|
||||
{
|
||||
if (!RegisteredScheduledTasks.ContainsKey(UID))
|
||||
throw new KeyNotFoundException($"No scheduled task has been found with UID '{UID}'");
|
||||
|
||||
if (RegisteredScheduledTasks[UID].TokenSource is null)
|
||||
throw new Exception($"Internal: There is no token source registered the specified task.");
|
||||
|
||||
_logger?.LogDebug("Deleting scheduled task with UID '{UID}'", UID);
|
||||
|
||||
lock (RegisteredScheduledTasks)
|
||||
{
|
||||
RegisteredScheduledTasks[UID].TokenSource?.Cancel();
|
||||
RegisteredScheduledTasks.Remove(UID);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of all registered tasks
|
||||
/// </summary>
|
||||
/// <returns>A list of all registered tasks</returns>
|
||||
public static IReadOnlyList<ScheduledTask>? GetScheduledTasks()
|
||||
=> RegisteredScheduledTasks.Select(x => x.Value).ToList().AsReadOnly();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a specific task
|
||||
/// </summary>
|
||||
/// <param name="UID">The unique identifier of what task to get</param>
|
||||
/// <returns>The task</returns>
|
||||
/// <exception cref="Exception">Throws if the task has not been found</exception>
|
||||
public static ScheduledTask GetScheduledTask(string UID)
|
||||
=> RegisteredScheduledTasks[UID];
|
||||
|
||||
internal static async Task LongDelay(TimeSpan delay, CancellationTokenSource token)
|
||||
{
|
||||
var st = new Stopwatch();
|
||||
st.Start();
|
||||
while (true && !token.IsCancellationRequested)
|
||||
{
|
||||
var remaining = (delay - st.Elapsed).TotalMilliseconds;
|
||||
if (remaining <= 0)
|
||||
break;
|
||||
if (remaining > Int16.MaxValue)
|
||||
remaining = Int16.MaxValue;
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(remaining), token.Token);
|
||||
}
|
||||
}
|
||||
}
|
||||
105
Extensions/StringExtensions.cs
Normal file
105
Extensions/StringExtensions.cs
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
namespace Xorog.UniversalExtensions;
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for string.IsNullOrWhiteSpace
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns>Whether the string is null, empty or only contains whitespaces</returns>
|
||||
public static bool IsNullOrWhiteSpace(this string str)
|
||||
=> string.IsNullOrWhiteSpace(str);
|
||||
|
||||
/// <summary>
|
||||
/// Extensions for string.IsNullOrEmpty
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns>Whether the string is null or empty</returns>
|
||||
public static bool IsNullOrEmpty(this string str)
|
||||
=> string.IsNullOrEmpty(str);
|
||||
|
||||
/// <summary>
|
||||
/// Check if a string contains only digits
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsDigitsOnly(this string str)
|
||||
{
|
||||
foreach (char c in str)
|
||||
{
|
||||
if (c is < '0' or > '9')
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all digits from a string
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetAllDigits(this string str) =>
|
||||
new(str.Where(Char.IsDigit).ToArray());
|
||||
|
||||
/// <summary>
|
||||
/// Get country flag emoji based on Iso Country Code
|
||||
/// </summary>
|
||||
/// <param name="country"></param>
|
||||
/// <returns></returns>
|
||||
public static string IsoCountryCodeToFlagEmoji(this string country)
|
||||
{
|
||||
return string.Concat(country.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shorten a string to the given length
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="maxLength"></param>
|
||||
/// <returns></returns>
|
||||
public static string Truncate(this string value, int maxLength)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
return value;
|
||||
return value.Length <= maxLength ? value : value[..maxLength];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shorten a string to the given length and add ".." at the end
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="maxLength"></param>
|
||||
/// <returns></returns>
|
||||
public static string TruncateWithIndication(this string value, int maxLength)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
return value;
|
||||
|
||||
return value.Length <= maxLength ? value : $"{value[..(maxLength - 2)]}..";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove unsupported characters from string to generate a valid filename
|
||||
/// </summary>
|
||||
/// <param name="name">The string with potentionally unwanted characters</param>
|
||||
/// <param name="replace_char">The character the unwanted characters get replaced with (default: <code>_</code>)</param>
|
||||
/// <returns>A valid filename</returns>
|
||||
public static string MakeValidFileName(this string name, char replace_char = '_')
|
||||
{
|
||||
string invalidChars = System.Text.RegularExpressions.Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars()));
|
||||
string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars);
|
||||
|
||||
return System.Text.RegularExpressions.Regex.Replace(name, invalidRegStr, replace_char.ToString()).Replace('&', replace_char);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the first letter to upper.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to modify.</param>
|
||||
/// <returns>The string with the first letter changed to upper.</returns>
|
||||
public static string FirstLetterToUpper(this string str)
|
||||
{
|
||||
return $"{str.First().ToString().ToUpper()}{str.Remove(0, 1)}";
|
||||
}
|
||||
}
|
||||
112
Extensions/TimeExtensions.cs
Normal file
112
Extensions/TimeExtensions.cs
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
namespace Xorog.UniversalExtensions;
|
||||
|
||||
public static class TimeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Get a timespan from now to the given time. Negative on values in the past.
|
||||
/// </summary>
|
||||
/// <param name="until"></param>
|
||||
/// <returns></returns>
|
||||
public static TimeSpan GetTimespanUntil(this DateTime until) =>
|
||||
(until.ToUniversalTime() - DateTime.UtcNow);
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc cref="UniversalExtensions.GetTimespanSince(DateTime)"/>
|
||||
/// </summary>
|
||||
/// <param name="until"></param>
|
||||
/// <returns></returns>
|
||||
public static TimeSpan GetTimespanUntil(this DateTimeOffset until) =>
|
||||
(until.ToUniversalTime() - DateTime.UtcNow);
|
||||
|
||||
/// <summary>
|
||||
/// Get the total seconds until a given DateTime. Negative on values in the past.
|
||||
/// </summary>
|
||||
/// <param name="until"></param>
|
||||
/// <returns></returns>
|
||||
public static long GetTotalSecondsUntil(this DateTime until) =>
|
||||
((long)Math.Ceiling((until.ToUniversalTime() - DateTime.UtcNow).TotalSeconds));
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc cref="UniversalExtensions.GetTotalSecondsUntil(DateTime)"/>
|
||||
/// </summary>
|
||||
/// <param name="until"></param>
|
||||
/// <returns></returns>
|
||||
public static long GetTotalSecondsUntil(this DateTimeOffset until) =>
|
||||
((long)Math.Ceiling((until.ToUniversalTime() - DateTime.UtcNow).TotalSeconds));
|
||||
|
||||
/// <summary>
|
||||
/// Get a timespan from now to the given time. Negative on values in the future.
|
||||
/// </summary>
|
||||
/// <param name="until"></param>
|
||||
/// <returns></returns>
|
||||
public static TimeSpan GetTimespanSince(this DateTime until) =>
|
||||
(DateTime.UtcNow - until.ToUniversalTime());
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc cref="UniversalExtensions.GetTimespanSince(DateTime)"/>
|
||||
/// </summary>
|
||||
/// <param name="until"></param>
|
||||
/// <returns></returns>
|
||||
public static TimeSpan GetTimespanSince(this DateTimeOffset until) =>
|
||||
(DateTime.UtcNow - until.ToUniversalTime());
|
||||
|
||||
/// <summary>
|
||||
/// Get the total seconds since a given DateTime. Negative on values in the future.
|
||||
/// </summary>
|
||||
/// <param name="until"></param>
|
||||
/// <returns></returns>
|
||||
public static long GetTotalSecondsSince(this DateTime until) =>
|
||||
((long)Math.Ceiling((DateTime.UtcNow - until.ToUniversalTime()).TotalSeconds));
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc cref="UniversalExtensions.GetTotalSecondsUntil(DateTime)"/>
|
||||
/// </summary>
|
||||
/// <param name="until"></param>
|
||||
/// <returns></returns>
|
||||
public static long GetTotalSecondsSince(this DateTimeOffset until) =>
|
||||
((long)Math.Ceiling((DateTime.UtcNow - until.ToUniversalTime()).TotalSeconds));
|
||||
|
||||
/// <summary>
|
||||
/// Get a short human readable string for the given amount of seconds
|
||||
/// </summary>
|
||||
/// <param name="seconds"></param>
|
||||
/// <param name="timeFormat"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetShortHumanReadable(this int seconds, TimeFormat timeFormat = TimeFormat.DAYS) =>
|
||||
TimeSpan.FromSeconds(seconds).GetShortTimeFormat(timeFormat);
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc cref="UniversalExtensions.GetShortHumanReadable(int, TimeFormat)"/>
|
||||
/// </summary>
|
||||
/// <param name="seconds"></param>
|
||||
/// <param name="timeFormat"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetShortHumanReadable(this long seconds, TimeFormat timeFormat = TimeFormat.DAYS) =>
|
||||
TimeSpan.FromSeconds(seconds).GetShortTimeFormat(timeFormat);
|
||||
|
||||
/// <summary>
|
||||
/// Get a short human readable string for the given amount of time
|
||||
/// </summary>
|
||||
/// <param name="timespan"></param>
|
||||
/// <param name="timeFormat"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetShortHumanReadable(this TimeSpan timespan, TimeFormat timeFormat = TimeFormat.DAYS) =>
|
||||
timespan.GetShortTimeFormat(timeFormat);
|
||||
|
||||
/// <summary>
|
||||
/// Get a human readable string for the given amount of time.
|
||||
/// </summary>
|
||||
/// <param name="seconds"></param>
|
||||
/// <param name="timeFormat"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetHumanReadable(this int seconds, TimeFormat timeFormat = TimeFormat.DAYS, HumanReadableTimeFormatConfig? config = null) =>
|
||||
TimeSpan.FromSeconds(seconds).GetTimeFormat(timeFormat, config);
|
||||
|
||||
/// <inheritdoc cref="UniversalExtensions.GetHumanReadable(int, TimeFormat)"/>
|
||||
public static string GetHumanReadable(this long seconds, TimeFormat timeFormat = TimeFormat.DAYS, HumanReadableTimeFormatConfig? config = null) =>
|
||||
TimeSpan.FromSeconds(seconds).GetTimeFormat(timeFormat, config);
|
||||
|
||||
/// <inheritdoc cref="UniversalExtensions.GetHumanReadable(int, TimeFormat)"/>
|
||||
public static string GetHumanReadable(this TimeSpan timeSpan, TimeFormat timeFormat = TimeFormat.DAYS, HumanReadableTimeFormatConfig? config = null) =>
|
||||
timeSpan.GetTimeFormat(timeFormat, config);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue