rapidyaml  0.10.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:816
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 816 of file format.hpp.

817 {
818  size_t pos = fmt.find("{}"); // @todo use _find_fmt()
819  if(C4_UNLIKELY(pos == csubstr::npos))
820  return to_chars(buf, fmt);
821  size_t num = to_chars(buf, fmt.sub(0, pos));
822  size_t out = num;
823  buf = buf.len >= num ? buf.sub(num) : substr{};
824  num = to_chars(buf, a);
825  out += num;
826  buf = buf.len >= num ? buf.sub(num) : substr{};
827  num = format(buf, fmt.sub(pos + 2), more...);
828  out += num;
829  return out;
830 }
size_t to_chars(substr buf, fmt::right_< T > const &align)
Definition: format.hpp:558
@ npos
a null string position
Definition: common.hpp:267

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 836 of file format.hpp.

837 {
838  size_t sz = c4::format(buf, fmt, args...);
839  C4_CHECK(sz <= buf.len);
840  return {buf.str, sz <= buf.len ? sz : buf.len};
841 }

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 1004 of file format.hpp.

1005 {
1006 retry:
1007  substr buf = to_substr(*cont);
1008  size_t ret = format(buf, fmt, args...);
1009  cont->resize(ret);
1010  if(ret > buf.len)
1011  goto retry;
1012 }
substr to_substr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2184

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 1020 of file format.hpp.

1021 {
1022  CharOwningContainer cont;
1023  formatrs(&cont, fmt, args...);
1024  return cont;
1025 }
CharOwningContainer formatrs(csubstr fmt, Args const &...args)
format+resize: like c4::format(), but create a new container with the result.
Definition: format.hpp:1020

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 1033 of file format.hpp.

1034 {
1035  const size_t pos = cont->size();
1036 retry:
1037  substr buf = to_substr(*cont).sub(pos);
1038  size_t ret = format(buf, fmt, args...);
1039  cont->resize(pos + ret);
1040  if(ret > buf.len)
1041  goto retry;
1042  return to_csubstr(*cont).range(pos, cont->size());
1043 }
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2186

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

Referenced by sample::sample_formatting().