Skip to content

Commit

Permalink
More extensions added
Browse files Browse the repository at this point in the history
  • Loading branch information
safalin1 committed May 8, 2019
1 parent 9b4789a commit 4baaeca
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Chiaki/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Chiaki
{
/// <summary>
/// Provides extensions for System.DateTime
/// Provides extensions for <see cref="DateTime"/>
/// </summary>
public static class DateTimeExtensions
{
Expand Down
26 changes: 26 additions & 0 deletions Chiaki/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Collections.Specialized;

namespace Chiaki
{
/// <summary>
/// Provides extensions for <see cref="Dictionary{TKey,TValue}"/>
/// </summary>
public static class DictionaryExtensions
{
/// <summary>
/// Converts a dictionary instance to a NameValueCollection.
/// </summary>
public static NameValueCollection ToNameValueCollection(this IDictionary<string, string> d)
{
var n = new NameValueCollection();

foreach (var i in d)
{
n.Add(i.Key, i.Value);
}

return n;
}
}
}
2 changes: 1 addition & 1 deletion Chiaki/DoubleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Chiaki
{
/// <summary>
/// Provides extensions for System.Double
/// Provides extensions for <see cref="double"/>
/// </summary>
public static class DoubleExtensions
{
Expand Down
28 changes: 27 additions & 1 deletion Chiaki/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Chiaki
{
/// <summary>
/// Provides common extensions for IEnumerables.
/// Provides extensions for <see cref="IEnumerable{T}"/>.
/// </summary>
public static class EnumerableExtensions
{
Expand Down Expand Up @@ -48,5 +48,31 @@ public static IEnumerable<T> OrderByRandom<T>(this IEnumerable<T> query)
{
return query.OrderBy(q => Guid.NewGuid());
}

/// <summary>
/// Removes all matching items from an <see cref="IList{T}"/>.
/// </summary>
public static void RemoveAll<T>(this IList<T> list, Func<T, bool> predicate)
{
for (var i = 0; i < list.Count; i++)
{
if (predicate(list[i]))
{
list.RemoveAt(i--);
}
}
}

/// <summary>
/// Removes all matching items from an <see cref="ICollection{T}"/>.
/// </summary>
public static void RemoveAll<T>(this ICollection<T> list, Func<T, bool> predicate)
{
var matches = list.Where(predicate).ToArray();
foreach (var match in matches)
{
list.Remove(match);
}
}
}
}
2 changes: 1 addition & 1 deletion Chiaki/RandomExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Chiaki
{
/// <summary>
/// Provides extension methods for the System.Random type.
/// Provides extensions for <see cref="Random"/>.
/// </summary>
public static class RandomExtensions
{
Expand Down
2 changes: 1 addition & 1 deletion Chiaki/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Chiaki
{
/// <summary>
/// Provides extension methods for the System.IO.Stream type.
/// Provides extensions for <see cref="Stream"/>
/// </summary>
public static class StreamExtensions
{
Expand Down
72 changes: 71 additions & 1 deletion Chiaki/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

// ReSharper disable UnusedMember.Global

namespace Chiaki
{
/// <summary>
/// Provides extensions for System.String
/// Provides extensions for <see cref="string"/>
/// </summary>
public static class StringExtensions
{
Expand Down Expand Up @@ -113,5 +116,72 @@ public static string TrimStart(this string input, string remove)

return input;
}

/// <summary>
/// Strips whitespace, tabs, carriage returns, new line, vertical tab and form feed characters from a string.
/// </summary>
/// <remarks>
/// This method uses Regex \s to match.
/// </remarks>
internal static string StripWhitespace(this string input)
{
return Regex.Replace(input, @"\s", string.Empty);
}

/// <summary>
/// Strips all HTML tags from a string.
/// </summary>
public static string StripHtml(this string input)
{
const string pattern = @"<(.|\n)*?>";

return Regex.Replace(input, pattern, string.Empty, RegexOptions.Compiled);
}

/// <summary>
/// Strips all carriage returns from a string.
/// </summary>
public static string StripNewLines(this string input)
{
return input
.Replace("\r", string.Empty)
.Replace("\n", string.Empty);
}

/// <summary>
/// Converts a string to hexadecimal format.
/// </summary>
public static string ToHexadecimal(this string input)
{
if (string.IsNullOrEmpty(input))
{
return string.Empty;
}

var builder = new StringBuilder(input.Length);

foreach (var c in input)
{
builder.AppendFormat("{0:x2}", Convert.ToUInt32(c));
}

return builder.ToString();
}

/// <summary>
/// Converts a string from hexadecimal format.
/// </summary>
public static string FromHexadecimal(this string hexadecimal)
{
var builder = new StringBuilder();

while (hexadecimal.Length > 0)
{
builder.Append(Convert.ToChar(Convert.ToUInt32(hexadecimal.Substring(0, 2), 16)).ToString());
hexadecimal = hexadecimal.Substring(2, hexadecimal.Length - 2);
}

return builder.ToString();
}
}
}

0 comments on commit 4baaeca

Please sign in to comment.