1// Copyright Kevlin Henney, 2000-2005.
2// Copyright Alexander Nasonov, 2006-2010.
3// Copyright Antony Polukhin, 2011-2020.
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 <cstddef>
31#include <cstring>
32#include <boost/limits.hpp>
33#include <boost/detail/workaround.hpp>
34#include <boost/math/special_functions/sign.hpp>
35#include <boost/math/special_functions/fpclassify.hpp>
36
37#include <boost/lexical_cast/detail/lcast_char_constants.hpp>
38
39namespace boost {
40 namespace detail
41 {
42 template <class CharT>
43 bool lc_iequal(const CharT* val, const CharT* lcase, const CharT* ucase, unsigned int len) BOOST_NOEXCEPT {
44 for( unsigned int i=0; i < len; ++i ) {
45 if ( val[i] != lcase[i] && val[i] != ucase[i] ) return false;
46 }
47
48 return true;
49 }
50
51 /* Returns true and sets the correct value if found NaN or Inf. */
52 template <class CharT, class T>
53 inline bool parse_inf_nan_impl(const CharT* begin, const CharT* end, T& value
54 , const CharT* lc_NAN, const CharT* lc_nan
55 , const CharT* lc_INFINITY, const CharT* lc_infinity
56 , const CharT opening_brace, const CharT closing_brace) BOOST_NOEXCEPT
57 {
58 using namespace std;
59 if (begin == end) return false;
60 const CharT minus = lcast_char_constants<CharT>::minus;
61 const CharT plus = lcast_char_constants<CharT>::plus;
62 const int inifinity_size = 8; // == sizeof("infinity") - 1
63
64 /* Parsing +/- */
65 bool const has_minus = (*begin == minus);
66 if (has_minus || *begin == plus) {
67 ++ begin;
68 }
69
70 if (end - begin < 3) return false;
71 if (lc_iequal(begin, lc_nan, lc_NAN, 3)) {
72 begin += 3;
73 if (end != begin) {
74 /* It is 'nan(...)' or some bad input*/
75
76 if (end - begin < 2) return false; // bad input
77 -- end;
78 if (*begin != opening_brace || *end != closing_brace) return false; // bad input
79 }
80
81 if( !has_minus ) value = std::numeric_limits<T>::quiet_NaN();
82 else value = (boost::math::changesign) (std::numeric_limits<T>::quiet_NaN());
83 return true;
84 } else if (
85 ( /* 'INF' or 'inf' */
86 end - begin == 3 // 3 == sizeof('inf') - 1
87 && lc_iequal(begin, lc_infinity, lc_INFINITY, 3)
88 )
89 ||
90 ( /* 'INFINITY' or 'infinity' */
91 end - begin == inifinity_size
92 && lc_iequal(begin, lc_infinity, lc_INFINITY, inifinity_size)
93 )
94 )
95 {
96 if( !has_minus ) value = std::numeric_limits<T>::infinity();
97 else value = (boost::math::changesign) (std::numeric_limits<T>::infinity());
98 return true;
99 }
100
101 return false;
102 }
103
104 template <class CharT, class T>
105 bool put_inf_nan_impl(CharT* begin, CharT*& end, const T& value
106 , const CharT* lc_nan
107 , const CharT* lc_infinity) BOOST_NOEXCEPT
108 {
109 using namespace std;
110 const CharT minus = lcast_char_constants<CharT>::minus;
111 if ((boost::math::isnan)(value)) {
112 if ((boost::math::signbit)(value)) {
113 *begin = minus;
114 ++ begin;
115 }
116
117 memcpy(begin, lc_nan, 3 * sizeof(CharT));
118 end = begin + 3;
119 return true;
120 } else if ((boost::math::isinf)(value)) {
121 if ((boost::math::signbit)(value)) {
122 *begin = minus;
123 ++ begin;
124 }
125
126 memcpy(begin, lc_infinity, 3 * sizeof(CharT));
127 end = begin + 3;
128 return true;
129 }
130
131 return false;
132 }
133
134
135#ifndef BOOST_LCAST_NO_WCHAR_T
136 template <class T>
137 bool parse_inf_nan(const wchar_t* begin, const wchar_t* end, T& value) BOOST_NOEXCEPT {
138 return parse_inf_nan_impl(begin, end, value
139 , L"NAN", L"nan"
140 , L"INFINITY", L"infinity"
141 , L'(', L')');
142 }
143
144 template <class T>
145 bool put_inf_nan(wchar_t* begin, wchar_t*& end, const T& value) BOOST_NOEXCEPT {
146 return put_inf_nan_impl(begin, end, value, L"nan", L"infinity");
147 }
148
149#endif
150#if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
151 template <class T>
152 bool parse_inf_nan(const char16_t* begin, const char16_t* end, T& value) BOOST_NOEXCEPT {
153 return parse_inf_nan_impl(begin, end, value
154 , u"NAN", u"nan"
155 , u"INFINITY", u"infinity"
156 , u'(', u')');
157 }
158
159 template <class T>
160 bool put_inf_nan(char16_t* begin, char16_t*& end, const T& value) BOOST_NOEXCEPT {
161 return put_inf_nan_impl(begin, end, value, u"nan", u"infinity");
162 }
163#endif
164#if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
165 template <class T>
166 bool parse_inf_nan(const char32_t* begin, const char32_t* end, T& value) BOOST_NOEXCEPT {
167 return parse_inf_nan_impl(begin, end, value
168 , U"NAN", U"nan"
169 , U"INFINITY", U"infinity"
170 , U'(', U')');
171 }
172
173 template <class T>
174 bool put_inf_nan(char32_t* begin, char32_t*& end, const T& value) BOOST_NOEXCEPT {
175 return put_inf_nan_impl(begin, end, value, U"nan", U"infinity");
176 }
177#endif
178
179 template <class CharT, class T>
180 bool parse_inf_nan(const CharT* begin, const CharT* end, T& value) BOOST_NOEXCEPT {
181 return parse_inf_nan_impl(begin, end, value
182 , "NAN", "nan"
183 , "INFINITY", "infinity"
184 , '(', ')');
185 }
186
187 template <class CharT, class T>
188 bool put_inf_nan(CharT* begin, CharT*& end, const T& value) BOOST_NOEXCEPT {
189 return put_inf_nan_impl(begin, end, value, "nan", "infinity");
190 }
191 }
192} // namespace boost
193
194#undef BOOST_LCAST_NO_WCHAR_T
195
196#endif // BOOST_LEXICAL_CAST_DETAIL_INF_NAN_HPP
197
198

source code of include/boost/lexical_cast/detail/inf_nan.hpp