示例#1
0
        public bool IsValid(string name)
        {
            string id = name;

            if (RequiredPrefixes != null && RequiredPrefixes.Length > 0)
            {
                var prefix = RequiredPrefixes.FirstOrDefault(p => id.StartsWith(p));
                if (prefix == null)
                {
                    return(false);
                }
                id = id.Substring(prefix.Length);
            }
            else if (ForbiddenPrefixes != null && ForbiddenPrefixes.Length > 0)
            {
                if (ForbiddenPrefixes.Any(p => id.StartsWith(p)))
                {
                    return(false);
                }
            }

            if (RequiredSuffixes != null && RequiredSuffixes.Length > 0)
            {
                var suffix = RequiredSuffixes.FirstOrDefault(s => id.EndsWith(s));
                if (suffix == null)
                {
                    return(false);
                }
                id = id.Substring(0, id.Length - suffix.Length);
            }
            else if (ForbiddenSuffixes != null && ForbiddenSuffixes.Length > 0)
            {
                if (ForbiddenSuffixes.Any(p => id.EndsWith(p)))
                {
                    return(false);
                }
            }

            switch (NamingStyle)
            {
            case NamingStyle.AllLower:
                return(!id.Any(ch => char.IsLetter(ch) && char.IsUpper(ch)));

            case NamingStyle.AllUpper:
                return(!id.Any(ch => char.IsLetter(ch) && char.IsLower(ch)));

            case NamingStyle.CamelCase:
                return(id.Length == 0 || (char.IsLower(id [0]) && NoUnderscoreWithoutNumber(id)));

            case NamingStyle.PascalCase:
                return(id.Length == 0 || (char.IsUpper(id [0]) && NoUnderscoreWithoutNumber(id)));

            case NamingStyle.FirstUpper:
                return(id.Length == 0 && char.IsUpper(id [0]) && !id.Skip(1).Any(ch => char.IsLetter(ch) && char.IsUpper(ch)));
            }
            return(true);
        }
示例#2
0
        public bool IsValid(string name)
        {
            string id          = name;
            bool   foundPrefix = false;

            if (RequiredPrefixes != null && RequiredPrefixes.Length > 0)
            {
                var prefix = RequiredPrefixes.FirstOrDefault(p => id.StartsWith(p, StringComparison.Ordinal));
                if (prefix == null)
                {
                    return(false);
                }
                id          = id.Substring(prefix.Length);
                foundPrefix = true;
            }

            if (!foundPrefix && AllowedPrefixes != null && AllowedPrefixes.Length > 0)
            {
                var prefix = AllowedPrefixes.FirstOrDefault(p => id.StartsWith(p, StringComparison.Ordinal));
                if (prefix != null)
                {
                    id          = id.Substring(prefix.Length);
                    foundPrefix = true;
                }
            }

            if (!foundPrefix && ForbiddenPrefixes != null && ForbiddenPrefixes.Length > 0)
            {
                if (ForbiddenPrefixes.Any(p => id.StartsWith(p, StringComparison.Ordinal)))
                {
                    return(false);
                }
            }

            if (RequiredSuffixes != null && RequiredSuffixes.Length > 0)
            {
                var suffix = RequiredSuffixes.FirstOrDefault(s => id.EndsWith(s, StringComparison.Ordinal));
                if (suffix == null)
                {
                    return(false);
                }
                id = id.Substring(0, id.Length - suffix.Length);
            }
            else if (ForbiddenSuffixes != null && ForbiddenSuffixes.Length > 0)
            {
                if (ForbiddenSuffixes.Any(p => id.EndsWith(p, StringComparison.Ordinal)))
                {
                    return(false);
                }
            }

            switch (NamingStyle)
            {
            case NamingStyle.AllLower:
                for (int i = 0; i < id.Length; i++)
                {
                    char ch = id[i];
                    if (ch == '_' && !HandleUnderscore(UnderscoreHandling.Allow, id, ref i))
                    {
                        return(false);
                    }
                    if (char.IsLetter(ch) && char.IsUpper(ch))
                    {
                        return(false);
                    }
                }
                return(true);

            case NamingStyle.AllUpper:
                for (int i = 0; i < id.Length; i++)
                {
                    char ch = id[i];
                    if (ch == '_' && !HandleUnderscore(UnderscoreHandling.Allow, id, ref i))
                    {
                        return(false);
                    }
                    if (char.IsLetter(ch) && char.IsLower(ch))
                    {
                        return(false);
                    }
                }
                return(true);

            case NamingStyle.CamelCase:
                if (id.Length > 0)
                {
                    if (char.IsUpper(id[0]) || id[0] == '_')
                    {
                        return(false);
                    }
                    for (int i = 1; i < id.Length; i++)
                    {
                        char ch = id[i];
                        if (ch == '_' && !HandleUnderscore(UnderscoreHandling.Forbid, id, ref i))
                        {
                            return(false);
                        }
                    }
                }
                return(true);

            case NamingStyle.CamelCaseWithLowerLetterUnderscore:
                if (id.Length > 0)
                {
                    if (char.IsUpper(id[0]) || id[0] == '_')
                    {
                        return(false);
                    }
                    for (int i = 1; i < id.Length; i++)
                    {
                        char ch = id[i];
                        if (ch == '_' && !HandleUnderscore(UnderscoreHandling.AllowWithLowerStartingLetter, id, ref i))
                        {
                            return(false);
                        }
                    }
                }
                return(true);

            case NamingStyle.CamelCaseWithUpperLetterUnderscore:
                if (id.Length > 0)
                {
                    if (char.IsUpper(id[0]) || id[0] == '_')
                    {
                        return(false);
                    }
                    for (int i = 1; i < id.Length; i++)
                    {
                        char ch = id[i];
                        if (ch == '_' && !HandleUnderscore(UnderscoreHandling.AllowWithUpperStartingLetter, id, ref i))
                        {
                            return(false);
                        }
                    }
                }
                return(true);

            case NamingStyle.PascalCase:
                if (id.Length > 0)
                {
                    if (char.IsLower(id[0]) || id[0] == '_')
                    {
                        return(false);
                    }
                    for (int i = 1; i < id.Length; i++)
                    {
                        char ch = id[i];
                        if (ch == '_' && !HandleUnderscore(UnderscoreHandling.Forbid, id, ref i))
                        {
                            return(false);
                        }
                    }
                }
                return(true);

            case NamingStyle.PascalCaseWithLowerLetterUnderscore:
                if (id.Length > 0)
                {
                    if (char.IsLower(id[0]) || id[0] == '_')
                    {
                        return(false);
                    }
                    for (int i = 1; i < id.Length; i++)
                    {
                        char ch = id[i];
                        if (ch == '_' && !HandleUnderscore(UnderscoreHandling.AllowWithLowerStartingLetter, id, ref i))
                        {
                            return(false);
                        }
                    }
                }
                return(true);

            case NamingStyle.PascalCaseWithUpperLetterUnderscore:
                if (id.Length > 0)
                {
                    if (char.IsLower(id[0]) || id[0] == '_')
                    {
                        return(false);
                    }
                    for (int i = 1; i < id.Length; i++)
                    {
                        char ch = id[i];
                        if (ch == '_' && !HandleUnderscore(UnderscoreHandling.AllowWithUpperStartingLetter, id, ref i))
                        {
                            return(false);
                        }
                    }
                }
                return(true);

            case NamingStyle.FirstUpper:
                if (id.Length > 0)
                {
                    if (char.IsLower(id[0]) || id[0] == '_')
                    {
                        return(false);
                    }

                    for (int i = 1; i < id.Length; i++)
                    {
                        char ch = id[i];
                        if (ch == '_' && !HandleUnderscore(UnderscoreHandling.Allow, id, ref i))
                        {
                            return(false);
                        }
                        if (char.IsLetter(ch) && char.IsUpper(ch))
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
            return(true);
        }