84 lines
5.5 KiB
C#

namespace DigitalData.Core.Security.Config
{
internal static class StringExtensions
{
public static string ToBase64String(this byte[] bytes) => Convert.ToBase64String(bytes);
public static byte[] Base64ToByte(this string base64String) => Convert.FromBase64String(base64String);
public static byte[] ToBytes(this string str) => System.Text.Encoding.UTF8.GetBytes(str);
public static string BytesToString(this byte[] bytes) => System.Text.Encoding.UTF8.GetString(bytes);
/// <summary>
/// Converts a <see cref="DateTime"/> to a formatted string based on the specified format string.
/// <br />
/// - If the format contains the "//" symbol, the method divides the numeric value derived from the left side of the format
/// by the numeric value of the right side of the format string. For instance:
/// <br />
/// - If the date is 02.03.2024 and the format is "MM//2", it extracts the month (02), divides it by 2 (resulting in 1),
/// and returns "1".
/// <br />
/// - If the format does not contain "//", the method uses the default <see cref="DateTime.ToString"/> format.
/// <br />
/// This method provides a way to format the date based on typical or customized rules, including mathematical operations like division.
/// </summary>
/// <param name="date">The <see cref="DateTime"/> value to be formatted.</param>
/// <param name="format">The format string that dictates the formatting of the date. If the format includes the "//" symbol,
/// it splits the string at "//" and divides the left-side value by the right-side value. The format string can include standard
/// <see cref="DateTime.ToString"/> format patterns.</param>
/// <returns>A string representation of the formatted date, or the result of the division operation if "//" is present in the format.</returns>
/// <exception cref="ArgumentException">Thrown if the format string is invalid, such as having an incorrect number of parts after "//".</exception>
/// <exception cref="DivideByZeroException">Thrown if the right side of the "//" contains a zero, resulting in division by zero.</exception>
/// <exception cref="FormatException">Thrown if either the left-side or right-side value of "//" cannot be parsed as an integer.</exception>
public static string ToTag(this DateTime date, string format)
{
if (format is not null && format.Contains("//"))
{
var subStrings = format.Split("//");
if (subStrings.Length != 2)
throw new ArgumentException("Format is invalid. It must contain exactly one '//' separator.");
var formattedLeft = date.ToString(subStrings[0]);
if (!int.TryParse(formattedLeft, out var dateValue))
throw new FormatException("The left-side value of the format could not be parsed to an integer.");
if (!int.TryParse(subStrings[1], out var mode))
throw new FormatException("The right-side value of the format could not be parsed to an integer.");
if (mode == 0)
throw new DivideByZeroException("Division by zero is not allowed.");
var result = dateValue / mode;
return result.ToString();
}
return date.ToString(format);
}
/// <summary>
/// Converts a <see cref="DateTime"/> to a formatted string based on the specified format string.
/// <br />
/// - If the format contains the "//" symbol, the method divides the numeric value derived from the left side of the format
/// by the numeric value of the right side of the format string. For instance:
/// <br />
/// - If the date is 02.03.2024 and the format is "MM//2", it extracts the month (02), divides it by 2 (resulting in 1),
/// and returns "1".
/// <br />
/// - If the format does not contain "//", the method uses the default <see cref="DateTime.ToString"/> format.
/// <br />
/// This method provides a way to format the date based on typical or customized rules, including mathematical operations like division.
/// </summary>
/// <param name="date">The <see cref="DateOnly"/> value to be formatted. It will convert to DateTime to use the method shared with DateTime.</param>
/// <param name="format">The format string that dictates the formatting of the date. If the format includes the "//" symbol,
/// it splits the string at "//" and divides the left-side value by the right-side value. The format string can include standard
/// <see cref="DateTime.ToString"/> format patterns.</param>
/// <returns>A string representation of the formatted date, or the result of the division operation if "//" is present in the format.</returns>
/// <exception cref="ArgumentException">Thrown if the format string is invalid, such as having an incorrect number of parts after "//".</exception>
/// <exception cref="DivideByZeroException">Thrown if the right side of the "//" contains a zero, resulting in division by zero.</exception>
/// <exception cref="FormatException">Thrown if either the left-side or right-side value of "//" cannot be parsed as an integer.</exception>
public static string ToTag(this DateOnly date, string format) => date.ToDateTime(new()).ToTag(format);
}
}