using System; using System.Net.Mail; using System.Text.RegularExpressions; using Random = UnityEngine.Random; namespace DTT.Utils.Workflow { /// /// Provides utility methods for strings. /// public static class StringUtility { /// /// The regular expression used for determining whether a string is a valid web url. /// public static readonly Regex webUrlRegex = new Regex(@"((www.?)|(https:\/\/))[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)", RegexOptions.Compiled); /// /// The regular expression used for determining whether a string is a valid hexadecimal. /// public static readonly Regex hexadecimalRegex = new Regex(@"^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$", RegexOptions.Compiled); /// /// The regular expression used for determining whether a string corresponds with a variable name /// usable in the CSharp programming language. /// public static readonly Regex variableRegex = new Regex(@"^[a-zA-Z0-9_]+$", RegexOptions.Compiled); /// /// Returns whether the given string is a valid email address. /// /// The email address to check. /// Whether the given string is a valid email address. public static bool IsEmail(string emailAddress) { try { MailAddress m = new MailAddress(emailAddress); return true; } catch (FormatException) { return false; } } /// /// Returns whether a string is a valid web url. /// /// The web url to check. /// Whether the string is a valid web url. public static bool IsWebUrl(string webUrl) { if (webUrl == null) throw new ArgumentNullException(nameof(webUrl)); return webUrlRegex.IsMatch(webUrl); } /// /// Returns whether the string can be used as a valid variable name that doesn't cause compile errors. /// Useful for code generation scripts. /// /// The name to check. /// Whether the name can be used. public static bool IsVariableName(string variableName) { if (variableName == null) throw new ArgumentNullException(nameof(variableName)); return variableRegex.IsMatch(variableName); } /// /// Returns whether a string is a valid hexadecimal. /// /// The string to check. /// Whether the string is a valid hexadecimal. public static bool IsHexadecimal(string hexadecimalString) { if (hexadecimalString == null) throw new ArgumentNullException(nameof(hexadecimalString)); return hexadecimalRegex.IsMatch(hexadecimalString); } /// /// Generates a random insecure string which can be used when needing dummy data to test. /// /// The length of the string. /// The random insecure string. public static string RandomInsecure(int length) => RandomInsecure(length, null); /// /// Generates a random insecure string which can be used when needing dummy data to test. /// /// The length of the string. /// The seed to initialize the 'Random' state with. /// The random insecure string. public static string RandomInsecure(int length, int? seed) { if (length < 0) throw new ArgumentOutOfRangeException(nameof(length)); Random.State randomState = Random.state; if (seed.HasValue) Random.InitState(seed.Value); const string SELECTION = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; char[] result = new char[length]; for (int i = 0; i < result.Length; i++) result[i] = SELECTION[Random.Range(0, SELECTION.Length)]; if(seed.HasValue) Random.state = randomState; return new string(result); } } }