ext-fmt/include/fmt/xchar.h

237 lines
9.0 KiB
C
Raw Normal View History

// Formatting library for C++ - optional wchar_t and exotic character support
2021-05-18 03:47:38 +00:00
//
// Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#ifndef FMT_XCHAR_H_
#define FMT_XCHAR_H_
2021-05-18 03:47:38 +00:00
2021-05-18 05:19:59 +00:00
#include <cwchar>
2021-06-03 00:06:02 +00:00
#include <tuple>
2021-05-18 05:19:59 +00:00
2021-05-18 03:47:38 +00:00
#include "format.h"
2021-05-19 02:38:52 +00:00
FMT_BEGIN_NAMESPACE
2021-05-24 02:50:17 +00:00
namespace detail {
template <typename T>
using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;
}
FMT_MODULE_EXPORT_BEGIN
2021-05-18 03:47:38 +00:00
2021-05-22 00:37:07 +00:00
using wstring_view = basic_string_view<wchar_t>;
using wformat_parse_context = basic_format_parse_context<wchar_t>;
using wformat_context = buffer_context<wchar_t>;
using wformat_args = basic_format_args<wformat_context>;
2021-05-29 15:26:04 +00:00
using wmemory_buffer = basic_memory_buffer<wchar_t>;
2021-05-18 03:47:38 +00:00
#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
// Workaround broken conversion on older gcc.
template <typename... Args> using wformat_string = wstring_view;
#else
template <typename... Args>
using wformat_string = basic_format_string<wchar_t, type_identity_t<Args>...>;
#endif
template <> struct is_char<wchar_t> : std::true_type {};
template <> struct is_char<detail::char8_type> : std::true_type {};
template <> struct is_char<char16_t> : std::true_type {};
template <> struct is_char<char32_t> : std::true_type {};
2021-05-18 03:47:38 +00:00
template <typename... Args>
constexpr format_arg_store<wformat_context, Args...> make_wformat_args(
const Args&... args) {
return {args...};
}
2021-05-19 02:38:52 +00:00
inline namespace literals {
constexpr auto operator"" _format(const wchar_t* s, size_t n)
-> detail::udl_formatter<wchar_t> {
return {{s, n}};
}
2021-05-22 00:37:07 +00:00
#if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t* s, size_t) {
return {s};
}
#endif
2021-05-19 02:38:52 +00:00
} // namespace literals
2021-05-22 00:37:07 +00:00
template <typename It, typename Sentinel>
2021-05-29 15:43:34 +00:00
auto join(It begin, Sentinel end, wstring_view sep)
-> join_view<It, Sentinel, wchar_t> {
2021-05-22 00:37:07 +00:00
return {begin, end, sep};
}
template <typename Range>
2021-05-29 15:43:34 +00:00
auto join(Range&& range, wstring_view sep)
-> join_view<detail::iterator_t<Range>, detail::sentinel_t<Range>,
wchar_t> {
2021-05-22 00:37:07 +00:00
return join(std::begin(range), std::end(range), sep);
}
template <typename T>
2021-05-29 15:43:34 +00:00
auto join(std::initializer_list<T> list, wstring_view sep)
-> join_view<const T*, const T*, wchar_t> {
2021-05-22 00:37:07 +00:00
return join(std::begin(list), std::end(list), sep);
}
template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
auto vformat(basic_string_view<Char> format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args)
-> std::basic_string<Char> {
basic_memory_buffer<Char> buffer;
detail::vformat_to(buffer, format_str, args);
return to_string(buffer);
}
// Pass char_t as a default template parameter instead of using
// std::basic_string<char_t<S>> to reduce the symbol size.
template <typename S, typename... Args, typename Char = char_t<S>,
FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
auto format(const S& format_str, Args&&... args) -> std::basic_string<Char> {
return vformat(to_string_view(format_str),
fmt::make_format_args<buffer_context<Char>>(args...));
}
2021-05-22 15:02:45 +00:00
template <typename Locale, typename S, typename Char = char_t<S>,
2021-05-24 02:50:17 +00:00
FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
detail::is_exotic_char<Char>::value)>
2021-06-03 00:06:02 +00:00
inline auto vformat(
2021-05-22 15:02:45 +00:00
const Locale& loc, const S& format_str,
2021-06-03 00:06:02 +00:00
basic_format_args<buffer_context<type_identity_t<Char>>> args)
-> std::basic_string<Char> {
2021-05-22 15:02:45 +00:00
return detail::vformat(loc, to_string_view(format_str), args);
}
template <typename Locale, typename S, typename... Args,
typename Char = char_t<S>,
2021-05-24 02:50:17 +00:00
FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
detail::is_exotic_char<Char>::value)>
2021-06-03 00:06:02 +00:00
inline auto format(const Locale& loc, const S& format_str, Args&&... args)
-> std::basic_string<Char> {
2021-05-22 15:02:45 +00:00
return detail::vformat(loc, to_string_view(format_str),
fmt::make_format_args<buffer_context<Char>>(args...));
2021-05-22 15:02:45 +00:00
}
2021-05-30 13:32:27 +00:00
template <typename OutputIt, typename S, typename Char = char_t<S>,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
detail::is_exotic_char<Char>::value)>
auto vformat_to(OutputIt out, const S& format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args)
-> OutputIt {
auto&& buf = detail::get_buffer<Char>(out);
detail::vformat_to(buf, to_string_view(format_str), args);
return detail::get_iterator(buf);
}
template <typename OutputIt, typename S, typename... Args,
typename Char = char_t<S>,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
detail::is_exotic_char<Char>::value)>
inline auto format_to(OutputIt out, const S& fmt, Args&&... args) -> OutputIt {
return vformat_to(out, to_string_view(fmt),
fmt::make_format_args<buffer_context<Char>>(args...));
2021-05-30 13:32:27 +00:00
}
template <typename S, typename... Args, typename Char, size_t SIZE,
typename Allocator, FMT_ENABLE_IF(detail::is_string<S>::value)>
FMT_DEPRECATED auto format_to(basic_memory_buffer<Char, SIZE, Allocator>& buf,
const S& format_str, Args&&... args) ->
typename buffer_context<Char>::iterator {
detail::vformat_to(buf, to_string_view(format_str),
fmt::make_format_args<buffer_context<Char>>(args...), {});
return detail::buffer_appender<Char>(buf);
}
2021-05-22 15:02:45 +00:00
template <typename Locale, typename S, typename OutputIt, typename... Args,
typename Char = char_t<S>,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
2021-05-24 02:50:17 +00:00
detail::is_locale<Locale>::value&&
detail::is_exotic_char<Char>::value)>
2021-06-03 00:06:02 +00:00
inline auto vformat_to(
2021-05-22 15:02:45 +00:00
OutputIt out, const Locale& loc, const S& format_str,
2021-06-03 00:06:02 +00:00
basic_format_args<buffer_context<type_identity_t<Char>>> args) -> OutputIt {
2021-05-22 15:02:45 +00:00
auto&& buf = detail::get_buffer<Char>(out);
vformat_to(buf, to_string_view(format_str), args, detail::locale_ref(loc));
return detail::get_iterator(buf);
}
2021-05-24 02:50:17 +00:00
template <
typename OutputIt, typename Locale, typename S, typename... Args,
typename Char = char_t<S>,
bool enable = detail::is_output_iterator<OutputIt, Char>::value&&
detail::is_locale<Locale>::value&& detail::is_exotic_char<Char>::value>
2021-05-22 15:02:45 +00:00
inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
Args&&... args) ->
typename std::enable_if<enable, OutputIt>::type {
return vformat_to(out, loc, to_string_view(format_str),
fmt::make_format_args<buffer_context<Char>>(args...));
2021-05-22 15:02:45 +00:00
}
2021-05-30 13:32:27 +00:00
template <typename OutputIt, typename Char, typename... Args,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
detail::is_exotic_char<Char>::value)>
inline auto vformat_to_n(
OutputIt out, size_t n, basic_string_view<Char> format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args)
-> format_to_n_result<OutputIt> {
detail::iterator_buffer<OutputIt, Char, detail::fixed_buffer_traits> buf(out,
n);
detail::vformat_to(buf, format_str, args);
return {buf.out(), buf.count()};
}
template <typename OutputIt, typename S, typename... Args,
typename Char = char_t<S>,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
detail::is_exotic_char<Char>::value)>
inline auto format_to_n(OutputIt out, size_t n, const S& fmt,
const Args&... args) -> format_to_n_result<OutputIt> {
return vformat_to_n(out, n, to_string_view(fmt),
fmt::make_format_args<buffer_context<Char>>(args...));
2021-05-30 13:32:27 +00:00
}
template <typename S, typename... Args, typename Char = char_t<S>,
FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
inline auto formatted_size(const S& fmt, Args&&... args) -> size_t {
detail::counting_buffer<Char> buf;
detail::vformat_to(buf, to_string_view(fmt),
fmt::make_format_args<buffer_context<Char>>(args...));
2021-05-30 13:32:27 +00:00
return buf.count();
}
2021-05-18 05:19:59 +00:00
inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
wmemory_buffer buffer;
detail::vformat_to(buffer, fmt, args);
buffer.push_back(L'\0');
if (std::fputws(buffer.data(), f) == -1)
2021-06-02 23:13:39 +00:00
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
2021-05-18 05:19:59 +00:00
}
inline void vprint(wstring_view fmt, wformat_args args) {
vprint(stdout, fmt, args);
}
2021-05-18 03:47:38 +00:00
template <typename... T>
2021-05-18 05:19:59 +00:00
void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...));
2021-05-18 03:47:38 +00:00
}
2021-05-18 05:19:59 +00:00
template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
2021-05-18 03:47:38 +00:00
}
2021-05-19 02:38:52 +00:00
2021-05-22 00:37:07 +00:00
/**
Converts *value* to ``std::wstring`` using the default format for type *T*.
*/
2021-06-03 00:06:02 +00:00
template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
2021-05-22 00:37:07 +00:00
return format(FMT_STRING(L"{}"), value);
}
FMT_MODULE_EXPORT_END
2021-05-19 02:38:52 +00:00
FMT_END_NAMESPACE
2021-05-18 03:47:38 +00:00
#endif // FMT_XCHAR_H_