Coverage Report

Created: 2026-08-14 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boost/boost/uuid/uuid_io.hpp
Line
Count
Source
1
#ifndef BOOST_UUID_UUID_IO_HPP_INCLUDED
2
#define BOOST_UUID_UUID_IO_HPP_INCLUDED
3
4
// Copyright 2009 Andy Tompkins
5
// Copyright 2024 Peter Dimov
6
// Distributed under the Boost Software License, Version 1.0.
7
// https://www.boost.org/LICENSE_1_0.txt
8
9
#include <boost/uuid/uuid.hpp>
10
#include <boost/uuid/detail/to_chars.hpp>
11
#include <boost/uuid/detail/from_chars.hpp>
12
#include <boost/uuid/detail/static_assert.hpp>
13
#include <boost/uuid/detail/uuid_from_string.hpp>
14
#include <boost/config.hpp>
15
#include <iosfwd>
16
#include <ios>
17
#include <iterator>
18
#include <string>
19
#include <cstddef>
20
21
namespace boost {
22
namespace uuids {
23
24
// to_chars
25
26
namespace detail
27
{
28
29
template<class Vt> struct output_value_type
30
{
31
    using type = char;
32
};
33
34
template<> struct output_value_type<wchar_t>
35
{
36
    using type = wchar_t;
37
};
38
39
template<> struct output_value_type<char16_t>
40
{
41
    using type = char16_t;
42
};
43
44
template<> struct output_value_type<char32_t>
45
{
46
    using type = char32_t;
47
};
48
49
#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L
50
51
template<> struct output_value_type<char8_t>
52
{
53
    using type = char8_t;
54
};
55
56
#endif
57
58
} // namespace detail
59
60
template<class OutputIterator,
61
    class Vt = typename std::iterator_traits<OutputIterator>::value_type
62
>
63
BOOST_CXX14_CONSTEXPR OutputIterator to_chars( uuid const& u, OutputIterator out )
64
{
65
    using Ch = typename detail::output_value_type<Vt>::type;
66
67
    alignas( 16 ) Ch tmp[ 36 ] = {};
68
    detail::to_chars( u, tmp );
69
70
    for( std::size_t i = 0; i < 36; ++i )
71
    {
72
        *out++ = tmp[ i ];
73
    }
74
75
    return out;
76
}
77
78
template<class Ch>
79
BOOST_CXX14_CONSTEXPR inline bool to_chars( uuid const& u, Ch* first, Ch* last ) noexcept
80
{
81
    if( last - first < 36 )
82
    {
83
        return false;
84
    }
85
86
    detail::to_chars( u, first );
87
    return true;
88
}
89
90
template<class Ch, std::size_t N>
91
BOOST_CXX14_CONSTEXPR inline Ch* to_chars( uuid const& u, Ch (&buffer)[ N ] ) noexcept
92
{
93
    BOOST_UUID_STATIC_ASSERT( N >= 37 );
94
95
    detail::to_chars( u, buffer + 0 );
96
    buffer[ 36 ] = 0;
97
98
    return buffer + 36;
99
}
100
101
// only provided for compatibility; deprecated
102
template<class Ch>
103
BOOST_DEPRECATED( "Use Ch[37] instead of Ch[36] to allow for the null terminator" )
104
BOOST_CXX14_CONSTEXPR inline Ch* to_chars( uuid const& u, Ch (&buffer)[ 36 ] ) noexcept
105
{
106
    detail::to_chars( u, buffer + 0 );
107
    return buffer + 36;
108
}
109
110
// operator<<
111
112
template<class Ch, class Traits>
113
std::basic_ostream<Ch, Traits>& operator<<( std::basic_ostream<Ch, Traits>& os, uuid const& u )
114
{
115
    alignas( 16 ) Ch tmp[ 37 ];
116
    to_chars( u, tmp );
117
118
    os << tmp;
119
    return os;
120
}
121
122
// operator>>
123
124
template<class Ch, class Traits>
125
std::basic_istream<Ch, Traits>& operator>>( std::basic_istream<Ch, Traits>& is, uuid& u )
126
{
127
    alignas( 16 ) Ch tmp[ 37 ] = {};
128
129
    is.width( 37 ); // required for pre-C++20
130
131
    if( is >> tmp )
132
    {
133
        if( !from_chars( tmp, tmp + 36, u ) )
134
        {
135
            is.setstate( std::ios_base::failbit );
136
        }
137
    }
138
139
    return is;
140
}
141
142
// to_string
143
144
inline std::string to_string( uuid const& u )
145
7
{
146
7
    std::string result( 36, char() );
147
148
    // string::data() returns const char* before C++17
149
7
    detail::to_chars( u, &result[0] );
150
7
    return result;
151
7
}
152
153
inline std::wstring to_wstring( uuid const& u )
154
7
{
155
7
    std::wstring result( 36, wchar_t() );
156
157
7
    detail::to_chars( u, &result[0] );
158
7
    return result;
159
7
}
160
161
}} //namespace boost::uuids
162
163
#endif // BOOST_UUID_UUID_IO_HPP_INCLUDED