lcast_unsigned_converters.hpp 12 KB

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