public static bool IsMatch(this char c, CharType type) { if (type.HasFlag(CharType.Space)) { if (Char.IsWhiteSpace(c)) { return(true); } } if (type.HasFlag(CharType.NonSpace)) { if (!Char.IsWhiteSpace(c)) { return(true); } } if (type.HasFlag(CharType.Digit)) { if (Char.IsDigit(c)) { return(true); } } if (type.HasFlag(CharType.Letter)) { if (Char.IsLetter(c)) { return(true); } } //if (type.HasFlag(CharType.Dot)) if (c == '.') return true; return(false); }
public string RandomString(int mintLength, int maxLength, CharType charType = CharType.AlphaUpper | CharType.AlphaLower | CharType.Numeric | CharType.Special) { var length = Random.Next(mintLength, maxLength); StringBuilder sb = new StringBuilder(length); var types = (charType.HasFlag(CharType.AlphaUpper) ? 1 : 0) + (charType.HasFlag(CharType.AlphaLower) ? 1 : 0) + (charType.HasFlag(CharType.Numeric) ? 1 : 0) + (charType.HasFlag(CharType.Special) ? 1 : 0); var probabilityAlphaUpper = charType.HasFlag(CharType.AlphaUpper) ? length - types : 0; var probabilityAlphaLower = charType.HasFlag(CharType.AlphaLower) ? length - types : 0; var probabilityNumeric = charType.HasFlag(CharType.Numeric) ? length - types : 0; var probabilitySpecial = charType.HasFlag(CharType.Special) ? length - types : 0; while (length > 0) { Char c = default(Char); var roll = Random.Next(probabilityAlphaUpper + probabilityAlphaLower + probabilityNumeric + probabilitySpecial); if (roll > probabilityAlphaUpper + probabilityAlphaLower + probabilityNumeric) { c = CharSet.Special[Random.Next(CharSet.Special.Length)]; --probabilitySpecial; } else if (roll > probabilityAlphaUpper + probabilityAlphaLower) { c = CharSet.Numeric[Random.Next(CharSet.Numeric.Length)]; --probabilityNumeric; } else if (roll > probabilityAlphaUpper) { c = Char.ToUpper(CharSet.Alpha[Random.Next(CharSet.Alpha.Length)]); --probabilityAlphaUpper; } else { c = Char.ToLower(CharSet.Alpha[Random.Next(CharSet.Alpha.Length)]); --probabilityAlphaLower; } sb.Append(c); --length; } return(sb.ToString()); }
public static bool IsMatch(this char c, CharType type) { if (type.HasFlag(CharType.Inverse)) { return(!c.IsMatch(type ^ CharType.Inverse)); } if (type.HasFlag(CharType.Space)) { if (Char.IsWhiteSpace(c)) { return(true); } } if (type.HasFlag(CharType.NonSpace)) { if (!Char.IsWhiteSpace(c)) { return(true); } } if (type.HasFlag(CharType.Digit)) { if (Char.IsDigit(c)) { return(true); } } if (type.HasFlag(CharType.Letter)) { if (Char.IsLetter(c)) { return(true); } } if (type.HasFlag(CharType.LeftBracket)) { if (c == '(') { return(true); } } if (type.HasFlag(CharType.RightBracket)) { if (c == ')') { return(true); } } if (type.HasFlag(CharType.LeftSquareBracket)) { if (c == '[') { return(true); } } if (type.HasFlag(CharType.RightSquareBracket)) { if (c == ']') { return(true); } } return(false); }
public static bool CharIsType(char C, CharType T) { CharType CT = GetCharType(C); return(CT == T || T.HasFlag(CT)); }