|
| template<class Arg , class... Args> |
| size_t | c4::format (substr buf, csubstr fmt, Arg const &a, Args const &...more) |
| | Using a format string, serialize the arguments into the given fixed-size buffer. More...
|
| |
| template<class... Args> |
| substr | c4::format_sub (substr buf, csubstr fmt, Args const &...args) |
| | like c4::format() but return a substr instead of a size More...
|
| |
| template<class CharOwningContainer , class... Args> |
| void | c4::formatrs (CharOwningContainer *cont, csubstr fmt, Args const &...args) |
| | format+resize: like c4::format(), but receives a container, and resizes it as needed to contain the result. More...
|
| |
| template<class CharOwningContainer , class... Args> |
| CharOwningContainer | c4::formatrs (csubstr fmt, Args const &...args) |
| | format+resize: like c4::format(), but create a new container with the result. More...
|
| |
| template<class CharOwningContainer , class... Args> |
| csubstr | c4::formatrs_append (CharOwningContainer *cont, csubstr fmt, Args const &...args) |
| | format+resize+append: like c4::format(), but receives a container, and appends the arguments, resizing the container as needed to contain the result. More...
|
| |
template<class Arg , class... Args>
| size_t c4::format |
( |
substr |
buf, |
|
|
csubstr |
fmt, |
|
|
Arg const & |
a, |
|
|
Args const &... |
more |
|
) |
| |
Using a format string, serialize the arguments into the given fixed-size buffer.
The buffer size is strictly respected: no writes will occur beyond its end. In the format string, each argument is marked with a compact curly-bracket pair "{}". This pair does not take any interior sequence numbers or extra formatting arguments inside it (contrary to eg the C++20 std::format implementation or the Python formatting facilities). To enable argument customization, use the formatting facilities in Format specifiers wrapping the arguments passed to this function.
- Returns
- the number of bytes needed to write into the buffer.
- See also
- c4::formatrs() if instead of a fixed-size buffer, a resizeable container is desired
-
c4::unformat() for the inverse function
-
c4::cat() if no format or separator is needed
-
c4::catsep() if no format is needed, but a separator must be used
For example:
c4::format(buf,
"the {} drank {} {}",
"partier", 5,
"beers");
c4::format(buf,
"the {} drank {} {}",
"programmer", 6,
"coffees");
Using Format specifiers enables control of the result. For example:
integral_< intptr_t > hex(std::nullptr_t)
format null as an hexadecimal value
integral_< intptr_t > bin(std::nullptr_t)
format null as a binary 0-1 value
integral_< intptr_t > oct(std::nullptr_t)
format null as an octal value
real_< T > real(T val, int precision, RealFormat_e fmt=FTOA_FLOAT)
integral_padded_< T > zpad(T val, size_t num_digits)
pad the argument with zeroes on the left, with decimal radix
- Note
- Arguments beyond the last curly bracket pair are silently ignored. Curly bracket pairs without a corresponding argument are printed as part of the result.
c4::format(buf,
"the {} drank {} {}",
"partier", 5,
"beers",
"and nothing else");
c4::format(buf,
"the {} drank {} {} this is ignored: {}",
"programmer", 6,
"coffees");
-
The curly bracket pair cannot be escaped, but can of course be placed into the result by passing an "{}" argument in its place, or if it is provided beyond the last argument passed to the function (see prior note).
c4::format(buf,
"let's show {} on the result and then {}",
"{}",
"this");
-
The arguments to format are restricted (legal because they are rvalues). This may require a workaround when arguments of type char[]/const char[] are passed repeatedly to the function. For example,
const char str[] = "Hi! ";
size_t cat(substr buf, Arg const &a, Args const &...more)
serialize the arguments, concatenating them to the given fixed-size buffer.
It is possible to work around the problem by suppressing -Wrestrict or by using the decayed type char* or const char*, or even wrapping the argument in a csubstr(): const char str[] = "Hi! ";
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition at line 940 of file format.hpp.
942 size_t pos = fmt.find(
"{}");
945 size_t num =
to_chars(buf, fmt.first(pos));
947 buf = buf.len >= num ? buf.sub(num) : substr{};
950 buf = buf.len >= num ? buf.sub(num) : substr{};
951 num =
format(buf, fmt.sub(pos + 2), more...);
size_t to_chars(substr buf, fmt::center_< T > const &align)
@ npos
a null string position
template<class CharOwningContainer , class... Args>
| void c4::formatrs |
( |
CharOwningContainer * |
cont, |
|
|
csubstr |
fmt, |
|
|
Args const &... |
args |
|
) |
| |
|
inline |
format+resize: like c4::format(), but receives a container, and resizes it as needed to contain the result.
The container is overwritten. To append to the container use c4::formatrs_append().
- See also
- c4::format()
-
c4::formatrs_append()
- Note
- The arguments to format are restricted (legal because they are rvalues). This may require a workaround when arguments of type char[]/const char[] are passed repeatedly to the function. For example,
const char str[] = "Hi! ";
It is possible to work around the problem by suppressing -Wrestrict or by using the decayed type char* or const char*, or even wrapping the argument in a csubstr(): const char str[] = "Hi! ";
Definition at line 1267 of file format.hpp.
1269 cont->resize(cont->capacity());
1272 size_t ret =
format(buf, fmt, args...);
substr to_substr(substr s) noexcept
neutral version for use in generic code
template<class CharOwningContainer , class... Args>
| csubstr c4::formatrs_append |
( |
CharOwningContainer * |
cont, |
|
|
csubstr |
fmt, |
|
|
Args const &... |
args |
|
) |
| |
|
inline |
format+resize+append: like c4::format(), but receives a container, and appends the arguments, resizing the container as needed to contain the result.
The buffer is appended to.
- Returns
- the region newly appended to the original container
- Note
- The arguments to format are restricted (legal because they are rvalues). This may require a workaround when arguments of type char[]/const char[] are passed repeatedly to the function. For example,
const char str[] = "Hi! ";
It is possible to work around the problem by suppressing -Wrestrict or by using the decayed type char* or const char*, or even wrapping the argument in a csubstr(): const char str[] = "Hi! ";
Definition at line 1334 of file format.hpp.
1336 const size_t pos = cont->size();
1337 cont->resize(cont->capacity());
1340 size_t ret =
format(buf, fmt, args...);
1341 cont->resize(pos + ret);
1344 return to_csubstr(*cont).range(pos, cont->size());