rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
cat: concatenate arguments to string

Functions

template<class Arg, class... Args>
size_t c4::cat (substr buf, Arg const &a, Args const &...more)
 serialize the arguments, concatenating them to the given fixed-size buffer.
template<class... Args>
substr c4::cat_sub (substr buf, Args const &...args)
 like c4::cat() but return a substr instead of a size
template<class CharOwningContainer, class... Args>
void c4::catrs (CharOwningContainer *cont, Args const &...args)
 cat+resize: like c4::cat(), but receives a container, and resizes it as needed to contain the result.
template<class CharOwningContainer, class... Args>
CharOwningContainer c4::catrs (Args const &...args)
 cat+resize: like c4::cat(), but creates and returns a new container sized as needed to contain the result.
template<class CharOwningContainer, class... Args>
csubstr c4::catrs_append (CharOwningContainer *cont, Args const &...args)
 cat+resize+append: like c4::cat(), but receives a container, and appends to it instead of overwriting it.

Detailed Description

Function Documentation

◆ cat()

template<class Arg, class... Args>
size_t c4::cat ( substr buf,
Arg const & a,
Args const &... more )

serialize the arguments, concatenating them to the given fixed-size buffer.

The buffer size is strictly respected: no writes will occur beyond its end.

Returns
the number of characters needed to write all the arguments into the buffer.
See also
c4::catrs() if instead of a fixed-size buffer, a resizeable container is desired
c4::uncat() for the inverse function
c4::catsep() if a separator between each argument is to be used
c4::format() if a format string is desired
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! ";
cat(buf, str, str, str); // compile error: 'passing argument 2 to ‘restrict’-qualified parameter aliases with arguments 3, 4'
size_t cat(substr buf, Arg const &a, Args const &...more)
serialize the arguments, concatenating them to the given fixed-size buffer.
Definition format.hpp:649
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 ss = to_csubstr(str);
cat(buf, ss, ss, ss); // ok! compiles cleanly
csubstr to_csubstr(const char(&s)[N]) noexcept
Definition substr.hpp:2381
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357

Definition at line 649 of file format.hpp.

650{
651 size_t num = to_chars(buf, a);
652 buf = buf.len >= num ? buf.sub(num) : substr{};
653 num += cat(buf, more...);
654 return num;
655}
basic_substring< char > substr
a mutable string view
Definition substr.hpp:2356
size_t to_chars(substr buf, uint8_t v) noexcept
size_t len
the length of the substring
Definition substr.hpp:218
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:503

◆ cat_sub()

template<class... Args>
substr c4::cat_sub ( substr buf,
Args const &... args )

like c4::cat() but return a substr instead of a size

Definition at line 659 of file format.hpp.

660{
661 size_t sz = cat(buf, args...);
662 C4_CHECK(sz <= buf.len);
663 return {buf.str, sz <= buf.len ? sz : buf.len};
664}
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

◆ catrs() [1/2]

template<class CharOwningContainer, class... Args>
void c4::catrs ( CharOwningContainer * cont,
Args const &... args )
inline

cat+resize: like c4::cat(), but receives a container, and resizes it as needed to contain the result.

The container is overwritten. To append to it, use c4::catrs_append().

See also
c4::cat()
c4::catrs_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! ";
cat(buf, str, str, str); // compile error: 'passing argument 2 to ‘restrict’-qualified parameter aliases with arguments 3, 4'
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 ss = to_csubstr(str);
cat(buf, ss, ss, ss); // ok! compiles cleanly

Definition at line 1040 of file format.hpp.

1041{
1042 cont->resize(cont->capacity()); // improve the odds of fitting in the original buffer
1043retry:
1044 substr buf = to_substr(*cont);
1045 size_t ret = cat(buf, args...);
1046 cont->resize(ret);
1047 if(ret > buf.len)
1048 goto retry;
1049}
substr to_substr(char(&s)[N]) noexcept
Definition substr.hpp:2377

◆ catrs() [2/2]

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

cat+resize: like c4::cat(), but creates and returns a new container sized as needed to contain the result.

See also
c4::cat()
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! ";
cat(buf, str, str, str); // compile error: 'passing argument 2 to ‘restrict’-qualified parameter aliases with arguments 3, 4'
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 ss = to_csubstr(str);
cat(buf, ss, ss, ss); // ok! compiles cleanly

Definition at line 1075 of file format.hpp.

1076{
1077 CharOwningContainer cont;
1078 catrs(&cont, args...);
1079 return cont;
1080}
void catrs(CharOwningContainer *cont, Args const &...args)
cat+resize: like c4::cat(), but receives a container, and resizes it as needed to contain the result.
Definition format.hpp:1040

◆ catrs_append()

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

cat+resize+append: like c4::cat(), but receives a container, and appends to it instead of overwriting it.

The container is resized as needed to contain the result.

Returns
the region newly appended to the original container
See also
c4::cat()
c4::catrs()
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! ";
cat(buf, str, str, str); // compile error: 'passing argument 2 to ‘restrict’-qualified parameter aliases with arguments 3, 4'
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 ss = to_csubstr(str);
cat(buf, ss, ss, ss); // ok! compiles cleanly

Definition at line 1109 of file format.hpp.

1110{
1111 const size_t pos = cont->size();
1112 cont->resize(cont->capacity()); // improve the odds of fitting in the original buffer
1113retry:
1114 substr buf = to_substr(*cont).sub(pos);
1115 size_t ret = cat(buf, args...);
1116 cont->resize(pos + ret);
1117 if(ret > buf.len)
1118 goto retry;
1119 return to_csubstr(*cont).range(pos, cont->size());
1120}
basic_substring range(size_t first, size_t last=npos) const noexcept
return [first,last[.
Definition substr.hpp:520