Skip to content

Instantly share code, notes, and snippets.

@abitofhelp
Last active August 14, 2018 22:01
Show Gist options
  • Save abitofhelp/0424a04e95692c7e37e63fdc5cc97f8d to your computer and use it in GitHub Desktop.
Save abitofhelp/0424a04e95692c7e37e63fdc5cc97f8d to your computer and use it in GitHub Desktop.

Revisions

  1. abitofhelp revised this gist Aug 14, 2018. No changes.
  2. abitofhelp created this gist Aug 14, 2018.
    166 changes: 166 additions & 0 deletions cs-stringextensions.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,166 @@
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    // file: Extensions\StringExtensions.cs
    //
    // summary: Implements the string extensions class
    ////////////////////////////////////////////////////////////////////////////////////////////////////

    using System;
    using System.Linq;
    using System.Security.Cryptography;

    namespace TVLib.Extensions
    {
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary> String extensions. </summary>
    ///
    /// <remarks> Mike Gardner 12/05/16. </remarks>
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    public static class StringExtensions
    {
    #region ENUMERATIONS

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary> Values that represent random string contents. </summary>
    ///
    /// <remarks> Mike Gardner 12/05/16. </remarks>
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    public enum RandomStringContent : long
    {
    /// <summary> An enum constant representing the alpha option. </summary>
    LettersOnly = 0,

    /// <summary> An enum constant representing the numeric option. </summary>
    DigitsOnly = 1,

    /// <summary> An enum constant representing the alphanumeric option. </summary>
    AlphaNumeric = 2
    }

    #endregion

    #region CONSTANTS

    /// <summary> The alpha numeric characters. </summary>
    private const string kAlphaNumericCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    /// <summary> The digit characters. </summary>
    private const string kDigitCharacters = "0123456789";

    /// <summary> The letter characters. </summary>
    private const string kLetterCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    #endregion

    #region ALL OTHER MEMBERS

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// Remove all escape sequences from a string and replace them with a specified character. By
    /// default, the character is nothing.
    /// </summary>
    ///
    /// <remarks> Mike Gardner 12/09/16. </remarks>
    ///
    /// <param name="text"> Text to sanitize. </param>
    /// <param name="replacement"> (Optional) Character used to replace each escape sequence. </param>
    ///
    /// <returns> Null on error. </returns>
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    public static string RemoveEscapeSequences(this string text, string replacement = "")
    {
    if (string.IsNullOrEmpty(text))
    {
    return null;
    }

    var cleaned = text.Replace("\a", replacement) // Warning
    .Replace("\b", replacement) // BACKSPACE
    .Replace("\f", replacement) // Form-feed
    .Replace("\n", replacement) // Line reverse
    .Replace("\r", replacement) // Carriage return
    .Replace("\t", replacement) // Horizontal tab
    .Replace("\v", replacement) // Vertical tab
    .Replace("\'", replacement) // Single quote
    .Replace("\"", replacement) // Double quote
    .Replace("\\", replacement); // Backslash
    //.Replace ("\?", replacement); // Literal question mark

    return cleaned;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary> A string extension method that converts a text "boolean" to a boolean. </summary>
    ///
    /// <remarks> Mike Gardner 12/12/16. </remarks>
    ///
    /// <param name="text"> Text to sanitize. </param>
    ///
    /// <returns> A boolean value or null on error. </returns>
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    public static bool? ToBoolean(this string text)
    {
    if (text.Equals("Yes") || text.Equals("True") || text.Equals("Y") || text.Equals("T")) return true;

    if (text.Equals("No") || text.Equals("False") || text.Equals("N") || text.Equals("F")) return false;

    return null;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary> Generates a random string of a desired length. </summary>
    ///
    /// <remarks>
    /// Mike Gardner 12/05/16. The random string is not guaranteed to be unique.
    /// </remarks>
    ///
    /// <exception cref="ArgumentOutOfRangeException"> Thrown when one or more arguments are outside
    /// the required range. </exception>
    ///
    /// <param name="content"> The content. </param>
    /// <param name="length"> (Optional) The length. </param>
    ///
    /// <returns> The random string, or null on error. </returns>
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    public static string GenerateRandomString(RandomStringContent content, uint length = 10)
    {
    var permittedCharacters = string.Empty;

    switch (content)
    {
    case RandomStringContent.LettersOnly:
    permittedCharacters = kLetterCharacters;
    break;
    case RandomStringContent.DigitsOnly:
    permittedCharacters = kDigitCharacters;
    break;
    case RandomStringContent.AlphaNumeric:
    permittedCharacters = kAlphaNumericCharacters;
    break;
    default:
    throw new ArgumentOutOfRangeException(nameof(content), content, null);
    }

    return new string(
    Enumerable
    // Permitted characters and length
    .Repeat(permittedCharacters, (int) length)
    .Select(
    s =>
    {
    var cryptoResult = new byte[4];
    new RNGCryptoServiceProvider()
    .GetBytes(cryptoResult);
    return
    s[
    new Random(
    BitConverter
    .ToInt32(
    cryptoResult,
    0))
    .Next(s.Length)];
    }).ToArray());
    }

    #endregion
    }
    }