![]() |
Plasma Engine
2.0
|
Implements formating of strings with placeholders and formatting options. More...
#include <FormatString.h>
Public Member Functions | |
PL_ALWAYS_INLINE | plFormatString (const char *szString) |
PL_ALWAYS_INLINE | plFormatString (plStringView sString) |
plFormatString (const plStringBuilder &s) | |
virtual plStringView | GetText (plStringBuilder &) const |
Generates the formatted text. Make sure to only call this function once and only when the formatted string is really needed. | |
virtual const char * | GetTextCStr (plStringBuilder &out_sString) const |
Similar to GetText() but guaranteed to copy the string into the given string builder, and thus guaranteeing that the generated string is zero terminated. | |
bool | IsEmpty () const |
plStringView | BuildFormattedText (plStringBuilder &ref_sStorage, plStringView *pArgs, plUInt32 uiNumArgs) const |
Helper function to build the formatted text with the given arguments. | |
Protected Attributes | |
plStringView | m_sString |
Implements formating of strings with placeholders and formatting options.
plFormatString can be used anywhere where a string should be formatable when passing it into a function. Good examples are plStringBuilder::SetFormat() or plLog::Info().
A function taking an plFormatString can internally call plFormatString::GetText() to retrieve he formatted result. When calling such a function, one must wrap the parameter into 'plFmt' to enable formatting options, example: void MyFunc(const plFormatString& text); MyFunc(plFmt("Cool Story {}", "Bro"));
To provide more convenience, one can add a template-function overload like this: template <typename... ARGS> void MyFunc(const char* szFormat, ARGS&&... args) { MyFunc(plFormatStringImpl<ARGS...>(szFormat, std::forward<ARGS>(args)...)); }
This allows to call MyFunc() without the 'plFmt' wrapper.
=== Formatting ===
Placeholders for variables are specified using '{}'. These may use numbers from 0 to 9, ie. {0}, {3}, {2}, etc. which allows to change the order or insert duplicates. If no number is provided, each {} instance represents the next argument.
To specify special formatting, wrap the argument into an plArgXY call: plArgC - for characters plArgI - for integer formatting plArgU - for unsigned integer formatting (e.g. HEX) plArgF - for floating point formatting plArgP - for pointer formatting plArgDateTime - for plDateTime formatting options plArgErrorCode - for Windows error code formatting plArgHumanReadable - for shortening numbers with common abbreviations plArgFileSize - for representing file sizes
Example: plStringBuilder::SetFormat("HEX: {}", plArgU(1337, 8 /*width*/, true /*pad with zeros*/, 16 /*base16*/, true/*upper case*/));
Arbitrary other types can support special formatting even without an plArgXY call. E.g. plTime and plAngle do special formatting. plArgXY calls are only necessary if formatting options are needed for a specific formatting should be enforced (e.g. plArgErrorCode would otherwise just use uint32 formatting).
To implement custom formatting see the various free standing 'BuildString' functions.
plStringView plFormatString::BuildFormattedText | ( | plStringBuilder & | ref_sStorage, |
plStringView * | pArgs, | ||
plUInt32 | uiNumArgs ) const |
Helper function to build the formatted text with the given arguments.
|
inlinenodiscardvirtual |
Generates the formatted text. Make sure to only call this function once and only when the formatted string is really needed.
Requires an plStringBuilder as storage, ie. POTENTIALLY writes the formatted text into it. However, if no formatting is required, it may not touch the string builder at all and just return a string directly.
Reimplemented in plFormatStringImpl< ARGS >.
|
virtual |
Similar to GetText() but guaranteed to copy the string into the given string builder, and thus guaranteeing that the generated string is zero terminated.
Reimplemented in plFormatStringImpl< ARGS >.