to_dec_array.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright Antony Polukhin, 2016-2019.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_STACKTRACE_DETAIL_TO_DEC_ARRAY_HPP
  7. #define BOOST_STACKTRACE_DETAIL_TO_DEC_ARRAY_HPP
  8. #include <boost/config.hpp>
  9. #ifdef BOOST_HAS_PRAGMA_ONCE
  10. # pragma once
  11. #endif
  12. #include <boost/array.hpp>
  13. namespace boost { namespace stacktrace { namespace detail {
  14. // We do not use boost::lexical_cast in this function to reduce module dependencies
  15. inline boost::array<char, 40> to_dec_array(std::size_t value) BOOST_NOEXCEPT {
  16. boost::array<char, 40> ret;
  17. if (!value) {
  18. ret[0] = '0';
  19. ret[1] = '\0';
  20. return ret;
  21. }
  22. std::size_t digits = 0;
  23. for (std::size_t value_copy = value; value_copy; value_copy /= 10) {
  24. ++ digits;
  25. }
  26. for (std::size_t i = 1; i <= digits; ++i) {
  27. ret[digits - i] = static_cast<char>('0' + (value % 10));
  28. value /= 10;
  29. }
  30. ret[digits] = '\0';
  31. return ret;
  32. }
  33. }}} // namespace boost::stacktrace::detail
  34. #endif // BOOST_STACKTRACE_DETAIL_TO_DEC_ARRAY_HPP