Initial helper classes

This commit is contained in:
Stone_Red
2024-03-17 22:04:12 +01:00
parent 24b409eb62
commit 62186f4397
22 changed files with 2554 additions and 6 deletions
+53
View File
@@ -0,0 +1,53 @@
namespace CuteUtils;
/// <summary>
/// <see cref="bool"/> Extensions
/// </summary>
public static class BoolExt
{
/// <summary>
/// Sets value to true if input is true. If input is false the value will not change.
/// </summary>
/// <param name="bol"></param>
/// <param name="input"></param>
public static void OneWayTrue(this ref bool bol, bool input)
{
if (!bol && input)
{
bol = true;
}
}
/// <summary>
/// Sets value to false if input is false. If input is true the value will not change.
/// </summary>
/// <param name="bol"></param>
/// <param name="input"></param>
public static void OneWayFalse(this ref bool bol, bool input)
{
if (bol && !input)
{
bol = false;
}
}
/// <summary>
/// Converts bool to int.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static int ToInt(this bool input)
{
return input ? 1 : 0;
}
/// <summary>
/// Converts int to bool.
/// </summary>
/// <param name="bol"></param>
/// <param name="input"></param>
public static void FromInt(this ref bool bol, int input)
{
bol = input == 1;
}
}
-6
View File
@@ -1,6 +0,0 @@
namespace CuteUtils;
public class Class1
{
}
+129
View File
@@ -0,0 +1,129 @@
using System.Diagnostics;
namespace CuteUtils;
/// <summary>
/// Table Style
/// </summary>
public enum TableStyle
{
/// <summary>
/// The default representation of the table
/// </summary>
Default,
/// <summary>
/// The minimal representation of the table
/// </summary>
Minimum,
/// <summary>
/// The alternative representation of the table
/// </summary>
Alternative,
/// <summary>
/// The list representation of the table
/// </summary>
List
}
/// <summary>
/// <see cref="IEnumerable{T}"/> and <see cref="Array"/> Extensions
/// </summary>
public static class CollectionExt
{
/// <summary>
/// Prints all items of an <see cref="IEnumerable{T}"/>
/// </summary>
/// <param name="collection"></param>
/// <param name="delimiter"></param>
/// <param name="printToDebugConsole"></param>
public static void Print<T>(this IEnumerable<T> collection, char delimiter = ',', bool printToDebugConsole = false)
{
int i = 0;
int length = collection.Count() - 1;
string split = delimiter + (delimiter == '\n' ? string.Empty : " ");
foreach (T item in collection)
{
if (item is IEnumerable<T> ie)
{
ie.Print(delimiter, printToDebugConsole);
}
else if (printToDebugConsole)
{
Debug.Write(item?.ToString() + (i < length ? split : string.Empty));
}
else
{
Console.Write(item?.ToString() + (i < length ? split : string.Empty));
}
i++;
}
}
/// <summary>
/// Creates and prints table from 2D array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="tableStyle"></param>
public static void PrintTable<T>(this T[,] array, TableStyle tableStyle = TableStyle.Default)
{
int[] itemLength = new int[array.GetLength(1)];
char verticalChar = tableStyle == TableStyle.Minimum ? ' ' : '|';
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
itemLength[j] = Math.Max(array[i, j]!.ToString()!.Length + 2, itemLength[j]);
}
}
PrintLine(tableStyle, itemLength, array.GetLength(1), tableStyle == TableStyle.List);
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
string item = " " + array[i, j]?.ToString() + " ";
item = verticalChar + item + new string(' ', itemLength[j] - item.Length);
Console.Write(tableStyle == TableStyle.Minimum && j == 0 ? item.TrimStart() : item);
}
Console.Write(verticalChar);
PrintLine(tableStyle, itemLength, array.GetLength(1), i == 0);
}
}
private static void PrintLine(TableStyle tableStyle, int[] itemLength, int itemCount, bool forcePrint = false)
{
char intersect = tableStyle is TableStyle.Alternative or TableStyle.List ? '+' : '-';
Console.WriteLine();
if (tableStyle is TableStyle.Minimum or TableStyle.List)
{
if (tableStyle == TableStyle.List && forcePrint)
{
Console.Write(intersect);
}
if (!forcePrint)
{
return;
}
}
else
{
Console.Write(intersect);
}
for (int k = 0; k < itemCount; k++)
{
Console.Write(new string('-', itemLength[k] - (tableStyle == TableStyle.Minimum ? 1 : 0)) + intersect);
}
Console.WriteLine();
}
}
+148
View File
@@ -0,0 +1,148 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
#pragma warning disable S3998 // Threads should not lock on objects with weak identity
namespace CuteUtils;
/// <summary>
/// <see cref="Console"/> Extensions
/// </summary>
public static class ConsoleExt
{
/// <summary>
/// Writes the text representation of the specified object to the standard output stream.
/// </summary>
/// <param name="value"></param>
/// <param name="color"></param>
public static void Write(object value, ConsoleColor color)
{
lock (Console.Out)
{
ConsoleColor oldColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(value);
Console.ForegroundColor = oldColor;
}
}
/// <summary>
/// Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream.
/// </summary>
/// <param name="value"></param>
/// <param name="color"></param>
public static void WriteLine(object value, ConsoleColor color)
{
lock (Console.Out)
{
ConsoleColor oldColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(value);
Console.ForegroundColor = oldColor;
}
}
/// <summary>
/// Reads the next line of characters from the standard input stream and tries to convert it to the specified type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>The input string converted to the specified type.</returns>
/// <exception cref="NotSupportedException"></exception>
public static T ReadLine<T>()
{
string attemptedValue = Console.ReadLine() ?? string.Empty;
Type type = typeof(T);
TypeConverter converter = TypeDescriptor.GetConverter(type);
return (T)converter.ConvertFromString(attemptedValue)!;
}
/// <summary>
/// Reads the next line of characters from the standard input stream and tries to convert it to the specified type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input">The input string converted to the specified type.</param>
/// <returns><see langword="true"/> if the conversion was successful. Otherwise <see langword="false"/></returns>
public static bool TryReadLine<T>([NotNullWhen(true)] out T? input)
{
string attemptedValue = Console.ReadLine() ?? string.Empty;
Type type = typeof(T);
TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter != null && converter.IsValid(attemptedValue))
{
input = (T)converter.ConvertFromString(attemptedValue)!;
return true;
}
else
{
input = default;
return false;
}
}
/// <summary>
/// Obtains the next character or function key pressed by the user and converts it to the specified type.
/// The pressed key is displayed in the console window.
/// </summary>
/// <typeparam name="T">The type of the </typeparam>
/// <returns>The input character converted to the specified type.</returns>
/// <exception cref="NotSupportedException"></exception>
public static T ReadKey<T>()
{
string attemptedValue = Console.ReadKey().KeyChar.ToString();
Type type = typeof(T);
TypeConverter converter = TypeDescriptor.GetConverter(type);
return (T)converter.ConvertFromString(attemptedValue)!;
}
/// <summary>
/// Obtains the next character or function key pressed by the user and tries to convert it to the specified type.
/// The pressed key is displayed in the console window.
/// </summary>
/// <param name="input">The input character converted to the specified type.</param>
/// <typeparam name="T">The type of the </typeparam>
/// <returns><see langword="true"/> if the conversion was successful. Otherwise <see langword="false"/></returns>
public static bool TryReadKey<T>([NotNullWhen(true)] out T? input)
{
string attemptedValue = Console.ReadKey().KeyChar.ToString();
Type type = typeof(T);
TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter != null && converter.IsValid(attemptedValue))
{
input = (T)converter.ConvertFromString(attemptedValue)!;
return true;
}
else
{
input = default;
return false;
}
}
/// <summary>
/// Suspends execution of the current method until the user presses a key
/// </summary>
/// <param name="key">The key that has to be pressed</param>
/// <param name="message">The message that will be displayed</param>
public static void Pause(ConsoleKey key, string? message = null)
{
Console.WriteLine(message ?? $"Press {key} to continue...");
ConsoleKey? consoleKey = null;
while (consoleKey != key)
{
consoleKey = Console.ReadKey(true).Key;
}
}
/// <summary>
/// Suspends execution of the current method until the user presses a key
/// </summary>
/// <param name="message">The message that will be displayed</param>
public static void Pause(string message = "Press any key to continue...")
{
Console.WriteLine(message);
_ = Console.ReadKey(true);
}
}
@@ -0,0 +1,43 @@
namespace CuteUtils.FluentMath.Shapes;
/// <summary>
/// Represents a rectangle
/// </summary>
public class Rectangle
{
/// <summary>
/// The length of the <see cref="Rectangle"/>
/// </summary>
public double Length { get; set; }
/// <summary>
/// The width of the <see cref="Rectangle"/>
/// </summary>
public double Width { get; set; }
/// <summary>
/// The diagonal of the <see cref="Rectangle"/>
/// </summary>
public double Diagonal => Math.Sqrt(Math.Pow(Length, 2) + Math.Pow(Width, 2));
/// <summary>
/// The area of the <see cref="Rectangle"/>
/// </summary>
public double Area => Length * Width;
/// <summary>
/// The perimeter of the <see cref="Rectangle"/>
/// </summary>
public double Perimeter => Length * 2 + Width * 2;
/// <summary>
/// Creates a new rectangle instance
/// </summary>
/// <param name="length">The length of the rectangle</param>
/// <param name="width">The width of the rectangle</param>
public Rectangle(double length, double width)
{
Length = length;
Width = width;
}
}
@@ -0,0 +1,161 @@
namespace CuteUtils.FluentMath.TypeExtentions;
/// <summary>
/// DecimalFluent class
/// </summary>
public static class DecimalFluent
{
/// <summary>
/// Converts number to <see cref="decimal"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="decimal"/></returns>
public static double ToDouble(this decimal num)
{
return (double)num;
}
/// <summary>
/// Converts number to <see cref="float"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="float"/></returns>
public static float ToSingle(this decimal num)
{
return (float)num;
}
/// <summary>
/// Converts number to <see cref="short"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="short"/></returns>
public static short ToInt16(this decimal num)
{
return (short)num;
}
/// <summary>
/// Converts number to <see cref="int"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="int"/></returns>
public static int ToInt32(this decimal num)
{
return (int)num;
}
/// <summary>
/// Converts number to <see cref="long"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="long"/></returns>
public static long ToInt64(this decimal num)
{
return (long)num;
}
/// <summary>
/// Adds the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static decimal Add(this decimal num, decimal value)
{
return num + value;
}
/// <summary>
/// Subtracts the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static decimal Subtract(this decimal num, decimal value)
{
return num - value;
}
/// <summary>
/// Multiples the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static decimal Multiply(this decimal num, decimal value)
{
return num * value;
}
/// <summary>
/// Divides the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static decimal Divide(this decimal num, decimal value)
{
return num / value;
}
/// <inheritdoc cref="Math.Abs(decimal)"/>
public static decimal Abs(this decimal num)
{
return Math.Abs(num);
}
/// <inheritdoc cref="Math.Ceiling(decimal)"/>
public static decimal Ceiling(this decimal num)
{
return Math.Ceiling(num);
}
/// <inheritdoc cref="Math.Clamp(decimal,decimal,decimal)"/>
public static decimal Clamp(this decimal num, decimal min, decimal max)
{
return Math.Clamp(num, min, max);
}
/// <inheritdoc cref="Math.Floor(decimal)"/>
public static decimal Floor(this decimal num)
{
return Math.Floor(num);
}
/// <inheritdoc cref="Math.Round(decimal)"/>
public static decimal Round(this decimal num)
{
return Math.Round(num);
}
/// <inheritdoc cref="Math.Round(decimal,MidpointRounding)"/>
public static decimal Round(this decimal num, MidpointRounding mode)
{
return Math.Round(num, mode);
}
/// <inheritdoc cref="Math.Round(decimal,int)"/>
public static decimal Round(this decimal num, int digits)
{
return Math.Round(num, digits);
}
/// <inheritdoc cref="Math.Round(decimal,int,MidpointRounding)"/>
public static decimal Round(this decimal num, int digits, MidpointRounding mode)
{
return Math.Round(num, digits, mode);
}
/// <inheritdoc cref="Math.Sign(decimal)"/>
public static int Sign(this decimal num)
{
return Math.Sign(num);
}
/// <inheritdoc cref="Math.Truncate(decimal)"/>
public static decimal Truncate(this decimal num)
{
return Math.Truncate(num);
}
}
@@ -0,0 +1,287 @@
namespace CuteUtils.FluentMath.TypeExtentions;
/// <summary>
/// DoubleFluent class
/// </summary>
public static class DoubleFluent
{
/// <summary>
/// Converts number to <see cref="decimal"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="decimal"/></returns>
public static decimal ToDecimal(this double num)
{
return (decimal)num;
}
/// <summary>
/// Converts number to <see cref="float"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="float"/></returns>
public static float ToSingle(this double num)
{
return (float)num;
}
/// <summary>
/// Converts number to <see cref="short"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="short"/></returns>
public static short ToInt16(this double num)
{
return (short)num;
}
/// <summary>
/// Converts number to <see cref="int"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="int"/></returns>
public static int ToInt32(this double num)
{
return (int)num;
}
/// <summary>
/// Converts number to <see cref="long"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="long"/></returns>
public static long ToInt64(this double num)
{
return (long)num;
}
/// <summary>
/// Adds the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static double Add(this double num, double value)
{
return num + value;
}
/// <summary>
/// Subtracts the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static double Subtract(this double num, double value)
{
return num - value;
}
/// <summary>
/// Multiples the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static double Multiply(this double num, double value)
{
return num * value;
}
/// <summary>
/// Divides the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static double Divide(this double num, double value)
{
return num / value;
}
/// <inheritdoc cref="Math.Abs(double)"/>
public static double Abs(this double num)
{
return Math.Abs(num);
}
/// <inheritdoc cref="Math.Acos(double)"/>
public static double Acos(this double num)
{
return Math.Acos(num);
}
/// <inheritdoc cref="Math.Acosh(double)"/>
public static double Acosh(this double num)
{
return Math.Acosh(num);
}
/// <inheritdoc cref="Math.Asin(double)"/>
public static double Asin(this double num)
{
return Math.Asin(num);
}
/// <inheritdoc cref="Math.Asinh(double)"/>
public static double Asinh(this double num)
{
return Math.Asinh(num);
}
/// <inheritdoc cref="Math.Atan(double)"/>
public static double Atan(this double num)
{
return Math.Atan(num);
}
/// <inheritdoc cref="Math.Atan2(double,double)"/>
public static double Atan2(this double num, double valuee)
{
return Math.Atan2(num, valuee);
}
/// <inheritdoc cref="Math.Atanh(double)"/>
public static double Atanh(this double num)
{
return Math.Atanh(num);
}
/// <inheritdoc cref="Math.Cbrt(double)"/>
public static double Cbrt(this double num)
{
return Math.Cbrt(num);
}
/// <inheritdoc cref="Math.Ceiling(double)"/>
public static double Ceiling(this double num)
{
return Math.Ceiling(num);
}
/// <inheritdoc cref="Math.Clamp(double,double,double)"/>
public static double Clamp(this double num, double min, double max)
{
return Math.Clamp(num, min, max);
}
/// <inheritdoc cref="Math.Cos(double)"/>
public static double Cos(this double num)
{
return Math.Cos(num);
}
/// <inheritdoc cref="Math.Cosh(double)"/>
public static double Cosh(this double num)
{
return Math.Cosh(num);
}
/// <inheritdoc cref="Math.Exp(double)"/>
public static double Exp(this double num)
{
return Math.Exp(num);
}
/// <inheritdoc cref="Math.Floor(double)"/>
public static double Floor(this double num)
{
return Math.Floor(num);
}
/// <inheritdoc cref="Math.IEEERemainder(double,double)"/>
public static double IEEERemainder(this double num, double valuee)
{
return Math.IEEERemainder(num, valuee);
}
/// <inheritdoc cref="Math.Log(double)"/>
public static double Log(this double num)
{
return Math.Log(num);
}
/// <inheritdoc cref="Math.Log(double,double)"/>
public static double Log(this double num, double newBase)
{
return Math.Log(num, newBase);
}
/// <inheritdoc cref="Math.Log10(double)"/>
public static double Log10(this double num)
{
return Math.Log10(num);
}
/// <inheritdoc cref="Math.Pow(double,double)"/>
public static double Pow(this double num, double power)
{
return Math.Pow(num, power);
}
/// <inheritdoc cref="Math.Round(double)"/>
public static double Round(this double num)
{
return Math.Round(num);
}
/// <inheritdoc cref="Math.Round(double,MidpointRounding)"/>
public static double Round(this double num, MidpointRounding mode)
{
return Math.Round(num, mode);
}
/// <inheritdoc cref="Math.Round(double,int)"/>
public static double Round(this double num, int digits)
{
return Math.Round(num, digits);
}
/// <inheritdoc cref="Math.Round(double,int,MidpointRounding)"/>
public static double Round(this double num, int digits, MidpointRounding mode)
{
return Math.Round(num, digits, mode);
}
/// <inheritdoc cref="Math.Sign(double)"/>
public static int Sign(this double num)
{
return Math.Sign(num);
}
/// <inheritdoc cref="Math.Sin(double)"/>
public static double Sin(this double num)
{
return Math.Sin(num);
}
/// <inheritdoc cref="Math.Sinh(double)"/>
public static double Sinh(this double num)
{
return Math.Sinh(num);
}
/// <inheritdoc cref="Math.Sqrt(double)"/>
public static double Sqrt(this double num)
{
return Math.Sqrt(num);
}
/// <inheritdoc cref="Math.Tan(double)"/>
public static double Tan(this double num)
{
return Math.Tan(num);
}
/// <inheritdoc cref="Math.Tanh(double)"/>
public static double Tanh(this double num)
{
return Math.Tanh(num);
}
/// <inheritdoc cref="Math.Truncate(double)"/>
public static double Truncate(this double num)
{
return Math.Truncate(num);
}
}
@@ -0,0 +1,119 @@
namespace CuteUtils.FluentMath.TypeExtentions;
/// <summary>
/// IntegerFluent class
/// </summary>
public static class Int16Fluent
{
/// <summary>
/// Converts number to <see cref="decimal"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="decimal"/></returns>
public static decimal ToDecimal(this short num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="float"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="float"/></returns>
public static float ToSingle(this short num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="double"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="double"/></returns>
public static double ToDouble(this short num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="int"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="int"/></returns>
public static int ToInt32(this short num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="long"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="long"/></returns>
public static long ToInt64(this short num)
{
return num;
}
/// <summary>
/// Adds the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int Add(this short num, short value)
{
return num + value;
}
/// <summary>
/// Subtracts the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int Subtract(this short num, short value)
{
return num - value;
}
/// <summary>
/// Multiples the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int Multiply(this short num, short value)
{
return num * value;
}
/// <summary>
/// Divides the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int Divide(this short num, short value)
{
return num / value;
}
/// <inheritdoc cref="Math.Abs(short)"/>
public static short Abs(this short num)
{
return Math.Abs(num);
}
/// <inheritdoc cref="Math.Clamp(short,short,short)"/>
public static short Clamp(this short num, short min, short max)
{
return Math.Clamp(num, min, max);
}
/// <inheritdoc cref="Math.Sign(short)"/>
public static int Sign(this short num)
{
return Math.Sign(num);
}
}
@@ -0,0 +1,119 @@
namespace CuteUtils.FluentMath.TypeExtentions;
/// <summary>
/// IntegerFluent class
/// </summary>
public static class Int32Fluent
{
/// <summary>
/// Converts number to <see cref="decimal"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="decimal"/></returns>
public static decimal ToDecimal(this int num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="float"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="float"/></returns>
public static float ToSingle(this int num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="double"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="double"/></returns>
public static double ToDouble(this int num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="short"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="short"/></returns>
public static short ToInt16(this int num)
{
return (short)num;
}
/// <summary>
/// Converts number to <see cref="long"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="long"/></returns>
public static long ToInt64(this int num)
{
return num;
}
/// <summary>
/// Adds the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int Add(this int num, int value)
{
return num + value;
}
/// <summary>
/// Subtracts the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int Subtract(this int num, int value)
{
return num - value;
}
/// <summary>
/// Multiples the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int Multiply(this int num, int value)
{
return num * value;
}
/// <summary>
/// Divides the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int Divide(this int num, int value)
{
return num / value;
}
/// <inheritdoc cref="Math.Abs(int)"/>
public static int Abs(this int num)
{
return Math.Abs(num);
}
/// <inheritdoc cref="Math.Clamp(int,int,int)"/>
public static int Clamp(this int num, int min, int max)
{
return Math.Clamp(num, min, max);
}
/// <inheritdoc cref="Math.Sign(int)"/>
public static int Sign(this int num)
{
return Math.Sign(num);
}
}
@@ -0,0 +1,119 @@
namespace CuteUtils.FluentMath.TypeExtentions;
/// <summary>
/// IntegerFluent class
/// </summary>
public static class Int64Fluent
{
/// <summary>
/// Converts number to <see cref="decimal"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="decimal"/></returns>
public static decimal ToDecimal(this long num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="float"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="float"/></returns>
public static float ToSingle(this long num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="double"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="double"/></returns>
public static double ToDouble(this long num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="short"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="short"/></returns>
public static short ToInt16(this long num)
{
return (short)num;
}
/// <summary>
/// Converts number to <see cref="int"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="int"/></returns>
public static int ToInt32(this long num)
{
return (int)num;
}
/// <summary>
/// Adds the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static long Add(this long num, long value)
{
return num + value;
}
/// <summary>
/// Subtracts the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static long Subtract(this long num, long value)
{
return num - value;
}
/// <summary>
/// Multiples the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static long Multiply(this long num, long value)
{
return num * value;
}
/// <summary>
/// Divides the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static long Divide(this long num, long value)
{
return num / value;
}
/// <inheritdoc cref="Math.Abs(long)"/>
public static long Abs(this long num)
{
return Math.Abs(num);
}
/// <inheritdoc cref="Math.Clamp(long,long,long)"/>
public static long Clamp(this long num, long min, long max)
{
return Math.Clamp(num, min, max);
}
/// <inheritdoc cref="Math.Sign(long)"/>
public static long Sign(this long num)
{
return Math.Sign(num);
}
}
@@ -0,0 +1,287 @@
namespace CuteUtils.FluentMath.TypeExtentions;
/// <summary>
/// FloatFluent class
/// </summary>
public static class SingleFluent
{
/// <summary>
/// Converts number to <see cref="decimal"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="decimal"/></returns>
public static decimal ToDecimal(this float num)
{
return (decimal)num;
}
/// <summary>
/// Converts number to <see cref="double"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="double"/></returns>
public static double ToDouble(this float num)
{
return num;
}
/// <summary>
/// Converts number to <see cref="short"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="short"/></returns>
public static short ToInt16(this float num)
{
return (short)num;
}
/// <summary>
/// Converts number to <see cref="int"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="int"/></returns>
public static int ToInt32(this float num)
{
return (int)num;
}
/// <summary>
/// Converts number to <see cref="long"/>
/// </summary>
/// <param name="num"></param>
/// <returns>Number as <see cref="long"/></returns>
public static long ToInt64(this float num)
{
return (long)num;
}
/// <summary>
/// Adds the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static float Add(this float num, float value)
{
return num + value;
}
/// <summary>
/// Subtracts the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static float Subtract(this float num, float value)
{
return num - value;
}
/// <summary>
/// Multiples the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static float Multiply(this float num, float value)
{
return num * value;
}
/// <summary>
/// Divides the two nums
/// </summary>
/// <param name="num"></param>
/// <param name="value"></param>
/// <returns></returns>
public static float Divide(this float num, float value)
{
return num / value;
}
/// <inheritdoc cref="MathF.Abs(float)"/>
public static float Abs(this float num)
{
return MathF.Abs(num);
}
/// <inheritdoc cref="MathF.Acos(float)"/>
public static float Acos(this float num)
{
return MathF.Acos(num);
}
/// <inheritdoc cref="MathF.Acosh(float)"/>
public static float Acosh(this float num)
{
return MathF.Acosh(num);
}
/// <inheritdoc cref="MathF.Asin(float)"/>
public static float Asin(this float num)
{
return MathF.Asin(num);
}
/// <inheritdoc cref="MathF.Asinh(float)"/>
public static float Asinh(this float num)
{
return MathF.Asinh(num);
}
/// <inheritdoc cref="MathF.Atan(float)"/>
public static float Atan(this float num)
{
return MathF.Atan(num);
}
/// <inheritdoc cref="MathF.Atan2(float,float)"/>
public static float Atan2(this float num, float value)
{
return MathF.Atan2(num, value);
}
/// <inheritdoc cref="MathF.Atanh(float)"/>
public static float Atanh(this float num)
{
return MathF.Atanh(num);
}
/// <inheritdoc cref="MathF.Cbrt(float)"/>
public static float Cbrt(this float num)
{
return MathF.Cbrt(num);
}
/// <inheritdoc cref="MathF.Ceiling(float)"/>
public static float Ceiling(this float num)
{
return MathF.Ceiling(num);
}
/// <inheritdoc cref="Math.Clamp(float,float,float)"/>
public static float Clamp(this float num, float min, float max)
{
return Math.Clamp(num, min, max);
}
/// <inheritdoc cref="MathF.Cos(float)"/>
public static float Cos(this float num)
{
return MathF.Cos(num);
}
/// <inheritdoc cref="MathF.Cosh(float)"/>
public static float Cosh(this float num)
{
return MathF.Cosh(num);
}
/// <inheritdoc cref="MathF.Exp(float)"/>
public static float Exp(this float num)
{
return MathF.Exp(num);
}
/// <inheritdoc cref="MathF.Floor(float)"/>
public static float Floor(this float num)
{
return MathF.Floor(num);
}
/// <inheritdoc cref="MathF.IEEERemainder(float,float)"/>
public static float IEEERemainder(this float num, float value)
{
return MathF.IEEERemainder(num, value);
}
/// <inheritdoc cref="MathF.Log(float)"/>
public static float Log(this float num)
{
return MathF.Log(num);
}
/// <inheritdoc cref="MathF.Log(float,float)"/>
public static float Log(this float num, float newBase)
{
return MathF.Log(num, newBase);
}
/// <inheritdoc cref="MathF.Log10(float)"/>
public static float Log10(this float num)
{
return MathF.Log10(num);
}
/// <inheritdoc cref="MathF.Pow(float,float)"/>
public static float Pow(this float num, float power)
{
return MathF.Pow(num, power);
}
/// <inheritdoc cref="MathF.Round(float)"/>
public static float Round(this float num)
{
return MathF.Round(num);
}
/// <inheritdoc cref="MathF.Round(float,MidpointRounding)"/>
public static float Round(this float num, MidpointRounding mode)
{
return MathF.Round(num, mode);
}
/// <inheritdoc cref="MathF.Round(float,int)"/>
public static float Round(this float num, int digits)
{
return MathF.Round(num, digits);
}
/// <inheritdoc cref="MathF.Round(float,int,MidpointRounding)"/>
public static float Round(this float num, int digits, MidpointRounding mode)
{
return MathF.Round(num, digits, mode);
}
/// <inheritdoc cref="MathF.Sign(float)"/>
public static int Sign(this float num)
{
return MathF.Sign(num);
}
/// <inheritdoc cref="MathF.Sin(float)"/>
public static float Sin(this float num)
{
return MathF.Sin(num);
}
/// <inheritdoc cref="MathF.Sinh(float)"/>
public static float Sinh(this float num)
{
return MathF.Sinh(num);
}
/// <inheritdoc cref="MathF.Sqrt(float)"/>
public static float Sqrt(this float num)
{
return MathF.Sqrt(num);
}
/// <inheritdoc cref="MathF.Tan(float)"/>
public static float Tan(this float num)
{
return MathF.Tan(num);
}
/// <inheritdoc cref="MathF.Tanh(float)"/>
public static float Tanh(this float num)
{
return MathF.Tanh(num);
}
/// <inheritdoc cref="MathF.Truncate(float)"/>
public static float Truncate(this float num)
{
return MathF.Truncate(num);
}
}
+83
View File
@@ -0,0 +1,83 @@
using Stone_Red_C_Sharp_Utilities.Logging;
using Stone_Red_Utilities.Logging;
namespace CuteUtils.Logging;
/// <summary>
/// Logging configuration.
/// </summary>
public class LogConfig
{
/// <summary>
/// The configuration for <see cref="LogSeverity.Debug"/> messages.
/// </summary>
public OutputConfig DebugConfig { get; set; } = new OutputConfig();
/// <summary>
/// The configuration for <see cref="LogSeverity.Info"/> messages.
/// </summary>
public OutputConfig InfoConfig { get; set; } = new OutputConfig();
/// <summary>
/// The configuration for <see cref="LogSeverity.Warn"/> messages.
/// </summary>
public OutputConfig WarnConfig { get; set; } = new OutputConfig();
/// <summary>
/// The configuration for <see cref="LogSeverity.Error"/> messages.
/// </summary>
public OutputConfig ErrorConfig { get; set; } = new OutputConfig();
/// <summary>
/// The configuration for <see cref="LogSeverity.Fatal"/> messages.
/// </summary>
public OutputConfig FatalConfig { get; set; } = new OutputConfig();
/// <summary>
/// The configuration for the message format.
/// </summary>
public FormatConfig FormatConfig { get; set; } = new FormatConfig();
}
/// <summary>
/// Output configuration.
/// </summary>
public class OutputConfig
{
/// <summary>
/// The console color of the log message.
/// </summary>
public ConsoleColor ConsoleColor { get; set; } = ConsoleColor.White;
/// <summary>
/// The target for the log message.
/// </summary>
public LogTarget LogTarget { get; set; } = LogTarget.DebugConsole;
/// <summary>
/// The log file path.
/// </summary>
public string FilePath { get; set; } = "log.log";
}
/// <summary>
/// Format configuration.
/// </summary>
public class FormatConfig
{
/// <summary>
/// The format for the debug console.
/// </summary>
public LogFormatBuilder DebugConsoleFormat { get; set; } = $"{{{LogFormatType.DateTime}:yyyy-MM-dd HH:mm:ss}} | {{{LogFormatType.LogSeverity},-5}} | {{{LogFormatType.Source},-15}} | {{{LogFormatType.Message}}}";
/// <summary>
/// The format for the console.
/// </summary>
public LogFormatBuilder ConsoleFormat { get; set; } = $"{{{LogFormatType.DateTime}:yyyy-MM-dd HH:mm:ss}} | {{{LogFormatType.LogSeverity},-5}} | {{{LogFormatType.Source},-15}} | {{{LogFormatType.Message}}}";
/// <summary>
/// The format for the log file.
/// </summary>
public LogFormatBuilder FileFormat { get; set; } = $"{{{LogFormatType.DateTime}:yyyy-MM-dd HH:mm:ss}} | {{{LogFormatType.LogSeverity},-5}} | {{{LogFormatType.Source},-15}} | {{{LogFormatType.Message}}}";
}
+147
View File
@@ -0,0 +1,147 @@
using Stone_Red_C_Sharp_Utilities.Logging;
using System.Text;
namespace CuteUtils.Logging;
/// <summary>
/// A builder for <see cref="FormatConfig"/>
/// </summary>
public class LogFormatBuilder
{
private readonly StringBuilder stringBuilder = new StringBuilder();
/// <summary>
/// Creates a new <see cref="LogFormatBuilder"/> instance.
/// </summary>
public LogFormatBuilder()
{
}
/// <summary>
/// Creates a new <see cref="LogFormatBuilder"/> instance.
/// </summary>
/// <param name="value">The inital format.</param>
public LogFormatBuilder(string value)
{
_ = stringBuilder.Append(value);
}
/// <summary>
/// Converts the <see cref="LogFormatBuilder" to <see cref="string"/>/>
/// </summary>
/// <param name="value">The <see cref="LogFormatBuilder"/> to convert.</param>
public static implicit operator string(LogFormatBuilder value)
{
return value.stringBuilder.ToString();
}
/// <summary>
/// Converts the <see cref="string" to <see cref="LogFormatBuilder"/>/>
/// </summary>
/// <param name="value">The <see cref="stringBuilder"/> to convert.</param>
public static implicit operator LogFormatBuilder(string value)
{
return new LogFormatBuilder(value);
}
/// <summary>
/// Appends text to the log format.
/// </summary>
/// <param name="value">The text to append.</param>
/// <returns>A reference to this <see cref="LogFormatBuilder"/> instance.</returns>
public LogFormatBuilder Text(string value)
{
_ = stringBuilder.Append(value);
return this;
}
/// <summary>
/// Appends the log datie time to the log format.
/// </summary>
/// <param name="format">The format to apply.</param>
/// <param name="padding">The padding to apply.</param>
/// <returns>A reference to this <see cref="LogFormatBuilder"/> instance.</returns>
public LogFormatBuilder DateTime(string format = "", int padding = 0)
{
_ = stringBuilder.Append($"{{{LogFormatType.DateTime},{padding}{GetFormat(format)}}}");
return this;
}
/// <summary>
/// Appends the log severity to the log format.
/// </summary>
/// <param name="format">The format to apply.</param>
/// <param name="padding">The padding to apply.</param>
/// <returns>A reference to this <see cref="LogFormatBuilder"/> instance.</returns>
public LogFormatBuilder LogSeverity(string format = "", int padding = 0)
{
_ = stringBuilder.Append($"{{{LogFormatType.LogSeverity},{padding}{GetFormat(format)}}}");
return this;
}
/// <summary>
/// Appends the line number to the log format.
/// </summary>
/// <param name="format">The format to apply.</param>
/// <param name="padding">The padding to apply.</param>
/// <returns>A reference to this <see cref="LogFormatBuilder"/> instance.</returns>
public LogFormatBuilder LineNumber(string format = "", int padding = 0)
{
_ = stringBuilder.Append($"{{{LogFormatType.LineNumber},{padding}{GetFormat(format)}}}");
return this;
}
/// <summary>
/// Appends the file path to the log format.
/// </summary>
/// <param name="format">The format to apply.</param>
/// <param name="padding">The padding to apply.</param>
/// <returns>A reference to this <see cref="LogFormatBuilder"/> instance.</returns>
public LogFormatBuilder FilePath(string format = "", int padding = 0)
{
_ = stringBuilder.Append($"{{{LogFormatType.FilePath},{padding}{GetFormat(format)}}}");
return this;
}
/// <summary>
/// Appends the log source to the log format.
/// </summary>
/// <param name="format">The format to apply.</param>
/// <param name="padding">The padding to apply.</param>
/// <returns>A reference to this <see cref="LogFormatBuilder"/> instance.</returns>
public LogFormatBuilder MemberName(string format = "", int padding = 0)
{
_ = stringBuilder.Append($"{{{LogFormatType.MemberName},{padding}{GetFormat(format)}}}");
return this;
}
/// <summary>
/// Appends the log source to the log format.
/// </summary>
/// <param name="format">The format to apply.</param>
/// <param name="padding">The padding to apply.</param>
/// <returns>A reference to this <see cref="LogFormatBuilder"/> instance.</returns>
public LogFormatBuilder Source(string format = "", int padding = 0)
{
_ = stringBuilder.Append($"{{{LogFormatType.Source},{padding}{GetFormat(format)}}}");
return this;
}
/// <summary>
/// Appends the log message to the log format.
/// </summary>
/// <param name="format">The format to apply.</param>
/// <param name="padding">The padding to apply.</param>
/// <returns>A reference to this <see cref="LogFormatBuilder"/> instance.</returns>
public LogFormatBuilder Message(string format = "", int padding = 0)
{
_ = stringBuilder.Append($"{{{LogFormatType.Message},{padding}{GetFormat(format)}}}");
return this;
}
private static string GetFormat(string format)
{
return string.IsNullOrWhiteSpace(format) ? string.Empty : $":{format}";
}
}
+42
View File
@@ -0,0 +1,42 @@
namespace CuteUtils.Logging;
/// <summary>
/// Specifies the info type of the log message format.
/// </summary>
public static class LogFormatType
{
/// <summary>
/// The <see cref="System.DateTime"/> of the log message.
/// </summary>
public const string DateTime = "<DateTime>";
/// <summary>
/// The <see cref="LogSeverity"/> of the log message.
/// </summary>
public const string LogSeverity = "<LogSeverity>";
/// <summary>
/// The line number of the log message.
/// </summary>
public const string LineNumber = "<LineNumber>";
/// <summary>
/// The file path of the log message.
/// </summary>
public const string FilePath = "<FilePath>";
/// <summary>
/// The member name of the log message.
/// </summary>
public const string MemberName = "<MemberName>";
/// <summary>
/// The source of the log message.
/// </summary>
public const string Source = "<Source>";
/// <summary>
/// The message of the log message.
/// </summary>
public const string Message = "<Message>";
}
+32
View File
@@ -0,0 +1,32 @@
namespace CuteUtils.Logging;
/// <summary>
/// Specifies the severity of the log message.
/// </summary>
public enum LogSeverity
{
/// <summary>
/// Logs that contain the most detailed messages.
/// </summary>
Debug,
/// <summary>
/// Logs that track the general flow of the application.
/// </summary>
Info,
/// <summary>
/// Logs that highlight an abnormal activity in the flow of execution.
/// </summary>
Warn,
/// <summary>
/// Logs that highlight when the flow of execution is stopped due to a failure.
/// </summary>
Error,
/// <summary>
/// Logs that contain the most severe level of error. This type of error indicate that immediate attention may be required.
/// </summary>
Fatal
}
+23
View File
@@ -0,0 +1,23 @@
namespace CuteUtils.Logging;
/// <summary>
/// Specifies the target of the log message.
/// </summary>
[Flags]
public enum LogTarget
{
/// <summary>
/// Writes log to console
/// </summary>
Console = 1,
/// <summary>
/// Writes log to debug console
/// </summary>
DebugConsole = 2,
/// <summary>
/// Writes log to file
/// </summary>
File = 3
}
+278
View File
@@ -0,0 +1,278 @@
using Stone_Red_C_Sharp_Utilities;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace CuteUtils.Logging;
/// <summary>
/// Class used for logging
/// </summary>
public class Logger
{
/// <summary>
/// The logging configuration.
/// </summary>
public LogConfig Config { get; init; } = new LogConfig();
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="source"></param>
/// <param name="logSeverity"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void Log(string message, string source, LogSeverity logSeverity, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, source, logSeverity, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="logSeverity"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void Log(string message, LogSeverity logSeverity, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, string.Empty, logSeverity, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="source"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogInfo(string message, string source, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, source, LogSeverity.Info, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogInfo(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, string.Empty, LogSeverity.Info, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="source"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogWarn(string message, string source, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, source, LogSeverity.Warn, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogWarn(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, string.Empty, LogSeverity.Warn, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogError(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, string.Empty, LogSeverity.Error, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="source"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogError(string message, string source, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, source, LogSeverity.Error, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogFatal(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, string.Empty, LogSeverity.Fatal, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="source"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogFatal(string message, string source, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, source, LogSeverity.Fatal, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogDebug(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, string.Empty, LogSeverity.Debug, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output
/// </summary>
/// <param name="message"></param>
/// <param name="source"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogDebug(string message, string source, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
WriteLog(message, source, LogSeverity.Debug, memberName, sourceFilePath, sourceLineNumber);
}
/// <summary>
/// Log the message to the specified output if the condition is met
/// </summary>
/// <param name="condition"></param>
/// <param name="message"></param>
/// <param name="source"></param>
/// <param name="logSeverity"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogIf(bool condition, string message, string source, LogSeverity logSeverity, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
if (condition)
{
WriteLog(message, source, logSeverity, memberName, sourceFilePath, sourceLineNumber);
}
}
/// <summary>
/// Log the message to the specified output if the condition is met
/// </summary>
/// <param name="condition"></param>
/// <param name="message"></param>
/// <param name="logSeverity"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void LogIf(bool condition, string message, LogSeverity logSeverity, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
if (condition)
{
WriteLog(message, string.Empty, logSeverity, memberName, sourceFilePath, sourceLineNumber);
}
}
/// <summary>
/// Clears the log file
/// </summary>
public void ClearLogFile(LogSeverity logSeverity)
{
OutputConfig outputConfig = GetOutputConfig(logSeverity);
lock (outputConfig)
{
if (File.Exists(outputConfig.FilePath))
{
File.WriteAllText(outputConfig.FilePath, string.Empty);
}
}
}
private static string GetFormattedString(string format, LogSeverity logSeverity, string source, string message, string memberName, string sourceFilePath, int sourceLineNumber)
{
format = format
.Replace(LogFormatType.DateTime, "0")
.Replace(LogFormatType.LogSeverity, "1")
.Replace(LogFormatType.LineNumber, "2")
.Replace(LogFormatType.FilePath, "3")
.Replace(LogFormatType.MemberName, "4")
.Replace(LogFormatType.Source, "5")
.Replace(LogFormatType.Message, "6");
return string.Format(format, DateTime.Now, logSeverity.ToString().ToUpper(), sourceLineNumber, sourceFilePath, memberName, source, message);
}
private void WriteLog(string message, string source, LogSeverity logSeverity, string memberName = "", string sourceFilePath = "", int sourceLineNumber = 0)
{
string consoleOutput = GetFormattedString(Config.FormatConfig.ConsoleFormat, logSeverity, source, message, memberName, sourceFilePath, sourceLineNumber);
string debugOutput = GetFormattedString(Config.FormatConfig.DebugConsoleFormat, logSeverity, source, message, memberName, sourceFilePath, sourceLineNumber);
string fileOutput = GetFormattedString(Config.FormatConfig.FileFormat, logSeverity, source, message, memberName, sourceFilePath, sourceLineNumber);
OutputConfig outputConfig = GetOutputConfig(logSeverity);
if ((outputConfig.LogTarget & LogTarget.Console) == LogTarget.Console)
{
ConsoleExt.WriteLine(consoleOutput, outputConfig.ConsoleColor);
}
if ((outputConfig.LogTarget & LogTarget.DebugConsole) == LogTarget.DebugConsole)
{
Trace.WriteLine(debugOutput);
}
lock (outputConfig)
{
if ((outputConfig.LogTarget & LogTarget.File) == LogTarget.File)
{
if (!File.Exists(outputConfig.FilePath))
{
File.Create(outputConfig.FilePath).Close();
}
File.AppendAllLines(outputConfig.FilePath, [fileOutput]);
}
}
}
private OutputConfig GetOutputConfig(LogSeverity logSeverity)
{
return logSeverity switch
{
LogSeverity.Fatal => Config.FatalConfig,
LogSeverity.Error => Config.ErrorConfig,
LogSeverity.Warn => Config.WarnConfig,
LogSeverity.Info => Config.InfoConfig,
_ => Config.DebugConfig
};
}
}
+30
View File
@@ -0,0 +1,30 @@
namespace CuteUtils;
/// <summary>
/// <see cref="Random"/> Extensions
/// </summary>
public static class RandomExt
{
public static T NextItem<T>(this Random random, IEnumerable<T> enumerable)
{
ArgumentNullException.ThrowIfNull(enumerable);
return enumerable.ElementAt(random.Next(enumerable.Count()));
}
public static bool NextBool(this Random random)
{
return random.Next(2) == 0;
}
public static T NextEnum<T>(this Random random) where T : struct, Enum
{
T[] values = Enum.GetValues<T>();
return values[random.Next(values.Length)];
}
public static T NextEnum<T>(this Random random, T[] values) where T : struct, Enum
{
return values[random.Next(values.Length)];
}
}
+60
View File
@@ -0,0 +1,60 @@
using System.Reflection;
namespace CuteUtils;
/// <summary>
/// Reflection class
/// </summary>
public static class Reflection
{
/// <summary>
/// Copies all properties of an object to a new one.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static T CopyProperties<T>(this object obj) where T : new()
{
T newObj = new T();
Type objType = obj.GetType();
Type newObjType = newObj.GetType();
foreach (PropertyInfo propertyInfo in objType.GetProperties())
{
PropertyInfo? newObjPropertyInfo = newObjType.GetProperty(propertyInfo.Name);
if (newObjPropertyInfo is not null && newObjPropertyInfo.PropertyType.IsAssignableFrom(propertyInfo.PropertyType))
{
newObjPropertyInfo.SetValue(newObj, propertyInfo.GetValue(obj));
}
}
return newObj;
}
/// <summary>
/// Copies all properties of an object to a different one.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="newObj"></param>
/// <returns></returns>
public static T CopyProperties<T>(this object obj, T newObj)
{
ArgumentNullException.ThrowIfNull(newObj);
Type objType = obj.GetType();
Type newObjType = newObj.GetType();
foreach (PropertyInfo propertyInfo in objType.GetProperties())
{
PropertyInfo? newObjPropertyInfo = newObjType.GetProperty(propertyInfo.Name);
if (newObjPropertyInfo is not null && newObjPropertyInfo.PropertyType.IsAssignableFrom(propertyInfo.PropertyType))
{
newObjPropertyInfo.SetValue(newObj, propertyInfo.GetValue(obj));
}
}
return newObj;
}
}
+169
View File
@@ -0,0 +1,169 @@
using System.Globalization;
using System.Text;
namespace CuteUtils;
/// <summary>
/// <see cref="string"/> Extensions
/// </summary>
public static class StringExt
{
/// <summary>
/// Removes all invalid chars from the specified <see cref="string"/>
/// </summary>
/// <param name="str"></param>
/// <param name="allowSpaces"></param>
/// <returns></returns>
public static string ToFileName(this string str, bool allowSpaces = false)
{
char[] invalidChars = Path.GetInvalidFileNameChars();
if (!allowSpaces)
{
str = str.Replace(" ", string.Empty);
}
foreach (char item in invalidChars)
{
str = str.Replace(item.ToString(), string.Empty);
}
string normalizedString = str.Normalize(NormalizationForm.FormD);
StringBuilder stringBuilder = new StringBuilder();
foreach (char c in normalizedString)
{
UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
{
_ = stringBuilder.Append(c);
}
}
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}
/// <summary>
/// Removes all invalid chars from the specified <see cref="string"/>
/// </summary>
/// <param name="str"></param>
/// <param name="allowSpaces"></param>
/// <returns></returns>
public static string ToPath(this string str, bool allowSpaces = false)
{
char[] invalidChars = Path.GetInvalidPathChars();
if (!allowSpaces)
{
str = str.Replace(" ", string.Empty);
}
foreach (char item in invalidChars)
{
str = str.Replace(item.ToString(), string.Empty);
}
string normalizedString = str.Normalize(NormalizationForm.FormD);
StringBuilder stringBuilder = new StringBuilder();
foreach (char c in normalizedString)
{
UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
{
_ = stringBuilder.Append(c);
}
}
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}
/// <summary>
/// Truncates a <see cref="string"/> to the specified length.
/// </summary>
/// <param name="str"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string Truncate(this string str, int length)
{
if (str.Length > length && length > 0)
{
return str[..length];
}
return str;
}
/// <summary>
/// Truncates a <see cref="string"/> to the specified length.
/// </summary>
/// <param name="str"></param>
/// <param name="length"></param>
/// <param name="ellipsis"></param>
/// <returns></returns>
public static string Truncate(this string str, int length, bool ellipsis)
{
if (str.Length > length && length > 0)
{
if (ellipsis && length > 3)
{
return $"{str[..(length - 3)]}...";
}
else
{
return str[..length];
}
}
return str;
}
/// <summary>
/// Uses the correct newline <see cref="string"/> defined for this environment.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string CorrectNewLine(this string str)
{
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
str = str.Replace("\r\n", "\n");
}
else
{
str = str.Replace("\n", "\r\n"); //Ik that this can produce wrong results
}
return str;
}
/// <summary>
/// Removes all white spaces from the specified <see cref="string"/>
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string RemoveWhitespaces(this string str)
{
StringBuilder result = new StringBuilder();
foreach (char c in str)
{
if (!char.IsWhiteSpace(c))
{
_ = result.Append(c);
}
}
return result.ToString();
}
/// <summary>
/// Reverses the specified <see cref="string"/>
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Reverse(this string str)
{
char[] array = str.ToCharArray();
Array.Reverse(array);
return new string(array);
}
}
+91
View File
@@ -0,0 +1,91 @@
namespace CuteUtils.Tasks;
/// <summary>
/// Represents a blocking task queue that allows enqueueing tasks and functions.
/// </summary>
public class BlockingTaskQueue
{
private readonly SemaphoreSlim semaphore;
/// <summary>
/// Initializes a new instance of the <see cref="BlockingTaskQueue"/> class.
/// </summary>
public BlockingTaskQueue()
{
semaphore = new SemaphoreSlim(1);
}
/// <summary>
/// Enqueues a task that returns a value.
/// </summary>
/// <typeparam name="T">The type of the return value.</typeparam>
/// <param name="function">The function to execute.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task<T> Enqueue<T>(Func<T> function)
{
await semaphore.WaitAsync();
try
{
return await Task.Run(function);
}
finally
{
_ = semaphore.Release();
}
}
/// <summary>
/// Enqueues a task that does not return a value.
/// </summary>
/// <param name="function">The action to execute.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task Enqueue(Action function)
{
await semaphore.WaitAsync();
try
{
await Task.Run(function);
}
finally
{
_ = semaphore.Release();
}
}
/// <summary>
/// Enqueues a task.
/// </summary>
/// <param name="task">The task to enqueue.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task Enqueue(Task task)
{
await semaphore.WaitAsync();
try
{
await task;
}
finally
{
_ = semaphore.Release();
}
}
/// <summary>
/// Enqueues a task that returns a value.
/// </summary>
/// <typeparam name="T">The type of the return value.</typeparam>
/// <param name="task">The task to enqueue.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task<T> Enqueue<T>(Task<T> task)
{
await semaphore.WaitAsync();
try
{
return await task;
}
finally
{
_ = semaphore.Release();
}
}
}
+134
View File
@@ -0,0 +1,134 @@
using System.Collections.Concurrent;
using System.Reactive.Linq;
using System.Reactive.Subjects;
namespace CuteUtils.Tasks;
/// <summary>
/// Represents a queue of tasks that can be enqueued and processed asynchronously.
/// </summary>
public class TaskQueue : IDisposable
{
private readonly BlockingCollection<(Task Task, Action Callback)> tasks = [];
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private bool processing = false;
private bool disposed;
/// <summary>
/// Enqueues a task that returns a value.
/// </summary>
/// <typeparam name="T">The type of the value returned by the task.</typeparam>
/// <param name="function">The function representing the task.</param>
/// <returns>An observable that emits the task when it completes.</returns>
public IObservable<Task<T>> Enqueue<T>(Func<T> function)
{
Subject<Task<T>> subject = new Subject<Task<T>>();
Task<T> task = new Task<T>(function);
tasks.Add((task, () => subject.OnNext(task)));
ProcessTasks();
return subject.AsObservable();
}
/// <summary>
/// Enqueues a task that does not return a value.
/// </summary>
/// <param name="function">The action representing the task.</param>
/// <returns>An observable that emits the task when it completes.</returns>
public IObservable<Task> Enqueue(Action function)
{
Subject<Task> subject = new Subject<Task>();
Task task = new Task(function);
tasks.Add((task, () => subject.OnNext(task)));
ProcessTasks();
return subject.AsObservable();
}
/// <summary>
/// Enqueues a pre-created task.
/// </summary>
/// <param name="task">The task to enqueue.</param>
/// <returns>An observable that emits the task when it completes.</returns>
public IObservable<Task> Enqueue(Task task)
{
Subject<Task> subject = new Subject<Task>();
tasks.Add((task, () => subject.OnNext(task)));
ProcessTasks();
return subject.AsObservable();
}
/// <summary>
/// Enqueues a pre-created task that returns a value.
/// </summary>
/// <typeparam name="T">The type of the value returned by the task.</typeparam>
/// <param name="task">The task to enqueue.</param>
/// <returns>An observable that emits the task when it completes.</returns>
public IObservable<Task<T>> Enqueue<T>(Task<T> task)
{
Subject<Task<T>> subject = new Subject<Task<T>>();
tasks.Add((task, () => subject.OnNext(task)));
ProcessTasks();
return subject.AsObservable();
}
/// <summary>
/// Disposes the task queue and cancels any pending tasks.
/// </summary>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
cancellationTokenSource.Dispose();
tasks.Dispose();
}
disposed = true;
}
}
private void ProcessTasks()
{
if (processing)
{
return;
}
processing = true;
_ = new TaskFactory().StartNew(async () =>
{
while (!disposed)
{
(Task Task, Action Callback) container = tasks.Take(cancellationTokenSource.Token);
if (container.Task.Status == TaskStatus.Created)
{
container.Task.Start();
}
await container.Task.WaitAsync(cancellationTokenSource.Token);
container.Callback?.Invoke();
}
}, cancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
}