MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
Format

Built-in formatting options and specifications. More...

Classes

struct  mtcore::io::FormatOptions
 Options for specifying formatting. More...
 
struct  mtcore::io::Formatter< T >
 Struct to override to specify how a type should be formatted. More...
 
struct  mtcore::io::PaddingOptions
 Represents parsed padding options for formatting output. More...
 
struct  mtcore::io::Padded< T >
 Formats data with padding into a writer's output stream Uses Formatter<T> under the hood Usually will use extract_padding_options to get padding options. More...
 
struct  mtcore::io::Formatter< char >
 Formatter for characters Format specifier options for output: More...
 
struct  mtcore::io::Formatter< T >
 Formats integer numbers (other than char) such as int, unsigned, long long, etc Format specifier options for output: More...
 
struct  mtcore::io::Formatter< char * >
 Format specifier for null-terminated char* (mutable C-strings) Has same format options as template<Iterable T> struct Formatter<T> More...
 
struct  mtcore::io::Formatter< const char * >
 Format specifier for null-terminated const char* (C-strings) Has same format options as template<Iterable T> struct Formatter<T> More...
 
struct  mtcore::io::Formatter< Optional< T > >
 Formatter for an optional Uses the formatter for the underlying value if available No formatting options. More...
 
struct  mtcore::io::Formatter< Result< V, E > >
 Formatter for a result Will check if the value has been moved out of the result Uses the formatter for the underlying value and/or error if available No formatting options. More...
 
struct  mtcore::io::Formatter< IPv4 >
 Formats IPv4 addresses No formatting options are available. More...
 
struct  mtcore::io::Formatter< IPv6 >
 Formats IPv6 addresses Formatting options: More...
 
struct  mtcore::io::Formatter< SubnetMaskV4 >
 Formats subnet v4 masks. More...
 
struct  mtcore::io::Formatter< SubnetMaskV6 >
 Formats subnet v4 masks. More...
 
struct  mtcore::io::Formatter< SubnetV4 >
 Formats a masked version 4 IP address with a subnet. More...
 
struct  mtcore::io::Formatter< SubnetV6 >
 Formats a masked version 6 IP address with a subnet. More...
 

Enumerations

enum class  mtcore::io::ContentAlignment { mtcore::io::ContentAlignment::LEFT , mtcore::io::ContentAlignment::RIGHT , mtcore::io::ContentAlignment::CENTER }
 Represents content alignment options for padded formatting options. More...
 

Functions

Optional< PaddingOptionsmtcore::io::extract_padding_options (Slice< const char > formatStr)
 Extracts padding options from a format string if present Padding options are in the following form: (padChar)(padDirection)(padLen); Where padChar is the character to pad with (maybe escaped with \ to avoid interpretation as a control character), padDirection is either < for left pad, > for right pad, or ^ for center padding, and padLen is an integer of the minimum length the output should be.
 
template<WriterImpl WI, typename... Args>
Result< size_t, typename Writer< WI >::ErrType > mtcore::io::print (Writer< WI > &writer, const char *fmt, const Args &...args)
 Prints arguments using a format string Element insert points are designated by a pair of curly braces {} Format parameters can be between the curly braces (e.g.
 
template<WriterImpl WI, typename... Args>
Result< size_t, typename Writer< WI >::ErrType > mtcore::io::print (Writer< WI > &writer, const Slice< const char > &fmt, const Args &...args)
 Prints arguments using a format string Element insert points are designated by a pair of curly braces {} Format parameters can be between the curly braces (e.g.
 

Detailed Description

Built-in formatting options and specifications.

Enumeration Type Documentation

◆ ContentAlignment

enum class mtcore::io::ContentAlignment
strong

Represents content alignment options for padded formatting options.

Enumerator
LEFT 
RIGHT 
CENTER 

Definition at line 35 of file formats.hpp.

Function Documentation

◆ extract_padding_options()

Optional< PaddingOptions > mtcore::io::extract_padding_options ( Slice< const char > formatStr)
inline

Extracts padding options from a format string if present Padding options are in the following form: (padChar)(padDirection)(padLen); Where padChar is the character to pad with (maybe escaped with \ to avoid interpretation as a control character), padDirection is either < for left pad, > for right pad, or ^ for center padding, and padLen is an integer of the minimum length the output should be.

Note that a semicolon at the end is required.

Examples:

  • 0<12; - Left pads with zeros for up to 12 characters
  • \^>14; - Right pads with carrots (^) for 14 characters
  • ^10; - Centers with spaces for 10 character length
Parameters
formatStrFormat string to parse
Returns
Padding options if present, or nullopt

Definition at line 71 of file formats.hpp.

71 {
72 if (formatStr.empty()) {
73 return nullopt;
74 }
75 auto semiColon = slices::first_index_not_proceeded_by('\\', ';', formatStr);
76 if (!semiColon.has_value()) {
77 return nullopt;
78 }
79
80 auto checkWriter = void_writer<char>();
81 // This checks if we have an effective single character
82 auto has_single_char = [&checkWriter](Slice<const char> s) {
83 auto res = ascii::write_unescaped(checkWriter, s);
84 // void writers don't error out, so only error is a bad format string
85 ensure(res.is_success(), "BAD FORMAT STRING!");
86 return res.value() == 1;
87 };
88
89 auto alignmentSearchSpace = formatStr.sub(0, semiColon.value());
90 auto leftPadLoc = slices::first_index_not_proceeded_by('\\', '<', alignmentSearchSpace);
91 if (leftPadLoc.has_value() && leftPadLoc.value() > 0) {
92 auto n = alignmentSearchSpace.sub(leftPadLoc.value() + 1);
93 if (!n.empty() && ascii::is_pos_int_str(n)) {
94 auto pad = formatStr.sub(0, leftPadLoc.value());
95 if (has_single_char(pad)) {
96 auto len = ascii::base10_to_int<size_t>(n).value();
97 return PaddingOptions{
98 .alignment = ContentAlignment::LEFT,
99 .padLen = len,
100 .pad = ascii::unescape_char(pad).value(),
101 .endPos = semiColon.value(),
102 };
103 }
104 }
105 }
106
107 auto rightPadLoc = slices::first_index_not_proceeded_by('\\', '>', alignmentSearchSpace);
108 if (rightPadLoc.has_value() && rightPadLoc.value() > 0) {
109 auto n = alignmentSearchSpace.sub(rightPadLoc.value() + 1);
110 if (!n.empty() && ascii::is_pos_int_str(n)) {
111 auto pad = formatStr.sub(0, rightPadLoc.value());
112 if (has_single_char(pad)) {
113 auto len = ascii::base10_to_int<size_t>(n).value();
114 return PaddingOptions{
115 .alignment = ContentAlignment::RIGHT,
116 .padLen = len,
117 .pad = ascii::unescape_char(pad).value(),
118 .endPos = semiColon.value(),
119 };
120 }
121 }
122 }
123
124 auto centerPadLoc = slices::first_index_not_proceeded_by('\\', '^', alignmentSearchSpace);
125 if (centerPadLoc.has_value() && centerPadLoc.value() > 0) {
126 auto n = alignmentSearchSpace.sub(centerPadLoc.value() + 1);
127 if (!n.empty() && ascii::is_pos_int_str(n)) {
128 auto pad = formatStr.sub(0, centerPadLoc.value());
129 if (has_single_char(pad)) {
130 auto len = ascii::base10_to_int<size_t>(n).value();
131 return PaddingOptions{
132 .alignment = ContentAlignment::CENTER,
133 .padLen = len,
134 .pad = ascii::unescape_char(pad).value(),
135 .endPos = semiColon.value(),
136 };
137 }
138 }
139 }
140
141 return nullopt;
142 }
Result< Out, AsciiNumericParseError > base10_to_int(const Slice< const char > &parse)
Tries to parse an ASCII numeric string into an integer Will return an error if the parsed string does...
Definition ascii.hpp:244
bool is_pos_int_str(Slice< const char > str)
Checks if a string is composed of only positive integer characters Will return false if string is emp...
Definition ascii.hpp:123
Result< size_t, std::variant< typename Writer::ErrType, UnescapeError > > write_unescaped(Writer &writer, Slice< const char > str, char escapeChar='\\')
Writes unescaped ASCII characters to a stream Note: This does NOT support writing Unicode escape code...
Definition ascii.hpp:447
Result< char, UnescapeError > unescape_char(Slice< const char > str, char escapeChar='\\')
Unescapes the first (potentially) escaped character in a character sequence If the sequence is empty,...
Definition ascii.hpp:343
constexpr auto nullopt
Placeholder value for an empty Optional.
Definition optional.hpp:409
mtcore::Optional< size_t > first_index_not_proceeded_by(const std::remove_const_t< T > &prefix, const std::remove_const_t< T > &needle, const Slice< T > &haystack)
Gets the first index that a needle appears in the haystack, or nullopt if the needle does not appear ...
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
io::Writer< impl::VoidWriterImpl< T > > void_writer()
Creates a writer which discards what's written (think of it as writer to /dev/null) Useful for doing ...
constexpr Slice sub(size_t start) const noexcept
Gets a sub Slice from start.
constexpr bool empty() const noexcept
Checks if a Slice is empty.
Represents parsed padding options for formatting output.
Definition formats.hpp:45
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print() [1/2]

template<WriterImpl WI, typename... Args>
Result< size_t, typename Writer< WI >::ErrType > mtcore::io::print ( Writer< WI > & writer,
const char * fmt,
const Args &... args )

Prints arguments using a format string Element insert points are designated by a pair of curly braces {} Format parameters can be between the curly braces (e.g.

{d}) Escaping characters with backslash \ is supported See the Formatter<> specialization for the type of objects your formatting for their specific formatting string options

General pattern is as follows:

For iterables

  • Element limit option (e.g. 5! to limit to 5 elements)
  • Full iterable padding option (e.g. <12; to left pad to 12 with space)
  • Separator options, also serves as indicator of start of element options (e.g. ,: to join with comma)
  • Element option(s) with pipe | separating each (e.g. x|d| <12;)

For paddable elements

  • Padding in form of padding character (e.g. .), padding direction (<, ^, or >), padding length (12), padding terminator ;

For numbers

  • d for decimal, x for hex, o for octal, b for binary

For print, the arguments may be of different types (e.g. 1, 4.5, "hello")

Template Parameters
WIWriter implementation
ArgsArguments to format
Parameters
writerWriter to write to
fmtFormat string to write with
argsArguments to write
Returns
Number of bytes written, or error

Definition at line 482 of file io/writer.hpp.

482 {
483 return print(writer, slice_from(fmt), args...);
484 }
constexpr Slice< const char32_t > slice_from(char32_t *cstr)
Creates a slice from a utf32 string in the form of a c string.
Result< size_t, typename Writer< WI >::ErrType > print(Writer< WI > &writer, const char *fmt, const Args &...args)
Prints arguments using a format string Element insert points are designated by a pair of curly braces...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print() [2/2]

template<WriterImpl WI, typename... Args>
Result< size_t, typename Writer< WI >::ErrType > mtcore::io::print ( Writer< WI > & writer,
const Slice< const char > & fmt,
const Args &... args )

Prints arguments using a format string Element insert points are designated by a pair of curly braces {} Format parameters can be between the curly braces (e.g.

{d}) Escaping characters with backslash \ is supported See the Formatter<> specialization for the type of objects your formatting for their specific formatting string options

General pattern is as follows:

For iterables

  • Element limit option (e.g. 5! to limit to 5 elements)
  • Full iterable padding option (e.g. <12; to left pad to 12 with space)
  • Separator options, also serves as indicator of start of element options (e.g. ,: to join with comma)
  • Element option(s) with pipe | separating each (e.g. x|d| <12;)

For paddable elements

  • Padding in form of padding character (e.g. .), padding direction (<, ^, or >), padding length (12), padding terminator ;

For numbers

  • d for decimal, x for hex, o for octal, b for binary

For print, the arguments may be of different types (e.g. 1, 4.5, "hello")

Template Parameters
WIWriter implementation
ArgsArguments to format
Parameters
writerWriter to write to
fmtFormat string to write with
argsArguments to write
Returns
Number of bytes written, or error

Definition at line 529 of file io/writer.hpp.

530 {
531 return impl::print(0, writer, fmt, args...);
532 }