lcast_unsigned_converters.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 - 2014
  17. #ifndef BOOST_LEXICAL_CAST_DETAIL_LCAST_UNSIGNED_CONVERTERS_HPP
  18. #define BOOST_LEXICAL_CAST_DETAIL_LCAST_UNSIGNED_CONVERTERS_HPP
  19. #include <boost/config.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. # pragma once
  22. #endif
  23. #include <climits>
  24. #include <cstddef>
  25. #include <string>
  26. #include <cstring>
  27. #include <cstdio>
  28. #include <type_traits>
  29. #include <boost/limits.hpp>
  30. #include <boost/config/workaround.hpp>
  31. #include <boost/lexical_cast/detail/type_traits.hpp>
  32. #ifndef BOOST_NO_STD_LOCALE
  33. # include <locale>
  34. #else
  35. # ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
  36. // Getting error at this point means, that your STL library is old/lame/misconfigured.
  37. // If nothing can be done with STL library, define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE,
  38. // but beware: lexical_cast will understand only 'C' locale delimeters and thousands
  39. // separators.
  40. # error "Unable to use <locale> header. Define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE to force "
  41. # error "boost::lexical_cast to use only 'C' locale during conversions."
  42. # endif
  43. #endif
  44. #include <boost/lexical_cast/detail/lcast_char_constants.hpp>
  45. #include <boost/core/noncopyable.hpp>
  46. namespace boost
  47. {
  48. namespace detail // lcast_to_unsigned
  49. {
  50. template<class T>
  51. #if defined(__clang__) && (__clang_major__ > 3 || __clang_minor__ > 6)
  52. __attribute__((no_sanitize("unsigned-integer-overflow")))
  53. #endif
  54. inline
  55. typename boost::detail::lcast::make_unsigned<T>::type lcast_to_unsigned(const T value) noexcept {
  56. typedef typename boost::detail::lcast::make_unsigned<T>::type result_type;
  57. return value < 0
  58. ? static_cast<result_type>(0u - static_cast<result_type>(value))
  59. : static_cast<result_type>(value);
  60. }
  61. }
  62. namespace detail // lcast_put_unsigned
  63. {
  64. template <class Traits, class T, class CharT>
  65. class lcast_put_unsigned: boost::noncopyable {
  66. typedef typename Traits::int_type int_type;
  67. typename std::conditional<
  68. (sizeof(unsigned) > sizeof(T))
  69. , unsigned
  70. , T
  71. >::type m_value;
  72. CharT* m_finish;
  73. CharT const m_czero;
  74. int_type const m_zero;
  75. public:
  76. lcast_put_unsigned(const T n_param, CharT* finish) noexcept
  77. : m_value(n_param), m_finish(finish)
  78. , m_czero(lcast_char_constants<CharT>::zero), m_zero(Traits::to_int_type(m_czero))
  79. {
  80. #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  81. static_assert(!std::numeric_limits<T>::is_signed, "");
  82. #endif
  83. }
  84. CharT* convert() {
  85. #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
  86. std::locale loc;
  87. if (loc == std::locale::classic()) {
  88. return main_convert_loop();
  89. }
  90. typedef std::numpunct<CharT> numpunct;
  91. numpunct const& np = BOOST_USE_FACET(numpunct, loc);
  92. std::string const grouping = np.grouping();
  93. std::string::size_type const grouping_size = grouping.size();
  94. if (!grouping_size || grouping[0] <= 0) {
  95. return main_convert_loop();
  96. }
  97. #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  98. // Check that ulimited group is unreachable:
  99. static_assert(std::numeric_limits<T>::digits10 < CHAR_MAX, "");
  100. #endif
  101. CharT const thousands_sep = np.thousands_sep();
  102. std::string::size_type group = 0; // current group number
  103. char last_grp_size = grouping[0];
  104. char left = last_grp_size;
  105. do {
  106. if (left == 0) {
  107. ++group;
  108. if (group < grouping_size) {
  109. char const grp_size = grouping[group];
  110. last_grp_size = (grp_size <= 0 ? static_cast<char>(CHAR_MAX) : grp_size);
  111. }
  112. left = last_grp_size;
  113. --m_finish;
  114. Traits::assign(*m_finish, thousands_sep);
  115. }
  116. --left;
  117. } while (main_convert_iteration());
  118. return m_finish;
  119. #else
  120. return main_convert_loop();
  121. #endif
  122. }
  123. private:
  124. inline bool main_convert_iteration() noexcept {
  125. --m_finish;
  126. int_type const digit = static_cast<int_type>(m_value % 10U);
  127. Traits::assign(*m_finish, Traits::to_char_type(m_zero + digit));
  128. m_value /= 10;
  129. return !!m_value; // suppressing warnings
  130. }
  131. inline CharT* main_convert_loop() noexcept {
  132. while (main_convert_iteration());
  133. return m_finish;
  134. }
  135. };
  136. }
  137. namespace detail // lcast_ret_unsigned
  138. {
  139. template <class Traits, class T, class CharT>
  140. class lcast_ret_unsigned: boost::noncopyable {
  141. bool m_multiplier_overflowed;
  142. T m_multiplier;
  143. T& m_value;
  144. const CharT* const m_begin;
  145. const CharT* m_end;
  146. public:
  147. lcast_ret_unsigned(T& value, const CharT* const begin, const CharT* end) noexcept
  148. : m_multiplier_overflowed(false), m_multiplier(1), m_value(value), m_begin(begin), m_end(end)
  149. {
  150. #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  151. static_assert(!std::numeric_limits<T>::is_signed, "");
  152. // GCC when used with flag -std=c++0x may not have std::numeric_limits
  153. // specializations for __int128 and unsigned __int128 types.
  154. // Try compilation with -std=gnu++0x or -std=gnu++11.
  155. //
  156. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40856
  157. static_assert(std::numeric_limits<T>::is_specialized,
  158. "std::numeric_limits are not specialized for integral type passed to boost::lexical_cast"
  159. );
  160. #endif
  161. }
  162. inline bool convert() {
  163. CharT const czero = lcast_char_constants<CharT>::zero;
  164. --m_end;
  165. m_value = static_cast<T>(0);
  166. if (m_begin > m_end || *m_end < czero || *m_end >= czero + 10)
  167. return false;
  168. m_value = static_cast<T>(*m_end - czero);
  169. --m_end;
  170. #ifdef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
  171. return main_convert_loop();
  172. #else
  173. std::locale loc;
  174. if (loc == std::locale::classic()) {
  175. return main_convert_loop();
  176. }
  177. typedef std::numpunct<CharT> numpunct;
  178. numpunct const& np = BOOST_USE_FACET(numpunct, loc);
  179. std::string const& grouping = np.grouping();
  180. std::string::size_type const grouping_size = grouping.size();
  181. /* According to Programming languages - C++
  182. * we MUST check for correct grouping
  183. */
  184. if (!grouping_size || grouping[0] <= 0) {
  185. return main_convert_loop();
  186. }
  187. unsigned char current_grouping = 0;
  188. CharT const thousands_sep = np.thousands_sep();
  189. char remained = static_cast<char>(grouping[current_grouping] - 1);
  190. for (;m_end >= m_begin; --m_end)
  191. {
  192. if (remained) {
  193. if (!main_convert_iteration()) {
  194. return false;
  195. }
  196. --remained;
  197. } else {
  198. if ( !Traits::eq(*m_end, thousands_sep) ) //|| begin == end ) return false;
  199. {
  200. /*
  201. * According to Programming languages - C++
  202. * Digit grouping is checked. That is, the positions of discarded
  203. * separators is examined for consistency with
  204. * use_facet<numpunct<charT> >(loc ).grouping()
  205. *
  206. * BUT what if there is no separators at all and grouping()
  207. * is not empty? Well, we have no extraced separators, so we
  208. * won`t check them for consistency. This will allow us to
  209. * work with "C" locale from other locales
  210. */
  211. return main_convert_loop();
  212. } else {
  213. if (m_begin == m_end) return false;
  214. if (current_grouping < grouping_size - 1) ++current_grouping;
  215. remained = grouping[current_grouping];
  216. }
  217. }
  218. } /*for*/
  219. return true;
  220. #endif
  221. }
  222. private:
  223. // Iteration that does not care about grouping/separators and assumes that all
  224. // input characters are digits
  225. #if defined(__clang__) && (__clang_major__ > 3 || __clang_minor__ > 6)
  226. __attribute__((no_sanitize("unsigned-integer-overflow")))
  227. #endif
  228. inline bool main_convert_iteration() noexcept {
  229. CharT const czero = lcast_char_constants<CharT>::zero;
  230. T const maxv = (std::numeric_limits<T>::max)();
  231. m_multiplier_overflowed = m_multiplier_overflowed || (maxv/10 < m_multiplier);
  232. m_multiplier = static_cast<T>(m_multiplier * 10);
  233. T const dig_value = static_cast<T>(*m_end - czero);
  234. T const new_sub_value = static_cast<T>(m_multiplier * dig_value);
  235. // We must correctly handle situations like `000000000000000000000000000001`.
  236. // So we take care of overflow only if `dig_value` is not '0'.
  237. if (*m_end < czero || *m_end >= czero + 10 // checking for correct digit
  238. || (dig_value && ( // checking for overflow of ...
  239. m_multiplier_overflowed // ... multiplier
  240. || static_cast<T>(maxv / dig_value) < m_multiplier // ... subvalue
  241. || static_cast<T>(maxv - new_sub_value) < m_value // ... whole expression
  242. ))
  243. ) return false;
  244. m_value = static_cast<T>(m_value + new_sub_value);
  245. return true;
  246. }
  247. bool main_convert_loop() noexcept {
  248. for ( ; m_end >= m_begin; --m_end) {
  249. if (!main_convert_iteration()) {
  250. return false;
  251. }
  252. }
  253. return true;
  254. }
  255. };
  256. }
  257. } // namespace boost
  258. #endif // BOOST_LEXICAL_CAST_DETAIL_LCAST_UNSIGNED_CONVERTERS_HPP