rapidyaml  0.7.0
parse and emit YAML, and do it fast
format: formatted string interpolation

Functions

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 format(), but receives a container, and appends the arguments, resizing the container as needed to contain the result. More...
 

Detailed Description

Function Documentation

◆ format()

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: {}. Arguments beyond the last curly bracket pair are silently ignored. For example:

c4::format(buf, "the {} drank {} {}", "partier", 5, "beers"); // the partier drank 5 beers
c4::format(buf, "the {} drank {} {}", "programmer", 6, "coffees"); // the programmer drank 6 coffees
size_t format(substr buf, csubstr fmt, Arg const &a, Args const &...more)
using a format string, serialize the arguments into the given fixed-size buffer.
Definition: format.hpp:806
Returns
the number of characters 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

Definition at line 806 of file format.hpp.

807 {
808  size_t pos = fmt.find("{}"); // @todo use _find_fmt()
809  if(C4_UNLIKELY(pos == csubstr::npos))
810  return to_chars(buf, fmt);
811  size_t num = to_chars(buf, fmt.sub(0, pos));
812  size_t out = num;
813  buf = buf.len >= num ? buf.sub(num) : substr{};
814  num = to_chars(buf, a);
815  out += num;
816  buf = buf.len >= num ? buf.sub(num) : substr{};
817  num = format(buf, fmt.sub(pos + 2), more...);
818  out += num;
819  return out;
820 }
size_t to_chars(substr buf, fmt::right_< T > const &align)
Definition: format.hpp:548
@ npos
a null string position
Definition: common.hpp:266

References c4::yml::npos, and c4::to_chars().

Referenced by c4::format_sub(), c4::formatrs(), c4::formatrs_append(), sample::sample_formatting(), and sample::to_chars().

◆ format_sub()

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

See also
c4::format()
c4::catsep(). uncatsep() is the inverse of catsep().

Definition at line 826 of file format.hpp.

827 {
828  size_t sz = c4::format(buf, fmt, args...);
829  C4_CHECK(sz <= buf.len);
830  return {buf.str, sz <= buf.len ? sz : buf.len};
831 }

References c4::format().

Referenced by sample::sample_formatting().

◆ formatrs() [1/2]

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 the append overload.

See also
c4::format()

Definition at line 995 of file format.hpp.

996 {
997 retry:
998  substr buf = to_substr(*cont);
999  size_t ret = format(buf, fmt, args...);
1000  cont->resize(ret);
1001  if(ret > buf.len)
1002  goto retry;
1003 }
substr to_substr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2187

References c4::format(), and c4::to_substr().

Referenced by c4::formatrs(), and sample::sample_formatting().

◆ formatrs() [2/2]

template<class CharOwningContainer , class... Args>
CharOwningContainer c4::formatrs ( csubstr  fmt,
Args const &...  args 
)
inline

format+resize: like c4::format(), but create a new container with the result.

Returns
the requested container

Definition at line 1011 of file format.hpp.

1012 {
1013  CharOwningContainer cont;
1014  formatrs(&cont, fmt, args...);
1015  return cont;
1016 }
CharOwningContainer formatrs(csubstr fmt, Args const &...args)
format+resize: like c4::format(), but create a new container with the result.
Definition: format.hpp:1011

References c4::formatrs().

◆ formatrs_append()

template<class CharOwningContainer , class... Args>
csubstr c4::formatrs_append ( CharOwningContainer *  cont,
csubstr  fmt,
Args const &...  args 
)
inline

format+resize+append: like 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

Definition at line 1025 of file format.hpp.

1026 {
1027  const size_t pos = cont->size();
1028 retry:
1029  substr buf = to_substr(*cont).sub(pos);
1030  size_t ret = format(buf, fmt, args...);
1031  cont->resize(pos + ret);
1032  if(ret > buf.len)
1033  goto retry;
1034  return to_csubstr(*cont).range(pos, cont->size());
1035 }
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2189

References c4::format(), c4::to_csubstr(), and c4::to_substr().

Referenced by sample::sample_formatting().