| 1 | // Copyright Kevlin Henney, 2000-2005. |
| 2 | // Copyright Alexander Nasonov, 2006-2010. |
| 3 | // Copyright Antony Polukhin, 2011-2023. |
| 4 | // |
| 5 | // Distributed under the Boost Software License, Version 1.0. (See |
| 6 | // accompanying file LICENSE_1_0.txt or copy at |
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
| 8 | // |
| 9 | // what: lexical_cast custom keyword cast |
| 10 | // who: contributed by Kevlin Henney, |
| 11 | // enhanced with contributions from Terje Slettebo, |
| 12 | // with additional fixes and suggestions from Gennaro Prota, |
| 13 | // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, |
| 14 | // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann, |
| 15 | // Cheng Yang, Matthew Bradbury, David W. Birdsall, Pavel Korzh and other Boosters |
| 16 | // when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2014 |
| 17 | |
| 18 | #ifndef BOOST_LEXICAL_CAST_DETAIL_INF_NAN_HPP |
| 19 | #define BOOST_LEXICAL_CAST_DETAIL_INF_NAN_HPP |
| 20 | |
| 21 | #include <boost/config.hpp> |
| 22 | #ifdef BOOST_HAS_PRAGMA_ONCE |
| 23 | # pragma once |
| 24 | #endif |
| 25 | |
| 26 | #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING) |
| 27 | #define BOOST_LCAST_NO_WCHAR_T |
| 28 | #endif |
| 29 | |
| 30 | #include <boost/limits.hpp> |
| 31 | #include <boost/detail/workaround.hpp> |
| 32 | #include <boost/core/cmath.hpp> |
| 33 | #include <cstddef> |
| 34 | #include <cstring> |
| 35 | |
| 36 | #include <boost/lexical_cast/detail/lcast_char_constants.hpp> |
| 37 | |
| 38 | namespace boost { |
| 39 | namespace detail |
| 40 | { |
| 41 | template <class CharT> |
| 42 | bool lc_iequal(const CharT* val, const CharT* lcase, const CharT* ucase, unsigned int len) BOOST_NOEXCEPT { |
| 43 | for( unsigned int i=0; i < len; ++i ) { |
| 44 | if ( val[i] != lcase[i] && val[i] != ucase[i] ) return false; |
| 45 | } |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | /* Returns true and sets the correct value if found NaN or Inf. */ |
| 51 | template <class CharT, class T> |
| 52 | inline bool parse_inf_nan_impl(const CharT* begin, const CharT* end, T& value |
| 53 | , const CharT* lc_NAN, const CharT* lc_nan |
| 54 | , const CharT* lc_INFINITY, const CharT* lc_infinity |
| 55 | , const CharT opening_brace, const CharT closing_brace) BOOST_NOEXCEPT |
| 56 | { |
| 57 | if (begin == end) return false; |
| 58 | const CharT minus = lcast_char_constants<CharT>::minus; |
| 59 | const CharT plus = lcast_char_constants<CharT>::plus; |
| 60 | const int inifinity_size = 8; // == sizeof("infinity") - 1 |
| 61 | |
| 62 | /* Parsing +/- */ |
| 63 | bool const has_minus = (*begin == minus); |
| 64 | if (has_minus || *begin == plus) { |
| 65 | ++ begin; |
| 66 | } |
| 67 | |
| 68 | if (end - begin < 3) return false; |
| 69 | if (lc_iequal(begin, lc_nan, lc_NAN, 3)) { |
| 70 | begin += 3; |
| 71 | if (end != begin) { |
| 72 | /* It is 'nan(...)' or some bad input*/ |
| 73 | |
| 74 | if (end - begin < 2) return false; // bad input |
| 75 | -- end; |
| 76 | if (*begin != opening_brace || *end != closing_brace) return false; // bad input |
| 77 | } |
| 78 | |
| 79 | if( !has_minus ) value = std::numeric_limits<T>::quiet_NaN(); |
| 80 | else value = boost::core::copysign(std::numeric_limits<T>::quiet_NaN(), static_cast<T>(-1)); |
| 81 | return true; |
| 82 | } else if ( |
| 83 | ( /* 'INF' or 'inf' */ |
| 84 | end - begin == 3 // 3 == sizeof('inf') - 1 |
| 85 | && lc_iequal(begin, lc_infinity, lc_INFINITY, 3) |
| 86 | ) |
| 87 | || |
| 88 | ( /* 'INFINITY' or 'infinity' */ |
| 89 | end - begin == inifinity_size |
| 90 | && lc_iequal(begin, lc_infinity, lc_INFINITY, inifinity_size) |
| 91 | ) |
| 92 | ) |
| 93 | { |
| 94 | if( !has_minus ) value = std::numeric_limits<T>::infinity(); |
| 95 | else value = -std::numeric_limits<T>::infinity(); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | template <class CharT, class T> |
| 103 | bool put_inf_nan_impl(CharT* begin, CharT*& end, const T& value |
| 104 | , const CharT* lc_nan |
| 105 | , const CharT* lc_infinity) BOOST_NOEXCEPT |
| 106 | { |
| 107 | const CharT minus = lcast_char_constants<CharT>::minus; |
| 108 | if (boost::core::isnan(value)) { |
| 109 | if (boost::core::signbit(value)) { |
| 110 | *begin = minus; |
| 111 | ++ begin; |
| 112 | } |
| 113 | |
| 114 | std::memcpy(dest: begin, src: lc_nan, n: 3 * sizeof(CharT)); |
| 115 | end = begin + 3; |
| 116 | return true; |
| 117 | } else if (boost::core::isinf(value)) { |
| 118 | if (boost::core::signbit(value)) { |
| 119 | *begin = minus; |
| 120 | ++ begin; |
| 121 | } |
| 122 | |
| 123 | std::memcpy(dest: begin, src: lc_infinity, n: 3 * sizeof(CharT)); |
| 124 | end = begin + 3; |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | #ifndef BOOST_LCAST_NO_WCHAR_T |
| 133 | template <class T> |
| 134 | bool parse_inf_nan(const wchar_t* begin, const wchar_t* end, T& value) BOOST_NOEXCEPT { |
| 135 | return parse_inf_nan_impl(begin, end, value |
| 136 | , L"NAN" , L"nan" |
| 137 | , L"INFINITY" , L"infinity" |
| 138 | , L'(', L')'); |
| 139 | } |
| 140 | |
| 141 | template <class T> |
| 142 | bool put_inf_nan(wchar_t* begin, wchar_t*& end, const T& value) BOOST_NOEXCEPT { |
| 143 | return put_inf_nan_impl(begin, end, value, L"nan" , L"infinity" ); |
| 144 | } |
| 145 | |
| 146 | #endif |
| 147 | #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS) |
| 148 | template <class T> |
| 149 | bool parse_inf_nan(const char16_t* begin, const char16_t* end, T& value) BOOST_NOEXCEPT { |
| 150 | return parse_inf_nan_impl(begin, end, value |
| 151 | , u"NAN" , u"nan" |
| 152 | , u"INFINITY" , u"infinity" |
| 153 | , u'(', u')'); |
| 154 | } |
| 155 | |
| 156 | template <class T> |
| 157 | bool put_inf_nan(char16_t* begin, char16_t*& end, const T& value) BOOST_NOEXCEPT { |
| 158 | return put_inf_nan_impl(begin, end, value, u"nan" , u"infinity" ); |
| 159 | } |
| 160 | #endif |
| 161 | #if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS) |
| 162 | template <class T> |
| 163 | bool parse_inf_nan(const char32_t* begin, const char32_t* end, T& value) BOOST_NOEXCEPT { |
| 164 | return parse_inf_nan_impl(begin, end, value |
| 165 | , U"NAN" , U"nan" |
| 166 | , U"INFINITY" , U"infinity" |
| 167 | , U'(', U')'); |
| 168 | } |
| 169 | |
| 170 | template <class T> |
| 171 | bool put_inf_nan(char32_t* begin, char32_t*& end, const T& value) BOOST_NOEXCEPT { |
| 172 | return put_inf_nan_impl(begin, end, value, U"nan" , U"infinity" ); |
| 173 | } |
| 174 | #endif |
| 175 | |
| 176 | template <class CharT, class T> |
| 177 | bool parse_inf_nan(const CharT* begin, const CharT* end, T& value) BOOST_NOEXCEPT { |
| 178 | return parse_inf_nan_impl(begin, end, value |
| 179 | , "NAN" , "nan" |
| 180 | , "INFINITY" , "infinity" |
| 181 | , '(', ')'); |
| 182 | } |
| 183 | |
| 184 | template <class CharT, class T> |
| 185 | bool put_inf_nan(CharT* begin, CharT*& end, const T& value) BOOST_NOEXCEPT { |
| 186 | return put_inf_nan_impl(begin, end, value, "nan" , "infinity" ); |
| 187 | } |
| 188 | } |
| 189 | } // namespace boost |
| 190 | |
| 191 | #undef BOOST_LCAST_NO_WCHAR_T |
| 192 | |
| 193 | #endif // BOOST_LEXICAL_CAST_DETAIL_INF_NAN_HPP |
| 194 | |
| 195 | |