Exemple #1
0
        private TimeSpan GetTimeout(HttpRequest request)
        {
            if (NumericTypeParser <double> .TryParse(request.Headers[HeaderNames.RequestTimeout], out var seconds))
            {
                return(seconds.Seconds());
            }

            return(ObtainFromProviders(request, options.AdditionalTimeoutProviders) ?? options.DefaultTimeoutProvider(request));
        }
        public static bool TryParse(string input, out TimeSpan result)
        {
            input = PrepareInput(input);

            if (TimeSpan.TryParse(input, out result))
            {
                return(true);
            }

            bool TryParse(string unit, out double res) => NumericTypeParser <double> .TryParse(PrepareInput(input, unit), out res);

            return
                (TryGet(TimeSpan.FromMilliseconds, MilliSeconds4, out result) ||
                 TryGet(TimeSpan.FromMilliseconds, MilliSeconds3, out result) ||
                 TryGet(TimeSpan.FromSeconds, Seconds4, out result) ||
                 TryGet(TimeSpan.FromSeconds, Seconds3, out result) ||
                 TryGet(TimeSpan.FromMinutes, Minutes4, out result) ||
                 TryGet(TimeSpan.FromMinutes, Minutes3, out result) ||
                 TryGet(TimeSpan.FromHours, Hours3, out result) ||
                 TryGet(TimeSpan.FromHours, Hours2, out result) ||
                 TryGet(TimeSpan.FromDays, Days3, out result) ||
                 TryGet(TimeSpan.FromDays, Days2, out result) ||
                 TryGet(TimeSpan.FromMilliseconds, MilliSeconds2, out result) ||
                 TryGet(TimeSpan.FromSeconds, Seconds2, out result) ||
                 TryGet(TimeSpan.FromMinutes, Minutes2, out result) ||
                 TryGet(TimeSpan.FromMilliseconds, MilliSeconds1, out result) ||
                 TryGet(TimeSpan.FromSeconds, Seconds1, out result) ||
                 TryGet(TimeSpan.FromMinutes, Minutes1, out result) ||
                 TryGet(TimeSpan.FromHours, Hours1, out result) ||
                 TryGet(TimeSpan.FromDays, Days1, out result));

            bool TryGet(FromValue method, string unit, out TimeSpan res)
            {
                res = default;
                if (!input.Contains(unit))
                {
                    return(false);
                }
                if (!TryParse(unit, out var val))
                {
                    return(false);
                }
                res = method(val);
                return(true);
            }
        }
Exemple #3
0
        public static bool TryParse(string input, out DataSize result)
        {
            input = input.ToLower();

            bool TryParse(string unit, out double res) => NumericTypeParser <double> .TryParse(PrepareInput(input, unit), out res);
            bool TryParseLong(string unit, out long res) => long.TryParse(PrepareInput(input, unit), out res);

            bool TryGet(FromDouble method, string unit, out DataSize res)
            {
                res = default;
                if (!input.Contains(unit))
                {
                    return(false);
                }
                if (!TryParse(unit, out var val))
                {
                    return(false);
                }
                res = method(val);
                return(true);
            }

            bool TryGetL(FromLong method, string unit, out DataSize res)
            {
                res = default;
                if (!input.Contains(unit))
                {
                    return(false);
                }
                if (!TryParseLong(unit, out var val))
                {
                    return(false);
                }
                res = method(val);
                return(true);
            }

            if (TryGet(DataSize.FromPetabytes, Petabytes2, out result) ||
                TryGet(DataSize.FromTerabytes, Terabytes2, out result) ||
                TryGet(DataSize.FromGigabytes, Gigabytes2, out result) ||
                TryGet(DataSize.FromMegabytes, Megabytes2, out result) ||
                TryGet(DataSize.FromKilobytes, Kilobytes2, out result) ||
                TryGetL(DataSize.FromBytes, Bytes2, out result) ||
                TryGet(DataSize.FromPetabytes, Petabytes1, out result) ||
                TryGet(DataSize.FromTerabytes, Terabytes1, out result) ||
                TryGet(DataSize.FromGigabytes, Gigabytes1, out result) ||
                TryGet(DataSize.FromMegabytes, Megabytes1, out result) ||
                TryGet(DataSize.FromKilobytes, Kilobytes1, out result) ||
                TryGetL(DataSize.FromBytes, Bytes1, out result))
            {
                return(true);
            }

            if (long.TryParse(input, out var bytes))
            {
                result = DataSize.FromBytes(bytes);
                return(true);
            }

            return(false);
        }
Exemple #4
0
 public void TryParse_should_return_false_for_invalid_input(string input)
 {
     NumericTypeParser <double> .TryParse(input, out _).Should().BeFalse();
 }
Exemple #5
0
 public void TryParse_should_throw_exception_for_invalid_number_type()
 {
     new Action(() => NumericTypeParser <object> .TryParse("123", out _)).Should().Throw <NotSupportedException>();
 }
Exemple #6
0
        public void Should_parse_float_in_different_formats(string input, float expected)
        {
            NumericTypeParser <float> .TryParse(input, out var result).Should().BeTrue();

            result.Should().BeApproximately(expected, 0.0001f);
        }
Exemple #7
0
        public void Should_parse_decimal_in_different_formats(string input, double expected)
        {
            NumericTypeParser <decimal> .TryParse(input, out var result).Should().BeTrue();

            result.Should().BeApproximately((decimal)expected, 0.0001m);
        }