2 // Copyright 2017 The Abseil Authors.
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // https://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 // This file declares INTERNAL parts of the Join API that are inlined/templated
18 // or otherwise need to be available at compile time. The main abstractions
19 // defined in this file are:
21 // - A handful of default Formatters
22 // - JoinAlgorithm() overloads
23 // - JoinRange() overloads
26 // DO NOT INCLUDE THIS FILE DIRECTLY. Use this file by including
27 // absl/strings/str_join.h
29 // IWYU pragma: private, include "absl/strings/str_join.h"
31 #ifndef ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_
32 #define ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_
38 #include <type_traits>
41 #include "absl/strings/internal/ostringstream.h"
42 #include "absl/strings/internal/resize_uninitialized.h"
43 #include "absl/strings/str_cat.h"
46 namespace strings_internal {
51 // The following are implementation classes for standard Formatter objects. The
52 // factory functions that users will call to create and use these formatters are
53 // defined and documented in strings/join.h.
56 // The default formatter. Converts alpha-numeric types to strings.
57 struct AlphaNumFormatterImpl {
58 // This template is needed in order to support passing in a dereferenced
59 // vector<bool>::iterator
61 void operator()(std::string* out, const T& t) const {
62 StrAppend(out, AlphaNum(t));
65 void operator()(std::string* out, const AlphaNum& t) const {
70 // A type that's used to overload the JoinAlgorithm() function (defined below)
71 // for ranges that do not require additional formatting (e.g., a range of
74 struct NoFormatter : public AlphaNumFormatterImpl {};
76 // Formats types to strings using the << operator.
77 class StreamFormatterImpl {
79 // The method isn't const because it mutates state. Making it const will
80 // render StreamFormatterImpl thread-hostile.
82 void operator()(std::string* out, const T& t) {
83 // The stream is created lazily to avoid paying the relatively high cost
84 // of its construction when joining an empty range.
86 strm_->clear(); // clear the bad, fail and eof bits in case they were set
89 strm_.reset(new strings_internal::OStringStream(out));
95 std::unique_ptr<strings_internal::OStringStream> strm_;
98 // Formats a std::pair<>. The 'first' member is formatted using f1_ and the
99 // 'second' member is formatted using f2_. sep_ is the separator.
100 template <typename F1, typename F2>
101 class PairFormatterImpl {
103 PairFormatterImpl(F1 f1, absl::string_view sep, F2 f2)
104 : f1_(std::move(f1)), sep_(sep), f2_(std::move(f2)) {}
106 template <typename T>
107 void operator()(std::string* out, const T& p) {
113 template <typename T>
114 void operator()(std::string* out, const T& p) const {
126 // Wraps another formatter and dereferences the argument to operator() then
127 // passes the dereferenced argument to the wrapped formatter. This can be
128 // useful, for example, to join a std::vector<int*>.
129 template <typename Formatter>
130 class DereferenceFormatterImpl {
132 DereferenceFormatterImpl() : f_() {}
133 explicit DereferenceFormatterImpl(Formatter&& f)
134 : f_(std::forward<Formatter>(f)) {}
136 template <typename T>
137 void operator()(std::string* out, const T& t) {
141 template <typename T>
142 void operator()(std::string* out, const T& t) const {
150 // DefaultFormatter<T> is a traits class that selects a default Formatter to use
151 // for the given type T. The ::Type member names the Formatter to use. This is
152 // used by the strings::Join() functions that do NOT take a Formatter argument,
153 // in which case a default Formatter must be chosen.
155 // AlphaNumFormatterImpl is the default in the base template, followed by
156 // specializations for other types.
157 template <typename ValueType>
158 struct DefaultFormatter {
159 typedef AlphaNumFormatterImpl Type;
162 struct DefaultFormatter<const char*> {
163 typedef AlphaNumFormatterImpl Type;
166 struct DefaultFormatter<char*> {
167 typedef AlphaNumFormatterImpl Type;
170 struct DefaultFormatter<std::string> {
171 typedef NoFormatter Type;
174 struct DefaultFormatter<absl::string_view> {
175 typedef NoFormatter Type;
177 template <typename ValueType>
178 struct DefaultFormatter<ValueType*> {
179 typedef DereferenceFormatterImpl<typename DefaultFormatter<ValueType>::Type>
183 template <typename ValueType>
184 struct DefaultFormatter<std::unique_ptr<ValueType>>
185 : public DefaultFormatter<ValueType*> {};
188 // JoinAlgorithm() functions
191 // The main joining algorithm. This simply joins the elements in the given
192 // iterator range, each separated by the given separator, into an output string,
193 // and formats each element using the provided Formatter object.
194 template <typename Iterator, typename Formatter>
195 std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s,
198 absl::string_view sep("");
199 for (Iterator it = start; it != end; ++it) {
200 result.append(sep.data(), sep.size());
207 // A joining algorithm that's optimized for a forward iterator range of
208 // string-like objects that do not need any additional formatting. This is to
209 // optimize the common case of joining, say, a std::vector<string> or a
210 // std::vector<absl::string_view>.
212 // This is an overload of the previous JoinAlgorithm() function. Here the
213 // Formatter argument is of type NoFormatter. Since NoFormatter is an internal
214 // type, this overload is only invoked when strings::Join() is called with a
215 // range of string-like objects (e.g., std::string, absl::string_view), and an
216 // explicit Formatter argument was NOT specified.
218 // The optimization is that the needed space will be reserved in the output
219 // string to avoid the need to resize while appending. To do this, the iterator
220 // range will be traversed twice: once to calculate the total needed size, and
221 // then again to copy the elements and delimiters to the output string.
222 template <typename Iterator,
223 typename = typename std::enable_if<std::is_convertible<
224 typename std::iterator_traits<Iterator>::iterator_category,
225 std::forward_iterator_tag>::value>::type>
226 std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s,
231 size_t result_size = start->size();
232 for (Iterator it = start; ++it != end;) {
233 result_size += s.size();
234 result_size += it->size();
237 if (result_size > 0) {
238 STLStringResizeUninitialized(&result, result_size);
241 char* result_buf = &*result.begin();
242 memcpy(result_buf, start->data(), start->size());
243 result_buf += start->size();
244 for (Iterator it = start; ++it != end;) {
245 memcpy(result_buf, s.data(), s.size());
246 result_buf += s.size();
247 memcpy(result_buf, it->data(), it->size());
248 result_buf += it->size();
256 // JoinTupleLoop implements a loop over the elements of a std::tuple, which
257 // are heterogeneous. The primary template matches the tuple interior case. It
258 // continues the iteration after appending a separator (for nonzero indices)
259 // and formatting an element of the tuple. The specialization for the I=N case
260 // matches the end-of-tuple, and terminates the iteration.
261 template <size_t I, size_t N>
262 struct JoinTupleLoop {
263 template <typename Tup, typename Formatter>
264 void operator()(std::string* out, const Tup& tup, absl::string_view sep,
266 if (I > 0) out->append(sep.data(), sep.size());
267 fmt(out, std::get<I>(tup));
268 JoinTupleLoop<I + 1, N>()(out, tup, sep, fmt);
272 struct JoinTupleLoop<N, N> {
273 template <typename Tup, typename Formatter>
274 void operator()(std::string*, const Tup&, absl::string_view, Formatter&&) {}
277 template <typename... T, typename Formatter>
278 std::string JoinAlgorithm(const std::tuple<T...>& tup, absl::string_view sep,
281 JoinTupleLoop<0, sizeof...(T)>()(&result, tup, sep, fmt);
285 template <typename Iterator>
286 std::string JoinRange(Iterator first, Iterator last,
287 absl::string_view separator) {
288 // No formatter was explicitly given, so a default must be chosen.
289 typedef typename std::iterator_traits<Iterator>::value_type ValueType;
290 typedef typename DefaultFormatter<ValueType>::Type Formatter;
291 return JoinAlgorithm(first, last, separator, Formatter());
294 template <typename Range, typename Formatter>
295 std::string JoinRange(const Range& range, absl::string_view separator,
299 return JoinAlgorithm(begin(range), end(range), separator, fmt);
302 template <typename Range>
303 std::string JoinRange(const Range& range, absl::string_view separator) {
306 return JoinRange(begin(range), end(range), separator);
309 } // namespace strings_internal
312 #endif // ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_