to_dec_array.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright Antony Polukhin, 2016-2024.
  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 <array>
  13. #include <cstddef> // std::size_t
  14. namespace boost { namespace stacktrace { namespace detail {
  15. // We do not use boost::lexical_cast in this function to reduce module dependencies
  16. inline std::array<char, 40> to_dec_array(std::size_t value) noexcept {
  17. std::array<char, 40> ret;
  18. if (!value) {
  19. ret[0] = '0';
  20. ret[1] = '\0';
  21. return ret;
  22. }
  23. std::size_t digits = 0;
  24. for (std::size_t value_copy = value; value_copy; value_copy /= 10) {
  25. ++ digits;
  26. }
  27. for (std::size_t i = 1; i <= digits; ++i) {
  28. ret[digits - i] = static_cast<char>('0' + (value % 10));
  29. value /= 10;
  30. }
  31. ret[digits] = '\0';
  32. return ret;
  33. }
  34. }}} // namespace boost::stacktrace::detail
  35. #endif // BOOST_STACKTRACE_DETAIL_TO_DEC_ARRAY_HPP