print.hpp 1.4 KB

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