lcast_unsigned_converters.hpp 12 KB

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