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_ = internal::make_arg<Context>(
94 static_cast<int>(
static_cast<target_type
>(
value)));
97 arg_ = internal::make_arg<Context>(
98 static_cast<unsigned>(
static_cast<unsigned_type
>(
value)));
105 arg_ = internal::make_arg<Context>(
static_cast<long long>(
value));
107 arg_ = internal::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_ = internal::make_arg<Context>(
140 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
141 void operator()(T) {}
155 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
162 unsigned int_max = max_value<int>();
164 return static_cast<unsigned>(width);
167 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
168 unsigned operator()(T) {
174 template <
typename Char,
typename Context>
177 Context(std::back_inserter(buf), format, args).format();
180 template <
typename OutputIt,
typename Char,
typename Context>
184 return Context(it, format, args).format();
199 template <
typename Range>
212 this->specs()->type = 0;
213 this->
write(
"(nil)");
217 this->specs()->type = 0;
218 this->
write(L
"(nil)");
232 :
base(Range(iter), &specs,
internal::locale_ref()), context_(ctx) {}
234 template <typename T, FMT_ENABLE_IF(fmt::internal::is_integral<T>::value)>
238 if (std::is_same<T, bool>::value) {
240 if (fmt_specs.type !=
's')
return base::operator()(value ? 1 : 0);
242 this->
write(value != 0);
243 }
else if (std::is_same<T, char_type>::value) {
245 if (fmt_specs.type && fmt_specs.type !=
'c')
246 return (*
this)(
static_cast<int>(value));
248 fmt_specs.alt =
false;
250 return base::operator()(value);
252 return base::operator()(value);
257 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
259 return base::operator()(value);
265 base::operator()(value);
266 else if (this->specs()->
type ==
'p')
269 this->
write(
"(null)");
276 base::operator()(value);
277 else if (this->specs()->
type ==
'p')
280 this->
write(L
"(null)");
285 return base::operator()(value);
292 if (value)
return base::operator()(value);
293 this->specs()->type = 0;
306 template <
typename ParseContext>
307 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
311 template <
typename FormatContext>
312 auto format(
const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
333 static void parse_flags(
format_specs& specs,
const Char*& it,
341 int parse_header(
const Char*& it,
const Char* end,
format_specs& specs);
353 : out_(out), args_(args), parse_ctx_(format_str) {}
355 OutputIt
out() {
return out_; }
367 template <
typename ArgFormatter = pr
intf_arg_formatter<buffer_range<Char>>>
371 template <
typename OutputIt,
typename Char>
375 for (; it != end; ++it) {
398 template <
typename OutputIt,
typename Char>
402 arg_index = parse_ctx_.next_arg_id();
404 parse_ctx_.check_arg_id(--arg_index);
408 template <
typename OutputIt,
typename Char>
410 const Char*& it,
const Char* end,
format_specs& specs) {
413 if (c >=
'0' && c <=
'9') {
418 if (it != end && *it ==
'$') {
422 if (c ==
'0') specs.
fill[0] =
'0';
431 parse_flags(specs, it, end);
434 if (*it >=
'0' && *it <=
'9') {
437 }
else if (*it ==
'*') {
446 template <
typename OutputIt,
typename Char>
447 template <
typename ArgFormatter>
449 auto out = this->out();
450 const Char* start = parse_ctx_.begin();
451 const Char* end = parse_ctx_.end();
455 if (c !=
'%')
continue;
456 if (it != end && *it == c) {
467 int arg_index = parse_header(it, end, specs);
468 if (arg_index == 0) on_error(
"argument index out of range");
471 if (it != end && *it ==
'.') {
473 c = it != end ? *it : 0;
474 if (
'0' <= c && c <=
'9') {
477 }
else if (c ==
'*') {
489 if (specs.
fill[0] ==
'0') {
497 c = it != end ? *it++ : 0;
504 t = it != end ? *it : 0;
505 convert_arg<signed char>(
arg, t);
507 convert_arg<short>(
arg, t);
513 t = it != end ? *it : 0;
514 convert_arg<long long>(
arg, t);
516 convert_arg<long>(
arg, t);
520 convert_arg<intmax_t>(
arg, t);
523 convert_arg<std::size_t>(
arg, t);
526 convert_arg<std::ptrdiff_t>(
arg, t);
534 convert_arg<void>(
arg, c);
539 specs.
type =
static_cast<char>(*it++);
542 switch (specs.
type) {
562 template <
typename Char>
579 template <
typename... Args>
581 const Args&... args) {
591 template <
typename... Args>
593 const Args&... args) {
597 template <
typename S,
typename Char =
char_t<S>>
614 template <
typename S,
typename... Args,
616 inline std::basic_string<Char>
sprintf(
const S&
format,
const Args&... args) {
621 template <
typename S,
typename Char =
char_t<S>>
626 std::size_t size = buffer.
size();
627 return std::fwrite(buffer.
data(),
sizeof(Char), size, f) < size
629 :
static_cast<int>(size);
641 template <
typename S,
typename... Args,
646 {make_format_args<context>(args...)});
649 template <
typename S,
typename Char =
char_t<S>>
664 template <
typename S,
typename... Args,
666 inline int printf(
const S& format_str,
const Args&... args) {
669 {make_format_args<context>(args...)});
672 template <
typename S,
typename Char =
char_t<S>>
678 return static_cast<int>(buffer.
size());
682 template <
typename ArgFormatter,
typename Char,
688 typename ArgFormatter::iterator iter(out);
689 Context(iter, format_str, args).template format<ArgFormatter>();
702 template <
typename S,
typename... Args,
typename Char =
char_t<S>>
703 inline int fprintf(std::basic_ostream<Char>& os,
const S& format_str,
704 const Args&... args) {
707 {make_format_args<context>(args...)});
711 #endif // FMT_PRINTF_H_
int vfprintf(std::FILE *f, const S &format, basic_format_args< basic_printf_context_t< Char >> args)
#define FMT_BEGIN_NAMESPACE
std::basic_string< Char > format(const text_style &ts, const S &format_str, const Args &... args)
std::size_t size() const FMT_NOEXCEPT
std::basic_string< Char > vsprintf(const S &format, basic_format_args< basic_printf_context_t< Char >> args)
int parse_header(const Char *&it, const Char *end, format_specs &specs)
basic_format_parse_context< Char > & parse_context()
basic_printf_context(OutputIt out, basic_string_view< char_type > format_str, basic_format_args< basic_printf_context > args)
void operator()(bool value)
int vprintf(const S &format, basic_format_args< basic_printf_context_t< Char >> args)
std::basic_string< Char > sprintf(const S &format, const Args &... args)
basic_format_arg< Context > & arg_
arg_converter(basic_format_arg< Context > &arg, char_type type)
FMT_CONSTEXPR Context::format_arg get_arg(Context &ctx, int id)
FMT_CONSTEXPR void on_error(const char *message)
FMT_CONSTEXPR void on_error(const char *message)
int fprintf(std::FILE *f, const S &format, const Args &... args)
void advance_to(OutputIt it)
FMT_CONSTEXPR bool is_negative(T value)
void convert_arg(basic_format_arg< Context > &arg, Char type)
typename internal::char_t_impl< S >::type char_t
void format_value(buffer< Char > &buf, const T &value, locale_ref loc=locale_ref())
basic_format_args< basic_printf_context > args_
#define FMT_END_NAMESPACE
format_arg get_arg(int arg_index=-1)
basic_format_arg< Context > & arg_
static bool fits_in_int(int)
format_arg_store< printf_context, Args... > make_printf_args(const Args &... args)
typename Context::char_type char_type
basic_format_parse_context< Char > parse_ctx_
typename std::enable_if< B, T >::type enable_if_t
FMT_CONSTEXPR int parse_nonnegative_int(const Char *&begin, const Char *end, ErrorHandler &&eh)
OutputIterator copy(const RangeT &range, OutputIterator out)
#define FMT_ENABLE_IF(...)
typename std::conditional< B, T, F >::type conditional_t
basic_printf_context_t< wchar_t > wprintf_context
static bool fits_in_int(T value)
FMT_CONSTEXPR auto visit_format_arg(Visitor &&vis, const basic_format_arg< Context > &arg) -> decltype(vis(0))
static void parse_flags(format_specs &specs, const Char *&it, const Char *end)
printf_width_handler(format_specs &specs)
basic_printf_context_t< char > printf_context
static bool fits_in_int(T value)
static bool fits_in_int(bool)
char_converter(basic_format_arg< Context > &arg)
conditional_t< std::numeric_limits< T >::digits<=32, uint32_t, conditional_t< std::numeric_limits< T >::digits<=64, uint64_t, uint128_t > > uint32_or_64_or_128_t
Container & get_container(std::back_insert_iterator< Container > it)
void write(std::basic_ostream< Char > &os, buffer< Char > &buf)
internal::named_arg< T, Char > arg(const S &name, const T &arg)
format_arg_store< wprintf_context, Args... > make_wprintf_args(const Args &... args)
format_arg arg(int id) const
void printf(buffer< Char > &buf, basic_string_view< Char > format, basic_format_args< Context > args)