Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::StringFormat< RangeIterator > Struct Template Reference

Formats String with a simple DSL embedded in the format string. More...

#include <StringFormat.h>

Classes

struct  Implementation
 

Static Public Member Functions

template<typename... Types>
static bool format (StringFormatOutput &data, StringView fmt, Types &&... args)
 Formats fmt StringView using simple DSL where {} are replaced with args. More...
 

Detailed Description

template<typename RangeIterator>
struct SC::StringFormat< RangeIterator >

Formats String with a simple DSL embedded in the format string.

This is a small implementation to format using a minimal string based DSL, but good enough for simple usages. It uses the same {} syntax and supports positional arguments.
StringFormat::format(output, "{1} {0}", "World", "Hello") is formatted as "Hello World".
Inside the {} after a colon (:) a specification string can be used to indicate how to format the given value. As the backend for actual number to string formatting is snprintf, such specification strings are the same as what would be given to snprintf. For example passing "{:02}" is transformed to "%.02f" when passed to snprintf.
{ is escaped if found near to another {. In other words format("{{") will print a single {.

Example:

String buffer(StringEncoding::Ascii);
StringBuilder builder(buffer);
SC_TEST_EXPECT(builder.format("{1}_{0}_{1}", 1, 0));
SC_TEST_EXPECT(buffer == "0_1_0");
SC_TEST_EXPECT(builder.format("{0:.2}_{1}_{0:.4}", 1.2222, "salve"));
SC_TEST_EXPECT(buffer == "1.22_salve_1.2222");
@ Ascii
Encoding is ASCII.
#define SC_TEST_EXPECT(e)
Records a test expectation (eventually aborting or breaking o n failed test)
Definition: Testing.h:113
Note
It's not convenient to use SC::StringFormat directly, as you should probably use SC::StringBuilder
Template Parameters
RangeIteratorType of the specific StringIterator used

Member Function Documentation

◆ format()

template<typename RangeIterator >
template<typename... Types>
bool SC::StringFormat< RangeIterator >::format ( StringFormatOutput data,
StringView  fmt,
Types &&...  args 
)
static

Formats fmt StringView using simple DSL where {} are replaced with args.

Template Parameters
TypesTypes of the arguments being formatted
Parameters
dataDestination abstraction (buffer or console)
fmtThe format string to be used
argsActual arguments being formatted
Returns
true if format succeeded

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