Example #1
0
        public static T FastReadParse <T>(Ps <char> str, JsonFormatterOptions options)
        {
            if (typeof(T) == typeof(DateTime))
            {
                if (DateTimeHelper.TryParseISODateTime(str.Pointer, str.Length, out DateTime date_time))
                {
                    return(As <DateTime, T>(date_time));
                }
            }
            else if (typeof(T) == typeof(DateTimeOffset))
            {
                if (DateTimeHelper.TryParseISODateTime(str.Pointer, str.Length, out DateTimeOffset date_time_offset))
                {
                    As <DateTimeOffset, T>(date_time_offset);
                }
            }
            else if (typeof(T) == typeof(Guid))
            {
                var(_, length, value) = NumberHelper.ParseGuid(str.Pointer, str.Length);

                if (length == str.Length)
                {
                    As <Guid, T>(value);
                }
            }
            else if (typeof(T) == typeof(long))
            {
                var(_, length, value) = NumberHelper.DecimalParseInt64(str.Pointer, str.Length);

                if (length == str.Length)
                {
                    As <long, T>(value);
                }
            }
            else if (typeof(T) == typeof(ulong))
            {
                var(_, length, value) = NumberHelper.DecimalParseUInt64(str.Pointer, str.Length);

                if (length == str.Length)
                {
                    As <ulong, T>(value);
                }
            }
            else if (typeof(T) == typeof(double))
            {
                if ((options & JsonFormatterOptions.UseSystemFloatingPointsMethods) != 0)
                {
#if Span
                    if (double.TryParse(str, out var value))
                    {
                        return(As <double, T>(value));
                    }
#endif
                }
                else
                {
                    var(_, length, value) = NumberHelper.DecimalParseDouble(str.Pointer, str.Length);

                    if (length == str.Length)
                    {
                        As <double, T>(value);
                    }
                }
            }
            else if (typeof(T) == typeof(decimal))
            {
                var(_, length, value) = NumberHelper.ParseDecimal(str.Pointer, str.Length);

                if (length == str.Length)
                {
                    As <decimal, T>(value);
                }
            }
            else if (typeof(T) == typeof(RWPathInfo))
            {
                return(As <object, T>(ParseReference(str.Pointer, str.Length)));
            }

            if (typeof(T) == typeof(long) ||
                typeof(T) == typeof(ulong) ||
                typeof(T) == typeof(double) ||
                typeof(T) == typeof(decimal))
            {
                var numberInfo = NumberHelper.GetNumberInfo(str.Pointer, str.Length);

                if (numberInfo.End == str.Length && numberInfo.IsNumber && numberInfo.IsCommonRadix(out var radix))
                {
                    if (typeof(T) == typeof(long))
                    {
                        return(As <long, T>(numberInfo.ToInt64(radix)));
                    }
                    if (typeof(T) == typeof(ulong))
                    {
                        return(As <ulong, T>(numberInfo.ToUInt64(radix)));
                    }
                    if (typeof(T) == typeof(double))
                    {
                        return(As <double, T>(numberInfo.ToDouble(radix)));
                    }

                    if (typeof(T) == typeof(decimal) && numberInfo.IsDecimal)
                    {
                        return(As <decimal, T>(numberInfo.ToDecimal()));
                    }
                }
            }

            return(SlowReadParse <T>(str.ToStringEx()));
        }