23 unsigned max = max_value<int>();
31 return value >= (std::numeric_limits<int>::min)() &&
39 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
43 return (std::max)(
static_cast<int>(
value), 0);
46 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
56 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
61 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
80 : arg_(arg), type_(type) {}
83 if (type_ !=
's') operator()<
bool>(value);
86 template <typename U, FMT_ENABLE_IF(std::is_integral<U>::value)>
88 bool is_signed = type_ ==
'd' || type_ ==
'i';
90 if (
const_check(
sizeof(target_type) <=
sizeof(int))) {
93 arg_ = detail::make_arg<Context>(
94 static_cast<int>(
static_cast<target_type
>(
value)));
97 arg_ = detail::make_arg<Context>(
98 static_cast<unsigned>(
static_cast<unsigned_type
>(
value)));
105 arg_ = detail::make_arg<Context>(
static_cast<long long>(
value));
107 arg_ = detail::make_arg<Context>(
113 template <typename U, FMT_ENABLE_IF(!std::is_integral<U>::value)>
114 void operator()(U) {}
121 template <
typename T,
typename Context,
typename Char>
134 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
136 arg_ = detail::make_arg<Context>(
140 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
141 void operator()(T) {}
147 template <
typename T>
const Char*
operator()(T) {
return nullptr; }
162 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
169 unsigned int_max = max_value<int>();
171 return static_cast<unsigned>(width);
174 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
175 unsigned operator()(T) {
181 template <
typename Char,
typename Context>
189 template <
typename Char,
typename Context>
197 template <
typename Char>
208 template <
typename OutputIt,
typename Char>
221 this->specs()->type = 0;
222 this->
write(
"(nil)");
226 this->specs()->type = 0;
227 this->
write(L
"(nil)");
241 :
base(iter, &specs,
detail::locale_ref()), context_(ctx) {}
243 template <typename T, FMT_ENABLE_IF(fmt::detail::is_integral<T>::value)>
247 if (std::is_same<T, bool>::value) {
249 if (fmt_specs.type !=
's')
return base::operator()(value ? 1 : 0);
251 this->
write(value != 0);
252 }
else if (std::is_same<T, char_type>::value) {
254 if (fmt_specs.type && fmt_specs.type !=
'c')
255 return (*
this)(
static_cast<int>(value));
257 fmt_specs.alt =
false;
258 fmt_specs.fill[0] =
' ';
263 return base::operator()(value);
265 return base::operator()(value);
270 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
272 return base::operator()(value);
278 base::operator()(value);
279 else if (this->specs()->
type ==
'p')
282 this->
write(
"(null)");
289 base::operator()(value);
290 else if (this->specs()->
type ==
'p')
293 this->
write(L
"(null)");
298 return base::operator()(value);
305 if (value)
return base::operator()(value);
306 this->specs()->type = 0;
321 template <
typename ParseContext>
322 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
326 template <
typename FormatContext>
327 auto format(
const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
352 static void parse_flags(
format_specs& specs,
const Char*& it,
360 int parse_header(
const Char*& it,
const Char* end,
format_specs& specs);
371 : out_(out), args_(args), parse_ctx_(format_str) {}
373 OutputIt
out() {
return out_; }
387 template <
typename ArgFormatter = pr
intf_arg_formatter<OutputIt, Char>>
391 template <
typename OutputIt,
typename Char>
395 for (; it != end; ++it) {
420 template <
typename OutputIt,
typename Char>
424 arg_index = parse_ctx_.next_arg_id();
426 parse_ctx_.check_arg_id(--arg_index);
430 template <
typename OutputIt,
typename Char>
436 if (c >=
'0' && c <=
'9') {
441 if (it != end && *it ==
'$') {
445 if (c ==
'0') specs.
fill[0] =
'0';
454 parse_flags(specs, it, end);
457 if (*it >=
'0' && *it <=
'9') {
460 }
else if (*it ==
'*') {
469 template <
typename OutputIt,
typename Char>
470 template <
typename ArgFormatter>
472 auto out = this->out();
473 const Char* start = parse_ctx_.begin();
474 const Char* end = parse_ctx_.end();
478 if (c !=
'%')
continue;
479 if (it != end && *it == c) {
490 int arg_index = parse_header(it, end, specs);
491 if (arg_index == 0) on_error(
"argument not found");
494 if (it != end && *it ==
'.') {
496 c = it != end ? *it : 0;
497 if (
'0' <= c && c <=
'9') {
500 }
else if (c ==
'*') {
518 auto nul =
std::find(str, str_end, Char());
525 if (specs.
fill[0] ==
'0') {
534 c = it != end ? *it++ : 0;
541 t = it != end ? *it : 0;
542 convert_arg<signed char>(
arg, t);
544 convert_arg<short>(
arg, t);
550 t = it != end ? *it : 0;
551 convert_arg<long long>(
arg, t);
553 convert_arg<long>(
arg, t);
557 convert_arg<intmax_t>(
arg, t);
560 convert_arg<size_t>(
arg, t);
563 convert_arg<std::ptrdiff_t>(
arg, t);
571 convert_arg<void>(
arg, c);
576 specs.
type =
static_cast<char>(*it++);
579 switch (specs.
type) {
599 template <
typename Char>
615 template <
typename... Args>
617 const Args&... args) {
627 template <
typename... Args>
629 const Args&... args) {
633 template <
typename S,
typename Char =
char_t<S>>
651 template <
typename S,
typename... Args,
653 inline std::basic_string<Char>
sprintf(
const S&
format,
const Args&... args) {
658 template <
typename S,
typename Char =
char_t<S>>
660 std::FILE* f,
const S&
format,
664 size_t size = buffer.
size();
665 return std::fwrite(buffer.
data(),
sizeof(Char), size, f) < size
667 :
static_cast<int>(size);
679 template <
typename S,
typename... Args,
684 make_format_args<context>(args...));
687 template <
typename S,
typename Char =
char_t<S>>
703 template <
typename S,
typename... Args,
705 inline int printf(
const S& format_str,
const Args&... args) {
708 make_format_args<context>(args...));
711 template <
typename S,
typename Char =
char_t<S>>
713 std::basic_ostream<Char>& os,
const S&
format,
718 return static_cast<int>(buffer.
size());
722 template <
typename ArgFormatter,
typename Char,
728 typename ArgFormatter::iterator iter(out);
729 Context(iter, format_str, args).template format<ArgFormatter>();
742 template <
typename S,
typename... Args,
typename Char =
char_t<S>>
743 inline int fprintf(std::basic_ostream<Char>& os,
const S& format_str,
744 const Args&... args) {
747 make_format_args<context>(args...));
751 #endif // FMT_PRINTF_H_
#define FMT_BEGIN_NAMESPACE
std::basic_string< Char > format(const text_style &ts, const S &format_str, const Args &... args)
typename Context::char_type char_type
const Char * operator()(T)
int parse_header(const Char *&it, const Char *end, format_specs &specs)
printf_width_handler(format_specs &specs)
basic_printf_context(OutputIt out, basic_string_view< char_type > format_str, basic_format_args< basic_printf_context > args)
parse_context_type parse_ctx_
std::basic_string< Char > sprintf(const S &format, const Args &... args)
FMT_CONSTEXPR int parse_nonnegative_int(const Char *&begin, const Char *end, ErrorHandler &&eh)
parse_context_type & parse_context()
int vfprintf(std::FILE *f, const S &format, basic_format_args< basic_printf_context_t< type_identity_t< Char >>> args)
detail::locale_ref locale()
OutputIt write(OutputIt out, basic_string_view< StrChar > s, const basic_format_specs< Char > &specs)
basic_format_arg< Context > & arg_
FMT_CONSTEXPR void on_error(const char *message)
FMT_CONSTEXPR void on_error(const char *message)
typename type_identity< T >::type type_identity_t
basic_format_arg< Context > & arg_
int fprintf(std::FILE *f, const S &format, const Args &... args)
void vprintf(buffer< Char > &buf, basic_string_view< Char > format, basic_format_args< Context > args)
static bool fits_in_int(T value)
conditional_t< num_bits< T >()<=32 &&!FMT_REDUCE_INT_INSTANTIATIONS, uint32_t, conditional_t< num_bits< T >()<=64, uint64_t, uint128_t > > uint32_or_64_or_128_t
void advance_to(OutputIt it)
FMT_CONSTEXPR std::make_unsigned< Int >::type to_unsigned(Int value)
void format_value(buffer< Char > &buf, const T &value, locale_ref loc=locale_ref())
static bool fits_in_int(T value)
basic_format_args< basic_printf_context > args_
#define FMT_END_NAMESPACE
format_arg get_arg(int arg_index=-1)
const Char * operator()(const Char *s)
format_arg_store< printf_context, Args... > make_printf_args(const Args &... args)
std::basic_string< Char > vsprintf(const S &format, basic_format_args< basic_printf_context_t< type_identity_t< Char >>> args)
FMT_DEPRECATED void printf(detail::buffer< Char > &buf, basic_string_view< Char > format, basic_format_args< Context > args)
OutputIterator copy(const RangeT &range, OutputIterator out)
typename std::enable_if< B, T >::type enable_if_t
static bool fits_in_int(bool)
FMT_CONSTEXPR bool is_negative(T value)
#define FMT_ENABLE_IF(...)
typename detail::char_t_impl< S >::type char_t
std::integral_constant< bool, std::numeric_limits< T >::is_signed||std::is_same< T, int128_t >::value > is_signed
typename std::conditional< B, T, F >::type conditional_t
void convert_arg(basic_format_arg< Context > &arg, Char type)
basic_printf_context_t< wchar_t > wprintf_context
size_t size() const FMT_NOEXCEPT
static bool fits_in_int(int)
static void parse_flags(format_specs &specs, const Char *&it, const Char *end)
FMT_CONSTEXPR_DECL FMT_INLINE auto visit_format_arg(Visitor &&vis, const basic_format_arg< Context > &arg) -> decltype(vis(0))
basic_printf_context_t< char > printf_context
arg_converter(basic_format_arg< Context > &arg, char_type type)
detail::named_arg< Char, T > arg(const Char *name, const T &arg)
char_converter(basic_format_arg< Context > &arg)
constexpr T const_check(T value)
Container & get_container(std::back_insert_iterator< Container > it)
FMT_CONSTEXPR Context::format_arg get_arg(Context &ctx, ID id)
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
format_arg_store< wprintf_context, Args... > make_wprintf_args(const Args &... args)
void operator()(bool value)
format_arg arg(int id) const
void write_buffer(std::basic_ostream< Char > &os, buffer< Char > &buf)