22 template <
typename Char>
46 : out_(out), args_(args) {}
48 OutputIt
out() {
return out_; }
51 detail::locale_ref
locale() {
return {}; }
56 detail::error_handler().on_error(message);
66 unsigned max = max_value<int>();
74 return value >= (std::numeric_limits<int>::min)() &&
82 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
86 return (std::max)(
static_cast<int>(
value), 0);
89 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
99 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
104 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
123 : arg_(arg), type_(type) {}
126 if (type_ !=
's') operator()<
bool>(value);
129 template <typename U, FMT_ENABLE_IF(std::is_integral<U>::value)>
131 bool is_signed = type_ ==
'd' || type_ ==
'i';
133 if (
const_check(
sizeof(target_type) <=
sizeof(int))) {
136 arg_ = detail::make_arg<Context>(
137 static_cast<int>(
static_cast<target_type
>(
value)));
140 arg_ = detail::make_arg<Context>(
141 static_cast<unsigned>(
static_cast<unsigned_type
>(
value)));
148 arg_ = detail::make_arg<Context>(
static_cast<long long>(
value));
150 arg_ = detail::make_arg<Context>(
156 template <typename U, FMT_ENABLE_IF(!std::is_integral<U>::value)>
157 void operator()(U) {}
164 template <
typename T,
typename Context,
typename Char>
177 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
179 arg_ = detail::make_arg<Context>(
180 static_cast<typename Context::char_type
>(
value));
183 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
184 void operator()(T) {}
190 template <
typename T>
const Char*
operator()(T) {
return nullptr; }
205 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
212 unsigned int_max = max_value<int>();
214 return static_cast<unsigned>(width);
217 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
218 unsigned operator()(T) {
225 template <
typename OutputIt,
typename Char>
235 auto s = this->specs;
246 template <typename T, FMT_ENABLE_IF(detail::is_integral<T>::value)>
250 if (std::is_same<T, Char>::value) {
252 if (fmt_specs.
type && fmt_specs.
type !=
'c')
253 return (*
this)(
static_cast<int>(
value));
255 fmt_specs.
alt =
false;
256 fmt_specs.
fill[0] =
' ';
261 return write<Char>(this->out,
static_cast<Char
>(
value), fmt_specs);
263 return base::operator()(
value);
266 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
268 return base::operator()(
value);
273 if (value)
return base::operator()(value);
274 return write_null_pointer(this->specs.type !=
'p');
279 if (value)
return base::operator()(value);
280 return write_null_pointer(this->specs.type !=
'p');
284 return base::operator()(value);
289 return value ? base::operator()(value) : write_null_pointer();
296 handle.
format(parse_ctx, context_);
301 template <
typename Char>
304 for (; it != end; ++it) {
329 template <
typename Char,
typename GetArg>
334 if (c >=
'0' && c <=
'9') {
338 if (it != end && *it ==
'$') {
340 arg_index = value != -1 ? value : max_value<int>();
342 if (c ==
'0') specs.
fill[0] =
'0';
355 if (*it >=
'0' && *it <=
'9') {
358 }
else if (*it ==
'*') {
361 detail::printf_width_handler<Char>(specs),
get_arg(-1)));
367 template <
typename Char,
typename Context>
371 auto out = OutputIt(buf);
377 auto get_arg = [&](
int arg_index) {
379 arg_index = parse_ctx.next_arg_id();
381 parse_ctx.check_arg_id(--arg_index);
385 const Char*
start = parse_ctx.begin();
386 const Char* end = parse_ctx.end();
389 if (!detail::find<false, Char>(it, end,
'%', it)) {
394 if (it != end && *it == c) {
408 if (arg_index == 0) parse_ctx.
on_error(
"argument not found");
411 if (it != end && *it ==
'.') {
413 c = it != end ? *it : 0;
414 if (
'0' <= c && c <=
'9') {
416 }
else if (c ==
'*') {
431 if (specs.
precision >= 0 &&
arg.type() == detail::type::cstring_type) {
434 auto nul =
std::find(str, str_end, Char());
435 arg = detail::make_arg<basic_printf_context<OutputIt, Char>>(
442 if (specs.
fill[0] ==
'0') {
451 c = it != end ? *it++ : 0;
452 Char t = it != end ? *it : 0;
458 t = it != end ? *it : 0;
459 convert_arg<signed char>(
arg, t);
461 convert_arg<short>(
arg, t);
467 t = it != end ? *it : 0;
468 convert_arg<long long>(
arg, t);
470 convert_arg<long>(
arg, t);
474 convert_arg<intmax_t>(
arg, t);
477 convert_arg<size_t>(
arg, t);
480 convert_arg<std::ptrdiff_t>(
arg, t);
488 convert_arg<void>(
arg, c);
493 specs.
type =
static_cast<char>(*it++);
494 if (
arg.is_integral()) {
496 switch (specs.
type) {
513 detail::printf_arg_formatter<OutputIt, Char>(out, specs, context),
arg);
519 template <
typename Char>
535 template <
typename... T>
547 template <
typename... T>
553 template <
typename S,
typename Char =
char_t<S>>
557 -> std::basic_string<Char> {
572 template <
typename S,
typename... T,
574 inline auto sprintf(
const S& fmt,
const T&...
args) -> std::basic_string<Char> {
579 template <
typename S,
typename Char =
char_t<S>>
581 std::FILE* f,
const S& fmt,
586 size_t size = buffer.size();
587 return std::fwrite(buffer.data(),
sizeof(Char), size,
f) < size
589 :
static_cast<int>(
size);
601 template <
typename S,
typename... T,
typename Char =
char_t<S>>
602 inline auto fprintf(std::FILE* f,
const S& fmt,
const T&...
args) ->
int {
605 fmt::make_format_args<context>(
args...));
608 template <
typename S,
typename Char =
char_t<S>>
625 template <
typename S,
typename... T,
FMT_ENABLE_IF(detail::is_string<S>::value)>
632 template <
typename S,
typename Char =
char_t<S>>
634 std::basic_ostream<Char>& os,
const S& fmt,
639 os.write(buffer.data(),
static_cast<std::streamsize
>(buffer.size()));
640 return static_cast<int>(buffer.size());
642 template <
typename S,
typename... T,
typename Char =
char_t<S>>
644 const T&...
args) ->
int {
652 #endif // FMT_PRINTF_H_ FMT_CONSTEXPR auto to_unsigned(Int value) -> typename std::make_unsigned< Int >::type
#define FMT_MODULE_EXPORT_END
static bool fits_in_int(int)
#define FMT_ENABLE_IF(...)
FMT_CONSTEXPR void on_error(const char *message)
typename detail::char_t_impl< S >::type char_t
#define FMT_BEGIN_DETAIL_NAMESPACE
format_arg arg(int id) const
#define FMT_END_DETAIL_NAMESPACE
auto printf(const S &fmt, const T &... args) -> int
typename std::conditional< B, T, F >::type conditional_t
char_converter(basic_format_arg< Context > &arg)
basic_printf_context_t< char > printf_context
constexpr auto const_check(T value) -> T
basic_format_arg< Context > & arg_
static bool fits_in_int(T value)
void operator()(bool value)
void parse_flags(basic_format_specs< Char > &specs, const Char *&it, const Char *end)
auto sprintf(const S &fmt, const T &... args) -> std::basic_string< Char >
void convert_arg(basic_format_arg< Context > &arg, Char type)
const Char * operator()(T)
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
FMT_CONSTEXPR auto parse_nonnegative_int(const Char *&begin, const Char *end, int error_value) noexcept -> int
constexpr auto make_format_args(const Args &... args) -> format_arg_store< Context, Args... >
#define FMT_END_NAMESPACE
const Char * operator()(const Char *s)
auto fprintf(std::FILE *f, const S &fmt, const T &... args) -> int
auto write(OutputIt out, const std::tm &time, const std::locale &loc, char format, char modifier=0) -> OutputIt
basic_format_arg< Context > & arg_
FMT_INLINE auto to_string_view(const Char *s) -> basic_string_view< Char >
basic_printf_context(OutputIt out, basic_format_args< basic_printf_context > args)
conditional_t< std::is_same< T, char >::value, appender, std::back_insert_iterator< buffer< T > >> buffer_appender
auto vfprintf(std::FILE *f, const S &fmt, basic_format_args< basic_printf_context_t< type_identity_t< Char >>> args) -> int
arg_converter(basic_format_arg< Context > &arg, char_type type)
std::string to_string(const std::string &arg)
auto make_wprintf_args(const T &... args) -> format_arg_store< wprintf_context, T... >
int parse_header(const Char *&it, const Char *end, basic_format_specs< Char > &specs, GetArg get_arg)
printf_width_handler(format_specs &specs)
typename type_identity< T >::type type_identity_t
void advance_to(OutputIt it)
basic_format_args< basic_printf_context > args_
FMT_CONSTEXPR FMT_INLINE auto visit_format_arg(Visitor &&vis, const basic_format_arg< Context > &arg) -> decltype(vis(0))
auto vsprintf(const S &fmt, basic_format_args< basic_printf_context_t< type_identity_t< Char >>> args) -> std::basic_string< Char >
typename std::enable_if< B, T >::type enable_if_t
detail::locale_ref locale()
span_constexpr std::size_t size(span< T, Extent > const &spn)
typename Context::char_type char_type
void vprintf(buffer< Char > &buf, basic_string_view< Char > format, basic_format_args< Context > args)
#define FMT_BEGIN_NAMESPACE
static bool fits_in_int(bool)
auto make_printf_args(const T &... args) -> format_arg_store< printf_context, T... >
#define FMT_MODULE_EXPORT_BEGIN
basic_printf_context_t< wchar_t > wprintf_context
std::basic_string< Char > format(const text_style &ts, const S &format_str, const Args &... args)
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
static bool fits_in_int(T value)