Skip to content

Commit

Permalink
Move detail::to_chars to detail/to_chars.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed May 1, 2024
1 parent 835a658 commit aaa8445
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 78 deletions.
48 changes: 48 additions & 0 deletions include/boost/uuid/detail/to_chars.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef BOOST_UUID_DETAIL_TO_CHARS_HPP_INCLUDED
#define BOOST_UUID_DETAIL_TO_CHARS_HPP_INCLUDED

// Copyright 2009 Andy Tompkins
// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/uuid/uuid.hpp>

namespace boost {
namespace uuids {
namespace detail {

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

constexpr wchar_t const* digits( wchar_t const* ) noexcept
{
return L"0123456789abcdef-";
}

template<class Ch> inline Ch* to_chars( uuid const& u, Ch* out ) noexcept
{
constexpr Ch const* p = digits( static_cast<Ch const*>( nullptr ) );

for( std::size_t i = 0; i < 16; ++i )
{
std::uint8_t ch = u.data[ i ];

*out++ = p[ (ch >> 4) & 0x0F ];
*out++ = p[ ch & 0x0F ];

if( i == 3 || i == 5 || i == 7 || i == 9 )
{
*out++ = p[ 16 ];
}
}

return out;
}

} // namespace detail
}} //namespace boost::uuids

#endif // BOOST_UUID_DETAIL_TO_CHARS_HPP_INCLUDED
92 changes: 14 additions & 78 deletions include/boost/uuid/uuid_io.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#ifndef BOOST_UUID_UUID_IO_HPP_INCLUDED
#define BOOST_UUID_UUID_IO_HPP_INCLUDED

// Boost uuid_io.hpp header file ----------------------------------------------//

// Copyright 2009 Andy Tompkins.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// Copyright 2009 Andy Tompkins
// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/detail/to_chars.hpp>
#include <iosfwd>
#include <istream>
#include <locale>
Expand All @@ -23,76 +22,7 @@
namespace boost {
namespace uuids {

namespace detail {

inline char to_char( unsigned i ) noexcept
{
if( i <= 9 )
{
return static_cast<char>( '0' + i );
}
else
{
// 'a'..'f' are actually consecutive in EBCDIC
return static_cast<char>( 'a' + ( i - 10 ) );
}
}

inline wchar_t to_wchar( unsigned i ) noexcept
{
if( i <= 9 )
{
return static_cast<wchar_t>( L'0' + i );
}
else
{
return static_cast<wchar_t>( L'a' + ( i - 10 ) );
}
}

inline char* to_chars( uuid const& u, char* out ) noexcept
{
std::size_t i = 0;

for( uuid::const_iterator it_data = u.begin(); it_data != u.end(); ++it_data, ++i )
{
const unsigned hi = ((*it_data) >> 4) & 0x0F;
*out++ = detail::to_char( hi );

const unsigned lo = (*it_data) & 0x0F;
*out++ = detail::to_char( lo );

if( i == 3 || i == 5 || i == 7 || i == 9 )
{
*out++ = '-';
}
}

return out;
}

inline wchar_t* to_chars( uuid const& u, wchar_t* out ) noexcept
{
std::size_t i = 0;

for( uuid::const_iterator it_data = u.begin(); it_data != u.end(); ++it_data, ++i )
{
const unsigned hi = ((*it_data) >> 4) & 0x0F;
*out++ = detail::to_wchar( hi );

const unsigned lo = (*it_data) & 0x0F;
*out++ = detail::to_wchar( lo );

if( i == 3 || i == 5 || i == 7 || i == 9 )
{
*out++ = L'-';
}
}

return out;
}

} // namespace detail
// to_chars

template<class OutputIterator>
OutputIterator to_chars( uuid const& u, OutputIterator out )
Expand Down Expand Up @@ -125,6 +55,8 @@ inline bool to_chars( uuid const& u, wchar_t* first, wchar_t* last ) noexcept
return true;
}

// operator<<

template<class Ch, class Traits>
std::basic_ostream<Ch, Traits>& operator<<( std::basic_ostream<Ch, Traits>& os, uuid const& u )
{
Expand All @@ -135,6 +67,8 @@ std::basic_ostream<Ch, Traits>& operator<<( std::basic_ostream<Ch, Traits>& os,
return os;
}

// operator>>

template<class Ch, class Traits>
std::basic_istream<Ch, Traits>& operator>>( std::basic_istream<Ch, Traits>& is, uuid& u )
{
Expand Down Expand Up @@ -212,20 +146,22 @@ std::basic_istream<Ch, Traits>& operator>>( std::basic_istream<Ch, Traits>& is,
return is;
}

// to_string

inline std::string to_string( uuid const& u )
{
std::string result( 36, char() );

// string::data() returns const char* before C++17
to_chars( u, &result[0] );
detail::to_chars( u, &result[0] );
return result;
}

inline std::wstring to_wstring( uuid const& u )
{
std::wstring result( 36, wchar_t() );

to_chars( u, &result[0] );
detail::to_chars( u, &result[0] );
return result;
}

Expand Down

0 comments on commit aaa8445

Please sign in to comment.