Пример #1
0
        public static bool IsValidCSharpIdentifier(string toTest)
        {
            if (toTest.Length == 0) // obviously..?
            {
                return(false);
            }

            if (SingleType.IsCSharpKeyword(toTest)) // best to avoid any complications
            {
                return(false);
            }

            char[] letters = toTest.ToCharArray();
            char   first   = letters[0];

            if (first != '_' && !validFirstChars.Contains(CharUnicodeInfo.GetUnicodeCategory(first)))
            {
                return(false);
            }

            foreach (char c in letters)
            {
                if (!validCSharpChars.Contains(CharUnicodeInfo.GetUnicodeCategory(c)))
                {
                    return(false);
                }
            }

            return(true);

            // TODO: fix this regex method, replace the above method with it,
            // and get rid of the three UnicodeCategory arrays
            // (regex method found at https://stackoverflow.com/a/1904462/4036588)

            //const string start = @"(\p{Lu}|\p{Ll}|\p{Lt}|\p{Lm}|\p{Lo}|\p{Nl})";
            //const string extend = @"(\p{Mn}|\p{Mc}|\p{Nd}|\p{Pc}|\p{Cf})";
            //Regex ident = new Regex(string.Format("{0}({0}|{1})*", start, extend));
            //toTest = toTest.Normalize();
            //return ident.IsMatch(toTest);
        }