예제 #1
0
        private static T Parse <T>(IValueHelper <T> valueHelper, string s, out T n, out T d)
            where T : struct, IComparable, IFormattable, IConvertible, IComparable <T>, IEquatable <T>
        {
            if (s == null)
            {
                throw new ArgumentNullException("s");
            }

            if (s.Length == 0)
            {
                throw new FormatException("Input string was empty.");
            }

            Match m = FractionParseRegex.Match(s);

            if (!m.Success)
            {
                throw new FormatException("Input string was not in a correct format.");
            }

            T w;

            if (m.Groups["w"].Success)
            {
                if (!valueHelper.TryParse(m.Groups["w"].Value, out w))
                {
                    throw new FormatException("Whole number in input string was not in a correct format.");
                }
            }
            else
            {
                w = valueHelper.Zero;
            }
            if (m.Groups["n"].Success)
            {
                if (!valueHelper.TryParse(m.Groups["n"].Value, out n))
                {
                    throw new FormatException("Numerator in input string was not in a correct format.");
                }
                if (!valueHelper.TryParse(m.Groups["d"].Value, out d))
                {
                    throw new FormatException("Denominator in input string was not in a correct format.");
                }
            }
            else
            {
                n = valueHelper.Zero;
                d = valueHelper.PositiveOne;
            }
            return(w);
        }
예제 #2
0
        private static bool TryParse <T>(IValueHelper <T> valueHelper, string s, out T w, out T n, out T d)
            where T : struct, IComparable, IFormattable, IConvertible, IComparable <T>, IEquatable <T>
        {
            Match m;

            if (String.IsNullOrEmpty(s) || !(m = FractionParseRegex.Match(s)).Success)
            {
                w = valueHelper.Zero;
                n = valueHelper.Zero;
                d = valueHelper.PositiveOne;
                return(false);
            }
            if (m.Groups["w"].Success)
            {
                if (!valueHelper.TryParse(m.Groups["w"].Value, out w))
                {
                    n = valueHelper.Zero;
                    d = valueHelper.PositiveOne;
                    return(false);
                }
            }
            else
            {
                w = valueHelper.Zero;
            }
            if (m.Groups["n"].Success)
            {
                if (!valueHelper.TryParse(m.Groups["n"].Value, out n))
                {
                    d = valueHelper.PositiveOne;
                    return(false);
                }
                if (!valueHelper.TryParse(m.Groups["d"].Value, out d))
                {
                    return(false);
                }
            }
            else
            {
                n = valueHelper.Zero;
                d = valueHelper.PositiveOne;
            }

            return(true);
        }