MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / try_as_double

Function try_as_double

include/jsoncons/basic_json.hpp:3785–3839  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3783 }
3784
3785 conversion_result<double> try_as_double() const
3786 {
3787 using result_type = conversion_result<double>;
3788
3789 switch (storage_kind())
3790 {
3791 case json_storage_kind::short_str:
3792 case json_storage_kind::long_str:
3793 {
3794 double x{0};
3795 const char_type* s = as_cstring();
3796 std::size_t len = as_string_view().length();
3797 if (JSONCONS_UNLIKELY(len > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')))
3798 {
3799 auto result = jsoncons::hexstr_to_double(s, len, x);
3800 if (result.ec == std::errc::invalid_argument)
3801 {
3802 return result_type(jsoncons::unexpect, conv_errc::not_double);
3803 }
3804 }
3805 else if (JSONCONS_UNLIKELY(len > 3 && s[0] == '-' && s[1] == '0' && (s[2] == 'x' || s[2] == 'X')))
3806 {
3807 auto result = jsoncons::hexstr_to_double(s, len, x);
3808 if (result.ec == std::errc::invalid_argument)
3809 {
3810 return result_type(jsoncons::unexpect, conv_errc::not_double);
3811 }
3812 }
3813 else
3814 {
3815 auto result = jsoncons::decstr_to_double(as_cstring(), as_string_view().length(), x);
3816 if (result.ec == std::errc::invalid_argument)
3817 {
3818 return result_type(jsoncons::unexpect, conv_errc::not_double);
3819 }
3820 }
3821
3822 return result_type(x);
3823 }
3824 case json_storage_kind::half_float:
3825 return result_type(binary::decode_half(cast<half_storage>().value()));
3826 case json_storage_kind::float64:
3827 return result_type(cast<double_storage>().value());
3828 case json_storage_kind::int64:
3829 return result_type(static_cast<double>(cast<int64_storage>().value()));
3830 case json_storage_kind::uint64:
3831 return result_type(static_cast<double>(cast<uint64_storage>().value()));
3832 case json_storage_kind::const_json_ref:
3833 return cast<const_json_ref_storage>().value().try_as_double();
3834 case json_storage_kind::json_ref:
3835 return cast<json_ref_storage>().value().try_as_double();
3836 default:
3837 return result_type(jsoncons::unexpect, conv_errc::not_double);
3838 }
3839 }
3840
3841 double as_double() const
3842 {

Callers 1

as_doubleFunction · 0.85

Calls 8

storage_kindFunction · 0.85
as_cstringFunction · 0.85
as_string_viewFunction · 0.85
hexstr_to_doubleFunction · 0.85
decstr_to_doubleFunction · 0.85
decode_halfFunction · 0.50
lengthMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected