Skip to content

Commit

Permalink
Add charN_t support to to_chars
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed May 4, 2024
1 parent 34eb75c commit 2480bed
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
19 changes: 19 additions & 0 deletions include/boost/uuid/detail/to_chars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ constexpr wchar_t const* digits( wchar_t const* ) noexcept
return L"0123456789abcdef-";
}

constexpr char16_t const* digits( char16_t const* ) noexcept
{
return u"0123456789abcdef-";
}

constexpr char32_t const* digits( char32_t const* ) noexcept
{
return U"0123456789abcdef-";
}

#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L

constexpr char8_t const* digits( char8_t const* ) noexcept
{
return u8"0123456789abcdef-";
}

#endif

template<class Ch> inline Ch* to_chars( uuid const& u, Ch* out ) noexcept
{
constexpr Ch const* p = digits( static_cast<Ch const*>( nullptr ) );
Expand Down
14 changes: 2 additions & 12 deletions include/boost/uuid/uuid_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,8 @@ OutputIterator to_chars( uuid const& u, OutputIterator out )
return std::copy_n( tmp, 36, out );
}

inline bool to_chars( uuid const& u, char* first, char* last ) noexcept
{
if( last - first < 36 )
{
return false;
}

detail::to_chars( u, first );
return true;
}

inline bool to_chars( uuid const& u, wchar_t* first, wchar_t* last ) noexcept
template<class Ch>
inline bool to_chars( uuid const& u, Ch* first, Ch* last ) noexcept
{
if( last - first < 36 )
{
Expand Down
12 changes: 12 additions & 0 deletions test/test_to_chars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,17 @@ int main()
test( u2, L"00010203-0405-0607-0809-0a0b0c0d0e0f" );
test( u3, L"12345678-90ab-cdef-1234-567890abcdef" );

test( u1, u"00000000-0000-0000-0000-000000000000" );
test( u2, u"00010203-0405-0607-0809-0a0b0c0d0e0f" );
test( u3, u"12345678-90ab-cdef-1234-567890abcdef" );

test( u1, U"00000000-0000-0000-0000-000000000000" );
test( u2, U"00010203-0405-0607-0809-0a0b0c0d0e0f" );
test( u3, U"12345678-90ab-cdef-1234-567890abcdef" );

test( u1, u8"00000000-0000-0000-0000-000000000000" );
test( u2, u8"00010203-0405-0607-0809-0a0b0c0d0e0f" );
test( u3, u8"12345678-90ab-cdef-1234-567890abcdef" );

return boost::report_errors();
}

0 comments on commit 2480bed

Please sign in to comment.