Esempio n. 1
0
        private static Boolean TryParse(ReadOnlySpan <char> s, NumberStyles style, NumberFormatInfo info, out Single result)
        {
            bool success = Number.TryParseSingle(s, style, info, out result);

            if (!success)
            {
                ReadOnlySpan <char> sTrim = s.Trim();
                if (StringSpanHelpers.Equals(sTrim, info.PositiveInfinitySymbol))
                {
                    result = PositiveInfinity;
                }
                else if (StringSpanHelpers.Equals(sTrim, info.NegativeInfinitySymbol))
                {
                    result = NegativeInfinity;
                }
                else if (StringSpanHelpers.Equals(sTrim, info.NaNSymbol))
                {
                    result = NaN;
                }
                else
                {
                    return(false); // We really failed
                }
            }
            return(true);
        }
Esempio n. 2
0
        internal unsafe static double ParseDouble(ReadOnlySpan <char> value, NumberStyles options, NumberFormatInfo numfmt)
        {
            NumberBuffer number = default;
            double       d      = 0;

            if (!TryStringToNumber(value, options, ref number, numfmt, false))
            {
                //If we failed TryStringToNumber, it may be from one of our special strings.
                //Check the three with which we're concerned and rethrow if it's not one of
                //those strings.
                ReadOnlySpan <char> sTrim = value.Trim();
                if (StringSpanHelpers.Equals(sTrim, numfmt.PositiveInfinitySymbol))
                {
                    return(double.PositiveInfinity);
                }
                if (StringSpanHelpers.Equals(sTrim, numfmt.NegativeInfinitySymbol))
                {
                    return(double.NegativeInfinity);
                }
                if (StringSpanHelpers.Equals(sTrim, numfmt.NaNSymbol))
                {
                    return(double.NaN);
                }
                throw new FormatException(SR.Format_InvalidString);
            }

            if (!NumberBufferToDouble(ref number, ref d))
            {
                throw new OverflowException(SR.Overflow_Double);
            }

            return(d);
        }
Esempio n. 3
0
        public static bool TryParse(ReadOnlySpan <char> value, out bool result)
        {
            ReadOnlySpan <char> trueSpan = TrueLiteral.AsReadOnlySpan();

            if (StringSpanHelpers.Equals(trueSpan, value, StringComparison.OrdinalIgnoreCase))
            {
                result = true;
                return(true);
            }

            ReadOnlySpan <char> falseSpan = FalseLiteral.AsReadOnlySpan();

            if (StringSpanHelpers.Equals(falseSpan, value, StringComparison.OrdinalIgnoreCase))
            {
                result = false;
                return(true);
            }

            // Special case: Trim whitespace as well as null characters.
            value = TrimWhiteSpaceAndNull(value);

            if (StringSpanHelpers.Equals(trueSpan, value, StringComparison.OrdinalIgnoreCase))
            {
                result = true;
                return(true);
            }

            if (StringSpanHelpers.Equals(falseSpan, value, StringComparison.OrdinalIgnoreCase))
            {
                result = false;
                return(true);
            }

            result = false;
            return(false);
        }