xchar.h 9.0 KB

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