libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
fly::detail::BasicFormatSpecifier< CharType > Struct Template Reference

#include <format_specifier.hpp>

Inheritance diagram for fly::detail::BasicFormatSpecifier< CharType >:

Public Types

enum class  Alignment : std::uint8_t { Default , Left , Right , Center }
 
enum class  Sign : std::uint8_t { Default , Always , NegativeOnly , NegativeOnlyWithPositivePadding }
 
enum class  Type : std::uint8_t {
  None = 20 , Character = 21 , String = 22 , Pointer = 23 ,
  Binary = 2 , Octal = 8 , Decimal = 10 , Hex = 16 ,
  HexFloat = 24 , Scientific = 25 , Fixed = 26 , General = 27
}
 
enum class  Case : std::uint8_t { Lower , Upper }
 
using FormatParseContext = BasicFormatParseContext< CharType >
 

Public Member Functions

constexpr BasicFormatSpecifier (FormatParseContext &context)
 
 BasicFormatSpecifier (const BasicFormatSpecifier &)=default
 
 BasicFormatSpecifier (BasicFormatSpecifier &&)=default
 
BasicFormatSpecifieroperator= (const BasicFormatSpecifier &)=default
 
BasicFormatSpecifieroperator= (BasicFormatSpecifier &&)=default
 
constexpr void parse (FormatParseContext &context)
 
template<typename FormatContext >
std::size_t width (FormatContext &context, std::size_t fallback) const
 
template<typename FormatContext >
std::size_t precision (FormatContext &context, std::size_t fallback) const
 
template<fly::DerivedFrom< BasicFormatSpecifier > FormatterType>
constexpr void copy_formatting_options_into (FormatterType &formatter) const
 

Public Attributes

std::size_t m_position {0}
 
std::optional< CharType > m_fill {std::nullopt}
 
Alignment m_alignment {Alignment::Default}
 
Sign m_sign {Sign::Default}
 
bool m_alternate_form {false}
 
bool m_zero_padding {false}
 
std::optional< std::size_t > m_width {std::nullopt}
 
std::optional< std::size_t > m_width_position {std::nullopt}
 
std::optional< std::size_t > m_precision {std::nullopt}
 
std::optional< std::size_t > m_precision_position {std::nullopt}
 
bool m_locale_specific_form {false}
 
Type m_type {Type::None}
 
Case m_case {Case::Lower}
 
std::optional< ParameterType > m_parameter_type {std::nullopt}
 
std::size_t m_parse_index {0}
 
std::size_t m_size {0}
 
bool m_was_parsed_as_standard_formatter {false}
 

Friends

template<typename T >
bool operator== (const BasicFormatSpecifier< T > &specifier1, const BasicFormatSpecifier< T > &specifier2)
 

Detailed Description

template<fly::StandardCharacter CharType>
struct fly::detail::BasicFormatSpecifier< CharType >

Structure to encapsulate positional and formatting options (which constitute a replacement field) based strongly upon:

https://en.cppreference.com/w/cpp/utility/format/format
https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specification

A replacement field has the following format:

1. An introductory "{" character.
2. An optional non-negative position.
3. An optional colon ":" following by formatting options.
4. A final "}" character.

The postition option specifies the index of the format parameter whose value is to be used for this replacement field. If not specified, the format parameters are used in order. The position option must be specified in all replacements fields or not in any replacement field; mixing of manual and automatic indexing is an error.

Formatting options have the following format, where every field is optional:

1. An optional fill character (which may be any ASCII character other than "{" or "}"),
   followed by an alignment option. The alignment option may be one of:

   "<" - Forces the field to be aligned to the start of the available space.
   ">" - Forces the field to be aligned to the end of the available space.
   "^" - Forces the field to be centered within the available space.

2. A sign indicator. The sign character may be one of:

   "+" - A sign should be used for both non-negative and negative numbers.
   "-" - A sign should be used for negative numbers only.
   " " - A sign should be used for negative numbers and a leading space should be used
         for non-negative numbers.

3. An alternate form indicator (a literal "#" character). If present, the following alternate
   forms will be used:

   Integral types - If binary, octal, or hexadecimal presentation types are used, the
   alternate form inserts 0b, 0, or 0x prefixes, respectively, before the value.

   Floating-point types - A decimal point character will always be inserted even if no digits
   follow it.

4. A zero-padding indicator (a literal "0" character). If present, the value is padded with
   leading zeros. This option is ignored if an alignment option is also specified.

5. A width value. A width is either a positive decimal number or a nested replacement field
   (*). If present, it specifies the minimum field width.

6. A precision value. A precision is a decimal (".") followed by a non-negative decimal
   number or a nested replacement field (*). If present, it specifies the precision or
   maximum field size. It may only be used for the following value types:

   String types - precision specifies the maxiumum number of characters to be used.

   Floating-point types - precision specifies the formatting precision.

7. A locale-specific form indicator (a literal "L" character). It may only be used for the
   following value types:

   Integral types - locale-specific form inserts appropriate digit group separator
   characters.

   Floating-point types - locale-specific form inserts appropriate digit group and radix
   separator characters.

   Boolean types - locale-specific form uses the appropriate string for textual
   representation.

8. A presentation type. The type determines how the value should be presented, where the
   allowed presentation type varies by value type:

   Character types - Valid presentations: none, "c", b", "B", "o", "d", "x", "X".

   String types - Valid presentations: none, "s".

   Pointer types - Valid presentations: none, "p".

   Integral types - Valid presentations: none, "c", b", "B", "o", "d", "x", "X".

   Floating-point types - Valid presentations: none, "a", A", "e", "E", "f", "F", "g", "G".

   Boolean types - Valid presentations: none, "c", s", b", "B", "o", "d", "x", "X".

   For details on each presentation type, see the above links.

(*) Nested replacement fields are a subset of the full replacement field, and may be of the form "{}" or "{n}", where n is an optional non-negative position. The corresponding format parameter must be an integral type. Its value has the same restrictions as the formatting option it is used for.

Author
Timothy Flynn (trfly.nosp@m.nn89.nosp@m.@pm.m.nosp@m.e)
Version
January 3, 2021

Constructor & Destructor Documentation

◆ BasicFormatSpecifier()

template<fly::StandardCharacter CharType>
constexpr fly::detail::BasicFormatSpecifier< CharType >::BasicFormatSpecifier ( FormatParseContext context)
explicitconstexpr

Constructor. Initializes the replacement field's format parameter position and infers its presentation type.

Parameters
contextThe context holding the format string parsing state.

Member Function Documentation

◆ copy_formatting_options_into()

template<fly::StandardCharacter CharType>
template<fly::DerivedFrom< BasicFormatSpecifier > FormatterType>
constexpr void fly::detail::BasicFormatSpecifier< CharType >::copy_formatting_options_into ( FormatterType &  formatter) const
inlineconstexpr

Copy formatting options from this replacement field into another replacement field (which may be an instance of or derived from this class).

Parameters
formatterThe replacement field to copy formatting options into.

◆ parse()

template<fly::StandardCharacter CharType>
constexpr void fly::detail::BasicFormatSpecifier< CharType >::parse ( FormatParseContext context)
constexpr

Parse the formatting options for a standard replacement field.

Parameters
contextThe context holding the format string parsing state.
Returns
The parsed specifier.

◆ precision()

template<fly::StandardCharacter CharType>
template<typename FormatContext >
std::size_t fly::detail::BasicFormatSpecifier< CharType >::precision ( FormatContext &  context,
std::size_t  fallback 
) const
inline

The precision formatting option may either be a number or a nested replacement field. If a numeric value was specified, return that value. If a nested replacement field was specified, return the value of the format parameter at the position indicated by the nested replacement field.

Template Parameters
FormatContextThe formatting context type.
Parameters
contextThe context holding the formatting state.
fallbackThe value to return if neither a number or replacement field was specified.
Returns
The replacement field's resolved precision.

◆ width()

template<fly::StandardCharacter CharType>
template<typename FormatContext >
std::size_t fly::detail::BasicFormatSpecifier< CharType >::width ( FormatContext &  context,
std::size_t  fallback 
) const
inline

The width formatting option may either be a number or a nested replacement field. If a numeric value was specified, return that value. If a nested replacement field was specified, return the value of the format parameter at the position indicated by the nested replacement field.

Template Parameters
FormatContextThe formatting context type.
Parameters
contextThe context holding the formatting state.
fallbackThe value to return if neither a number or replacement field was specified.
Returns
The replacement field's resolved width.

Friends And Related Function Documentation

◆ operator==

template<fly::StandardCharacter CharType>
template<typename T >
bool operator== ( const BasicFormatSpecifier< T > &  specifier1,
const BasicFormatSpecifier< T > &  specifier2 
)
friend

Compare two replacement field instances for equality.

Returns
True if the two instances are equal.

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