libpqxx
The C++ client library for PostgreSQL
concat.hxx
1 #if !defined(PQXX_CONCAT_HXX)
2 # define PQXX_CONCAT_HXX
3 
4 # include <string>
5 # include <string_view>
6 
7 # include "pqxx/strconv.hxx"
8 
9 namespace pqxx::internal
10 {
12 template<typename TYPE>
13 void render_item(TYPE const &item, char *&here, char *end)
14 {
15  auto const next = string_traits<TYPE>::into_buf(here, end, item) - 1;
16  PQXX_ASSUME(next >= here);
17  here = next;
18 }
19 
20 
21 // C++20: Support non-random_access_range ranges.
23 
30 template<typename... TYPE>
31 [[nodiscard]] inline std::string concat(TYPE... item)
32 {
33  std::string buf;
34  // Size to accommodate string representations of all inputs, minus their
35  // terminating zero bytes.
36  buf.resize(size_buffer(item...));
37 
38  char *const data{buf.data()};
39  char *here = data;
40  char *end = data + std::size(buf);
41  (render_item(item, here, end), ...);
42 
43  buf.resize(static_cast<std::size_t>(here - data));
44  return buf;
45 }
46 } // namespace pqxx::internal
47 #endif
std::string concat(TYPE...item)
Efficiently combine a bunch of items into one big string.
Definition: concat.hxx:31
Internal items for libpqxx' own use. Do not use these yourself.
Definition: encodings.cxx:32
std::size_t size_buffer(TYPE const &...value) noexcept
Estimate how much buffer space is needed to represent values as a string.
Definition: strconv.hxx:526
static char * into_buf(char *begin, char *end, TYPE const &value)
Write value's string representation into buffer at begin.
void render_item(TYPE const &item, char *&here, char *end)
Convert item to a string, write it into [here, end).
Definition: concat.hxx:13