Beispiel #1
0
 /// <summary>
 /// Parses the specified string starting at the index specified by
 /// <paramref name="position"/>. If the <paramref name="str"/> is successfully parsed then the index of
 /// the <see cref="ParsePosition"/> is updated to the index following the parsed
 /// text. On error, the index is unchanged and the error index of
 /// <see cref="ParsePosition"/> is set to the index where the error occurred.
 /// </summary>
 /// <param name="str">the string to parse.</param>
 /// <param name="position">input/output parameter, specifies the start index in
 /// <paramref name="str"/> from where to start parsing. If parsing is
 /// successful, it is updated with the index following the parsed
 /// text; on error, the index is unchanged and the error index is
 /// set to the index where the error occurred.</param>
 /// <returns>The object resulting from the parse or <c>null</c> if there is
 /// an error.</returns>
 public abstract object ParseObject(string str, ParsePosition position);
Beispiel #2
0
        /// <summary>
        /// Parses the pattern to determine new strings and ranges for this
        /// <see cref="ChoiceFormat"/>.
        /// </summary>
        /// <param name="template">The pattern of strings and ranges.</param>
        /// <exception cref="ArgumentException">If an error occurs while parsing the pattern.</exception>
        public void ApplyPattern(string template)
        {
            double[]      limits = new double[5];
            List <string> formats = new List <string>();
            int           length = template.Length, limitCount = 0, index = 0;
            StringBuffer  buffer   = new StringBuffer();
            NumberFormat  format   = NumberFormat.GetInstance(CultureInfo.InvariantCulture);
            ParsePosition position = new ParsePosition(0);

            while (true)
            {
                index = SkipWhitespace(template, index);
                if (index >= length)
                {
                    if (limitCount == limits.Length)
                    {
                        choiceLimits = limits;
                    }
                    else
                    {
                        choiceLimits = new double[limitCount];
                        System.Array.Copy(limits, 0, choiceLimits, 0, limitCount);
                    }
                    choiceFormats = new String[formats.Count];
                    for (int i = 0; i < formats.Count; i++)
                    {
                        choiceFormats[i] = formats[i];
                    }
                    return;
                }
                position.Index = index;
                object value = format.Parse(template, position);

                index = SkipWhitespace(template, position.Index);
                if (position.ErrorIndex != -1 || index >= length)
                {
                    // Fix Harmony 540
                    choiceLimits  = new double[0];
                    choiceFormats = new string[0];
                    return;
                }
                char ch = template[index++];
                if (limitCount == limits.Length)
                {
                    double[] newLimits = new double[limitCount * 2];
                    System.Array.Copy(limits, 0, newLimits, 0, limitCount);
                    limits = newLimits;
                }
                double next;
                switch (ch)
                {
                case '#':
                case '\u2264':
                    next = Convert.ToDouble(value);
                    break;

                case '<':
                    next = NextDouble(Convert.ToDouble(value));
                    break;

                default:
                    throw new ArgumentException();     // ICU4N TODO: Shouldn't this be FormatException in .NET?
                }
                if (limitCount > 0 && next <= limits[limitCount - 1])
                {
                    throw new ArgumentException(); // ICU4N TODO: Shouldn't this be FormatException in .NET?
                }
                buffer.Length  = (0);
                position.Index = (index);
                UpTo(template, position, buffer, '|');
                index = position.Index;
                limits[limitCount++] = next;
                formats.Add(buffer.ToString());
            }
        }