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 <cstdint>
13  
#include <cstdint>
14  
#include <type_traits>
14  
#include <type_traits>
15  

15  

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

19  

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

28  

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

36  

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

59  

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

67  

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

74  

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

78  

79  
#endif
79  
#endif