예제 #1
0
        private static Exception TryFormulateE164Number(string number, out string formulated)
        {
            formulated = null;
            if (string.IsNullOrEmpty(number) || 3 > number.Length || 65 < number.Length)
            {
                return(new FormatException(ServerStrings.ErrorInvalidPhoneNumberFormat));
            }
            number = UnicodeToAnsiConverter.Convert(number, false, '?');
            StringBuilder stringBuilder = new StringBuilder(number.Length);
            bool          flag          = false;
            int           num           = 0;

            while (number.Length > num)
            {
                char c = number[num];
                if ((!flag && '+' == c) || char.IsDigit(c))
                {
                    flag = true;
                    stringBuilder.Append(number[num]);
                }
                else if (!char.IsWhiteSpace(c) && !E164Number.acceptableSymbolsInNumber.Contains((int)c))
                {
                    return(new FormatException(ServerStrings.ErrorInvalidPhoneNumberFormat));
                }
                num++;
            }
            if (0 < stringBuilder.Length)
            {
                formulated = stringBuilder.ToString().ToLowerInvariant();
                return(null);
            }
            return(new FormatException(ServerStrings.ErrorInvalidPhoneNumberFormat));
        }
예제 #2
0
        private static string GenerateAlias(string input, int desiredLength, bool mapToAnsi, bool skipInvalidCharacter, char invalidCharacterReplacement)
        {
            if (string.IsNullOrEmpty(input))
            {
                throw new ArgumentNullException("input");
            }
            if (AliasHelper.Analyze(invalidCharacterReplacement) != AliasHelper.CharacterCategory.RegularCharacter)
            {
                throw new ArgumentOutOfRangeException("invalidCharacterReplacement", invalidCharacterReplacement, new LocalizedString("DEV BUG: Parameter 'invalidCharacterReplacement' should be category of CharacterCategory.RegularCharacter."));
            }
            if (mapToAnsi)
            {
                input = UnicodeToAnsiConverter.Convert(input, skipInvalidCharacter, invalidCharacterReplacement);
            }
            StringBuilder stringBuilder = new StringBuilder(input.Length);

            AliasHelper.RecognisingState recognisingState = AliasHelper.RecognisingState.Start;
            int num  = 0;
            int num2 = 0;

            while (input.Length > num && desiredLength > num2)
            {
                char c = input[num];
                AliasHelper.CharacterCategory characterCategory = AliasHelper.Analyze(c);
                if (characterCategory != AliasHelper.CharacterCategory.WhiteSpaceSymbol)
                {
                    switch (recognisingState)
                    {
                    case AliasHelper.RecognisingState.Start:
                        switch (characterCategory)
                        {
                        case AliasHelper.CharacterCategory.RegularCharacter:
                            recognisingState = AliasHelper.RecognisingState.Terminal;
                            break;

                        case AliasHelper.CharacterCategory.InvalidCharacter:
                        case AliasHelper.CharacterCategory.DotSymbol:
                            if (skipInvalidCharacter)
                            {
                                goto IL_149;
                            }
                            c = invalidCharacterReplacement;
                            recognisingState = AliasHelper.RecognisingState.Terminal;
                            break;
                        }
                        break;

                    case AliasHelper.RecognisingState.Terminal:
                        switch (characterCategory)
                        {
                        case AliasHelper.CharacterCategory.InvalidCharacter:
                            if (skipInvalidCharacter)
                            {
                                goto IL_149;
                            }
                            c = invalidCharacterReplacement;
                            break;

                        case AliasHelper.CharacterCategory.DotSymbol:
                            if (input.Length - 1 == num || desiredLength - 1 == num2)
                            {
                                if (skipInvalidCharacter)
                                {
                                    goto IL_149;
                                }
                                c = invalidCharacterReplacement;
                            }
                            else
                            {
                                recognisingState = AliasHelper.RecognisingState.Middle;
                            }
                            break;
                        }
                        break;

                    case AliasHelper.RecognisingState.Middle:
                        switch (characterCategory)
                        {
                        case AliasHelper.CharacterCategory.RegularCharacter:
                            recognisingState = AliasHelper.RecognisingState.Terminal;
                            break;

                        case AliasHelper.CharacterCategory.InvalidCharacter:
                        case AliasHelper.CharacterCategory.DotSymbol:
                            if (skipInvalidCharacter && input.Length - 1 > num && desiredLength - 1 > num2)
                            {
                                goto IL_149;
                            }
                            c = invalidCharacterReplacement;
                            recognisingState = AliasHelper.RecognisingState.Terminal;
                            break;
                        }
                        break;
                    }
                    stringBuilder.Append(c);
                    num2++;
                }
IL_149:
                num++;
            }
            if (AliasHelper.RecognisingState.Terminal != recognisingState)
            {
                return(null);
            }
            return(stringBuilder.ToString());
        }