xchar.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 <cwchar>
  10. #include "format.h"
  11. #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
  12. # include <locale>
  13. #endif
  14. FMT_BEGIN_NAMESPACE
  15. namespace detail {
  16. template <typename T>
  17. using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;
  18. inline auto write_loc(std::back_insert_iterator<detail::buffer<wchar_t>> out,
  19. loc_value value, const format_specs<wchar_t>& specs,
  20. locale_ref loc) -> bool {
  21. #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
  22. auto& numpunct =
  23. std::use_facet<std::numpunct<wchar_t>>(loc.get<std::locale>());
  24. auto separator = std::wstring();
  25. auto grouping = numpunct.grouping();
  26. if (!grouping.empty()) separator = std::wstring(1, numpunct.thousands_sep());
  27. return value.visit(loc_writer<wchar_t>{out, specs, separator, grouping, {}});
  28. #endif
  29. return false;
  30. }
  31. } // namespace detail
  32. FMT_BEGIN_EXPORT
  33. using wstring_view = basic_string_view<wchar_t>;
  34. using wformat_parse_context = basic_format_parse_context<wchar_t>;
  35. using wformat_context = buffer_context<wchar_t>;
  36. using wformat_args = basic_format_args<wformat_context>;
  37. using wmemory_buffer = basic_memory_buffer<wchar_t>;
  38. #if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
  39. // Workaround broken conversion on older gcc.
  40. template <typename... Args> using wformat_string = wstring_view;
  41. inline auto runtime(wstring_view s) -> wstring_view { return s; }
  42. #else
  43. template <typename... Args>
  44. using wformat_string = basic_format_string<wchar_t, type_identity_t<Args>...>;
  45. inline auto runtime(wstring_view s) -> runtime_format_string<wchar_t> {
  46. return {{s}};
  47. }
  48. #endif
  49. template <> struct is_char<wchar_t> : std::true_type {};
  50. template <> struct is_char<detail::char8_type> : std::true_type {};
  51. template <> struct is_char<char16_t> : std::true_type {};
  52. template <> struct is_char<char32_t> : std::true_type {};
  53. template <typename... T>
  54. constexpr auto make_wformat_args(const T&... args)
  55. -> format_arg_store<wformat_context, T...> {
  56. return {args...};
  57. }
  58. inline namespace literals {
  59. #if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS
  60. constexpr auto operator""_a(const wchar_t* s, size_t)
  61. -> detail::udl_arg<wchar_t> {
  62. return {s};
  63. }
  64. #endif
  65. } // namespace literals
  66. template <typename It, typename Sentinel>
  67. auto join(It begin, Sentinel end, wstring_view sep)
  68. -> join_view<It, Sentinel, wchar_t> {
  69. return {begin, end, sep};
  70. }
  71. template <typename Range>
  72. auto join(Range&& range, wstring_view sep)
  73. -> join_view<detail::iterator_t<Range>, detail::sentinel_t<Range>,
  74. wchar_t> {
  75. return join(std::begin(range), std::end(range), sep);
  76. }
  77. template <typename T>
  78. auto join(std::initializer_list<T> list, wstring_view sep)
  79. -> join_view<const T*, const T*, wchar_t> {
  80. return join(std::begin(list), std::end(list), sep);
  81. }
  82. template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
  83. auto vformat(basic_string_view<Char> format_str,
  84. basic_format_args<buffer_context<type_identity_t<Char>>> args)
  85. -> std::basic_string<Char> {
  86. auto buf = basic_memory_buffer<Char>();
  87. detail::vformat_to(buf, format_str, args);
  88. return to_string(buf);
  89. }
  90. template <typename... T>
  91. auto format(wformat_string<T...> fmt, T&&... args) -> std::wstring {
  92. return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...));
  93. }
  94. // Pass char_t as a default template parameter instead of using
  95. // std::basic_string<char_t<S>> to reduce the symbol size.
  96. template <typename S, typename... T, typename Char = char_t<S>,
  97. FMT_ENABLE_IF(!std::is_same<Char, char>::value &&
  98. !std::is_same<Char, wchar_t>::value)>
  99. auto format(const S& format_str, T&&... args) -> std::basic_string<Char> {
  100. return vformat(detail::to_string_view(format_str),
  101. fmt::make_format_args<buffer_context<Char>>(args...));
  102. }
  103. template <typename Locale, typename S, typename Char = char_t<S>,
  104. FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
  105. detail::is_exotic_char<Char>::value)>
  106. inline auto vformat(
  107. const Locale& loc, const S& format_str,
  108. basic_format_args<buffer_context<type_identity_t<Char>>> args)
  109. -> std::basic_string<Char> {
  110. return detail::vformat(loc, detail::to_string_view(format_str), args);
  111. }
  112. template <typename Locale, typename S, typename... T, typename Char = char_t<S>,
  113. FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
  114. detail::is_exotic_char<Char>::value)>
  115. inline auto format(const Locale& loc, const S& format_str, T&&... args)
  116. -> std::basic_string<Char> {
  117. return detail::vformat(loc, detail::to_string_view(format_str),
  118. fmt::make_format_args<buffer_context<Char>>(args...));
  119. }
  120. template <typename OutputIt, typename S, typename Char = char_t<S>,
  121. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  122. detail::is_exotic_char<Char>::value)>
  123. auto vformat_to(OutputIt out, const S& format_str,
  124. basic_format_args<buffer_context<type_identity_t<Char>>> args)
  125. -> OutputIt {
  126. auto&& buf = detail::get_buffer<Char>(out);
  127. detail::vformat_to(buf, detail::to_string_view(format_str), args);
  128. return detail::get_iterator(buf, out);
  129. }
  130. template <typename OutputIt, typename S, typename... T,
  131. typename Char = char_t<S>,
  132. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  133. detail::is_exotic_char<Char>::value)>
  134. inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
  135. return vformat_to(out, detail::to_string_view(fmt),
  136. fmt::make_format_args<buffer_context<Char>>(args...));
  137. }
  138. template <typename Locale, typename S, typename OutputIt, typename... Args,
  139. typename Char = char_t<S>,
  140. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  141. detail::is_locale<Locale>::value&&
  142. detail::is_exotic_char<Char>::value)>
  143. inline auto vformat_to(
  144. OutputIt out, const Locale& loc, const S& format_str,
  145. basic_format_args<buffer_context<type_identity_t<Char>>> args) -> OutputIt {
  146. auto&& buf = detail::get_buffer<Char>(out);
  147. vformat_to(buf, detail::to_string_view(format_str), args,
  148. detail::locale_ref(loc));
  149. return detail::get_iterator(buf, out);
  150. }
  151. template <typename OutputIt, typename Locale, typename S, typename... T,
  152. typename Char = char_t<S>,
  153. bool enable = detail::is_output_iterator<OutputIt, Char>::value &&
  154. detail::is_locale<Locale>::value &&
  155. detail::is_exotic_char<Char>::value>
  156. inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
  157. T&&... args) ->
  158. typename std::enable_if<enable, OutputIt>::type {
  159. return vformat_to(out, loc, detail::to_string_view(format_str),
  160. fmt::make_format_args<buffer_context<Char>>(args...));
  161. }
  162. template <typename OutputIt, typename Char, typename... Args,
  163. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  164. detail::is_exotic_char<Char>::value)>
  165. inline auto vformat_to_n(
  166. OutputIt out, size_t n, basic_string_view<Char> format_str,
  167. basic_format_args<buffer_context<type_identity_t<Char>>> args)
  168. -> format_to_n_result<OutputIt> {
  169. using traits = detail::fixed_buffer_traits;
  170. auto buf = detail::iterator_buffer<OutputIt, Char, traits>(out, n);
  171. detail::vformat_to(buf, format_str, args);
  172. return {buf.out(), buf.count()};
  173. }
  174. template <typename OutputIt, typename S, typename... T,
  175. typename Char = char_t<S>,
  176. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
  177. detail::is_exotic_char<Char>::value)>
  178. inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
  179. -> format_to_n_result<OutputIt> {
  180. return vformat_to_n(out, n, detail::to_string_view(fmt),
  181. fmt::make_format_args<buffer_context<Char>>(args...));
  182. }
  183. template <typename S, typename... T, typename Char = char_t<S>,
  184. FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
  185. inline auto formatted_size(const S& fmt, T&&... args) -> size_t {
  186. auto buf = detail::counting_buffer<Char>();
  187. detail::vformat_to(buf, detail::to_string_view(fmt),
  188. fmt::make_format_args<buffer_context<Char>>(args...));
  189. return buf.count();
  190. }
  191. inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
  192. auto buf = wmemory_buffer();
  193. detail::vformat_to(buf, fmt, args);
  194. buf.push_back(L'\0');
  195. if (std::fputws(buf.data(), f) == -1)
  196. FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
  197. }
  198. inline void vprint(wstring_view fmt, wformat_args args) {
  199. vprint(stdout, fmt, args);
  200. }
  201. template <typename... T>
  202. void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
  203. return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
  204. }
  205. template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
  206. return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
  207. }
  208. template <typename... T>
  209. void println(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
  210. return print(f, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
  211. }
  212. template <typename... T> void println(wformat_string<T...> fmt, T&&... args) {
  213. return print(L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
  214. }
  215. /**
  216. Converts *value* to ``std::wstring`` using the default format for type *T*.
  217. */
  218. template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
  219. return format(FMT_STRING(L"{}"), value);
  220. }
  221. FMT_END_EXPORT
  222. FMT_END_NAMESPACE
  223. #endif // FMT_XCHAR_H_