コード例 #1
0
ファイル: Single.cs プロジェクト: anydream/coreclr
        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);
        }
コード例 #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);
        }
コード例 #3
0
ファイル: Boolean.cs プロジェクト: wecing/coreclr
        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);
        }