Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | List of all members
basic_printf_context< OutputIt, Char, ArgFormatter > Class Template Reference

#include <printf.h>

Inheritance diagram for basic_printf_context< OutputIt, Char, ArgFormatter >:
internal::context_base< OutputIt, basic_printf_context< OutputIt, Char, ArgFormatter >, Char >

Classes

struct  formatter_type
 

Public Types

typedef Char char_type
 
- Public Types inherited from internal::context_base< OutputIt, basic_printf_context< OutputIt, Char, ArgFormatter >, Char >
typedef OutputIt iterator
 

Public Member Functions

 basic_printf_context (OutputIt out, basic_string_view< char_type > format_str, basic_format_args< basic_printf_context > args)
 
void format ()
 
- Public Member Functions inherited from internal::context_base< OutputIt, basic_printf_context< OutputIt, Char, ArgFormatter >, Char >
basic_parse_context< char_type > & parse_context ()
 
basic_format_args< basic_printf_context< OutputIt, Char, ArgFormatter > > args () const
 
basic_format_arg< basic_printf_context< OutputIt, Char, ArgFormatter > > arg (unsigned id) const
 
internal::error_handler error_handler ()
 
void on_error (const char *message)
 
iterator out ()
 
iterator begin ()
 
void advance_to (iterator it)
 
locale_ref locale ()
 

Private Types

typedef internal::context_base< OutputIt, basic_printf_context, Char > base
 
typedef base::format_arg format_arg
 
typedef basic_format_specs< char_typeformat_specs
 
typedef internal::null_terminating_iterator< char_typeiterator
 

Private Member Functions

void parse_flags (format_specs &spec, iterator &it)
 
format_arg get_arg (iterator it, unsigned arg_index=(std::numeric_limits< unsigned >::max)())
 
unsigned parse_header (iterator &it, format_specs &spec)
 

Additional Inherited Members

- Protected Types inherited from internal::context_base< OutputIt, basic_printf_context< OutputIt, Char, ArgFormatter >, Char >
typedef Char char_type
 
typedef basic_format_arg< basic_printf_context< OutputIt, Char, ArgFormatter > > format_arg
 
- Protected Member Functions inherited from internal::context_base< OutputIt, basic_printf_context< OutputIt, Char, ArgFormatter >, Char >
 context_base (OutputIt out, basic_string_view< char_type > format_str, basic_format_args< basic_printf_context< OutputIt, Char, ArgFormatter > > ctx_args, locale_ref loc=locale_ref())
 
format_arg do_get_arg (unsigned arg_id)
 
format_arg get_arg (unsigned arg_id)
 

Detailed Description

template<typename OutputIt, typename Char, typename ArgFormatter>
class basic_printf_context< OutputIt, Char, ArgFormatter >

This template formats data and writes the output to a writer.

Definition at line 336 of file printf.h.

Member Typedef Documentation

template<typename OutputIt, typename Char, typename ArgFormatter>
typedef internal::context_base<OutputIt, basic_printf_context, Char> basic_printf_context< OutputIt, Char, ArgFormatter >::base
private

Definition at line 485 of file printf.h.

template<typename OutputIt, typename Char, typename ArgFormatter>
typedef Char basic_printf_context< OutputIt, Char, ArgFormatter >::char_type

The character type for the output.

Definition at line 479 of file printf.h.

template<typename OutputIt, typename Char, typename ArgFormatter>
typedef base::format_arg basic_printf_context< OutputIt, Char, ArgFormatter >::format_arg
private

Definition at line 486 of file printf.h.

template<typename OutputIt, typename Char, typename ArgFormatter>
typedef basic_format_specs<char_type> basic_printf_context< OutputIt, Char, ArgFormatter >::format_specs
private

Definition at line 487 of file printf.h.

template<typename OutputIt, typename Char, typename ArgFormatter>
typedef internal::null_terminating_iterator<char_type> basic_printf_context< OutputIt, Char, ArgFormatter >::iterator
private

Definition at line 488 of file printf.h.

Constructor & Destructor Documentation

template<typename OutputIt, typename Char, typename ArgFormatter>
basic_printf_context< OutputIt, Char, ArgFormatter >::basic_printf_context ( OutputIt  out,
basic_string_view< char_type format_str,
basic_format_args< basic_printf_context< OutputIt, Char, ArgFormatter > >  args 
)
inline

Constructs a printf_context object. References to the arguments and the writer are stored in the context object so make sure they have appropriate lifetimes.

Definition at line 509 of file printf.h.

511  : base(out, format_str, args) {}
internal::context_base< OutputIt, basic_printf_context, Char > base
Definition: printf.h:485

Member Function Documentation

template<typename OutputIt , typename Char , typename AF >
void basic_printf_context< OutputIt, Char, AF >::format ( )

Formats stored arguments and writes the output to the range.

Definition at line 596 of file printf.h.

596  {
597  auto &buffer = internal::get_container(this->out());
598  auto start = iterator(this->parse_context());
599  auto it = start;
601  while (*it) {
602  char_type c = *it++;
603  if (c != '%') continue;
604  if (*it == c) {
605  buffer.append(pointer_from(start), pointer_from(it));
606  start = ++it;
607  continue;
608  }
609  buffer.append(pointer_from(start), pointer_from(it) - 1);
610 
611  format_specs spec;
612  spec.align_ = ALIGN_RIGHT;
613 
614  // Parse argument index, flags and width.
615  unsigned arg_index = parse_header(it, spec);
616 
617  // Parse precision.
618  if (*it == '.') {
619  ++it;
620  if ('0' <= *it && *it <= '9') {
622  spec.precision = static_cast<int>(parse_nonnegative_int(it, eh));
623  } else if (*it == '*') {
624  ++it;
625  spec.precision =
627  } else {
628  spec.precision = 0;
629  }
630  }
631 
632  format_arg arg = get_arg(it, arg_index);
634  spec.flags = static_cast<uint_least8_t>(spec.flags & (~internal::to_unsigned<int>(HASH_FLAG)));
635  if (spec.fill_ == '0') {
636  if (arg.is_arithmetic())
637  spec.align_ = ALIGN_NUMERIC;
638  else
639  spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
640  }
641 
642  // Parse length and convert the argument to the required type.
643  using internal::convert_arg;
644  switch (*it++) {
645  case 'h':
646  if (*it == 'h')
647  convert_arg<signed char>(arg, *++it);
648  else
649  convert_arg<short>(arg, *it);
650  break;
651  case 'l':
652  if (*it == 'l')
653  convert_arg<long long>(arg, *++it);
654  else
655  convert_arg<long>(arg, *it);
656  break;
657  case 'j':
658  convert_arg<intmax_t>(arg, *it);
659  break;
660  case 'z':
661  convert_arg<std::size_t>(arg, *it);
662  break;
663  case 't':
664  convert_arg<std::ptrdiff_t>(arg, *it);
665  break;
666  case 'L':
667  // printf produces garbage when 'L' is omitted for long double, no
668  // need to do the same.
669  break;
670  default:
671  --it;
672  convert_arg<void>(arg, *it);
673  }
674 
675  // Parse type.
676  if (!*it)
677  FMT_THROW(format_error("invalid format string"));
678  spec.type = static_cast<char>(*it++);
679  if (arg.is_integral()) {
680  // Normalize type.
681  switch (spec.type) {
682  case 'i': case 'u':
683  spec.type = 'd';
684  break;
685  case 'c':
686  // TODO: handle wchar_t better?
689  break;
690  }
691  }
692 
693  start = it;
694 
695  // Format argument.
696  visit_format_arg(AF(buffer, spec, *this), arg);
697  }
698  buffer.append(pointer_from(start), pointer_from(it));
699 }
wchar_t fill_
Definition: format.h:1091
Container & get_container(std::back_insert_iterator< Container > it)
Definition: core.h:313
FMT_CONSTEXPR_DECL const Char * pointer_from(null_terminating_iterator< Char > it)
Definition: printf.h:107
FMT_CONSTEXPR unsigned parse_nonnegative_int(Iterator &it, ErrorHandler &&eh)
Definition: printf.h:116
alignment align_
Definition: format.h:1092
void append(const U *begin, const U *end)
Definition: format.h:394
FMT_CONSTEXPR bool has(unsigned f) const
Definition: format.h:1106
basic_format_arg< basic_printf_context< OutputIt, Char, ArgFormatter > > arg(unsigned id) const
Definition: core.h:1025
#define FMT_THROW(x)
Definition: format.h:115
unsigned parse_header(iterator &it, format_specs &spec)
Definition: printf.h:559
uint_least8_t flags
Definition: format.h:1102
internal::null_terminating_iterator< char_type > iterator
Definition: printf.h:488
base::format_arg format_arg
Definition: printf.h:486
void convert_arg(basic_format_arg< Context > &arg, Char type)
Definition: printf.h:259
format_arg get_arg(iterator it, unsigned arg_index=(std::numeric_limits< unsigned >::max)())
Definition: printf.h:550
FMT_CONSTEXPR internal::result_of< Visitor(int)>::type visit_format_arg(Visitor &&vis, const basic_format_arg< Context > &arg)
Definition: core.h:831
FMT_CONSTEXPR const Char * pointer_from(null_terminating_iterator< Char > it)
Definition: printf.h:107
template<typename OutputIt , typename Char , typename AF >
basic_printf_context< OutputIt, Char, AF >::format_arg basic_printf_context< OutputIt, Char, AF >::get_arg ( iterator  it,
unsigned  arg_index = (std::numeric_limits<unsigned>::max)() 
)
private

Definition at line 550 of file printf.h.

551  {
552  (void)it;
553  if (arg_index == std::numeric_limits<unsigned>::max())
554  return this->do_get_arg(this->parse_context().next_arg_id());
555  return base::get_arg(arg_index - 1);
556 }
format_arg get_arg(unsigned arg_id)
Definition: core.h:1017
static int max(int a, int b)
template<typename OutputIt , typename Char , typename AF >
void basic_printf_context< OutputIt, Char, AF >::parse_flags ( format_specs spec,
iterator it 
)
private

Definition at line 522 of file printf.h.

523  {
524  for (;;) {
525  switch (*it++) {
526  case '-':
527  spec.align_ = ALIGN_LEFT;
528  break;
529  case '+':
530  spec.flags |= SIGN_FLAG | PLUS_FLAG;
531  break;
532  case '0':
533  spec.fill_ = '0';
534  break;
535  case ' ':
536  spec.flags |= SIGN_FLAG;
537  break;
538  case '#':
539  spec.flags |= HASH_FLAG;
540  break;
541  default:
542  --it;
543  return;
544  }
545  }
546 }
wchar_t fill_
Definition: format.h:1091
alignment align_
Definition: format.h:1092
uint_least8_t flags
Definition: format.h:1102
template<typename OutputIt , typename Char , typename AF >
unsigned basic_printf_context< OutputIt, Char, AF >::parse_header ( iterator it,
format_specs spec 
)
private

Definition at line 559 of file printf.h.

560  {
561  unsigned arg_index = std::numeric_limits<unsigned>::max();
562  char_type c = *it;
563  if (c >= '0' && c <= '9') {
564  // Parse an argument index (if followed by '$') or a width possibly
565  // preceded with '0' flag(s).
567  unsigned value = parse_nonnegative_int(it, eh);
568  if (*it == '$') { // value is an argument index
569  ++it;
570  arg_index = value;
571  } else {
572  if (c == '0')
573  spec.fill_ = '0';
574  if (value != 0) {
575  // Nonzero value means that we parsed width and don't need to
576  // parse it or flags again, so return now.
577  spec.width_ = value;
578  return arg_index;
579  }
580  }
581  }
582  parse_flags(spec, it);
583  // Parse width.
584  if (*it >= '0' && *it <= '9') {
586  spec.width_ = parse_nonnegative_int(it, eh);
587  } else if (*it == '*') {
588  ++it;
589  spec.width_ = visit_format_arg(
591  }
592  return arg_index;
593 }
wchar_t fill_
Definition: format.h:1091
FMT_CONSTEXPR unsigned parse_nonnegative_int(Iterator &it, ErrorHandler &&eh)
Definition: printf.h:116
static int max(int a, int b)
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
void parse_flags(format_specs &spec, iterator &it)
Definition: printf.h:522
unsigned width_
Definition: format.h:1088
format_arg get_arg(iterator it, unsigned arg_index=(std::numeric_limits< unsigned >::max)())
Definition: printf.h:550
FMT_CONSTEXPR internal::result_of< Visitor(int)>::type visit_format_arg(Visitor &&vis, const basic_format_arg< Context > &arg)
Definition: core.h:831

The documentation for this class was generated from the following file: