include/boost/url/detail/print.hpp

100.0% Lines (19/19) 100.0% Functions (3/3)
include/boost/url/detail/print.hpp
Line TLA Hits Source Code
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 <boost/core/detail/string_view.hpp>
14 #include <cstdint>
15 #include <type_traits>
16
17 namespace boost {
18 namespace urls {
19 namespace detail {
20
21 // std::uint64_t
22 // 18446744073709551615
23 // 1 2
24 template<class T>
25 struct printed
26 : std::false_type
27 {
28 };
29
30 // 16-bit unsigned
31 template<>
32 class printed<std::uint16_t>
33 : std::false_type
34 {
35 char n_;
36 char buf_[5];
37
38 public:
39 215 printed(std::uint16_t n)
40 215 {
41 215 char* it =
42 215 buf_ + sizeof(buf_);
43 215 if(n == 0)
44 {
45 16 *--it = '0';
46 16 n_ = 1;
47 }
48 else
49 {
50 1145 while(n > 0)
51 {
52 946 *--it = '0' + (n % 10);
53 946 n /= 10;
54 }
55 199 n_ = static_cast<char>(
56 199 sizeof(buf_) - (
57 199 it - buf_));
58 }
59 215 }
60
61 core::string_view
62 645 string() const noexcept
63 {
64 1290 return core::string_view(buf_ +
65 645 sizeof(buf_) - n_, n_);
66 }
67 };
68
69 template<class T>
70 printed<T>
71 215 make_printed(T t)
72 {
73 215 return printed<T>(t);
74 }
75
76 } // detail
77 } // urls
78 } // boost
79
80 #endif
81