libpqxx  7.7.1
separated_list.hxx
1 /* Helper similar to Python's `str.join()`.
2  *
3  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/separated_list instead.
4  *
5  * Copyright (c) 2000-2022, Jeroen T. Vermeulen.
6  *
7  * See COPYING for copyright license. If you did not receive a file called
8  * COPYING with this source code, please notify the distributor of this
9  * mistake, or contact the author.
10  */
11 #ifndef PQXX_H_SEPARATED_LIST
12 #define PQXX_H_SEPARATED_LIST
13 
14 #if !defined(PQXX_HEADER_PRE)
15 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
16 #endif
17 
18 #include <algorithm>
19 #include <numeric>
20 
21 #include "pqxx/strconv.hxx"
22 
23 // C++20: Simplify using std::ranges::range.
24 // C++20: Optimise buffer allocation using random_access_range/iterator.
25 namespace pqxx
26 {
31 
33 
41 template<typename ITER, typename ACCESS>
42 [[nodiscard]] inline std::string
43 separated_list(std::string_view sep, ITER begin, ITER end, ACCESS access)
44 {
45  if (end == begin)
46  return {};
47  auto next{begin};
48  ++next;
49  if (next == end)
50  return to_string(access(begin));
51 
52  // From here on, we've got at least 2 elements -- meaning that we need sep.
53  using elt_type = strip_t<decltype(access(begin))>;
54  using traits = string_traits<elt_type>;
55 
56  std::size_t budget{0};
57  for (ITER cnt{begin}; cnt != end; ++cnt)
58  budget += traits::size_buffer(access(cnt));
59  budget +=
60  static_cast<std::size_t>(std::distance(begin, end)) * std::size(sep);
61 
62  std::string result;
63  result.resize(budget);
64 
65  char *const data{result.data()};
66  char *here{data};
67  char *stop{data + budget};
68  here = traits::into_buf(here, stop, access(begin)) - 1;
69  for (++begin; begin != end; ++begin)
70  {
71  here += sep.copy(here, std::size(sep));
72  here = traits::into_buf(here, stop, access(begin)) - 1;
73  }
74  result.resize(static_cast<std::size_t>(here - data));
75  return result;
76 }
77 
78 
80 template<typename ITER>
81 [[nodiscard]] inline std::string
82 separated_list(std::string_view sep, ITER begin, ITER end)
83 {
84  return separated_list(sep, begin, end, [](ITER i) { return *i; });
85 }
86 
87 
89 template<typename CONTAINER>
90 [[nodiscard]] inline auto
91 separated_list(std::string_view sep, CONTAINER const &c)
92  /*
93  Always std::string; necessary because SFINAE doesn't work with the
94  contents of function bodies, so the check for iterability has to be in
95  the signature.
96  */
97  -> typename std::enable_if<
98  (not std::is_void<decltype(std::begin(c))>::value and
99  not std::is_void<decltype(std::end(c))>::value),
100  std::string>::type
101 {
102  return separated_list(sep, std::begin(c), std::end(c));
103 }
104 
105 
107 template<
108  typename TUPLE, std::size_t INDEX = 0, typename ACCESS,
109  typename std::enable_if<
110  (INDEX == std::tuple_size<TUPLE>::value - 1), int>::type = 0>
111 [[nodiscard]] inline std::string separated_list(
112  std::string_view /* sep */, TUPLE const &t, ACCESS const &access)
113 {
114  return to_string(access(&std::get<INDEX>(t)));
115 }
116 
117 template<
118  typename TUPLE, std::size_t INDEX = 0, typename ACCESS,
119  typename std::enable_if<
120  (INDEX < std::tuple_size<TUPLE>::value - 1), int>::type = 0>
121 [[nodiscard]] inline std::string
122 separated_list(std::string_view sep, TUPLE const &t, ACCESS const &access)
123 {
124  std::string out{to_string(access(&std::get<INDEX>(t)))};
125  out.append(sep);
126  out.append(separated_list<TUPLE, INDEX + 1>(sep, t, access));
127  return out;
128 }
129 
130 template<
131  typename TUPLE, std::size_t INDEX = 0,
132  typename std::enable_if<
133  (INDEX <= std::tuple_size<TUPLE>::value), int>::type = 0>
134 [[nodiscard]] inline std::string
135 separated_list(std::string_view sep, TUPLE const &t)
136 {
137  // TODO: Optimise allocation.
138  return separated_list(sep, t, [](TUPLE const &tup) { return *tup; });
139 }
141 } // namespace pqxx
142 #endif
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
Traits class for use in string conversions.
Definition: strconv.hxx:154
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:533
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: types.hxx:91
Result set containing data returned by a query or command.
Definition: result.hxx:72
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:378
std::string separated_list(std::string_view sep, ITER begin, ITER end, ACCESS access)
Represent sequence of values as a string, joined by a given separator.
Definition: separated_list.hxx:43