libpqxx  7.1.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-2020, 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 namespace pqxx
23 {
28 
30 
38 template<typename ITER, typename ACCESS>
39 [[nodiscard]] inline std::string
40 separated_list(std::string_view sep, ITER begin, ITER end, ACCESS access)
41 {
42  if (end == begin)
43  return std::string{};
44  auto next{begin};
45  ++next;
46  if (next == end)
47  return to_string(access(begin));
48 
49  // From here on, we've got at least 2 elements -- meaning that we need sep.
50  using elt_type = strip_t<decltype(access(begin))>;
51  using traits = string_traits<elt_type>;
52 
53  std::size_t budget{0};
54  for (ITER cnt{begin}; cnt != end; ++cnt)
55  budget += traits::size_buffer(access(cnt));
56  budget += static_cast<std::size_t>(std::distance(begin, end)) * sep.size();
57 
58  std::string result;
59  result.resize(budget);
60 
61  char *here{result.data()};
62  char *stop{here + budget};
63  here = traits::into_buf(here, stop, access(begin)) - 1;
64  for (++begin; begin != end; ++begin)
65  {
66  here += sep.copy(here, sep.size());
67  here = traits::into_buf(here, stop, access(begin)) - 1;
68  }
69  result.resize(static_cast<std::size_t>(here - result.data()));
70  return result;
71 }
72 
73 
75 template<typename ITER>
76 [[nodiscard]] inline std::string
77 separated_list(std::string_view sep, ITER begin, ITER end)
78 {
79  return separated_list(sep, begin, end, [](ITER i) { return *i; });
80 }
81 
82 
84 template<typename CONTAINER>
85 [[nodiscard]] inline auto
86 separated_list(std::string_view sep, CONTAINER const &c)
87  /*
88  Always std::string; necessary because SFINAE doesn't work with the
89  contents of function bodies, so the check for iterability has to be in
90  the signature.
91  */
92  -> typename std::enable_if<
93  (not std::is_void<decltype(std::begin(c))>::value and
94  not std::is_void<decltype(std::end(c))>::value),
95  std::string>::type
96 {
97  return separated_list(sep, std::begin(c), std::end(c));
98 }
99 
100 
102 template<
103  typename TUPLE, std::size_t INDEX = 0, typename ACCESS,
104  typename std::enable_if<
105  (INDEX == std::tuple_size<TUPLE>::value - 1), int>::type = 0>
106 [[nodiscard]] inline std::string separated_list(
107  std::string_view /* sep */, TUPLE const &t, ACCESS const &access)
108 {
109  return to_string(access(&std::get<INDEX>(t)));
110 }
111 
112 template<
113  typename TUPLE, std::size_t INDEX = 0, typename ACCESS,
114  typename std::enable_if<
115  (INDEX < std::tuple_size<TUPLE>::value - 1), int>::type = 0>
116 [[nodiscard]] inline std::string
117 separated_list(std::string_view sep, TUPLE const &t, ACCESS const &access)
118 {
119  std::string out{to_string(access(&std::get<INDEX>(t)))};
120  out.append(sep);
121  out.append(separated_list<TUPLE, INDEX + 1>(sep, t, access));
122  return out;
123 }
124 
125 template<
126  typename TUPLE, std::size_t INDEX = 0,
127  typename std::enable_if<
128  (INDEX <= std::tuple_size<TUPLE>::value), int>::type = 0>
129 [[nodiscard]] inline std::string
130 separated_list(std::string_view sep, TUPLE const &t)
131 {
132  return separated_list(sep, t, [](TUPLE const &tup) { return *tup; });
133 }
135 } // namespace pqxx
136 
137 #include "pqxx/internal/compiler-internal-post.hxx"
138 #endif
Traits class for use in string conversions.
Definition: strconv.hxx:123
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:498
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: util.hxx:119
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
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:40
Result set containing data returned by a query or command.
Definition: result.hxx:70