Skip to content

Commit

Permalink
Added extension methods to utility classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
GregaMohorko committed Oct 27, 2017
1 parent 7bff426 commit 8858351
Show file tree
Hide file tree
Showing 26 changed files with 709 additions and 336 deletions.
17 changes: 6 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@
## List of classes

**Utilities**:
- [Array](src/GM.Utility/GM.Utility/ArrayUtility.cs)
- [Bool](src/GM.Utility/GM.Utility/BoolUtility.cs)
- [DateTime](src/GM.Utility/GM.Utility/DateTimeUtility.cs)
- [Decimal](src/GM.Utility/GM.Utility/DecimalUtility.cs)
- [Dictionary](src/GM.Utility/GM.Utility/DictionaryUtility.cs)
- [Double](src/GM.Utility/GM.Utility/DoubleUtility.cs)
- [Environment](src/GM.Utility/GM.Utility/EnvironmentUtility.cs)
- [IO](src/GM.Utility/GM.Utility/IOUtility.cs)
- [IEnumerable](src/GM.Utility/GM.Utility/IEnumerableUtility.cs)
- [Int](src/GM.Utility/GM.Utility/IntUtility.cs)
- [IO](src/GM.Utility/GM.Utility/IOUtility.cs)
- [Long](src/GM.Utility/GM.Utility/LongUtility.cs)
- [Object](src/GM.Utility/GM.Utility/ObjectUtility.cs)
- [Processes](src/GM.Utility/GM.Utility/ProcessesUtility.cs)
- [Reflection](src/GM.Utility/GM.Utility/ReflectionUtility.cs)
- [SecureString](src/GM.Utility/GM.Utility/SecureStringUtility.cs)
- [Statistic](src/GM.Utility/GM.Utility/StatisticUtility.cs)
- [String](src/GM.Utility/GM.Utility/StringUtility.cs)
- [XML](src/GM.Utility/GM.Utility/XMLUtility.cs)

**Extensions**:
- [Array](src/GM.Utility/GM.Utility/ArrayExtensions.cs)
- [DateTime](src/GM.Utility/GM.Utility/DateTimeExtensions.cs)
- [Decimal](src/GM.Utility/GM.Utility/DecimalExtensions.cs)
- [IEnumerable](src/GM.Utility/GM.Utility/IEnumerableExtensions.cs)
- [Object](src/GM.Utility/GM.Utility/ObjectExtensions.cs)
- [SecureString](src/GM.Utility/GM.Utility/SecureStringExtensions.cs)
- [String](src/GM.Utility/GM.Utility/StringExtensions.cs)
- [Type](src/GM.Utility/GM.Utility/TypeExtensions.cs)

**Other**:
- [Util](src/GM.Utility/GM.Utility/Util.cs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

namespace GM.Utility
{
public static class ArrayExtensions
/// <summary>
/// Utilities for arrays.
/// </summary>
public static class ArrayUtility
{
/// <summary>
/// Adds the provided item to the end of the array.
Expand All @@ -24,7 +27,7 @@ public static T[] Add<T>(this T[] array, T item)
/// <summary>
/// Performs the specified action for each element in the array. Supports multiple dimensions, the second parameter of the action are current indices for the dimensions.
/// <para>
/// To use the indices, you can use the <see cref="Array.GetValue(int[])"/> or <see cref="Array.SetValue(object, int[])"/>.
/// To use the indices, you can use the <see cref="Array.GetValue(int[])"/> and <see cref="Array.SetValue(object, int[])"/>.
/// </para>
/// </summary>
/// <param name="array">The array that contains the elements.</param>
Expand All @@ -39,8 +42,33 @@ public static void ForEach(this Array array, Action<Array, int[]> action)

do {
action(array, walker.Position);
} while(walker.Step());
}

/// <summary>
/// Swaps two indexes in the array.
/// </summary>
/// <typeparam name="T">The type of the elements in the array.</typeparam>
/// <param name="array">The array in which to swap two elements.</param>
/// <param name="index1">The index of the first element.</param>
/// <param name="index2">The index of the second element.</param>
public static void Swap<T>(this T[] array, int index1, int index2)
{
Util.Swap(ref array[index1], ref array[index2]);
}

/// <summary>
/// Swaps two rows in the array.
/// </summary>
/// <typeparam name="T">The type of the elements in the array.</typeparam>
/// <param name="array">The array in which to swap two rows.</param>
/// <param name="row1">First row.</param>
/// <param name="row2">Second row.</param>
public static void SwapRow<T>(this T[,] array, int row1, int row2)
{
for(int i = array.GetLength(1) - 1; i >= 0; --i) {
Util.Swap(ref array[row1, i], ref array[row2, i]);
}
while(walker.Step());
}

private class ArrayTraverse
Expand All @@ -53,17 +81,19 @@ public ArrayTraverse(Array array)
Position = new int[array.Rank];

maxLengths = new int[array.Rank];
for(int i = 0; i < array.Rank; i++)
for(int i = 0; i < array.Rank; i++) {
maxLengths[i] = array.GetLength(i) - 1;
}
}

public bool Step()
{
for(int i = 0; i < Position.Length; i++)
if(Position[i] < maxLengths[i]) {
Position[i]++;
for(int j = 0; j < i; j++)
for(int j = 0; j < i; j++) {
Position[j] = 0;
}

return true;
}
Expand Down
68 changes: 58 additions & 10 deletions src/GM.Utility/GM.Utility/BoolUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,88 @@

namespace GM.Utility
{
/// <summary>
/// Utilities for boolean.
/// </summary>
public static class BoolUtility
{

/// <summary>
/// Determines whether all the texts in the provided collection represent a valid bool.
/// </summary>
/// <param name="texts">A collection of texts.</param>
public static bool AreBools(IEnumerable<string> texts)
{
return texts.All(t => IsBool(t));
}

/// <summary>
/// Returns null if the provided text is not a valid bool.
/// Determines whether the provided text represents a valid bool.
/// </summary>
/// <param name="text">The text.</param>
public static bool IsBool(string text)
{
return bool.TryParse(text, out bool value);
}

/// <summary>
/// Converts a collection of string representations of bools to actual bools.
/// </summary>
/// <param name="texts">A collection of texts to parse.</param>
public static IEnumerable<bool> Parse(IEnumerable<string> texts)
{
return texts.Select(t => bool.Parse(t));
}

/// <summary>
/// Converts a collection of string representations of bools to actual bools. Sets null if a text is invalid.
/// </summary>
/// <param name="texts">A collection of texts to parse.</param>
public static IEnumerable<bool?> ParseNullable(IEnumerable<string> texts)
{
return texts.Select(t => ParseNullable(t));
}

/// <summary>
/// Parses the provided text to a bool. Returns null if it is invalid.
/// <para>
/// Valid values: true, false, 0, 1, empty string.
/// </para>
/// </summary>
/// <param name="text">The text to parse.</param>
public static bool? ParseNullable(string text)
{
bool value;
if(TryParse(text, out value))
if(TryParse(text, out bool value)) {
return value;

}
return null;
}

/// <summary>
/// Valid values: true, false, 0, 1.
/// Attempts to parse the provided text and returns true, if it was successful.
/// <para>
/// Valid values: true, false, 0, 1, empty string.
/// </para>
/// </summary>
public static bool TryParse(string value, out bool result)
/// <param name="text">The text to parse.</param>
/// <param name="result">Will contain true or false.</param>
public static bool TryParse(string text, out bool result)
{
result = false;

if(value == null) {
if(text == null) {
return false;
}

if(value == bool.FalseString) {
if(text == bool.FalseString) {
return true;
}
if(value == bool.TrueString) {
if(text == bool.TrueString) {
result = true;
return true;
}

switch(value.ToLower()) {
switch(text.ToLower()) {
case "true":
case "1":
result = true;
Expand Down
59 changes: 0 additions & 59 deletions src/GM.Utility/GM.Utility/DateTimeExtensions.cs

This file was deleted.

Loading

0 comments on commit 8858351

Please sign in to comment.