public static string GenerateRandomString(int length, CharKinds kinds = CharKinds.UpperLetter | CharKinds.Digit) { Util.Check(kinds != CharKinds.None, "CharKinds may not be empty"); var rnd = new Random(); var chars = new char[length]; for (int i = 0; i < length; i++) { chars[i] = GenerateRandomChar(rnd, kinds); } return(new String(chars)); }
private static CharKinds[] MakeCharClassArray() { CharKinds[] a = new CharKinds[65536]; foreach (char c in backslashChars) { a[c] = CharKinds.Backslash; } foreach (char c in quoteChars) { a[c] = CharKinds.Quote; } return(a); }
private static char GenerateRandomChar(Random random, CharKinds kinds) { var kind = GetRandomKind(random, kinds); switch (kind) { case CharKinds.Digit: return((char)('0' + random.Next(10))); case CharKinds.UpperLetter: return((char)('A' + random.Next(26))); case CharKinds.LowerLetter: return((char)('a' + random.Next(26))); case CharKinds.SpecialChar: default: return(_specialChars[random.Next(_specialChars.Length)]); } }
public static string MySQLEscape(this string value) { if (!IsQuoting(value)) { return(value); } StringBuilder sb = new StringBuilder(); foreach (char c in value) { CharKinds charClass = charClassArray[c]; if (charClass != CharKinds.None) { sb.Append("\\"); } sb.Append(c); } return(sb.ToString()); }
private static CharKinds GetRandomKind(Random random, CharKinds kinds) { Util.Check(kinds != CharKinds.None, "Char kinds may not be empty."); // if it is a single kind, just return it. switch (kinds) { case CharKinds.UpperLetter: case CharKinds.LowerLetter: case CharKinds.Digit: case CharKinds.SpecialChar: return(kinds); } // if more than one, throw dice while (true) { var kind = _allKinds[random.Next(_allKinds.Length)]; if (kinds.IsSet(kind)) { return(kind); } } }
public static bool IsSet(this CharKinds kinds, CharKinds kind) { return((kinds & kind) != 0); }