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