1 -
 //
 
2 -
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
 
3 -
//
 
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)
 
6 -
//
 
7 -
// Official repository: https://github.com/boostorg/url
 
8 -
//
 
9 -

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

 
13 -
#include <cstdint>
 
14 -
#include <type_traits>
 
15 -

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

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

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

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

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

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

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

 
79 -
#endif