libpqxx 7.8.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-2023, 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// TODO: Can we pass separators at compile time?
26namespace pqxx
27{
32
34
42template<typename ITER, typename ACCESS>
43[[nodiscard]] inline std::string
44separated_list(std::string_view sep, ITER begin, ITER end, ACCESS access)
45{
46 if (end == begin)
47 return {};
48 auto next{begin};
49 ++next;
50 if (next == end)
51 return to_string(access(begin));
52
53 // From here on, we've got at least 2 elements -- meaning that we need sep.
54 using elt_type = strip_t<decltype(access(begin))>;
55 using traits = string_traits<elt_type>;
56
57 std::size_t budget{0};
58 for (ITER cnt{begin}; cnt != end; ++cnt)
59 budget += traits::size_buffer(access(cnt));
60 budget +=
61 static_cast<std::size_t>(std::distance(begin, end)) * std::size(sep);
62
63 std::string result;
64 result.resize(budget);
65
66 char *const data{result.data()};
67 char *here{data};
68 char *stop{data + budget};
69 here = traits::into_buf(here, stop, access(begin)) - 1;
70 for (++begin; begin != end; ++begin)
71 {
72 here += sep.copy(here, std::size(sep));
73 here = traits::into_buf(here, stop, access(begin)) - 1;
74 }
75 result.resize(static_cast<std::size_t>(here - data));
76 return result;
77}
78
79
81template<typename ITER>
82[[nodiscard]] inline std::string
83separated_list(std::string_view sep, ITER begin, ITER end)
84{
85 return separated_list(sep, begin, end, [](ITER i) { return *i; });
86}
87
88
89// C++20: Use a concept.
91template<typename CONTAINER>
92[[nodiscard]] inline auto
93separated_list(std::string_view sep, CONTAINER const &c)
94 /*
95 Always std::string; necessary because SFINAE doesn't work with the
96 contents of function bodies, so the check for iterability has to be in
97 the signature.
98 */
99 -> typename std::enable_if<
100 (not std::is_void<decltype(std::begin(c))>::value and
101 not std::is_void<decltype(std::end(c))>::value),
102 std::string>::type
103{
104 return separated_list(sep, std::begin(c), std::end(c));
105}
106
107
109template<
110 typename TUPLE, std::size_t INDEX = 0, typename ACCESS,
111 typename std::enable_if<
112 (INDEX == std::tuple_size<TUPLE>::value - 1), int>::type = 0>
113[[nodiscard]] inline std::string separated_list(
114 std::string_view /* sep */, TUPLE const &t, ACCESS const &access)
115{
116 return to_string(access(&std::get<INDEX>(t)));
117}
118
119template<
120 typename TUPLE, std::size_t INDEX = 0, typename ACCESS,
121 typename std::enable_if<
122 (INDEX < std::tuple_size<TUPLE>::value - 1), int>::type = 0>
123[[nodiscard]] inline std::string
124separated_list(std::string_view sep, TUPLE const &t, ACCESS const &access)
125{
126 std::string out{to_string(access(&std::get<INDEX>(t)))};
127 out.append(sep);
128 out.append(separated_list<TUPLE, INDEX + 1>(sep, t, access));
129 return out;
130}
131
132template<
133 typename TUPLE, std::size_t INDEX = 0,
134 typename std::enable_if<
135 (INDEX <= std::tuple_size<TUPLE>::value), int>::type = 0>
136[[nodiscard]] inline std::string
137separated_list(std::string_view sep, TUPLE const &t)
138{
139 // TODO: Optimise allocation.
140 return separated_list(sep, t, [](TUPLE const &tup) { return *tup; });
141}
143} // namespace pqxx
144#endif
The home of all libpqxx classes, functions, templates, etc.
Definition array.hxx:33
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:44
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
std::string to_string(field const &value)
Convert a field to a string.
Definition result.cxx:549
Result set containing data returned by a query or command.
Definition result.hxx:73
Traits class for use in string conversions.
Definition strconv.hxx:155