ranges.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 <initializer_list>
  14. #include <tuple>
  15. #include <type_traits>
  16. #include "format.h"
  17. FMT_BEGIN_NAMESPACE
  18. namespace detail {
  19. template <typename RangeT, typename OutputIterator>
  20. OutputIterator copy(const RangeT& range, OutputIterator out) {
  21. for (auto it = range.begin(), end = range.end(); it != end; ++it)
  22. *out++ = *it;
  23. return out;
  24. }
  25. template <typename OutputIterator>
  26. OutputIterator copy(const char* str, OutputIterator out) {
  27. while (*str) *out++ = *str++;
  28. return out;
  29. }
  30. template <typename OutputIterator>
  31. OutputIterator copy(char ch, OutputIterator out) {
  32. *out++ = ch;
  33. return out;
  34. }
  35. template <typename OutputIterator>
  36. OutputIterator copy(wchar_t ch, OutputIterator out) {
  37. *out++ = ch;
  38. return out;
  39. }
  40. // Returns true if T has a std::string-like interface, like std::string_view.
  41. template <typename T> class is_std_string_like {
  42. template <typename U>
  43. static auto check(U* p)
  44. -> decltype((void)p->find('a'), p->length(), (void)p->data(), int());
  45. template <typename> static void check(...);
  46. public:
  47. static FMT_CONSTEXPR_DECL const bool value =
  48. is_string<T>::value ||
  49. std::is_convertible<T, std_string_view<char>>::value ||
  50. !std::is_void<decltype(check<T>(nullptr))>::value;
  51. };
  52. template <typename Char>
  53. struct is_std_string_like<fmt::basic_string_view<Char>> : std::true_type {};
  54. template <typename T> class is_map {
  55. template <typename U> static auto check(U*) -> typename U::mapped_type;
  56. template <typename> static void check(...);
  57. public:
  58. #ifdef FMT_FORMAT_MAP_AS_LIST
  59. static FMT_CONSTEXPR_DECL const bool value = false;
  60. #else
  61. static FMT_CONSTEXPR_DECL const bool value =
  62. !std::is_void<decltype(check<T>(nullptr))>::value;
  63. #endif
  64. };
  65. template <typename T> class is_set {
  66. template <typename U> static auto check(U*) -> typename U::key_type;
  67. template <typename> static void check(...);
  68. public:
  69. #ifdef FMT_FORMAT_SET_AS_LIST
  70. static FMT_CONSTEXPR_DECL const bool value = false;
  71. #else
  72. static FMT_CONSTEXPR_DECL const bool value =
  73. !std::is_void<decltype(check<T>(nullptr))>::value && !is_map<T>::value;
  74. #endif
  75. };
  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. # define FMT_DECLTYPE_RETURN(val) \
  80. ->decltype(val) { return val; } \
  81. static_assert( \
  82. true, "") // This makes it so that a semicolon is required after the
  83. // macro, which helps clang-format handle the formatting.
  84. // C array overload
  85. template <typename T, std::size_t N>
  86. auto range_begin(const T (&arr)[N]) -> const T* {
  87. return arr;
  88. }
  89. template <typename T, std::size_t N>
  90. auto range_end(const T (&arr)[N]) -> const T* {
  91. return arr + N;
  92. }
  93. template <typename T, typename Enable = void>
  94. struct has_member_fn_begin_end_t : std::false_type {};
  95. template <typename T>
  96. struct has_member_fn_begin_end_t<T, void_t<decltype(std::declval<T>().begin()),
  97. decltype(std::declval<T>().end())>>
  98. : std::true_type {};
  99. // Member function overload
  100. template <typename T>
  101. auto range_begin(T&& rng) FMT_DECLTYPE_RETURN(static_cast<T&&>(rng).begin());
  102. template <typename T>
  103. auto range_end(T&& rng) FMT_DECLTYPE_RETURN(static_cast<T&&>(rng).end());
  104. // ADL overload. Only participates in overload resolution if member functions
  105. // are not found.
  106. template <typename T>
  107. auto range_begin(T&& rng)
  108. -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
  109. decltype(begin(static_cast<T&&>(rng)))> {
  110. return begin(static_cast<T&&>(rng));
  111. }
  112. template <typename T>
  113. auto range_end(T&& rng) -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
  114. decltype(end(static_cast<T&&>(rng)))> {
  115. return end(static_cast<T&&>(rng));
  116. }
  117. template <typename T, typename Enable = void>
  118. struct has_const_begin_end : std::false_type {};
  119. template <typename T, typename Enable = void>
  120. struct has_mutable_begin_end : std::false_type {};
  121. template <typename T>
  122. struct has_const_begin_end<
  123. T,
  124. void_t<
  125. decltype(detail::range_begin(std::declval<const remove_cvref_t<T>&>())),
  126. decltype(detail::range_end(std::declval<const remove_cvref_t<T>&>()))>>
  127. : std::true_type {};
  128. template <typename T>
  129. struct has_mutable_begin_end<
  130. T, void_t<decltype(detail::range_begin(std::declval<T>())),
  131. decltype(detail::range_end(std::declval<T>())),
  132. enable_if_t<std::is_copy_constructible<T>::value>>>
  133. : std::true_type {};
  134. template <typename T>
  135. struct is_range_<T, void>
  136. : std::integral_constant<bool, (has_const_begin_end<T>::value ||
  137. has_mutable_begin_end<T>::value)> {};
  138. # undef FMT_DECLTYPE_RETURN
  139. #endif
  140. // tuple_size and tuple_element check.
  141. template <typename T> class is_tuple_like_ {
  142. template <typename U>
  143. static auto check(U* p) -> decltype(std::tuple_size<U>::value, int());
  144. template <typename> static void check(...);
  145. public:
  146. static FMT_CONSTEXPR_DECL const bool value =
  147. !std::is_void<decltype(check<T>(nullptr))>::value;
  148. };
  149. // Check for integer_sequence
  150. #if defined(__cpp_lib_integer_sequence) || FMT_MSC_VER >= 1900
  151. template <typename T, T... N>
  152. using integer_sequence = std::integer_sequence<T, N...>;
  153. template <size_t... N> using index_sequence = std::index_sequence<N...>;
  154. template <size_t N> using make_index_sequence = std::make_index_sequence<N>;
  155. #else
  156. template <typename T, T... N> struct integer_sequence {
  157. using value_type = T;
  158. static FMT_CONSTEXPR size_t size() { return sizeof...(N); }
  159. };
  160. template <size_t... N> using index_sequence = integer_sequence<size_t, N...>;
  161. template <typename T, size_t N, T... Ns>
  162. struct make_integer_sequence : make_integer_sequence<T, N - 1, N - 1, Ns...> {};
  163. template <typename T, T... Ns>
  164. struct make_integer_sequence<T, 0, Ns...> : integer_sequence<T, Ns...> {};
  165. template <size_t N>
  166. using make_index_sequence = make_integer_sequence<size_t, N>;
  167. #endif
  168. template <class Tuple, class F, size_t... Is>
  169. void for_each(index_sequence<Is...>, Tuple&& tup, F&& f) FMT_NOEXCEPT {
  170. using std::get;
  171. // using free function get<I>(T) now.
  172. const int _[] = {0, ((void)f(get<Is>(tup)), 0)...};
  173. (void)_; // blocks warnings
  174. }
  175. template <class T>
  176. FMT_CONSTEXPR make_index_sequence<std::tuple_size<T>::value> get_indexes(
  177. T const&) {
  178. return {};
  179. }
  180. template <class Tuple, class F> void for_each(Tuple&& tup, F&& f) {
  181. const auto indexes = get_indexes(tup);
  182. for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
  183. }
  184. template <typename Range>
  185. using value_type =
  186. remove_cvref_t<decltype(*detail::range_begin(std::declval<Range>()))>;
  187. template <typename OutputIt> OutputIt write_delimiter(OutputIt out) {
  188. *out++ = ',';
  189. *out++ = ' ';
  190. return out;
  191. }
  192. struct singleton {
  193. unsigned char upper;
  194. unsigned char lower_count;
  195. };
  196. inline auto is_printable(uint16_t x, const singleton* singletons,
  197. size_t singletons_size,
  198. const unsigned char* singleton_lowers,
  199. const unsigned char* normal, size_t normal_size)
  200. -> bool {
  201. auto upper = x >> 8;
  202. auto lower_start = 0;
  203. for (size_t i = 0; i < singletons_size; ++i) {
  204. auto s = singletons[i];
  205. auto lower_end = lower_start + s.lower_count;
  206. if (upper < s.upper) break;
  207. if (upper == s.upper) {
  208. for (auto j = lower_start; j < lower_end; ++j) {
  209. if (singleton_lowers[j] == (x & 0xff)) return false;
  210. }
  211. }
  212. lower_start = lower_end;
  213. }
  214. auto xsigned = static_cast<int>(x);
  215. auto current = true;
  216. for (size_t i = 0; i < normal_size; ++i) {
  217. auto v = static_cast<int>(normal[i]);
  218. auto len = (v & 0x80) != 0 ? (v & 0x7f) << 8 | normal[++i] : v;
  219. xsigned -= len;
  220. if (xsigned < 0) break;
  221. current = !current;
  222. }
  223. return current;
  224. }
  225. // Returns true iff the code point cp is printable.
  226. // This code is generated by support/printable.py.
  227. inline auto is_printable(uint32_t cp) -> bool {
  228. static constexpr singleton singletons0[] = {
  229. {0x00, 1}, {0x03, 5}, {0x05, 6}, {0x06, 3}, {0x07, 6}, {0x08, 8},
  230. {0x09, 17}, {0x0a, 28}, {0x0b, 25}, {0x0c, 20}, {0x0d, 16}, {0x0e, 13},
  231. {0x0f, 4}, {0x10, 3}, {0x12, 18}, {0x13, 9}, {0x16, 1}, {0x17, 5},
  232. {0x18, 2}, {0x19, 3}, {0x1a, 7}, {0x1c, 2}, {0x1d, 1}, {0x1f, 22},
  233. {0x20, 3}, {0x2b, 3}, {0x2c, 2}, {0x2d, 11}, {0x2e, 1}, {0x30, 3},
  234. {0x31, 2}, {0x32, 1}, {0xa7, 2}, {0xa9, 2}, {0xaa, 4}, {0xab, 8},
  235. {0xfa, 2}, {0xfb, 5}, {0xfd, 4}, {0xfe, 3}, {0xff, 9},
  236. };
  237. static constexpr unsigned char singletons0_lower[] = {
  238. 0xad, 0x78, 0x79, 0x8b, 0x8d, 0xa2, 0x30, 0x57, 0x58, 0x8b, 0x8c, 0x90,
  239. 0x1c, 0x1d, 0xdd, 0x0e, 0x0f, 0x4b, 0x4c, 0xfb, 0xfc, 0x2e, 0x2f, 0x3f,
  240. 0x5c, 0x5d, 0x5f, 0xb5, 0xe2, 0x84, 0x8d, 0x8e, 0x91, 0x92, 0xa9, 0xb1,
  241. 0xba, 0xbb, 0xc5, 0xc6, 0xc9, 0xca, 0xde, 0xe4, 0xe5, 0xff, 0x00, 0x04,
  242. 0x11, 0x12, 0x29, 0x31, 0x34, 0x37, 0x3a, 0x3b, 0x3d, 0x49, 0x4a, 0x5d,
  243. 0x84, 0x8e, 0x92, 0xa9, 0xb1, 0xb4, 0xba, 0xbb, 0xc6, 0xca, 0xce, 0xcf,
  244. 0xe4, 0xe5, 0x00, 0x04, 0x0d, 0x0e, 0x11, 0x12, 0x29, 0x31, 0x34, 0x3a,
  245. 0x3b, 0x45, 0x46, 0x49, 0x4a, 0x5e, 0x64, 0x65, 0x84, 0x91, 0x9b, 0x9d,
  246. 0xc9, 0xce, 0xcf, 0x0d, 0x11, 0x29, 0x45, 0x49, 0x57, 0x64, 0x65, 0x8d,
  247. 0x91, 0xa9, 0xb4, 0xba, 0xbb, 0xc5, 0xc9, 0xdf, 0xe4, 0xe5, 0xf0, 0x0d,
  248. 0x11, 0x45, 0x49, 0x64, 0x65, 0x80, 0x84, 0xb2, 0xbc, 0xbe, 0xbf, 0xd5,
  249. 0xd7, 0xf0, 0xf1, 0x83, 0x85, 0x8b, 0xa4, 0xa6, 0xbe, 0xbf, 0xc5, 0xc7,
  250. 0xce, 0xcf, 0xda, 0xdb, 0x48, 0x98, 0xbd, 0xcd, 0xc6, 0xce, 0xcf, 0x49,
  251. 0x4e, 0x4f, 0x57, 0x59, 0x5e, 0x5f, 0x89, 0x8e, 0x8f, 0xb1, 0xb6, 0xb7,
  252. 0xbf, 0xc1, 0xc6, 0xc7, 0xd7, 0x11, 0x16, 0x17, 0x5b, 0x5c, 0xf6, 0xf7,
  253. 0xfe, 0xff, 0x80, 0x0d, 0x6d, 0x71, 0xde, 0xdf, 0x0e, 0x0f, 0x1f, 0x6e,
  254. 0x6f, 0x1c, 0x1d, 0x5f, 0x7d, 0x7e, 0xae, 0xaf, 0xbb, 0xbc, 0xfa, 0x16,
  255. 0x17, 0x1e, 0x1f, 0x46, 0x47, 0x4e, 0x4f, 0x58, 0x5a, 0x5c, 0x5e, 0x7e,
  256. 0x7f, 0xb5, 0xc5, 0xd4, 0xd5, 0xdc, 0xf0, 0xf1, 0xf5, 0x72, 0x73, 0x8f,
  257. 0x74, 0x75, 0x96, 0x2f, 0x5f, 0x26, 0x2e, 0x2f, 0xa7, 0xaf, 0xb7, 0xbf,
  258. 0xc7, 0xcf, 0xd7, 0xdf, 0x9a, 0x40, 0x97, 0x98, 0x30, 0x8f, 0x1f, 0xc0,
  259. 0xc1, 0xce, 0xff, 0x4e, 0x4f, 0x5a, 0x5b, 0x07, 0x08, 0x0f, 0x10, 0x27,
  260. 0x2f, 0xee, 0xef, 0x6e, 0x6f, 0x37, 0x3d, 0x3f, 0x42, 0x45, 0x90, 0x91,
  261. 0xfe, 0xff, 0x53, 0x67, 0x75, 0xc8, 0xc9, 0xd0, 0xd1, 0xd8, 0xd9, 0xe7,
  262. 0xfe, 0xff,
  263. };
  264. static constexpr singleton singletons1[] = {
  265. {0x00, 6}, {0x01, 1}, {0x03, 1}, {0x04, 2}, {0x08, 8}, {0x09, 2},
  266. {0x0a, 5}, {0x0b, 2}, {0x0e, 4}, {0x10, 1}, {0x11, 2}, {0x12, 5},
  267. {0x13, 17}, {0x14, 1}, {0x15, 2}, {0x17, 2}, {0x19, 13}, {0x1c, 5},
  268. {0x1d, 8}, {0x24, 1}, {0x6a, 3}, {0x6b, 2}, {0xbc, 2}, {0xd1, 2},
  269. {0xd4, 12}, {0xd5, 9}, {0xd6, 2}, {0xd7, 2}, {0xda, 1}, {0xe0, 5},
  270. {0xe1, 2}, {0xe8, 2}, {0xee, 32}, {0xf0, 4}, {0xf8, 2}, {0xf9, 2},
  271. {0xfa, 2}, {0xfb, 1},
  272. };
  273. static constexpr unsigned char singletons1_lower[] = {
  274. 0x0c, 0x27, 0x3b, 0x3e, 0x4e, 0x4f, 0x8f, 0x9e, 0x9e, 0x9f, 0x06, 0x07,
  275. 0x09, 0x36, 0x3d, 0x3e, 0x56, 0xf3, 0xd0, 0xd1, 0x04, 0x14, 0x18, 0x36,
  276. 0x37, 0x56, 0x57, 0x7f, 0xaa, 0xae, 0xaf, 0xbd, 0x35, 0xe0, 0x12, 0x87,
  277. 0x89, 0x8e, 0x9e, 0x04, 0x0d, 0x0e, 0x11, 0x12, 0x29, 0x31, 0x34, 0x3a,
  278. 0x45, 0x46, 0x49, 0x4a, 0x4e, 0x4f, 0x64, 0x65, 0x5c, 0xb6, 0xb7, 0x1b,
  279. 0x1c, 0x07, 0x08, 0x0a, 0x0b, 0x14, 0x17, 0x36, 0x39, 0x3a, 0xa8, 0xa9,
  280. 0xd8, 0xd9, 0x09, 0x37, 0x90, 0x91, 0xa8, 0x07, 0x0a, 0x3b, 0x3e, 0x66,
  281. 0x69, 0x8f, 0x92, 0x6f, 0x5f, 0xee, 0xef, 0x5a, 0x62, 0x9a, 0x9b, 0x27,
  282. 0x28, 0x55, 0x9d, 0xa0, 0xa1, 0xa3, 0xa4, 0xa7, 0xa8, 0xad, 0xba, 0xbc,
  283. 0xc4, 0x06, 0x0b, 0x0c, 0x15, 0x1d, 0x3a, 0x3f, 0x45, 0x51, 0xa6, 0xa7,
  284. 0xcc, 0xcd, 0xa0, 0x07, 0x19, 0x1a, 0x22, 0x25, 0x3e, 0x3f, 0xc5, 0xc6,
  285. 0x04, 0x20, 0x23, 0x25, 0x26, 0x28, 0x33, 0x38, 0x3a, 0x48, 0x4a, 0x4c,
  286. 0x50, 0x53, 0x55, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x63, 0x65, 0x66,
  287. 0x6b, 0x73, 0x78, 0x7d, 0x7f, 0x8a, 0xa4, 0xaa, 0xaf, 0xb0, 0xc0, 0xd0,
  288. 0xae, 0xaf, 0x79, 0xcc, 0x6e, 0x6f, 0x93,
  289. };
  290. static constexpr unsigned char normal0[] = {
  291. 0x00, 0x20, 0x5f, 0x22, 0x82, 0xdf, 0x04, 0x82, 0x44, 0x08, 0x1b, 0x04,
  292. 0x06, 0x11, 0x81, 0xac, 0x0e, 0x80, 0xab, 0x35, 0x28, 0x0b, 0x80, 0xe0,
  293. 0x03, 0x19, 0x08, 0x01, 0x04, 0x2f, 0x04, 0x34, 0x04, 0x07, 0x03, 0x01,
  294. 0x07, 0x06, 0x07, 0x11, 0x0a, 0x50, 0x0f, 0x12, 0x07, 0x55, 0x07, 0x03,
  295. 0x04, 0x1c, 0x0a, 0x09, 0x03, 0x08, 0x03, 0x07, 0x03, 0x02, 0x03, 0x03,
  296. 0x03, 0x0c, 0x04, 0x05, 0x03, 0x0b, 0x06, 0x01, 0x0e, 0x15, 0x05, 0x3a,
  297. 0x03, 0x11, 0x07, 0x06, 0x05, 0x10, 0x07, 0x57, 0x07, 0x02, 0x07, 0x15,
  298. 0x0d, 0x50, 0x04, 0x43, 0x03, 0x2d, 0x03, 0x01, 0x04, 0x11, 0x06, 0x0f,
  299. 0x0c, 0x3a, 0x04, 0x1d, 0x25, 0x5f, 0x20, 0x6d, 0x04, 0x6a, 0x25, 0x80,
  300. 0xc8, 0x05, 0x82, 0xb0, 0x03, 0x1a, 0x06, 0x82, 0xfd, 0x03, 0x59, 0x07,
  301. 0x15, 0x0b, 0x17, 0x09, 0x14, 0x0c, 0x14, 0x0c, 0x6a, 0x06, 0x0a, 0x06,
  302. 0x1a, 0x06, 0x59, 0x07, 0x2b, 0x05, 0x46, 0x0a, 0x2c, 0x04, 0x0c, 0x04,
  303. 0x01, 0x03, 0x31, 0x0b, 0x2c, 0x04, 0x1a, 0x06, 0x0b, 0x03, 0x80, 0xac,
  304. 0x06, 0x0a, 0x06, 0x21, 0x3f, 0x4c, 0x04, 0x2d, 0x03, 0x74, 0x08, 0x3c,
  305. 0x03, 0x0f, 0x03, 0x3c, 0x07, 0x38, 0x08, 0x2b, 0x05, 0x82, 0xff, 0x11,
  306. 0x18, 0x08, 0x2f, 0x11, 0x2d, 0x03, 0x20, 0x10, 0x21, 0x0f, 0x80, 0x8c,
  307. 0x04, 0x82, 0x97, 0x19, 0x0b, 0x15, 0x88, 0x94, 0x05, 0x2f, 0x05, 0x3b,
  308. 0x07, 0x02, 0x0e, 0x18, 0x09, 0x80, 0xb3, 0x2d, 0x74, 0x0c, 0x80, 0xd6,
  309. 0x1a, 0x0c, 0x05, 0x80, 0xff, 0x05, 0x80, 0xdf, 0x0c, 0xee, 0x0d, 0x03,
  310. 0x84, 0x8d, 0x03, 0x37, 0x09, 0x81, 0x5c, 0x14, 0x80, 0xb8, 0x08, 0x80,
  311. 0xcb, 0x2a, 0x38, 0x03, 0x0a, 0x06, 0x38, 0x08, 0x46, 0x08, 0x0c, 0x06,
  312. 0x74, 0x0b, 0x1e, 0x03, 0x5a, 0x04, 0x59, 0x09, 0x80, 0x83, 0x18, 0x1c,
  313. 0x0a, 0x16, 0x09, 0x4c, 0x04, 0x80, 0x8a, 0x06, 0xab, 0xa4, 0x0c, 0x17,
  314. 0x04, 0x31, 0xa1, 0x04, 0x81, 0xda, 0x26, 0x07, 0x0c, 0x05, 0x05, 0x80,
  315. 0xa5, 0x11, 0x81, 0x6d, 0x10, 0x78, 0x28, 0x2a, 0x06, 0x4c, 0x04, 0x80,
  316. 0x8d, 0x04, 0x80, 0xbe, 0x03, 0x1b, 0x03, 0x0f, 0x0d,
  317. };
  318. static constexpr unsigned char normal1[] = {
  319. 0x5e, 0x22, 0x7b, 0x05, 0x03, 0x04, 0x2d, 0x03, 0x66, 0x03, 0x01, 0x2f,
  320. 0x2e, 0x80, 0x82, 0x1d, 0x03, 0x31, 0x0f, 0x1c, 0x04, 0x24, 0x09, 0x1e,
  321. 0x05, 0x2b, 0x05, 0x44, 0x04, 0x0e, 0x2a, 0x80, 0xaa, 0x06, 0x24, 0x04,
  322. 0x24, 0x04, 0x28, 0x08, 0x34, 0x0b, 0x01, 0x80, 0x90, 0x81, 0x37, 0x09,
  323. 0x16, 0x0a, 0x08, 0x80, 0x98, 0x39, 0x03, 0x63, 0x08, 0x09, 0x30, 0x16,
  324. 0x05, 0x21, 0x03, 0x1b, 0x05, 0x01, 0x40, 0x38, 0x04, 0x4b, 0x05, 0x2f,
  325. 0x04, 0x0a, 0x07, 0x09, 0x07, 0x40, 0x20, 0x27, 0x04, 0x0c, 0x09, 0x36,
  326. 0x03, 0x3a, 0x05, 0x1a, 0x07, 0x04, 0x0c, 0x07, 0x50, 0x49, 0x37, 0x33,
  327. 0x0d, 0x33, 0x07, 0x2e, 0x08, 0x0a, 0x81, 0x26, 0x52, 0x4e, 0x28, 0x08,
  328. 0x2a, 0x56, 0x1c, 0x14, 0x17, 0x09, 0x4e, 0x04, 0x1e, 0x0f, 0x43, 0x0e,
  329. 0x19, 0x07, 0x0a, 0x06, 0x48, 0x08, 0x27, 0x09, 0x75, 0x0b, 0x3f, 0x41,
  330. 0x2a, 0x06, 0x3b, 0x05, 0x0a, 0x06, 0x51, 0x06, 0x01, 0x05, 0x10, 0x03,
  331. 0x05, 0x80, 0x8b, 0x62, 0x1e, 0x48, 0x08, 0x0a, 0x80, 0xa6, 0x5e, 0x22,
  332. 0x45, 0x0b, 0x0a, 0x06, 0x0d, 0x13, 0x39, 0x07, 0x0a, 0x36, 0x2c, 0x04,
  333. 0x10, 0x80, 0xc0, 0x3c, 0x64, 0x53, 0x0c, 0x48, 0x09, 0x0a, 0x46, 0x45,
  334. 0x1b, 0x48, 0x08, 0x53, 0x1d, 0x39, 0x81, 0x07, 0x46, 0x0a, 0x1d, 0x03,
  335. 0x47, 0x49, 0x37, 0x03, 0x0e, 0x08, 0x0a, 0x06, 0x39, 0x07, 0x0a, 0x81,
  336. 0x36, 0x19, 0x80, 0xb7, 0x01, 0x0f, 0x32, 0x0d, 0x83, 0x9b, 0x66, 0x75,
  337. 0x0b, 0x80, 0xc4, 0x8a, 0xbc, 0x84, 0x2f, 0x8f, 0xd1, 0x82, 0x47, 0xa1,
  338. 0xb9, 0x82, 0x39, 0x07, 0x2a, 0x04, 0x02, 0x60, 0x26, 0x0a, 0x46, 0x0a,
  339. 0x28, 0x05, 0x13, 0x82, 0xb0, 0x5b, 0x65, 0x4b, 0x04, 0x39, 0x07, 0x11,
  340. 0x40, 0x05, 0x0b, 0x02, 0x0e, 0x97, 0xf8, 0x08, 0x84, 0xd6, 0x2a, 0x09,
  341. 0xa2, 0xf7, 0x81, 0x1f, 0x31, 0x03, 0x11, 0x04, 0x08, 0x81, 0x8c, 0x89,
  342. 0x04, 0x6b, 0x05, 0x0d, 0x03, 0x09, 0x07, 0x10, 0x93, 0x60, 0x80, 0xf6,
  343. 0x0a, 0x73, 0x08, 0x6e, 0x17, 0x46, 0x80, 0x9a, 0x14, 0x0c, 0x57, 0x09,
  344. 0x19, 0x80, 0x87, 0x81, 0x47, 0x03, 0x85, 0x42, 0x0f, 0x15, 0x85, 0x50,
  345. 0x2b, 0x80, 0xd5, 0x2d, 0x03, 0x1a, 0x04, 0x02, 0x81, 0x70, 0x3a, 0x05,
  346. 0x01, 0x85, 0x00, 0x80, 0xd7, 0x29, 0x4c, 0x04, 0x0a, 0x04, 0x02, 0x83,
  347. 0x11, 0x44, 0x4c, 0x3d, 0x80, 0xc2, 0x3c, 0x06, 0x01, 0x04, 0x55, 0x05,
  348. 0x1b, 0x34, 0x02, 0x81, 0x0e, 0x2c, 0x04, 0x64, 0x0c, 0x56, 0x0a, 0x80,
  349. 0xae, 0x38, 0x1d, 0x0d, 0x2c, 0x04, 0x09, 0x07, 0x02, 0x0e, 0x06, 0x80,
  350. 0x9a, 0x83, 0xd8, 0x08, 0x0d, 0x03, 0x0d, 0x03, 0x74, 0x0c, 0x59, 0x07,
  351. 0x0c, 0x14, 0x0c, 0x04, 0x38, 0x08, 0x0a, 0x06, 0x28, 0x08, 0x22, 0x4e,
  352. 0x81, 0x54, 0x0c, 0x15, 0x03, 0x03, 0x05, 0x07, 0x09, 0x19, 0x07, 0x07,
  353. 0x09, 0x03, 0x0d, 0x07, 0x29, 0x80, 0xcb, 0x25, 0x0a, 0x84, 0x06,
  354. };
  355. auto lower = static_cast<uint16_t>(cp);
  356. if (cp < 0x10000) {
  357. return is_printable(lower, singletons0,
  358. sizeof(singletons0) / sizeof(*singletons0),
  359. singletons0_lower, normal0, sizeof(normal0));
  360. }
  361. if (cp < 0x20000) {
  362. return is_printable(lower, singletons1,
  363. sizeof(singletons1) / sizeof(*singletons1),
  364. singletons1_lower, normal1, sizeof(normal1));
  365. }
  366. if (0x2a6de <= cp && cp < 0x2a700) return false;
  367. if (0x2b735 <= cp && cp < 0x2b740) return false;
  368. if (0x2b81e <= cp && cp < 0x2b820) return false;
  369. if (0x2cea2 <= cp && cp < 0x2ceb0) return false;
  370. if (0x2ebe1 <= cp && cp < 0x2f800) return false;
  371. if (0x2fa1e <= cp && cp < 0x30000) return false;
  372. if (0x3134b <= cp && cp < 0xe0100) return false;
  373. if (0xe01f0 <= cp && cp < 0x110000) return false;
  374. return cp < 0x110000;
  375. }
  376. inline auto needs_escape(uint32_t cp) -> bool {
  377. return cp < 0x20 || cp == 0x7f || cp == '"' || cp == '\\' ||
  378. !is_printable(cp);
  379. }
  380. template <typename Char> struct find_escape_result {
  381. const Char* begin;
  382. const Char* end;
  383. uint32_t cp;
  384. };
  385. template <typename Char>
  386. auto find_escape(const Char* begin, const Char* end)
  387. -> find_escape_result<Char> {
  388. for (; begin != end; ++begin) {
  389. auto cp = static_cast<typename std::make_unsigned<Char>::type>(*begin);
  390. if (sizeof(Char) == 1 && cp >= 0x80) continue;
  391. if (needs_escape(cp)) return {begin, begin + 1, cp};
  392. }
  393. return {begin, nullptr, 0};
  394. }
  395. inline auto find_escape(const char* begin, const char* end)
  396. -> find_escape_result<char> {
  397. if (!is_utf8()) return find_escape<char>(begin, end);
  398. auto result = find_escape_result<char>{end, nullptr, 0};
  399. for_each_codepoint(string_view(begin, to_unsigned(end - begin)),
  400. [&](uint32_t cp, string_view sv) {
  401. if (needs_escape(cp)) {
  402. result = {sv.begin(), sv.end(), cp};
  403. return false;
  404. }
  405. return true;
  406. });
  407. return result;
  408. }
  409. template <typename Char, typename OutputIt>
  410. auto write_range_entry(OutputIt out, basic_string_view<Char> str) -> OutputIt {
  411. *out++ = '"';
  412. auto begin = str.begin(), end = str.end();
  413. do {
  414. auto escape = find_escape(begin, end);
  415. out = copy_str<Char>(begin, escape.begin, out);
  416. begin = escape.end;
  417. if (!begin) break;
  418. auto c = static_cast<Char>(escape.cp);
  419. switch (escape.cp) {
  420. case '\n':
  421. *out++ = '\\';
  422. c = 'n';
  423. break;
  424. case '\r':
  425. *out++ = '\\';
  426. c = 'r';
  427. break;
  428. case '\t':
  429. *out++ = '\\';
  430. c = 't';
  431. break;
  432. case '"':
  433. FMT_FALLTHROUGH;
  434. case '\\':
  435. *out++ = '\\';
  436. break;
  437. default:
  438. if (is_utf8()) {
  439. if (escape.cp < 0x100) {
  440. out = format_to(out, "\\x{:02x}", escape.cp);
  441. continue;
  442. }
  443. if (escape.cp < 0x10000) {
  444. out = format_to(out, "\\u{:04x}", escape.cp);
  445. continue;
  446. }
  447. if (escape.cp < 0x110000) {
  448. out = format_to(out, "\\U{:08x}", escape.cp);
  449. continue;
  450. }
  451. }
  452. for (Char escape_char : basic_string_view<Char>(
  453. escape.begin, to_unsigned(escape.end - escape.begin))) {
  454. out = format_to(
  455. out, "\\x{:02x}",
  456. static_cast<typename std::make_unsigned<Char>::type>(escape_char));
  457. }
  458. continue;
  459. }
  460. *out++ = c;
  461. } while (begin != end);
  462. *out++ = '"';
  463. return out;
  464. }
  465. template <typename Char, typename OutputIt, typename T,
  466. FMT_ENABLE_IF(std::is_convertible<T, std_string_view<char>>::value)>
  467. inline auto write_range_entry(OutputIt out, const T& str) -> OutputIt {
  468. auto sv = std_string_view<Char>(str);
  469. return write_range_entry<Char>(out, basic_string_view<Char>(sv));
  470. }
  471. template <typename Char, typename OutputIt, typename Arg,
  472. FMT_ENABLE_IF(std::is_same<Arg, Char>::value)>
  473. OutputIt write_range_entry(OutputIt out, const Arg v) {
  474. *out++ = '\'';
  475. *out++ = v;
  476. *out++ = '\'';
  477. return out;
  478. }
  479. template <
  480. typename Char, typename OutputIt, typename Arg,
  481. FMT_ENABLE_IF(!is_std_string_like<typename std::decay<Arg>::type>::value &&
  482. !std::is_same<Arg, Char>::value)>
  483. OutputIt write_range_entry(OutputIt out, const Arg& v) {
  484. return write<Char>(out, v);
  485. }
  486. } // namespace detail
  487. template <typename T> struct is_tuple_like {
  488. static FMT_CONSTEXPR_DECL const bool value =
  489. detail::is_tuple_like_<T>::value && !detail::is_range_<T>::value;
  490. };
  491. template <typename TupleT, typename Char>
  492. struct formatter<TupleT, Char, enable_if_t<fmt::is_tuple_like<TupleT>::value>> {
  493. private:
  494. // C++11 generic lambda for format().
  495. template <typename FormatContext> struct format_each {
  496. template <typename T> void operator()(const T& v) {
  497. if (i > 0) out = detail::write_delimiter(out);
  498. out = detail::write_range_entry<Char>(out, v);
  499. ++i;
  500. }
  501. int i;
  502. typename FormatContext::iterator& out;
  503. };
  504. public:
  505. template <typename ParseContext>
  506. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  507. return ctx.begin();
  508. }
  509. template <typename FormatContext = format_context>
  510. auto format(const TupleT& values, FormatContext& ctx) -> decltype(ctx.out()) {
  511. auto out = ctx.out();
  512. *out++ = '(';
  513. detail::for_each(values, format_each<FormatContext>{0, out});
  514. *out++ = ')';
  515. return out;
  516. }
  517. };
  518. template <typename T, typename Char> struct is_range {
  519. static FMT_CONSTEXPR_DECL const bool value =
  520. detail::is_range_<T>::value && !detail::is_std_string_like<T>::value &&
  521. !detail::is_map<T>::value &&
  522. !std::is_convertible<T, std::basic_string<Char>>::value &&
  523. !std::is_constructible<detail::std_string_view<Char>, T>::value;
  524. };
  525. template <typename T, typename Char>
  526. struct formatter<
  527. T, Char,
  528. enable_if_t<
  529. fmt::is_range<T, Char>::value
  530. // Workaround a bug in MSVC 2019 and earlier.
  531. #if !FMT_MSC_VER
  532. && (is_formattable<detail::value_type<T>, Char>::value ||
  533. detail::has_fallback_formatter<detail::value_type<T>, Char>::value)
  534. #endif
  535. >> {
  536. template <typename ParseContext>
  537. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  538. return ctx.begin();
  539. }
  540. template <
  541. typename FormatContext, typename U,
  542. FMT_ENABLE_IF(
  543. std::is_same<U, conditional_t<detail::has_const_begin_end<T>::value,
  544. const T, T>>::value)>
  545. auto format(U& range, FormatContext& ctx) -> decltype(ctx.out()) {
  546. #ifdef FMT_DEPRECATED_BRACED_RANGES
  547. Char prefix = '{';
  548. Char postfix = '}';
  549. #else
  550. Char prefix = detail::is_set<T>::value ? '{' : '[';
  551. Char postfix = detail::is_set<T>::value ? '}' : ']';
  552. #endif
  553. auto out = ctx.out();
  554. *out++ = prefix;
  555. int i = 0;
  556. auto it = std::begin(range);
  557. auto end = std::end(range);
  558. for (; it != end; ++it) {
  559. if (i > 0) out = detail::write_delimiter(out);
  560. out = detail::write_range_entry<Char>(out, *it);
  561. ++i;
  562. }
  563. *out++ = postfix;
  564. return out;
  565. }
  566. };
  567. template <typename T, typename Char>
  568. struct formatter<
  569. T, Char,
  570. enable_if_t<
  571. detail::is_map<T>::value
  572. // Workaround a bug in MSVC 2019 and earlier.
  573. #if !FMT_MSC_VER
  574. && (is_formattable<detail::value_type<T>, Char>::value ||
  575. detail::has_fallback_formatter<detail::value_type<T>, Char>::value)
  576. #endif
  577. >> {
  578. template <typename ParseContext>
  579. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  580. return ctx.begin();
  581. }
  582. template <
  583. typename FormatContext, typename U,
  584. FMT_ENABLE_IF(
  585. std::is_same<U, conditional_t<detail::has_const_begin_end<T>::value,
  586. const T, T>>::value)>
  587. auto format(U& map, FormatContext& ctx) -> decltype(ctx.out()) {
  588. auto out = ctx.out();
  589. *out++ = '{';
  590. int i = 0;
  591. for (const auto& item : map) {
  592. if (i > 0) out = detail::write_delimiter(out);
  593. out = detail::write_range_entry<Char>(out, item.first);
  594. *out++ = ':';
  595. *out++ = ' ';
  596. out = detail::write_range_entry<Char>(out, item.second);
  597. ++i;
  598. }
  599. *out++ = '}';
  600. return out;
  601. }
  602. };
  603. template <typename Char, typename... T> struct tuple_join_view : detail::view {
  604. const std::tuple<T...>& tuple;
  605. basic_string_view<Char> sep;
  606. tuple_join_view(const std::tuple<T...>& t, basic_string_view<Char> s)
  607. : tuple(t), sep{s} {}
  608. };
  609. template <typename Char, typename... T>
  610. using tuple_arg_join = tuple_join_view<Char, T...>;
  611. // Define FMT_TUPLE_JOIN_SPECIFIERS to enable experimental format specifiers
  612. // support in tuple_join. It is disabled by default because of issues with
  613. // the dynamic width and precision.
  614. #ifndef FMT_TUPLE_JOIN_SPECIFIERS
  615. # define FMT_TUPLE_JOIN_SPECIFIERS 0
  616. #endif
  617. template <typename Char, typename... T>
  618. struct formatter<tuple_join_view<Char, T...>, Char> {
  619. template <typename ParseContext>
  620. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  621. return do_parse(ctx, std::integral_constant<size_t, sizeof...(T)>());
  622. }
  623. template <typename FormatContext>
  624. auto format(const tuple_join_view<Char, T...>& value,
  625. FormatContext& ctx) const -> typename FormatContext::iterator {
  626. return do_format(value, ctx,
  627. std::integral_constant<size_t, sizeof...(T)>());
  628. }
  629. private:
  630. std::tuple<formatter<typename std::decay<T>::type, Char>...> formatters_;
  631. template <typename ParseContext>
  632. FMT_CONSTEXPR auto do_parse(ParseContext& ctx,
  633. std::integral_constant<size_t, 0>)
  634. -> decltype(ctx.begin()) {
  635. return ctx.begin();
  636. }
  637. template <typename ParseContext, size_t N>
  638. FMT_CONSTEXPR auto do_parse(ParseContext& ctx,
  639. std::integral_constant<size_t, N>)
  640. -> decltype(ctx.begin()) {
  641. auto end = ctx.begin();
  642. #if FMT_TUPLE_JOIN_SPECIFIERS
  643. end = std::get<sizeof...(T) - N>(formatters_).parse(ctx);
  644. if (N > 1) {
  645. auto end1 = do_parse(ctx, std::integral_constant<size_t, N - 1>());
  646. if (end != end1)
  647. FMT_THROW(format_error("incompatible format specs for tuple elements"));
  648. }
  649. #endif
  650. return end;
  651. }
  652. template <typename FormatContext>
  653. auto do_format(const tuple_join_view<Char, T...>&, FormatContext& ctx,
  654. std::integral_constant<size_t, 0>) const ->
  655. typename FormatContext::iterator {
  656. return ctx.out();
  657. }
  658. template <typename FormatContext, size_t N>
  659. auto do_format(const tuple_join_view<Char, T...>& value, FormatContext& ctx,
  660. std::integral_constant<size_t, N>) const ->
  661. typename FormatContext::iterator {
  662. auto out = std::get<sizeof...(T) - N>(formatters_)
  663. .format(std::get<sizeof...(T) - N>(value.tuple), ctx);
  664. if (N > 1) {
  665. out = std::copy(value.sep.begin(), value.sep.end(), out);
  666. ctx.advance_to(out);
  667. return do_format(value, ctx, std::integral_constant<size_t, N - 1>());
  668. }
  669. return out;
  670. }
  671. };
  672. FMT_MODULE_EXPORT_BEGIN
  673. /**
  674. \rst
  675. Returns an object that formats `tuple` with elements separated by `sep`.
  676. **Example**::
  677. std::tuple<int, char> t = {1, 'a'};
  678. fmt::print("{}", fmt::join(t, ", "));
  679. // Output: "1, a"
  680. \endrst
  681. */
  682. template <typename... T>
  683. FMT_CONSTEXPR auto join(const std::tuple<T...>& tuple, string_view sep)
  684. -> tuple_join_view<char, T...> {
  685. return {tuple, sep};
  686. }
  687. template <typename... T>
  688. FMT_CONSTEXPR auto join(const std::tuple<T...>& tuple,
  689. basic_string_view<wchar_t> sep)
  690. -> tuple_join_view<wchar_t, T...> {
  691. return {tuple, sep};
  692. }
  693. /**
  694. \rst
  695. Returns an object that formats `initializer_list` with elements separated by
  696. `sep`.
  697. **Example**::
  698. fmt::print("{}", fmt::join({1, 2, 3}, ", "));
  699. // Output: "1, 2, 3"
  700. \endrst
  701. */
  702. template <typename T>
  703. auto join(std::initializer_list<T> list, string_view sep)
  704. -> join_view<const T*, const T*> {
  705. return join(std::begin(list), std::end(list), sep);
  706. }
  707. FMT_MODULE_EXPORT_END
  708. FMT_END_NAMESPACE
  709. #endif // FMT_RANGES_H_