include/boost/url/detail/print.hpp

100.0% Lines (19/19) 100.0% List of functions (3/3)
print.hpp
f(x) Functions (3)
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 215x printed(std::uint16_t n)
40 215x {
41 215x char* it =
42 215x buf_ + sizeof(buf_);
43 215x if(n == 0)
44 {
45 16x *--it = '0';
46 16x n_ = 1;
47 }
48 else
49 {
50 1145x while(n > 0)
51 {
52 946x *--it = '0' + (n % 10);
53 946x n /= 10;
54 }
55 199x n_ = static_cast<char>(
56 199x sizeof(buf_) - (
57 199x it - buf_));
58 }
59 215x }
60
61 core::string_view
62 645x string() const noexcept
63 {
64 1290x return core::string_view(buf_ +
65 645x sizeof(buf_) - n_, n_);
66 }
67 };
68
69 template<class T>
70 printed<T>
71 215x make_printed(T t)
72 {
73 215x return printed<T>(t);
74 }
75
76 } // detail
77 } // urls
78 } // boost
79
80 #endif
81