诸暨麻将添加redis
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

112 líneas
2.8 KiB

  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #include <chrono>
  5. #include <type_traits>
  6. #include <spdlog/fmt/fmt.h>
  7. #include <spdlog/common.h>
  8. // Some fmt helpers to efficiently format and pad ints and strings
  9. namespace spdlog {
  10. namespace details {
  11. namespace fmt_helper {
  12. inline spdlog::string_view_t to_string_view(const memory_buf_t &buf) SPDLOG_NOEXCEPT
  13. {
  14. return spdlog::string_view_t{buf.data(), buf.size()};
  15. }
  16. inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest)
  17. {
  18. auto *buf_ptr = view.data();
  19. if (buf_ptr != nullptr)
  20. {
  21. dest.append(buf_ptr, buf_ptr + view.size());
  22. }
  23. }
  24. template<typename T>
  25. inline void append_int(T n, memory_buf_t &dest)
  26. {
  27. fmt::format_int i(n);
  28. dest.append(i.data(), i.data() + i.size());
  29. }
  30. template<typename T>
  31. inline unsigned count_digits(T n)
  32. {
  33. using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
  34. return static_cast<unsigned>(fmt::internal::count_digits(static_cast<count_type>(n)));
  35. }
  36. inline void pad2(int n, memory_buf_t &dest)
  37. {
  38. if (n > 99)
  39. {
  40. append_int(n, dest);
  41. }
  42. else if (n > 9) // 10-99
  43. {
  44. dest.push_back(static_cast<char>('0' + n / 10));
  45. dest.push_back(static_cast<char>('0' + n % 10));
  46. }
  47. else if (n >= 0) // 0-9
  48. {
  49. dest.push_back('0');
  50. dest.push_back(static_cast<char>('0' + n));
  51. }
  52. else // negatives (unlikely, but just in case, let fmt deal with it)
  53. {
  54. fmt::format_to(dest, "{:02}", n);
  55. }
  56. }
  57. template<typename T>
  58. inline void pad_uint(T n, unsigned int width, memory_buf_t &dest)
  59. {
  60. static_assert(std::is_unsigned<T>::value, "pad_uint must get unsigned T");
  61. auto digits = count_digits(n);
  62. if (width > digits)
  63. {
  64. const char *zeroes = "0000000000000000000";
  65. dest.append(zeroes, zeroes + width - digits);
  66. }
  67. append_int(n, dest);
  68. }
  69. template<typename T>
  70. inline void pad3(T n, memory_buf_t &dest)
  71. {
  72. pad_uint(n, 3, dest);
  73. }
  74. template<typename T>
  75. inline void pad6(T n, memory_buf_t &dest)
  76. {
  77. pad_uint(n, 6, dest);
  78. }
  79. template<typename T>
  80. inline void pad9(T n, memory_buf_t &dest)
  81. {
  82. pad_uint(n, 9, dest);
  83. }
  84. // return fraction of a second of the given time_point.
  85. // e.g.
  86. // fraction<std::milliseconds>(tp) -> will return the millis part of the second
  87. template<typename ToDuration>
  88. inline ToDuration time_fraction(log_clock::time_point tp)
  89. {
  90. using std::chrono::duration_cast;
  91. using std::chrono::seconds;
  92. auto duration = tp.time_since_epoch();
  93. auto secs = duration_cast<seconds>(duration);
  94. return duration_cast<ToDuration>(duration) - duration_cast<ToDuration>(secs);
  95. }
  96. } // namespace fmt_helper
  97. } // namespace details
  98. } // namespace spdlog