lcast_unsigned_converters.hpp 12 KB

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