예제 #1
0
        /// <summary>
        /// Return tif rhs is equal to this.
        /// </summary>
        ///
        /// <param name="rhs">the PluralRules to compare to.</param>
        /// <returns>true if this and rhs are equal.</returns>
        /// @draft ICU 3.8
        /// @provisional This API might change or be removed in a future release.
        public bool Equals(PluralRules rhs)
        {
            if (rhs == null)
            {
                return(false);
            }
            if (rhs == this)
            {
                return(true);
            }
            if (!rhs.GetKeywords().Equals(keywords))
            {
                return(false);
            }

            int limit = Math.Max(GetRepeatLimit(), rhs.GetRepeatLimit());

            for (int i = 0; i < limit; ++i)
            {
                if (!Select(i).Equals(rhs.Select(i)))
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #2
0
        /// <summary>
        /// Sets the pattern used by this plural format. The method parses the
        /// pattern and creates a map of format strings for the plural rules.
        /// Patterns and their interpretation are specified in the class description.
        /// </summary>
        ///
        /// <param name="pattern_0">the pattern for this plural format.</param>
        /// <exception cref="IllegalArgumentException">if the pattern is invalid.</exception>
        /// @draft ICU 3.8
        /// @provisional This API might change or be removed in a future release.
        public void ApplyPattern(String pattern_0)
        {
            this.pattern = pattern_0;
            int braceStack = 0;

            ILOG.J2CsMapping.Collections.ISet ruleNames = pluralRules.GetKeywords();
            parsedValues = new Hashtable();

            // Format string has to include keywords.
            // states:
            // 0: Reading keyword.
            // 1: Reading value for preceding keyword.
            int           state                 = 0;
            StringBuilder token                 = new StringBuilder();
            String        currentKeyword        = null;
            bool          readSpaceAfterKeyword = false;

            for (int i = 0; i < pattern_0.Length; ++i)
            {
                char ch = pattern_0[i];
                switch (state)
                {
                case 0:     // Reading value.
                    if (token.Length == 0)
                    {
                        readSpaceAfterKeyword = false;
                    }
                    if (IBM.ICU.Impl.UCharacterProperty.IsRuleWhiteSpace(ch))
                    {
                        if (token.Length > 0)
                        {
                            readSpaceAfterKeyword = true;
                        }
                        // Skip leading and trailing whitespaces.
                        break;
                    }
                    if (ch == '{')       // End of keyword definition reached.
                    {
                        currentKeyword = token.ToString().ToLower(System.Globalization.CultureInfo.CreateSpecificCulture("en"));
                        if (!ILOG.J2CsMapping.Collections.Collections.Contains(currentKeyword, ruleNames))
                        {
                            ParsingFailure("Malformed formatting expression. "
                                           + "Unknown keyword \"" + currentKeyword
                                           + "\" at position " + i + ".");
                        }
                        if (ILOG.J2CsMapping.Collections.Collections.Get(parsedValues, currentKeyword) != null)
                        {
                            ParsingFailure("Malformed formatting expression. "
                                           + "Text for case \"" + currentKeyword
                                           + "\" at position " + i + " already defined!");
                        }
                        token.Remove(0, token.Length - (0));
                        braceStack++;
                        state = 1;
                        break;
                    }
                    if (readSpaceAfterKeyword)
                    {
                        ParsingFailure("Malformed formatting expression. "
                                       + "Invalid keyword definition. Character \"" + ch
                                       + "\" at position " + i + " not expected!");
                    }
                    token.Append(ch);
                    break;

                case 1:     // Reading value.
                    switch ((int)ch)
                    {
                    case '{':
                        braceStack++;
                        token.Append(ch);
                        break;

                    case '}':
                        braceStack--;
                        if (braceStack == 0)       // End of value reached.
                        {
                            ILOG.J2CsMapping.Collections.Collections.Put(parsedValues, currentKeyword, token.ToString());
                            token.Remove(0, token.Length - (0));
                            state = 0;
                        }
                        else if (braceStack < 0)
                        {
                            ParsingFailure("Malformed formatting expression. "
                                           + "Braces do not match.");
                        }
                        else         // braceStack > 0
                        {
                            token.Append(ch);
                        }
                        break;

                    default:
                        token.Append(ch);
                        break;
                    }
                    break;
                } // switch state
            }     // for loop.
            if (braceStack != 0)
            {
                ParsingFailure("Malformed formatting expression. Braces do not match.");
            }
            CheckSufficientDefinition();
        }