to_chars.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef BOOST_UUID_DETAIL_TO_CHARS_HPP_INCLUDED
  2. #define BOOST_UUID_DETAIL_TO_CHARS_HPP_INCLUDED
  3. // Copyright 2009 Andy Tompkins
  4. // Copyright 2024 Peter Dimov
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // https://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/uuid/uuid.hpp>
  8. namespace boost {
  9. namespace uuids {
  10. namespace detail {
  11. constexpr char const* digits( char const* ) noexcept
  12. {
  13. return "0123456789abcdef-";
  14. }
  15. constexpr wchar_t const* digits( wchar_t const* ) noexcept
  16. {
  17. return L"0123456789abcdef-";
  18. }
  19. constexpr char16_t const* digits( char16_t const* ) noexcept
  20. {
  21. return u"0123456789abcdef-";
  22. }
  23. constexpr char32_t const* digits( char32_t const* ) noexcept
  24. {
  25. return U"0123456789abcdef-";
  26. }
  27. #if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L
  28. constexpr char8_t const* digits( char8_t const* ) noexcept
  29. {
  30. return u8"0123456789abcdef-";
  31. }
  32. #endif
  33. template<class Ch> inline Ch* to_chars( uuid const& u, Ch* out ) noexcept
  34. {
  35. constexpr Ch const* p = digits( static_cast<Ch const*>( nullptr ) );
  36. for( std::size_t i = 0; i < 16; ++i )
  37. {
  38. std::uint8_t ch = u.data[ i ];
  39. *out++ = p[ (ch >> 4) & 0x0F ];
  40. *out++ = p[ ch & 0x0F ];
  41. if( i == 3 || i == 5 || i == 7 || i == 9 )
  42. {
  43. *out++ = p[ 16 ];
  44. }
  45. }
  46. return out;
  47. }
  48. } // namespace detail
  49. }} //namespace boost::uuids
  50. #endif // BOOST_UUID_DETAIL_TO_CHARS_HPP_INCLUDED