xchar.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. // Formatting library for C++ - optional wchar_t and exotic character support
  3. //
  4. // Copyright (c) 2012 - present, Victor Zverovich
  5. // All rights reserved.
  6. //
  7. // For the license information refer to format.h.
  8. #ifndef FMT_XCHAR_H_
  9. #define FMT_XCHAR_H_
  10. #include "color.h"
  11. #include "format.h"
  12. #include "ostream.h"
  13. #include "ranges.h"
  14. #ifndef FMT_MODULE
  15. # include <cwchar>
  16. # if FMT_USE_LOCALE
  17. # include <locale>
  18. # endif
  19. #endif
  20. FMT_BEGIN_NAMESPACE
  21. namespace detail {
  22. template <typename T>
  23. using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;
  24. template <typename S, typename = void> struct format_string_char {};
  25. template <typename S>
  26. struct format_string_char<
  27. S, void_t<decltype(sizeof(detail::to_string_view(std::declval<S>())))>> {
  28. using type = char_t<S>;
  29. };
  30. template <typename S>
  31. struct format_string_char<
  32. S, enable_if_t<std::is_base_of<detail::compile_string, S>::value>> {
  33. using type = typename S::char_type;
  34. };
  35. template <typename S>
  36. using format_string_char_t = typename format_string_char<S>::type;
  37. inline auto write_loc(basic_appender<wchar_t> out, loc_value value,
  38. const format_specs& specs, locale_ref loc) -> bool {
  39. #if FMT_USE_LOCALE
  40. auto& numpunct =
  41. std::use_facet<std::numpunct<wchar_t>>(loc.get<std::locale>());
  42. auto separator = std::wstring();
  43. auto grouping = numpunct.grouping();
  44. if (!grouping.empty()) separator = std::wstring(1, numpunct.thousands_sep());
  45. return value.visit(loc_writer<wchar_t>{out, specs, separator, grouping, {}});
  46. #endif
  47. return false;
  48. }
  49. template <typename Char>
  50. void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
  51. basic_format_args<buffered_context<Char>> args,
  52. locale_ref loc = {}) {
  53. static_assert(!std::is_same<Char, char>::value, "");
  54. auto out = basic_appender<Char>(buf);
  55. parse_format_string(
  56. fmt, format_handler<Char>{parse_context<Char>(fmt), {out, args, loc}});
  57. }
  58. } // namespace detail
  59. FMT_BEGIN_EXPORT
  60. using wstring_view = basic_string_view<wchar_t>;
  61. using wformat_parse_context = parse_context<wchar_t>;
  62. using wformat_context = buffered_context<wchar_t>;
  63. using wformat_args = basic_format_args<wformat_context>;
  64. using wmemory_buffer = basic_memory_buffer<wchar_t>;
  65. template <typename Char, typename... T> struct basic_fstring {
  66. private:
  67. basic_string_view<Char> str_;
  68. static constexpr int num_static_named_args =
  69. detail::count_static_named_args<T...>();
  70. using checker = detail::format_string_checker<
  71. Char, static_cast<int>(sizeof...(T)), num_static_named_args,
  72. num_static_named_args != detail::count_named_args<T...>()>;
  73. using arg_pack = detail::arg_pack<T...>;
  74. public:
  75. using t = basic_fstring;
  76. template <typename S,
  77. FMT_ENABLE_IF(
  78. std::is_convertible<const S&, basic_string_view<Char>>::value)>
  79. FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_fstring(const S& s) : str_(s) {
  80. if (FMT_USE_CONSTEVAL)
  81. detail::parse_format_string<Char>(s, checker(s, arg_pack()));
  82. }
  83. template <typename S,
  84. FMT_ENABLE_IF(std::is_base_of<detail::compile_string, S>::value&&
  85. std::is_same<typename S::char_type, Char>::value)>
  86. FMT_ALWAYS_INLINE basic_fstring(const S&) : str_(S()) {
  87. FMT_CONSTEXPR auto sv = basic_string_view<Char>(S());
  88. FMT_CONSTEXPR int ignore =
  89. (parse_format_string(sv, checker(sv, arg_pack())), 0);
  90. detail::ignore_unused(ignore);
  91. }
  92. basic_fstring(runtime_format_string<Char> fmt) : str_(fmt.str) {}
  93. operator basic_string_view<Char>() const { return str_; }
  94. auto get() const -> basic_string_view<Char> { return str_; }
  95. };
  96. template <typename Char, typename... T>
  97. using basic_format_string = basic_fstring<Char, T...>;
  98. template <typename... T>
  99. using wformat_string = typename basic_format_string<wchar_t, T...>::t;
  100. inline auto runtime(wstring_view s) -> runtime_format_string<wchar_t> {
  101. return {{s}};
  102. }
  103. template <typename... T>
  104. constexpr auto make_wformat_args(T&... args)
  105. -> decltype(fmt::make_format_args<wformat_context>(args...)) {
  106. return fmt::make_format_args<wformat_context>(args...);
  107. }
  108. #if !FMT_USE_NONTYPE_TEMPLATE_ARGS
  109. inline namespace literals {
  110. inline auto operator""_a(const wchar_t* s, size_t) -> detail::udl_arg<wchar_t> {
  111. return {s};
  112. }
  113. } // namespace literals
  114. #endif
  115. template <typename It, typename Sentinel>
  116. auto join(It begin, Sentinel end, wstring_view sep)
  117. -> join_view<It, Sentinel, wchar_t> {
  118. return {begin, end, sep};
  119. }
  120. template <typename Range, FMT_ENABLE_IF(!is_tuple_like<Range>::value)>
  121. auto join(Range&& range, wstring_view sep)
  122. -> join_view<decltype(std::begin(range)), decltype(std::end(range)),
  123. wchar_t> {
  124. return join(std::begin(range), std::end(range), sep);
  125. }
  126. template <typename T>
  127. auto join(std::initializer_list<T> list, wstring_view sep)
  128. -> join_view<const T*, const T*, wchar_t> {
  129. return join(std::begin(list), std::end(list), sep);
  130. }
  131. template <typename Tuple, FMT_ENABLE_IF(is_tuple_like<Tuple>::value)>
  132. auto join(const Tuple& tuple, basic_string_view<wchar_t> sep)
  133. -> tuple_join_view<Tuple, wchar_t> {
  134. return {tuple, sep};
  135. }
  136. template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
  137. auto vformat(basic_string_view<Char> fmt,
  138. basic_format_args<buffered_context<Char>> args)
  139. -> std::basic_string<Char> {
  140. auto buf = basic_memory_buffer<Char>();
  141. detail::vformat_to(buf, fmt, args);
  142. return {buf.data(), buf.size()};
  143. }
  144. template <typename... T>
  145. auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
  146. return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
  147. }
  148. template <typename OutputIt, typename... T>
  149. auto format_to(OutputIt out, wformat_string<T...> fmt, T&&... args)
  150. -> OutputIt {
  151. return vformat_to(out, fmt::wstring_view(fmt),
  152. fmt::make_wformat_args(args...));
  153. }
  154. // Pass char_t as a default template parameter instead of using
  155. // std::basic_string<char_t<S>> to reduce the symbol size.
  156. template <typename S, typename... T,
  157. typename Char = detail::format_string_char_t<S>,
  158. FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
  159. !std::is_same<Char, wchar_t>::value)>
  160. auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {
  161. return vformat(detail::to_string_view(fmt),
  162. fmt::make_format_args<buffered_context<Char>>(args...));
  163. }
  164. template <typename S, typename Char = detail::format_string_char_t<S>,
  165. FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
  166. inline auto vformat(locale_ref loc, const S& fmt,
  167. basic_format_args<buffered_context<Char>> args)
  168. -> std::basic_string<Char> {
  169. auto buf = basic_memory_buffer<Char>();
  170. detail::vformat_to(buf, detail::to_string_view(fmt), args, loc);
  171. return {buf.data(), buf.size()};
  172. }
  173. template <typename S, typename... T,
  174. typename Char = detail::format_string_char_t<S>,
  175. FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
  176. inline auto format(locale_ref loc, const S& fmt, T&&... args)
  177. -> std::basic_string<Char> {
  178. return vformat(loc, detail::to_string_view(fmt),
  179. fmt::make_format_args<buffered_context<Char>>(args...));
  180. }
  181. template <typename OutputIt, typename S,
  182. typename Char = detail::format_string_char_t<S>,
  183. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  184. detail::is_exotic_char<Char>::value)>
  185. auto vformat_to(OutputIt out, const S& fmt,
  186. basic_format_args<buffered_context<Char>> args) -> OutputIt {
  187. auto&& buf = detail::get_buffer<Char>(out);
  188. detail::vformat_to(buf, detail::to_string_view(fmt), args);
  189. return detail::get_iterator(buf, out);
  190. }
  191. template <typename OutputIt, typename S, typename... T,
  192. typename Char = detail::format_string_char_t<S>,
  193. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value &&
  194. !std::is_same<Char, char>::value &&
  195. !std::is_same<Char, wchar_t>::value)>
  196. inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
  197. return vformat_to(out, detail::to_string_view(fmt),
  198. fmt::make_format_args<buffered_context<Char>>(args...));
  199. }
  200. template <typename S, typename OutputIt, typename... Args,
  201. typename Char = detail::format_string_char_t<S>,
  202. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  203. detail::is_exotic_char<Char>::value)>
  204. inline auto vformat_to(OutputIt out, locale_ref loc, const S& fmt,
  205. basic_format_args<buffered_context<Char>> args)
  206. -> OutputIt {
  207. auto&& buf = detail::get_buffer<Char>(out);
  208. vformat_to(buf, detail::to_string_view(fmt), args, loc);
  209. return detail::get_iterator(buf, out);
  210. }
  211. template <typename OutputIt, typename S, typename... T,
  212. typename Char = detail::format_string_char_t<S>,
  213. bool enable = detail::is_output_iterator<OutputIt, Char>::value &&
  214. detail::is_exotic_char<Char>::value>
  215. inline auto format_to(OutputIt out, locale_ref loc, const S& fmt, T&&... args)
  216. -> typename std::enable_if<enable, OutputIt>::type {
  217. return vformat_to(out, loc, detail::to_string_view(fmt),
  218. fmt::make_format_args<buffered_context<Char>>(args...));
  219. }
  220. template <typename OutputIt, typename Char, typename... Args,
  221. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  222. detail::is_exotic_char<Char>::value)>
  223. inline auto vformat_to_n(OutputIt out, size_t n, basic_string_view<Char> fmt,
  224. basic_format_args<buffered_context<Char>> args)
  225. -> format_to_n_result<OutputIt> {
  226. using traits = detail::fixed_buffer_traits;
  227. auto buf = detail::iterator_buffer<OutputIt, Char, traits>(out, n);
  228. detail::vformat_to(buf, fmt, args);
  229. return {buf.out(), buf.count()};
  230. }
  231. template <typename OutputIt, typename S, typename... T,
  232. typename Char = detail::format_string_char_t<S>,
  233. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  234. detail::is_exotic_char<Char>::value)>
  235. inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
  236. -> format_to_n_result<OutputIt> {
  237. return vformat_to_n(out, n, fmt::basic_string_view<Char>(fmt),
  238. fmt::make_format_args<buffered_context<Char>>(args...));
  239. }
  240. template <typename S, typename... T,
  241. typename Char = detail::format_string_char_t<S>,
  242. FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
  243. inline auto formatted_size(const S& fmt, T&&... args) -> size_t {
  244. auto buf = detail::counting_buffer<Char>();
  245. detail::vformat_to(buf, detail::to_string_view(fmt),
  246. fmt::make_format_args<buffered_context<Char>>(args...));
  247. return buf.count();
  248. }
  249. inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
  250. auto buf = wmemory_buffer();
  251. detail::vformat_to(buf, fmt, args);
  252. buf.push_back(L'\0');
  253. if (std::fputws(buf.data(), f) == -1)
  254. FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
  255. }
  256. inline void vprint(wstring_view fmt, wformat_args args) {
  257. vprint(stdout, fmt, args);
  258. }
  259. template <typename... T>
  260. void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
  261. return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
  262. }
  263. template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
  264. return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
  265. }
  266. template <typename... T>
  267. void println(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
  268. return print(f, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
  269. }
  270. template <typename... T> void println(wformat_string<T...> fmt, T&&... args) {
  271. return print(L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
  272. }
  273. inline auto vformat(text_style ts, wstring_view fmt, wformat_args args)
  274. -> std::wstring {
  275. auto buf = wmemory_buffer();
  276. detail::vformat_to(buf, ts, fmt, args);
  277. return {buf.data(), buf.size()};
  278. }
  279. template <typename... T>
  280. inline auto format(text_style ts, wformat_string<T...> fmt, T&&... args)
  281. -> std::wstring {
  282. return fmt::vformat(ts, fmt, fmt::make_wformat_args(args...));
  283. }
  284. inline void vprint(std::wostream& os, wstring_view fmt, wformat_args args) {
  285. auto buffer = basic_memory_buffer<wchar_t>();
  286. detail::vformat_to(buffer, fmt, args);
  287. detail::write_buffer(os, buffer);
  288. }
  289. template <typename... T>
  290. void print(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
  291. vprint(os, fmt, fmt::make_format_args<buffered_context<wchar_t>>(args...));
  292. }
  293. template <typename... T>
  294. void println(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
  295. print(os, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
  296. }
  297. /// Converts `value` to `std::wstring` using the default format for type `T`.
  298. template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
  299. return format(FMT_STRING(L"{}"), value);
  300. }
  301. FMT_END_EXPORT
  302. FMT_END_NAMESPACE
  303. #endif // FMT_XCHAR_H_
  304. #else
  305. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  306. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)