1  
 //
1  
 //
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/boostorg/url
7  
// Official repository: https://github.com/boostorg/url
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_URL_DETAIL_PRINT_HPP
10  
#ifndef BOOST_URL_DETAIL_PRINT_HPP
11  
#define BOOST_URL_DETAIL_PRINT_HPP
11  
#define BOOST_URL_DETAIL_PRINT_HPP
12  

12  

13  
#include <boost/core/detail/string_view.hpp>
13  
#include <boost/core/detail/string_view.hpp>
14  
#include <cstdint>
14  
#include <cstdint>
15  
#include <type_traits>
15  
#include <type_traits>
16  

16  

17  
namespace boost {
17  
namespace boost {
18  
namespace urls {
18  
namespace urls {
19  
namespace detail {
19  
namespace detail {
20  

20  

21  
// std::uint64_t
21  
// std::uint64_t
22  
// 18446744073709551615
22  
// 18446744073709551615
23  
//          1          2
23  
//          1          2
24  
template<class T>
24  
template<class T>
25  
struct printed
25  
struct printed
26  
    : std::false_type
26  
    : std::false_type
27  
{
27  
{
28  
};
28  
};
29  

29  

30  
// 16-bit unsigned
30  
// 16-bit unsigned
31  
template<>
31  
template<>
32  
class printed<std::uint16_t>
32  
class printed<std::uint16_t>
33  
    : std::false_type
33  
    : std::false_type
34  
{
34  
{
35  
    char n_;
35  
    char n_;
36  
    char buf_[5];
36  
    char buf_[5];
37  

37  

38  
public:
38  
public:
39  
    printed(std::uint16_t n)
39  
    printed(std::uint16_t n)
40  
    {
40  
    {
41  
        char* it =
41  
        char* it =
42  
            buf_ + sizeof(buf_);
42  
            buf_ + sizeof(buf_);
43  
        if(n == 0)
43  
        if(n == 0)
44  
        {
44  
        {
45  
            *--it = '0';
45  
            *--it = '0';
46  
            n_ = 1;
46  
            n_ = 1;
47  
        }
47  
        }
48  
        else
48  
        else
49  
        {
49  
        {
50  
            while(n > 0)
50  
            while(n > 0)
51  
            {
51  
            {
52  
                *--it = '0' + (n % 10);
52  
                *--it = '0' + (n % 10);
53  
                n /= 10;
53  
                n /= 10;
54  
            }
54  
            }
55  
            n_ = static_cast<char>(
55  
            n_ = static_cast<char>(
56  
                sizeof(buf_) - (
56  
                sizeof(buf_) - (
57  
                    it - buf_));
57  
                    it - buf_));
58  
        }
58  
        }
59  
    }
59  
    }
60  

60  

61  
    core::string_view
61  
    core::string_view
62  
    string() const noexcept
62  
    string() const noexcept
63  
    {
63  
    {
64  
        return core::string_view(buf_ +
64  
        return core::string_view(buf_ +
65  
            sizeof(buf_) - n_, n_);
65  
            sizeof(buf_) - n_, n_);
66  
    }
66  
    }
67  
};
67  
};
68  

68  

69  
template<class T>
69  
template<class T>
70  
printed<T>
70  
printed<T>
71  
make_printed(T t)
71  
make_printed(T t)
72  
{
72  
{
73  
    return printed<T>(t);
73  
    return printed<T>(t);
74  
}
74  
}
75  

75  

76  
} // detail
76  
} // detail
77  
} // urls
77  
} // urls
78  
} // boost
78  
} // boost
79  

79  

80  
#endif
80  
#endif