DAW JSON Link
daw_fp_fallback.h
Go to the documentation of this file.
1// Copyright (c) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/
7//
8
9#pragma once
10
11#include "daw_json_assert.h"
12
13#include <daw/daw_attributes.h>
14#include <daw/daw_cpp_feature_check.h>
15
16#if not defined( DAW_JSON_USE_STRTOD ) and defined( __cpp_lib_to_chars )
17#include <charconv>
18#endif
19
20namespace daw::json {
21 inline namespace DAW_JSON_VER {
22 namespace json_details {
23
24 template<typename Real, std::enable_if_t<std::is_floating_point_v<Real>,
25 std::nullptr_t> = nullptr>
26 DAW_ATTRIB_NOINLINE Real parse_with_strtod( char const *first,
27 char const *last ) {
28#if not defined( DAW_JSON_USE_STRTOD ) and defined( __cpp_lib_to_chars )
29 Real result;
30 std::from_chars_result fc_res = std::from_chars( first, last, result );
31 if( fc_res.ec == std::errc::result_out_of_range ) {
32 if( *first == '-' ) {
33 return -std::numeric_limits<Real>::infinity( );
34 }
35 return std::numeric_limits<Real>::infinity( );
36 }
37 daw_json_assert( fc_res.ec == std::errc( ),
38 ErrorReason::InvalidNumber );
39 return result;
40#else
41 (void)last;
42 char **end = nullptr;
43 if constexpr( std::is_same_v<Real, float> ) {
44 return static_cast<Real>( strtof( first, end ) );
45 } else if( std::is_same_v<Real, double> ) {
46 return static_cast<Real>( strtod( first, end ) );
47 } else {
48 return static_cast<Real>( strtold( first, end ) );
49 }
50#endif
51 }
52 } // namespace json_details
53 } // namespace DAW_JSON_VER
54} // namespace daw::json
#define daw_json_assert(Bool,...)
Definition: daw_json_assert.h:179
DAW_ATTRIB_NOINLINE Real parse_with_strtod(char const *first, char const *last)
Definition: daw_fp_fallback.h:26
Definition: daw_from_json.h:22
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition: version.h:16