/src/glaze/include/glaze/util/non_finite.hpp
Line | Count | Source |
1 | | // Glaze Library |
2 | | // For the license information refer to glaze.hpp |
3 | | |
4 | | #pragma once |
5 | | |
6 | | #include <cmath> |
7 | | #include <concepts> |
8 | | #include <cstddef> |
9 | | #include <limits> |
10 | | #include <string_view> |
11 | | |
12 | | #include "glaze/util/dump.hpp" |
13 | | #include "glaze/util/inline.hpp" |
14 | | |
15 | | // Helpers for formats that spell non-finite floats as tokens. The JSON number writer emits null for |
16 | | // these values, so formats with their own spellings handle them before falling through to it. |
17 | | |
18 | | namespace glz::detail |
19 | | { |
20 | | // Reads inf or nan, optionally signed with '+' or '-', when the token ends at `end` or at a character |
21 | | // for which `is_terminator` returns true. These are the TOML spellings, which the CSV format reuses. |
22 | | // Returns false and leaves `it` unchanged otherwise. |
23 | | template <std::floating_point T> |
24 | | constexpr bool parse_non_finite_float(T& value, auto& it, auto end, auto&& is_terminator) noexcept |
25 | 3.73k | { |
26 | 3.73k | auto p = it; |
27 | 3.73k | bool negative = false; |
28 | 3.73k | if (p != end && (*p == '+' || *p == '-')) { |
29 | 1.35k | negative = *p == '-'; |
30 | 1.35k | ++p; |
31 | 1.35k | } |
32 | 3.73k | if (end - p < 3) { |
33 | 126 | return false; |
34 | 126 | } |
35 | | |
36 | 3.60k | const bool inf = p[0] == 'i' && p[1] == 'n' && p[2] == 'f'; |
37 | 3.60k | if (not inf && not(p[0] == 'n' && p[1] == 'a' && p[2] == 'n')) { |
38 | 537 | return false; |
39 | 537 | } |
40 | 3.06k | p += 3; |
41 | 3.06k | if (p != end && not is_terminator(*p)) { |
42 | 35 | return false; |
43 | 35 | } |
44 | | |
45 | 3.03k | value = inf ? std::numeric_limits<T>::infinity() : std::numeric_limits<T>::quiet_NaN(); |
46 | 3.03k | if (negative) { |
47 | 835 | value = -value; |
48 | 835 | } |
49 | 3.03k | it = p; |
50 | 3.03k | return true; |
51 | 3.06k | } _ZN3glz6detail22parse_non_finite_floatITkNSt3__114floating_pointEfPKcS4_RKZNS_4fromILj10000EfE2opITnDaXtlNS_8opts_csvELj10000ELh1ELb1ELb0ELc44EEERS4_RfTkNS_10is_contextERNS_7contextES4_EEvOT1_OT2_OT0_T3_EUlcE_EEbRT_RSH_SD_SG_ Line | Count | Source | 25 | 1.99k | { | 26 | 1.99k | auto p = it; | 27 | 1.99k | bool negative = false; | 28 | 1.99k | if (p != end && (*p == '+' || *p == '-')) { | 29 | 732 | negative = *p == '-'; | 30 | 732 | ++p; | 31 | 732 | } | 32 | 1.99k | if (end - p < 3) { | 33 | 57 | return false; | 34 | 57 | } | 35 | | | 36 | 1.93k | const bool inf = p[0] == 'i' && p[1] == 'n' && p[2] == 'f'; | 37 | 1.93k | if (not inf && not(p[0] == 'n' && p[1] == 'a' && p[2] == 'n')) { | 38 | 268 | return false; | 39 | 268 | } | 40 | 1.66k | p += 3; | 41 | 1.66k | if (p != end && not is_terminator(*p)) { | 42 | 16 | return false; | 43 | 16 | } | 44 | | | 45 | 1.65k | value = inf ? std::numeric_limits<T>::infinity() : std::numeric_limits<T>::quiet_NaN(); | 46 | 1.65k | if (negative) { | 47 | 421 | value = -value; | 48 | 421 | } | 49 | 1.65k | it = p; | 50 | 1.65k | return true; | 51 | 1.66k | } |
_ZN3glz6detail22parse_non_finite_floatITkNSt3__114floating_pointEfPKcS4_RKZNS_4fromILj10000EfE2opITnDaXtlNS_8opts_csvELj10000ELh0ELb1ELb0ELc44EEERS4_RfTkNS_10is_contextERNS_7contextES4_EEvOT1_OT2_OT0_T3_EUlcE_EEbRT_RSH_SD_SG_ Line | Count | Source | 25 | 1.73k | { | 26 | 1.73k | auto p = it; | 27 | 1.73k | bool negative = false; | 28 | 1.73k | if (p != end && (*p == '+' || *p == '-')) { | 29 | 619 | negative = *p == '-'; | 30 | 619 | ++p; | 31 | 619 | } | 32 | 1.73k | if (end - p < 3) { | 33 | 69 | return false; | 34 | 69 | } | 35 | | | 36 | 1.66k | const bool inf = p[0] == 'i' && p[1] == 'n' && p[2] == 'f'; | 37 | 1.66k | if (not inf && not(p[0] == 'n' && p[1] == 'a' && p[2] == 'n')) { | 38 | 269 | return false; | 39 | 269 | } | 40 | 1.40k | p += 3; | 41 | 1.40k | if (p != end && not is_terminator(*p)) { | 42 | 19 | return false; | 43 | 19 | } | 44 | | | 45 | 1.38k | value = inf ? std::numeric_limits<T>::infinity() : std::numeric_limits<T>::quiet_NaN(); | 46 | 1.38k | if (negative) { | 47 | 414 | value = -value; | 48 | 414 | } | 49 | 1.38k | it = p; | 50 | 1.38k | return true; | 51 | 1.40k | } |
|
52 | | |
53 | | // Writes a non-finite float using a format's spellings and returns true. Returns false, writing |
54 | | // nothing, for a finite value. A NaN is written without its sign. |
55 | | template <class B> |
56 | | GLZ_ALWAYS_INLINE bool write_non_finite_float(const std::floating_point auto value, const std::string_view nan, |
57 | | const std::string_view inf, const std::string_view negative_inf, B& b, |
58 | | size_t& ix) |
59 | | { |
60 | | if (std::isfinite(value)) [[likely]] { |
61 | | return false; |
62 | | } |
63 | | if (std::isnan(value)) { |
64 | | dump(nan, b, ix); |
65 | | } |
66 | | else { |
67 | | dump(value < 0 ? negative_inf : inf, b, ix); |
68 | | } |
69 | | return true; |
70 | | } |
71 | | } |