System.Xml.Xsl.Runtime.TokenInfo.CreateFormat C# (CSharp) Méthode

CreateFormat() public static méthode

public static CreateFormat ( string formatString, int startIdx, int tokLen ) : TokenInfo
formatString string
startIdx int
tokLen int
Résultat TokenInfo
        public static TokenInfo CreateFormat(string formatString, int startIdx, int tokLen) {
            Debug.Assert(startIdx >= 0 && tokLen > 0);
            TokenInfo token = new TokenInfo();
            token.formatString = null;
            token.length = 1;

            bool useDefault = false;
            char ch = formatString[startIdx];

            switch (ch) {
            case '1':
            case 'A':
            case 'I':
            case 'a':
            case 'i':
                break;
            default:
                // NOTE: We do not support Tamil and Ethiopic numbering systems having no zeros
                if (CharUtil.IsDecimalDigitOne(ch)) {
                    break;
                }
                if (CharUtil.IsDecimalDigitOne((char)(ch + 1))) {
                    // Leading zeros request padding.  Track how much.
                    int idx = startIdx;
                    do {
                        token.length++;
                    } while (--tokLen > 0 && ch == formatString[++idx]);

                    // Recognize the token only if the next character is "one"
                    if (formatString[idx] == ++ch) {
                        break;
                    }
                }
                useDefault = true;
                break;
            }

            if (tokLen != 1) {
                // If remaining token length is not 1, do not recognize the token
                useDefault = true;
            }

            if (useDefault) {
                // Default to Arabic numbering with no zero padding
                token.startChar = NumberFormatter.DefaultStartChar;
                token.length = 1;
            } else {
                token.startChar = ch;
            }
            return token;
        }
    }

Usage Example

        // Creates a Format object parsing format string into format tokens (alphanumeric) and separators (non-alphanumeric).
        public NumberFormatter(string formatString, int lang, string letterValue, string groupingSeparator, int groupingSize)
        {
            Debug.Assert(groupingSeparator.Length <= 1);
            _formatString      = formatString;
            _lang              = lang;
            _letterValue       = letterValue;
            _groupingSeparator = groupingSeparator;
            _groupingSize      = groupingSeparator.Length > 0 ? groupingSize : 0;

            if (formatString == "1" || formatString.Length == 0)
            {
                // Special case of the default format
                return;
            }

            _tokens = new List <TokenInfo>();
            int  idxStart       = 0;
            bool isAlphaNumeric = CharUtil.IsAlphaNumeric(formatString[idxStart]);

            if (isAlphaNumeric)
            {
                // If the first one is alpha num add empty separator as a prefix
                _tokens.Add(null);
            }

            for (int idx = 0; idx <= formatString.Length; idx++)
            {
                // Loop until a switch from formatString token to separator is detected (or vice-versa)
                if (idx == formatString.Length || isAlphaNumeric != CharUtil.IsAlphaNumeric(formatString[idx]))
                {
                    if (isAlphaNumeric)
                    {
                        // Just finished a format token
                        _tokens.Add(TokenInfo.CreateFormat(formatString, idxStart, idx - idxStart));
                    }
                    else
                    {
                        // Just finished a separator token
                        _tokens.Add(TokenInfo.CreateSeparator(formatString, idxStart, idx - idxStart));
                    }

                    // Begin parsing the next format token or separator
                    idxStart = idx;

                    // Flip flag from format token to separator or vice-versa
                    isAlphaNumeric = !isAlphaNumeric;
                }
            }
        }