1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 07:35:42 +00:00

Use file-scoped namespaces

This commit is contained in:
Oliver Booth 2022-04-20 18:51:20 +01:00
parent 37c7b74379
commit d4e3c8ab50
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 137 additions and 140 deletions

View File

@ -1,22 +1,21 @@
using System.ComponentModel; using System.ComponentModel;
namespace X10D namespace X10D;
/// <summary>
/// Represents an enumeration of endianness values.
/// </summary>
public enum Endianness
{ {
/// <summary> /// <summary>
/// Represents an enumeration of endianness values. /// The value should be read as though it uses little endian encoding.
/// </summary> /// </summary>
public enum Endianness [Description("The value should be read as though it uses little endian encoding.")]
{ LittleEndian,
/// <summary>
/// The value should be read as though it uses little endian encoding.
/// </summary>
[Description("The value should be read as though it uses little endian encoding.")]
LittleEndian,
/// <summary> /// <summary>
/// The value should be read as though it uses big endian encoding. /// The value should be read as though it uses big endian encoding.
/// </summary> /// </summary>
[Description("The value should be read as though it uses big endian encoding.")] [Description("The value should be read as though it uses big endian encoding.")]
BigEndian BigEndian
}
} }

View File

@ -1,40 +1,39 @@
namespace X10D namespace X10D;
/// <summary>
/// Provides static helpers methods for mathematical functions not found in the .NET <see cref="System.Math" /> class.
/// </summary>
public static class MathUtils
{ {
/// <summary> /// <summary>
/// Provides static helpers methods for mathematical functions not found in the .NET <see cref="System.Math" /> class. /// Linearly interpolates from one value to a target using a specified alpha.
/// </summary> /// </summary>
public static class MathUtils /// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float Lerp(float value, float target, float alpha)
{ {
/// <summary> // rookie mistake: a + t * (b - a)
/// Linearly interpolates from one value to a target using a specified alpha. // "precise" method: (1 - t) * a + t * b
/// </summary> return ((1.0f - alpha) * value) + (alpha * target);
/// <param name="value">The interpolation source.</param> }
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float Lerp(float value, float target, float alpha)
{
// rookie mistake: a + t * (b - a)
// "precise" method: (1 - t) * a + t * b
return ((1.0f - alpha) * value) + (alpha * target);
}
/// <summary> /// <summary>
/// Linearly interpolates from one value to a target using a specified alpha. /// Linearly interpolates from one value to a target using a specified alpha.
/// </summary> /// </summary>
/// <param name="value">The interpolation source.</param> /// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param> /// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param> /// <param name="alpha">The interpolation alpha.</param>
/// <returns> /// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>. /// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns> /// </returns>
public static double Lerp(double value, double target, double alpha) public static double Lerp(double value, double target, double alpha)
{ {
// rookie mistake: a + t * (b - a) // rookie mistake: a + t * (b - a)
// "precise" method: (1 - t) * a + t * b // "precise" method: (1 - t) * a + t * b
return ((1.0 - alpha) * value) + (alpha * target); return ((1.0 - alpha) * value) + (alpha * target);
}
} }
} }

View File

@ -1,103 +1,102 @@
using System.Globalization; using System.Globalization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace X10D namespace X10D;
/// <summary>
/// Represents a class which contains a <see cref="string" /> parser which converts into <see cref="TimeSpan" />.
/// </summary>
public static class TimeSpanParser
{ {
private const string RealNumberPattern = @"(\d*\.\d+|\d+)";
private static readonly string Pattern = $"^(?:{RealNumberPattern} *y)? *" +
$"^(?:{RealNumberPattern} *mo)? *" +
$"^(?:{RealNumberPattern} *w)? *" +
$"(?:{RealNumberPattern} *d)? *" +
$"(?:{RealNumberPattern} *h)? *" +
$"(?:{RealNumberPattern} *m)? *" +
$"(?:{RealNumberPattern} *s)? *" +
$"(?:{RealNumberPattern} *ms)?$";
private static readonly Regex Regex = new(Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
/// <summary> /// <summary>
/// Represents a class which contains a <see cref="string" /> parser which converts into <see cref="TimeSpan" />. /// Attempts to parses a shorthand time span string (e.g. 3w 2d 1.5h), converting it to an instance of
/// <see cref="TimeSpan" /> which represents that duration of time.
/// </summary> /// </summary>
public static class TimeSpanParser /// <param name="input">The input string.</param>
/// <param name="result">The parsed result.</param>
/// <param name="provider">The format provider.</param>
/// <returns><see langword="true" /> if the parse was successful, <see langword="false" /> otherwise.</returns>
public static bool TryParse(string input, out TimeSpan result, IFormatProvider? provider = null)
{ {
private const string RealNumberPattern = @"(\d*\.\d+|\d+)"; result = default;
private static readonly string Pattern = $"^(?:{RealNumberPattern} *y)? *" + Match? match = Regex.Match(input);
$"^(?:{RealNumberPattern} *mo)? *" +
$"^(?:{RealNumberPattern} *w)? *" +
$"(?:{RealNumberPattern} *d)? *" +
$"(?:{RealNumberPattern} *h)? *" +
$"(?:{RealNumberPattern} *m)? *" +
$"(?:{RealNumberPattern} *s)? *" +
$"(?:{RealNumberPattern} *ms)?$";
private static readonly Regex Regex = new(Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); if (!match.Success)
/// <summary>
/// Attempts to parses a shorthand time span string (e.g. 3w 2d 1.5h), converting it to an instance of
/// <see cref="TimeSpan" /> which represents that duration of time.
/// </summary>
/// <param name="input">The input string.</param>
/// <param name="result">The parsed result.</param>
/// <param name="provider">The format provider.</param>
/// <returns><see langword="true" /> if the parse was successful, <see langword="false" /> otherwise.</returns>
public static bool TryParse(string input, out TimeSpan result, IFormatProvider? provider = null)
{ {
result = default; return false;
Match? match = Regex.Match(input);
if (!match.Success)
{
return false;
}
bool TryParseAt(int group, out double parsedResult)
{
parsedResult = 0;
return match.Groups[group].Success
&& double.TryParse(match.Groups[group].Value, NumberStyles.Number, provider, out parsedResult);
}
if (!TryParseAt(1, out double years))
{
return false;
}
if (!TryParseAt(2, out double months))
{
return false;
}
if (!TryParseAt(3, out double weeks))
{
return false;
}
if (!TryParseAt(4, out double days))
{
return false;
}
if (!TryParseAt(5, out double hours))
{
return false;
}
if (!TryParseAt(6, out double minutes))
{
return false;
}
if (!TryParseAt(7, out double seconds))
{
return false;
}
if (!TryParseAt(8, out double milliseconds))
{
return false;
}
result += TimeSpan.FromDays(years * 365);
result += TimeSpan.FromDays(months * 30);
result += TimeSpan.FromDays(weeks * 7);
result += TimeSpan.FromDays(days);
result += TimeSpan.FromHours(hours);
result += TimeSpan.FromMinutes(minutes);
result += TimeSpan.FromSeconds(seconds);
result += TimeSpan.FromMilliseconds(milliseconds);
return true;
} }
bool TryParseAt(int group, out double parsedResult)
{
parsedResult = 0;
return match.Groups[group].Success
&& double.TryParse(match.Groups[group].Value, NumberStyles.Number, provider, out parsedResult);
}
if (!TryParseAt(1, out double years))
{
return false;
}
if (!TryParseAt(2, out double months))
{
return false;
}
if (!TryParseAt(3, out double weeks))
{
return false;
}
if (!TryParseAt(4, out double days))
{
return false;
}
if (!TryParseAt(5, out double hours))
{
return false;
}
if (!TryParseAt(6, out double minutes))
{
return false;
}
if (!TryParseAt(7, out double seconds))
{
return false;
}
if (!TryParseAt(8, out double milliseconds))
{
return false;
}
result += TimeSpan.FromDays(years * 365);
result += TimeSpan.FromDays(months * 30);
result += TimeSpan.FromDays(weeks * 7);
result += TimeSpan.FromDays(days);
result += TimeSpan.FromHours(hours);
result += TimeSpan.FromMinutes(minutes);
result += TimeSpan.FromSeconds(seconds);
result += TimeSpan.FromMilliseconds(milliseconds);
return true;
} }
} }