//////////////////////////////////////////////////////////////////////////////////////////////////// // file: Extensions\StringExtensions.cs // // summary: Implements the string extensions class //////////////////////////////////////////////////////////////////////////////////////////////////// using System; using System.Linq; using System.Security.Cryptography; namespace TVLib.Extensions { //////////////////////////////////////////////////////////////////////////////////////////////////// /// String extensions. /// /// Mike Gardner 12/05/16. //////////////////////////////////////////////////////////////////////////////////////////////////// public static class StringExtensions { #region ENUMERATIONS //////////////////////////////////////////////////////////////////////////////////////////////////// /// Values that represent random string contents. /// /// Mike Gardner 12/05/16. //////////////////////////////////////////////////////////////////////////////////////////////////// public enum RandomStringContent : long { /// An enum constant representing the alpha option. LettersOnly = 0, /// An enum constant representing the numeric option. DigitsOnly = 1, /// An enum constant representing the alphanumeric option. AlphaNumeric = 2 } #endregion #region CONSTANTS /// The alpha numeric characters. private const string kAlphaNumericCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; /// The digit characters. private const string kDigitCharacters = "0123456789"; /// The letter characters. private const string kLetterCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; #endregion #region ALL OTHER MEMBERS //////////////////////////////////////////////////////////////////////////////////////////////////// /// /// Remove all escape sequences from a string and replace them with a specified character. By /// default, the character is nothing. /// /// /// Mike Gardner 12/09/16. /// /// Text to sanitize. /// (Optional) Character used to replace each escape sequence. /// /// Null on error. //////////////////////////////////////////////////////////////////////////////////////////////////// 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; } //////////////////////////////////////////////////////////////////////////////////////////////////// /// A string extension method that converts a text "boolean" to a boolean. /// /// Mike Gardner 12/12/16. /// /// Text to sanitize. /// /// A boolean value or null on error. //////////////////////////////////////////////////////////////////////////////////////////////////// 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; } //////////////////////////////////////////////////////////////////////////////////////////////////// /// Generates a random string of a desired length. /// /// /// Mike Gardner 12/05/16. The random string is not guaranteed to be unique. /// /// /// Thrown when one or more arguments are outside /// the required range. /// /// The content. /// (Optional) The length. /// /// The random string, or null on error. //////////////////////////////////////////////////////////////////////////////////////////////////// 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 } }