print.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef BOOST_URL_DETAIL_PRINT_HPP
  10. #define BOOST_URL_DETAIL_PRINT_HPP
  11. #ifndef BOOST_URL_SOURCE
  12. #error
  13. #endif
  14. #include <cstdint>
  15. #include <type_traits>
  16. namespace boost {
  17. namespace urls {
  18. namespace detail {
  19. // std::uint64_t
  20. // 18446744073709551615
  21. // 1 2
  22. template<class T>
  23. struct printed
  24. : std::false_type
  25. {
  26. };
  27. // 16-bit unsigned
  28. template<>
  29. class printed<std::uint16_t>
  30. : std::false_type
  31. {
  32. char n_;
  33. char buf_[5];
  34. public:
  35. printed(std::uint16_t n)
  36. {
  37. char* it =
  38. buf_ + sizeof(buf_);
  39. if(n == 0)
  40. {
  41. *--it = '0';
  42. n_ = 1;
  43. }
  44. else
  45. {
  46. while(n > 0)
  47. {
  48. *--it = '0' + (n % 10);
  49. n /= 10;
  50. }
  51. n_ = static_cast<char>(
  52. sizeof(buf_) - (
  53. it - buf_));
  54. }
  55. }
  56. string_view
  57. string() const noexcept
  58. {
  59. return string_view(buf_ +
  60. sizeof(buf_) - n_, n_);
  61. }
  62. };
  63. template<class T>
  64. printed<T>
  65. make_printed(T t)
  66. {
  67. return printed<T>(t);
  68. }
  69. } // detail
  70. } // urls
  71. } // boost
  72. #endif