converter_numeric.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright Kevlin Henney, 2000-2005.
  2. // Copyright Alexander Nasonov, 2006-2010.
  3. // Copyright Antony Polukhin, 2011-2025.
  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 - 2016
  17. #ifndef BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP
  18. #define BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP
  19. #include <boost/config.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. # pragma once
  22. #endif
  23. #include <type_traits>
  24. #include <boost/core/cmath.hpp>
  25. #include <boost/limits.hpp>
  26. #include <boost/lexical_cast/detail/type_traits.hpp>
  27. namespace boost { namespace detail {
  28. template <class Source, class Target>
  29. bool ios_numeric_comparer_float(Source x, Source y) noexcept {
  30. return x == y
  31. || (boost::core::isnan(x) && boost::core::isnan(y))
  32. || (x < (std::numeric_limits<Target>::min)())
  33. ;
  34. }
  35. template <class RangeType, class T>
  36. constexpr bool is_out_of_range_for(T value) noexcept {
  37. return value > static_cast<T>((std::numeric_limits<RangeType>::max)())
  38. || value < static_cast<T>((std::numeric_limits<RangeType>::min)())
  39. || boost::core::isnan(value);
  40. }
  41. // integral -> integral
  42. template <typename Target, typename Source>
  43. typename std::enable_if<
  44. !std::is_floating_point<Source>::value && !std::is_floating_point<Target>::value, bool
  45. >::type noexcept_numeric_convert(Source arg, Target& result) noexcept {
  46. const Target target_tmp = static_cast<Target>(arg);
  47. const Source arg_restored = static_cast<Source>(target_tmp);
  48. if (arg == arg_restored) {
  49. result = target_tmp;
  50. return true;
  51. }
  52. return false;
  53. }
  54. // integral -> floating point
  55. template <typename Target, typename Source>
  56. typename std::enable_if<
  57. !std::is_floating_point<Source>::value && std::is_floating_point<Target>::value, bool
  58. >::type noexcept_numeric_convert(Source arg, Target& result) noexcept {
  59. const Target target_tmp = static_cast<Target>(arg);
  60. result = target_tmp;
  61. return true;
  62. }
  63. // floating point -> floating point
  64. template <typename Target, typename Source>
  65. typename std::enable_if<
  66. std::is_floating_point<Source>::value && std::is_floating_point<Target>::value, bool
  67. >::type noexcept_numeric_convert(Source arg, Target& result) noexcept {
  68. const Target target_tmp = static_cast<Target>(arg);
  69. const Source arg_restored = static_cast<Source>(target_tmp);
  70. if (detail::ios_numeric_comparer_float<Source, Target>(arg, arg_restored)) {
  71. result = target_tmp;
  72. return true;
  73. }
  74. return false;
  75. }
  76. // floating point -> integral
  77. template <typename Target, typename Source>
  78. typename std::enable_if<
  79. std::is_floating_point<Source>::value && !std::is_floating_point<Target>::value, bool
  80. >::type noexcept_numeric_convert(Source arg, Target& result) noexcept {
  81. if (detail::is_out_of_range_for<Target>(arg)) {
  82. return false;
  83. }
  84. const Target target_tmp = static_cast<Target>(arg);
  85. const Source arg_restored = static_cast<Source>(target_tmp);
  86. if (arg == arg_restored /* special values are handled in detail::is_out_of_range_for */) {
  87. result = target_tmp;
  88. return true;
  89. }
  90. return false;
  91. }
  92. struct lexical_cast_dynamic_num_not_ignoring_minus
  93. {
  94. template <typename Target, typename Source>
  95. static inline bool try_convert(Source arg, Target& result) noexcept {
  96. return boost::detail::noexcept_numeric_convert<Target, Source >(arg, result);
  97. }
  98. };
  99. struct lexical_cast_dynamic_num_ignoring_minus
  100. {
  101. template <typename Target, typename Source>
  102. #if defined(__clang__) && (__clang_major__ > 3 || __clang_minor__ > 6)
  103. __attribute__((no_sanitize("unsigned-integer-overflow")))
  104. #endif
  105. static inline bool try_convert(Source arg, Target& result) noexcept {
  106. typedef typename std::conditional<
  107. std::is_floating_point<Source>::value,
  108. std::conditional<true, Source, Source>, // std::type_identity emulation
  109. boost::detail::lcast::make_unsigned<Source>
  110. >::type usource_lazy_t;
  111. typedef typename usource_lazy_t::type usource_t;
  112. if (arg < 0) {
  113. const bool res = boost::detail::noexcept_numeric_convert<Target, usource_t>(
  114. static_cast<usource_t>(0u - static_cast<usource_t>(arg)), result
  115. );
  116. result = static_cast<Target>(0u - result);
  117. return res;
  118. } else {
  119. return boost::detail::noexcept_numeric_convert<Target, usource_t>(arg, result);
  120. }
  121. }
  122. };
  123. /*
  124. * dynamic_num_converter_impl follows the rules:
  125. * 1) If Source can be converted to Target without precision loss and
  126. * without overflows, then assign Source to Target and return
  127. *
  128. * 2) If Source is less than 0 and Target is an unsigned integer,
  129. * then negate Source, check the requirements of rule 1) and if
  130. * successful, assign static_casted Source to Target and return
  131. *
  132. * 3) Otherwise throw a bad_lexical_cast exception
  133. *
  134. *
  135. * Rule 2) required because boost::lexical_cast has the behavior of
  136. * stringstream, which uses the rules of scanf for conversions. And
  137. * in the C99 standard for unsigned input value minus sign is
  138. * optional, so if a negative number is read, no errors will arise
  139. * and the result will be the two's complement.
  140. */
  141. template <typename Target, typename Source>
  142. struct dynamic_num_converter_impl
  143. {
  144. static inline bool try_convert(Source arg, Target& result) noexcept {
  145. typedef typename std::conditional<
  146. boost::detail::lcast::is_unsigned<Target>::value &&
  147. (boost::detail::lcast::is_signed<Source>::value || std::is_floating_point<Source>::value) &&
  148. !(std::is_same<Source, bool>::value) &&
  149. !(std::is_same<Target, bool>::value),
  150. lexical_cast_dynamic_num_ignoring_minus,
  151. lexical_cast_dynamic_num_not_ignoring_minus
  152. >::type caster_type;
  153. return caster_type::try_convert(arg, result);
  154. }
  155. };
  156. }} // namespace boost::detail
  157. #endif // BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP