src/Misc/EFTools/EntityDesignModel/ModelHelper.cs
/// <summary>
///     Returns a string that can be used as TSimpleIdentifier.
/// </summary>
Returns a string that can be used as TSimpleIdentifier.
Returns a string that can be used as TSimpleIdentifier .
internal static string CreateValidSimpleIdentifier(string identifier)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new ArgumentException("Identifier is null or empty!");
            }
            var isFirst = true;
            var requiresPrefix = false;
            var charsToReplace = new List<char>();
            var firstRegex = new Regex(@"[\p{L}\p{Nl}]");
            var subsequentRegex = new Regex(@"[\p{L}\p{Nl}\p{Nd}\p{Mn}\p{Mc}\p{Pc}\p{Cf}]");
            foreach (var c in identifier)
            {
                if (isFirst)
                {
                    if (!firstRegex.IsMatch(c.ToString()))
                    {
                        requiresPrefix = true;
                        if (!charsToReplace.Contains(c)
                            && !subsequentRegex.IsMatch(c.ToString()))
                        {
                            charsToReplace.Add(c);
                        }
                    }
                    isFirst = false;
                }
                else
                {
                    if (!charsToReplace.Contains(c)
                        && !subsequentRegex.IsMatch(c.ToString()))
                    {
                        charsToReplace.Add(c);
                    }
                }
            }
            StringBuilder resultStringBuilder;
            if (requiresPrefix)
            {
                resultStringBuilder = new StringBuilder("C");
                resultStringBuilder.Append(identifier);
            }
            else
            {
                resultStringBuilder = new StringBuilder(identifier);
            }
            foreach (var c in charsToReplace)
            {
                resultStringBuilder = resultStringBuilder.Replace(c, '_');
            }
            return resultStringBuilder.ToString();
        }
internal static string CreateValidSimpleIdentifier ( string identifier ) { if ( string . IsNullOrEmpty ( identifier ) ) { throw new ArgumentException ( " " ) ; } var isFirst = true ; var requiresPrefix = false ; var charsToReplace = new List < char > ( ) ; var firstRegex = new Regex ( @"[\p{L}\p{Nl}]" ) ; var subsequentRegex = new Regex ( @"[\p{L}\p{Nl}\p{Nd}\p{Mn}\p{Mc}\p{Pc}\p{Cf}]" ) ; foreach ( var c in identifier ) { if ( isFirst ) { if ( ! firstRegex . IsMatch ( c . ToString ( ) ) ) { requiresPrefix = true ; if ( ! charsToReplace . Contains ( c ) && ! subsequentRegex . IsMatch ( c . ToString ( ) ) ) { charsToReplace . Add ( c ) ; } } isFirst = false ; } else { if ( ! charsToReplace . Contains ( c ) && ! subsequentRegex . IsMatch ( c . ToString ( ) ) ) { charsToReplace . Add ( c ) ; } } } StringBuilder resultStringBuilder ; if ( requiresPrefix ) { resultStringBuilder = new StringBuilder ( " " ) ; resultStringBuilder . Append ( identifier ) ; } else { resultStringBuilder = new StringBuilder ( identifier ) ; } foreach ( var c in charsToReplace ) { resultStringBuilder = resultStringBuilder . Replace ( c , ' ' ) ; } return resultStringBuilder . ToString ( ) ; }
Returns a string that can be used as TSimpleIdentifier.
Returns a string that can be used as TSimpleIdentifier .
// Replace anything that doesn't adhere to the ECMA specification for identifiers with an underscore, // unless it's the first which the schema definition says must be a letter... // System.Data.Resource.CSMSL_2.xsd - [\p{L}\p{Nl}][\p{L}\p{Nl}\p{Nd}\p{Mn}\p{Mc}\p{Pc}\p{Cf}]{0,} // If we're adding a prefix, this character is no longer the first so we need to match it // against the second regex // Need to start with a letter... just going to copy what EF is doing, not sure if there are any better alternatives...
identifier
string