itos.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2019 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. //
  6. // We used to use lexical_cast internally for quick conversions from integers
  7. // to strings, but that breaks if the global locale is something other than "C".
  8. // See https://github.com/boostorg/multiprecision/issues/167.
  9. //
  10. #ifndef BOOST_MP_DETAIL_ITOS_HPP
  11. #define BOOST_MP_DETAIL_ITOS_HPP
  12. namespace boost { namespace multiprecision { namespace detail {
  13. template <class Integer>
  14. std::string itos(Integer val)
  15. {
  16. if (!val) return "0";
  17. std::string result;
  18. bool isneg = false;
  19. if (val < 0)
  20. {
  21. val = -val;
  22. isneg = true;
  23. }
  24. while (val)
  25. {
  26. result.insert(result.begin(), char('0' + (val % 10)));
  27. val /= 10;
  28. }
  29. if (isneg)
  30. result.insert(result.begin(), '-');
  31. return result;
  32. }
  33. }}} // namespace boost::multiprecision::detail
  34. #endif