libpqxx  7.5.2
separated_list.hxx
1 /* Helper similar to Python's @c str.join().
2  *
3  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/separated_list instead.
4  *
5  * Copyright (c) 2000-2021, 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 #include "pqxx/compiler-public.hxx"
15 #include "pqxx/internal/compiler-internal-pre.hxx"
16 
17 #include <algorithm>
18 #include <numeric>
19 
20 #include "pqxx/strconv.hxx"
21 
22 // TODO: Simplify using std::ranges::range, once we're on C++20.
23 // TODO: Optimise buffer allocation using C++20 random_access_range.
24 namespace pqxx
25 {
30 
32 
40 template<typename ITER, typename ACCESS>
41 [[nodiscard]] inline std::string
42 separated_list(std::string_view sep, ITER begin, ITER end, ACCESS access)
43 {
44  if (end == begin)
45  return std::string{};
46  auto next{begin};
47  ++next;
48  if (next == end)
49  return to_string(access(begin));
50 
51  // From here on, we've got at least 2 elements -- meaning that we need sep.
52  using elt_type = strip_t<decltype(access(begin))>;
53  using traits = string_traits<elt_type>;
54 
55  std::size_t budget{0};
56  for (ITER cnt{begin}; cnt != end; ++cnt)
57  budget += traits::size_buffer(access(cnt));
58  budget +=
59  static_cast<std::size_t>(std::distance(begin, end)) * std::size(sep);
60 
61  std::string result;
62  result.resize(budget);
63 
64  char *here{result.data()};
65  char *stop{here + budget};
66  here = traits::into_buf(here, stop, access(begin)) - 1;
67  for (++begin; begin != end; ++begin)
68  {
69  here += sep.copy(here, std::size(sep));
70  here = traits::into_buf(here, stop, access(begin)) - 1;
71  }
72  result.resize(static_cast<std::size_t>(here - result.data()));
73  return result;
74 }
75 
76 
78 template<typename ITER>
79 [[nodiscard]] inline std::string
80 separated_list(std::string_view sep, ITER begin, ITER end)
81 {
82  return separated_list(sep, begin, end, [](ITER i) { return *i; });
83 }
84 
85 
87 template<typename CONTAINER>
88 [[nodiscard]] inline auto
89 separated_list(std::string_view sep, CONTAINER const &c)
90  /*
91  Always std::string; necessary because SFINAE doesn't work with the
92  contents of function bodies, so the check for iterability has to be in
93  the signature.
94  */
95  -> typename std::enable_if<
96  (not std::is_void<decltype(std::begin(c))>::value and
97  not std::is_void<decltype(std::end(c))>::value),
98  std::string>::type
99 {
100  return separated_list(sep, std::begin(c), std::end(c));
101 }
102 
103 
105 template<
106  typename TUPLE, std::size_t INDEX = 0, typename ACCESS,
107  typename std::enable_if<
108  (INDEX == std::tuple_size<TUPLE>::value - 1), int>::type = 0>
109 [[nodiscard]] inline std::string separated_list(
110  std::string_view /* sep */, TUPLE const &t, ACCESS const &access)
111 {
112  return to_string(access(&std::get<INDEX>(t)));
113 }
114 
115 template<
116  typename TUPLE, std::size_t INDEX = 0, typename ACCESS,
117  typename std::enable_if<
118  (INDEX < std::tuple_size<TUPLE>::value - 1), int>::type = 0>
119 [[nodiscard]] inline std::string
120 separated_list(std::string_view sep, TUPLE const &t, ACCESS const &access)
121 {
122  std::string out{to_string(access(&std::get<INDEX>(t)))};
123  out.append(sep);
124  out.append(separated_list<TUPLE, INDEX + 1>(sep, t, access));
125  return out;
126 }
127 
128 template<
129  typename TUPLE, std::size_t INDEX = 0,
130  typename std::enable_if<
131  (INDEX <= std::tuple_size<TUPLE>::value), int>::type = 0>
132 [[nodiscard]] inline std::string
133 separated_list(std::string_view sep, TUPLE const &t)
134 {
135  // TODO: Optimise allocation.
136  return separated_list(sep, t, [](TUPLE const &tup) { return *tup; });
137 }
139 } // namespace pqxx
140 
141 #include "pqxx/internal/compiler-internal-post.hxx"
142 #endif
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
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:42
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: util.hxx:147
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:503
Result set containing data returned by a query or command.
Definition: result.hxx:71
Traits class for use in string conversions.
Definition: strconv.hxx:149