1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:18:46 +00:00

(#7) Move Int32Extensions to child namespace

This commit is contained in:
Oliver Booth 2021-03-04 01:35:59 +00:00
parent ab08b1c1c0
commit 39dd2158de
2 changed files with 89 additions and 175 deletions

View File

@ -1,175 +0,0 @@
using System;
namespace X10D
{
/// <summary>
/// Extension methods for <see cref="int" />.
/// </summary>
public static class Int32Extensions
{
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value" /> itself otherwise.
/// </returns>
public static int Clamp(this int value, int min, int max)
{
return Math.Min(Math.Max(value, min), max);
}
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value" /> itself otherwise.
/// </returns>
[CLSCompliant(false)]
public static uint Clamp(this uint value, uint min, uint max)
{
return Math.Min(Math.Max(value, min), max);
}
/// <summary>
/// Converts the <see cref="int" /> to a <see cref="DateTime" /> treating it as a Unix timestamp.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <param name="isMillis">
/// Optional. Whether or not the input value should be treated as milliseconds. Defaults
/// to <see langword="false" />..
/// </param>
/// <returns>
/// Returns a <see cref="DateTime" /> representing <paramref name="timestamp" /> seconds since the Unix
/// epoch.
/// </returns>
public static DateTime FromUnixTimestamp(this int timestamp, bool isMillis = false)
{
return ((long)timestamp).FromUnixTimestamp(isMillis);
}
/// <summary>
/// Converts the <see cref="uint" /> to a <see cref="byte" />[].
/// </summary>
/// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="byte" />[].</returns>
[CLSCompliant(false)]
public static byte[] GetBytes(this uint number)
{
return BitConverter.GetBytes(number);
}
/// <summary>
/// Converts the <see cref="int" /> to a <see cref="byte" />[].
/// </summary>
/// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="byte" />[].</returns>
public static byte[] GetBytes(this int number)
{
return BitConverter.GetBytes(number);
}
/// <summary>
/// Determines if the <see cref="int" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsEven(this int number)
{
return ((long)number).IsEven();
}
/// <summary>
/// Determines if the <see cref="uint" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsEven(this uint number)
{
return ((ulong)number).IsEven();
}
/// <summary>
/// Determines if the <see cref="int" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsOdd(this int number)
{
return !number.IsEven();
}
/// <summary>
/// Determines if the <see cref="uint" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsOdd(this uint number)
{
return !number.IsEven();
}
/// <summary>
/// Determines if the <see cref="int" /> is a prime number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsPrime(this int number)
{
return ((long)number).IsPrime();
}
/// <summary>
/// Gets an boolean value that represents this integer.
/// </summary>
/// <param name="value">The integer.</param>
/// <returns>
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
/// <see langword="true" /> otherwise.
/// </returns>
public static bool ToBoolean(this int value)
{
return ((long)value).ToBoolean();
}
/// <summary>
/// Gets an boolean value that represents this integer.
/// </summary>
/// <param name="value">The integer.</param>
/// <returns>
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
/// <see langword="true" /> otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool ToBoolean(this uint value)
{
return ((ulong)value).ToBoolean();
}
}
}

View File

@ -0,0 +1,89 @@
using System;
using X10D.Int64Extensions;
namespace X10D.Int32Extensions
{
/// <summary>
/// Extension methods for <see cref="int" />.
/// </summary>
public static class Int32Extensions
{
/// <summary>
/// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="value">
/// A Unix time, expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z (January 1,
/// 1970, at 12:00 AM UTC). For Unix times before this date, its value is negative.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
public static DateTimeOffset FromUnixTimeMilliseconds(this int value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
}
/// <summary>
/// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="value">
/// A Unix time, expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at
/// 12:00 AM UTC). For Unix times before this date, its value is negative.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
public static DateTimeOffset FromUnixTimeSeconds(this int value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
}
/// <summary>
/// Returns the current 32-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
public static byte[] GetBytes(this int value)
{
return BitConverter.GetBytes(value);
}
/// <summary>
/// Returns a value indicating whether the current 32-bit signed integer is even.
/// </summary>
/// <param name="value">The number to check.</param>
/// <returns><see langword="true" /> if <paramref name="value" /> is even, or <see langword="false" /> otherwise.</returns>
public static bool IsEven(this int value)
{
return value % 2 == 0;
}
/// <summary>
/// Returns a value indicating whether the current 32-bit signed integer is odd.
/// </summary>
/// <param name="value">The number to check.</param>
/// <returns><see langword="true" /> if <paramref name="value" /> is odd, or <see langword="false" /> otherwise.</returns>
public static bool IsOdd(this int value)
{
return !value.IsEven();
}
/// <summary>
/// Returns a value indicating whether the current 32-bit signed integer is prime.
/// </summary>
/// <param name="value">The number to check.</param>
/// <returns><see langword="true" /> if <paramref name="value" /> is prime, or <see langword="false" /> otherwise.</returns>
public static bool IsPrime(this int value)
{
return ((long)value).IsPrime();
}
/// <summary>
/// Converts the value of the current 32-bit signed integer to an equivalent <see cref="bool" /> value.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns><see langword="true" /> if <paramref name="value" /> is not zero, or <see langword="false" /> otherwise.</returns>
public static bool ToBoolean(this int value)
{
return value != 0;
}
}
}