诸暨麻将添加redis
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

289 linhas
9.1 KiB

  1. // Formatting library for C++ - experimental range support
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. //
  8. // Copyright (c) 2018 - present, Remotion (Igor Schulz)
  9. // All Rights Reserved
  10. // {fmt} support for ranges, containers and types tuple interface.
  11. #ifndef FMT_RANGES_H_
  12. #define FMT_RANGES_H_
  13. #include <type_traits>
  14. #include "format.h"
  15. // output only up to N items from the range.
  16. #ifndef FMT_RANGE_OUTPUT_LENGTH_LIMIT
  17. # define FMT_RANGE_OUTPUT_LENGTH_LIMIT 256
  18. #endif
  19. FMT_BEGIN_NAMESPACE
  20. template <typename Char> struct formatting_base {
  21. template <typename ParseContext>
  22. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  23. return ctx.begin();
  24. }
  25. };
  26. template <typename Char, typename Enable = void>
  27. struct formatting_range : formatting_base<Char> {
  28. static FMT_CONSTEXPR_DECL const std::size_t range_length_limit =
  29. FMT_RANGE_OUTPUT_LENGTH_LIMIT; // output only up to N items from the
  30. // range.
  31. Char prefix;
  32. Char delimiter;
  33. Char postfix;
  34. formatting_range() : prefix('{'), delimiter(','), postfix('}') {}
  35. static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true;
  36. static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false;
  37. };
  38. template <typename Char, typename Enable = void>
  39. struct formatting_tuple : formatting_base<Char> {
  40. Char prefix;
  41. Char delimiter;
  42. Char postfix;
  43. formatting_tuple() : prefix('('), delimiter(','), postfix(')') {}
  44. static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true;
  45. static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false;
  46. };
  47. namespace internal {
  48. template <typename RangeT, typename OutputIterator>
  49. OutputIterator copy(const RangeT& range, OutputIterator out) {
  50. for (auto it = range.begin(), end = range.end(); it != end; ++it)
  51. *out++ = *it;
  52. return out;
  53. }
  54. template <typename OutputIterator>
  55. OutputIterator copy(const char* str, OutputIterator out) {
  56. while (*str) *out++ = *str++;
  57. return out;
  58. }
  59. template <typename OutputIterator>
  60. OutputIterator copy(char ch, OutputIterator out) {
  61. *out++ = ch;
  62. return out;
  63. }
  64. /// Return true value if T has std::string interface, like std::string_view.
  65. template <typename T> class is_like_std_string {
  66. template <typename U>
  67. static auto check(U* p)
  68. -> decltype((void)p->find('a'), p->length(), (void)p->data(), int());
  69. template <typename> static void check(...);
  70. public:
  71. static FMT_CONSTEXPR_DECL const bool value =
  72. is_string<T>::value || !std::is_void<decltype(check<T>(nullptr))>::value;
  73. };
  74. template <typename Char>
  75. struct is_like_std_string<fmt::basic_string_view<Char>> : std::true_type {};
  76. template <typename... Ts> struct conditional_helper {};
  77. template <typename T, typename _ = void> struct is_range_ : std::false_type {};
  78. #if !FMT_MSC_VER || FMT_MSC_VER > 1800
  79. template <typename T>
  80. struct is_range_<
  81. T, conditional_t<false,
  82. conditional_helper<decltype(std::declval<T>().begin()),
  83. decltype(std::declval<T>().end())>,
  84. void>> : std::true_type {};
  85. #endif
  86. /// tuple_size and tuple_element check.
  87. template <typename T> class is_tuple_like_ {
  88. template <typename U>
  89. static auto check(U* p)
  90. -> decltype(std::tuple_size<U>::value,
  91. (void)std::declval<typename std::tuple_element<0, U>::type>(),
  92. int());
  93. template <typename> static void check(...);
  94. public:
  95. static FMT_CONSTEXPR_DECL const bool value =
  96. !std::is_void<decltype(check<T>(nullptr))>::value;
  97. };
  98. // Check for integer_sequence
  99. #if defined(__cpp_lib_integer_sequence) || FMT_MSC_VER >= 1900
  100. template <typename T, T... N>
  101. using integer_sequence = std::integer_sequence<T, N...>;
  102. template <std::size_t... N> using index_sequence = std::index_sequence<N...>;
  103. template <std::size_t N>
  104. using make_index_sequence = std::make_index_sequence<N>;
  105. #else
  106. template <typename T, T... N> struct integer_sequence {
  107. using value_type = T;
  108. static FMT_CONSTEXPR std::size_t size() { return sizeof...(N); }
  109. };
  110. template <std::size_t... N>
  111. using index_sequence = integer_sequence<std::size_t, N...>;
  112. template <typename T, std::size_t N, T... Ns>
  113. struct make_integer_sequence : make_integer_sequence<T, N - 1, N - 1, Ns...> {};
  114. template <typename T, T... Ns>
  115. struct make_integer_sequence<T, 0, Ns...> : integer_sequence<T, Ns...> {};
  116. template <std::size_t N>
  117. using make_index_sequence = make_integer_sequence<std::size_t, N>;
  118. #endif
  119. template <class Tuple, class F, size_t... Is>
  120. void for_each(index_sequence<Is...>, Tuple&& tup, F&& f) FMT_NOEXCEPT {
  121. using std::get;
  122. // using free function get<I>(T) now.
  123. const int _[] = {0, ((void)f(get<Is>(tup)), 0)...};
  124. (void)_; // blocks warnings
  125. }
  126. template <class T>
  127. FMT_CONSTEXPR make_index_sequence<std::tuple_size<T>::value> get_indexes(
  128. T const&) {
  129. return {};
  130. }
  131. template <class Tuple, class F> void for_each(Tuple&& tup, F&& f) {
  132. const auto indexes = get_indexes(tup);
  133. for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
  134. }
  135. template <typename Arg, FMT_ENABLE_IF(!is_like_std_string<
  136. typename std::decay<Arg>::type>::value)>
  137. FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&) {
  138. return add_space ? " {}" : "{}";
  139. }
  140. template <typename Arg, FMT_ENABLE_IF(is_like_std_string<
  141. typename std::decay<Arg>::type>::value)>
  142. FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&) {
  143. return add_space ? " \"{}\"" : "\"{}\"";
  144. }
  145. FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char*) {
  146. return add_space ? " \"{}\"" : "\"{}\"";
  147. }
  148. FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t*) {
  149. return add_space ? L" \"{}\"" : L"\"{}\"";
  150. }
  151. FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char) {
  152. return add_space ? " '{}'" : "'{}'";
  153. }
  154. FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t) {
  155. return add_space ? L" '{}'" : L"'{}'";
  156. }
  157. } // namespace internal
  158. template <typename T> struct is_tuple_like {
  159. static FMT_CONSTEXPR_DECL const bool value =
  160. internal::is_tuple_like_<T>::value && !internal::is_range_<T>::value;
  161. };
  162. template <typename TupleT, typename Char>
  163. struct formatter<TupleT, Char, enable_if_t<fmt::is_tuple_like<TupleT>::value>> {
  164. private:
  165. // C++11 generic lambda for format()
  166. template <typename FormatContext> struct format_each {
  167. template <typename T> void operator()(const T& v) {
  168. if (i > 0) {
  169. if (formatting.add_prepostfix_space) {
  170. *out++ = ' ';
  171. }
  172. out = internal::copy(formatting.delimiter, out);
  173. }
  174. out = format_to(out,
  175. internal::format_str_quoted(
  176. (formatting.add_delimiter_spaces && i > 0), v),
  177. v);
  178. ++i;
  179. }
  180. formatting_tuple<Char>& formatting;
  181. std::size_t& i;
  182. typename std::add_lvalue_reference<decltype(
  183. std::declval<FormatContext>().out())>::type out;
  184. };
  185. public:
  186. formatting_tuple<Char> formatting;
  187. template <typename ParseContext>
  188. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  189. return formatting.parse(ctx);
  190. }
  191. template <typename FormatContext = format_context>
  192. auto format(const TupleT& values, FormatContext& ctx) -> decltype(ctx.out()) {
  193. auto out = ctx.out();
  194. std::size_t i = 0;
  195. internal::copy(formatting.prefix, out);
  196. internal::for_each(values, format_each<FormatContext>{formatting, i, out});
  197. if (formatting.add_prepostfix_space) {
  198. *out++ = ' ';
  199. }
  200. internal::copy(formatting.postfix, out);
  201. return ctx.out();
  202. }
  203. };
  204. template <typename T, typename Char> struct is_range {
  205. static FMT_CONSTEXPR_DECL const bool value =
  206. internal::is_range_<T>::value &&
  207. !internal::is_like_std_string<T>::value &&
  208. !std::is_convertible<T, std::basic_string<Char>>::value;
  209. };
  210. template <typename RangeT, typename Char>
  211. struct formatter<RangeT, Char,
  212. enable_if_t<fmt::is_range<RangeT, Char>::value>> {
  213. formatting_range<Char> formatting;
  214. template <typename ParseContext>
  215. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  216. return formatting.parse(ctx);
  217. }
  218. template <typename FormatContext>
  219. typename FormatContext::iterator format(const RangeT& values,
  220. FormatContext& ctx) {
  221. auto out = internal::copy(formatting.prefix, ctx.out());
  222. std::size_t i = 0;
  223. for (auto it = values.begin(), end = values.end(); it != end; ++it) {
  224. if (i > 0) {
  225. if (formatting.add_prepostfix_space) *out++ = ' ';
  226. out = internal::copy(formatting.delimiter, out);
  227. }
  228. out = format_to(out,
  229. internal::format_str_quoted(
  230. (formatting.add_delimiter_spaces && i > 0), *it),
  231. *it);
  232. if (++i > formatting.range_length_limit) {
  233. out = format_to(out, " ... <other elements>");
  234. break;
  235. }
  236. }
  237. if (formatting.add_prepostfix_space) *out++ = ' ';
  238. return internal::copy(formatting.postfix, out);
  239. }
  240. };
  241. FMT_END_NAMESPACE
  242. #endif // FMT_RANGES_H_