rapidyaml  0.7.2
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:815
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 815 of file format.hpp.

816 {
817  size_t pos = fmt.find("{}"); // @todo use _find_fmt()
818  if(C4_UNLIKELY(pos == csubstr::npos))
819  return to_chars(buf, fmt);
820  size_t num = to_chars(buf, fmt.sub(0, pos));
821  size_t out = num;
822  buf = buf.len >= num ? buf.sub(num) : substr{};
823  num = to_chars(buf, a);
824  out += num;
825  buf = buf.len >= num ? buf.sub(num) : substr{};
826  num = format(buf, fmt.sub(pos + 2), more...);
827  out += num;
828  return out;
829 }
size_t to_chars(substr buf, fmt::right_< T > const &align)
Definition: format.hpp:557
@ 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 835 of file format.hpp.

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

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

1035 {
1036  const size_t pos = cont->size();
1037 retry:
1038  substr buf = to_substr(*cont).sub(pos);
1039  size_t ret = format(buf, fmt, args...);
1040  cont->resize(pos + ret);
1041  if(ret > buf.len)
1042  goto retry;
1043  return to_csubstr(*cont).range(pos, cont->size());
1044 }
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().