xchar.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // Formatting library for C++ - optional wchar_t and exotic character support
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #ifndef FMT_XCHAR_H_
  8. #define FMT_XCHAR_H_
  9. #include "color.h"
  10. #include "format.h"
  11. #include "ostream.h"
  12. #include "ranges.h"
  13. #ifndef FMT_MODULE
  14. # include <cwchar>
  15. # if FMT_USE_LOCALE
  16. # include <locale>
  17. # endif
  18. #endif
  19. FMT_BEGIN_NAMESPACE
  20. namespace detail {
  21. template <typename T>
  22. using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;
  23. template <typename S, typename = void> struct format_string_char {};
  24. template <typename S>
  25. struct format_string_char<
  26. S, void_t<decltype(sizeof(detail::to_string_view(std::declval<S>())))>> {
  27. using type = char_t<S>;
  28. };
  29. template <typename S>
  30. struct format_string_char<
  31. S, enable_if_t<std::is_base_of<detail::compile_string, S>::value>> {
  32. using type = typename S::char_type;
  33. };
  34. template <typename S>
  35. using format_string_char_t = typename format_string_char<S>::type;
  36. inline auto write_loc(basic_appender<wchar_t> out, loc_value value,
  37. const format_specs& specs, locale_ref loc) -> bool {
  38. #if FMT_USE_LOCALE
  39. auto& numpunct =
  40. std::use_facet<std::numpunct<wchar_t>>(loc.get<std::locale>());
  41. auto separator = std::wstring();
  42. auto grouping = numpunct.grouping();
  43. if (!grouping.empty()) separator = std::wstring(1, numpunct.thousands_sep());
  44. return value.visit(loc_writer<wchar_t>{out, specs, separator, grouping, {}});
  45. #endif
  46. return false;
  47. }
  48. } // namespace detail
  49. FMT_BEGIN_EXPORT
  50. using wstring_view = basic_string_view<wchar_t>;
  51. using wformat_parse_context = parse_context<wchar_t>;
  52. using wformat_context = buffered_context<wchar_t>;
  53. using wformat_args = basic_format_args<wformat_context>;
  54. using wmemory_buffer = basic_memory_buffer<wchar_t>;
  55. template <typename Char, typename... T> struct basic_fstring {
  56. private:
  57. basic_string_view<Char> str_;
  58. static constexpr int num_static_named_args =
  59. detail::count_static_named_args<T...>();
  60. using checker = detail::format_string_checker<
  61. Char, static_cast<int>(sizeof...(T)), num_static_named_args,
  62. num_static_named_args != detail::count_named_args<T...>()>;
  63. using arg_pack = detail::arg_pack<T...>;
  64. public:
  65. using t = basic_fstring;
  66. template <typename S,
  67. FMT_ENABLE_IF(
  68. std::is_convertible<const S&, basic_string_view<Char>>::value)>
  69. FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_fstring(const S& s) : str_(s) {
  70. if (FMT_USE_CONSTEVAL)
  71. detail::parse_format_string<Char>(s, checker(s, arg_pack()));
  72. }
  73. template <typename S,
  74. FMT_ENABLE_IF(std::is_base_of<detail::compile_string, S>::value&&
  75. std::is_same<typename S::char_type, Char>::value)>
  76. FMT_ALWAYS_INLINE basic_fstring(const S&) : str_(S()) {
  77. FMT_CONSTEXPR auto sv = basic_string_view<Char>(S());
  78. FMT_CONSTEXPR int ignore =
  79. (parse_format_string(sv, checker(sv, arg_pack())), 0);
  80. detail::ignore_unused(ignore);
  81. }
  82. basic_fstring(runtime_format_string<Char> fmt) : str_(fmt.str) {}
  83. operator basic_string_view<Char>() const { return str_; }
  84. auto get() const -> basic_string_view<Char> { return str_; }
  85. };
  86. template <typename Char, typename... T>
  87. using basic_format_string = basic_fstring<Char, T...>;
  88. template <typename... T>
  89. using wformat_string = typename basic_format_string<wchar_t, T...>::t;
  90. inline auto runtime(wstring_view s) -> runtime_format_string<wchar_t> {
  91. return {{s}};
  92. }
  93. #ifdef __cpp_char8_t
  94. template <> struct is_char<char8_t> : bool_constant<detail::is_utf8_enabled> {};
  95. #endif
  96. template <typename... T>
  97. constexpr auto make_wformat_args(T&... args)
  98. -> decltype(fmt::make_format_args<wformat_context>(args...)) {
  99. return fmt::make_format_args<wformat_context>(args...);
  100. }
  101. #if !FMT_USE_NONTYPE_TEMPLATE_ARGS
  102. inline namespace literals {
  103. inline auto operator""_a(const wchar_t* s, size_t) -> detail::udl_arg<wchar_t> {
  104. return {s};
  105. }
  106. } // namespace literals
  107. #endif
  108. template <typename It, typename Sentinel>
  109. auto join(It begin, Sentinel end, wstring_view sep)
  110. -> join_view<It, Sentinel, wchar_t> {
  111. return {begin, end, sep};
  112. }
  113. template <typename Range, FMT_ENABLE_IF(!is_tuple_like<Range>::value)>
  114. auto join(Range&& range, wstring_view sep)
  115. -> join_view<decltype(std::begin(range)), decltype(std::end(range)),
  116. wchar_t> {
  117. return join(std::begin(range), std::end(range), sep);
  118. }
  119. template <typename T>
  120. auto join(std::initializer_list<T> list, wstring_view sep)
  121. -> join_view<const T*, const T*, wchar_t> {
  122. return join(std::begin(list), std::end(list), sep);
  123. }
  124. template <typename Tuple, FMT_ENABLE_IF(is_tuple_like<Tuple>::value)>
  125. auto join(const Tuple& tuple, basic_string_view<wchar_t> sep)
  126. -> tuple_join_view<wchar_t, Tuple> {
  127. return {tuple, sep};
  128. }
  129. template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
  130. auto vformat(basic_string_view<Char> fmt,
  131. typename detail::vformat_args<Char>::type args)
  132. -> std::basic_string<Char> {
  133. auto buf = basic_memory_buffer<Char>();
  134. detail::vformat_to(buf, fmt, args);
  135. return {buf.data(), buf.size()};
  136. }
  137. template <typename... T>
  138. auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
  139. return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
  140. }
  141. template <typename OutputIt, typename... T>
  142. auto format_to(OutputIt out, wformat_string<T...> fmt, T&&... args)
  143. -> OutputIt {
  144. return vformat_to(out, fmt::wstring_view(fmt),
  145. fmt::make_wformat_args(args...));
  146. }
  147. // Pass char_t as a default template parameter instead of using
  148. // std::basic_string<char_t<S>> to reduce the symbol size.
  149. template <typename S, typename... T,
  150. typename Char = detail::format_string_char_t<S>,
  151. FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
  152. !std::is_same<Char, wchar_t>::value)>
  153. auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {
  154. return vformat(detail::to_string_view(fmt),
  155. fmt::make_format_args<buffered_context<Char>>(args...));
  156. }
  157. template <typename Locale, typename S,
  158. typename Char = detail::format_string_char_t<S>,
  159. FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
  160. detail::is_exotic_char<Char>::value)>
  161. inline auto vformat(const Locale& loc, const S& fmt,
  162. typename detail::vformat_args<Char>::type args)
  163. -> std::basic_string<Char> {
  164. auto buf = basic_memory_buffer<Char>();
  165. detail::vformat_to(buf, detail::to_string_view(fmt), args,
  166. detail::locale_ref(loc));
  167. return {buf.data(), buf.size()};
  168. }
  169. template <typename Locale, typename S, typename... T,
  170. typename Char = detail::format_string_char_t<S>,
  171. FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
  172. detail::is_exotic_char<Char>::value)>
  173. inline auto format(const Locale& loc, const S& fmt, T&&... args)
  174. -> std::basic_string<Char> {
  175. return vformat(loc, detail::to_string_view(fmt),
  176. fmt::make_format_args<buffered_context<Char>>(args...));
  177. }
  178. template <typename OutputIt, typename S,
  179. typename Char = detail::format_string_char_t<S>,
  180. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  181. detail::is_exotic_char<Char>::value)>
  182. auto vformat_to(OutputIt out, const S& fmt,
  183. typename detail::vformat_args<Char>::type args) -> OutputIt {
  184. auto&& buf = detail::get_buffer<Char>(out);
  185. detail::vformat_to(buf, detail::to_string_view(fmt), args);
  186. return detail::get_iterator(buf, out);
  187. }
  188. template <typename OutputIt, typename S, typename... T,
  189. typename Char = detail::format_string_char_t<S>,
  190. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value &&
  191. !std::is_same<Char, char>::value &&
  192. !std::is_same<Char, wchar_t>::value)>
  193. inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
  194. return vformat_to(out, detail::to_string_view(fmt),
  195. fmt::make_format_args<buffered_context<Char>>(args...));
  196. }
  197. template <typename Locale, typename S, typename OutputIt, typename... Args,
  198. typename Char = detail::format_string_char_t<S>,
  199. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  200. detail::is_locale<Locale>::value&&
  201. detail::is_exotic_char<Char>::value)>
  202. inline auto vformat_to(OutputIt out, const Locale& loc, const S& fmt,
  203. typename detail::vformat_args<Char>::type args)
  204. -> OutputIt {
  205. auto&& buf = detail::get_buffer<Char>(out);
  206. vformat_to(buf, detail::to_string_view(fmt), args, detail::locale_ref(loc));
  207. return detail::get_iterator(buf, out);
  208. }
  209. template <typename Locale, typename OutputIt, typename S, typename... T,
  210. typename Char = detail::format_string_char_t<S>,
  211. bool enable = detail::is_output_iterator<OutputIt, Char>::value &&
  212. detail::is_locale<Locale>::value &&
  213. detail::is_exotic_char<Char>::value>
  214. inline auto format_to(OutputIt out, const Locale& loc, const S& fmt,
  215. 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. typename detail::vformat_args<Char>::type 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. template <typename... T>
  285. FMT_DEPRECATED void print(std::FILE* f, text_style ts, wformat_string<T...> fmt,
  286. const T&... args) {
  287. vprint(f, ts, fmt, fmt::make_wformat_args(args...));
  288. }
  289. template <typename... T>
  290. FMT_DEPRECATED void print(text_style ts, wformat_string<T...> fmt,
  291. const T&... args) {
  292. return print(stdout, ts, fmt, args...);
  293. }
  294. inline void vprint(std::wostream& os, wstring_view fmt, wformat_args args) {
  295. auto buffer = basic_memory_buffer<wchar_t>();
  296. detail::vformat_to(buffer, fmt, args);
  297. detail::write_buffer(os, buffer);
  298. }
  299. template <typename... T>
  300. void print(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
  301. vprint(os, fmt, fmt::make_format_args<buffered_context<wchar_t>>(args...));
  302. }
  303. template <typename... T>
  304. void println(std::wostream& os, wformat_string<T...> fmt, T&&... args) {
  305. print(os, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
  306. }
  307. /// Converts `value` to `std::wstring` using the default format for type `T`.
  308. template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
  309. return format(FMT_STRING(L"{}"), value);
  310. }
  311. FMT_END_EXPORT
  312. FMT_END_NAMESPACE
  313. #endif // FMT_XCHAR_H_