Coverage Report

Created: 2025-10-28 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/strings/internal/str_join_internal.h
Line
Count
Source
1
//
2
// Copyright 2017 The Abseil Authors.
3
//
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
7
//
8
//      https://www.apache.org/licenses/LICENSE-2.0
9
//
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.
15
//
16
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:
20
//
21
//   - A handful of default Formatters
22
//   - JoinAlgorithm() overloads
23
//   - JoinRange() overloads
24
//   - JoinTuple()
25
//
26
// DO NOT INCLUDE THIS FILE DIRECTLY. Use this file by including
27
// absl/strings/str_join.h
28
//
29
// IWYU pragma: private, include "absl/strings/str_join.h"
30
31
#ifndef ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_
32
#define ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_
33
34
#include <cstdint>
35
#include <cstring>
36
#include <initializer_list>
37
#include <iterator>
38
#include <limits>
39
#include <memory>
40
#include <string>
41
#include <tuple>
42
#include <type_traits>
43
#include <utility>
44
45
#include "absl/base/config.h"
46
#include "absl/base/internal/iterator_traits.h"
47
#include "absl/base/internal/raw_logging.h"
48
#include "absl/strings/internal/ostringstream.h"
49
#include "absl/strings/internal/resize_uninitialized.h"
50
#include "absl/strings/resize_and_overwrite.h"
51
#include "absl/strings/str_cat.h"
52
#include "absl/strings/string_view.h"
53
54
namespace absl {
55
ABSL_NAMESPACE_BEGIN
56
namespace strings_internal {
57
58
//
59
// Formatter objects
60
//
61
// The following are implementation classes for standard Formatter objects. The
62
// factory functions that users will call to create and use these formatters are
63
// defined and documented in strings/join.h.
64
//
65
66
// The default formatter. Converts alpha-numeric types to strings.
67
struct AlphaNumFormatterImpl {
68
  // This template is needed in order to support passing in a dereferenced
69
  // vector<bool>::iterator
70
  template <typename T>
71
  void operator()(std::string* out, const T& t) const {
72
    StrAppend(out, AlphaNum(t));
73
  }
74
75
0
  void operator()(std::string* out, const AlphaNum& t) const {
76
0
    StrAppend(out, t);
77
0
  }
78
};
79
80
// A type that's used to overload the JoinAlgorithm() function (defined below)
81
// for ranges that do not require additional formatting (e.g., a range of
82
// strings).
83
84
struct NoFormatter : public AlphaNumFormatterImpl {};
85
86
// Formats types to strings using the << operator.
87
class StreamFormatterImpl {
88
 public:
89
  // The method isn't const because it mutates state. Making it const will
90
  // render StreamFormatterImpl thread-hostile.
91
  template <typename T>
92
  void operator()(std::string* out, const T& t) {
93
    // The stream is created lazily to avoid paying the relatively high cost
94
    // of its construction when joining an empty range.
95
    if (strm_) {
96
      strm_->clear();  // clear the bad, fail and eof bits in case they were set
97
      strm_->str(out);
98
    } else {
99
      strm_.reset(new strings_internal::OStringStream(out));
100
    }
101
    *strm_ << t;
102
  }
103
104
 private:
105
  std::unique_ptr<strings_internal::OStringStream> strm_;
106
};
107
108
// Formats a std::pair<>. The 'first' member is formatted using f1_ and the
109
// 'second' member is formatted using f2_. sep_ is the separator.
110
template <typename F1, typename F2>
111
class PairFormatterImpl {
112
 public:
113
  PairFormatterImpl(F1 f1, absl::string_view sep, F2 f2)
114
      : f1_(std::move(f1)), sep_(sep), f2_(std::move(f2)) {}
115
116
  template <typename T>
117
  void operator()(std::string* out, const T& p) {
118
    f1_(out, p.first);
119
    out->append(sep_);
120
    f2_(out, p.second);
121
  }
122
123
  template <typename T>
124
  void operator()(std::string* out, const T& p) const {
125
    f1_(out, p.first);
126
    out->append(sep_);
127
    f2_(out, p.second);
128
  }
129
130
 private:
131
  F1 f1_;
132
  std::string sep_;
133
  F2 f2_;
134
};
135
136
// Wraps another formatter and dereferences the argument to operator() then
137
// passes the dereferenced argument to the wrapped formatter. This can be
138
// useful, for example, to join a std::vector<int*>.
139
template <typename Formatter>
140
class DereferenceFormatterImpl {
141
 public:
142
  DereferenceFormatterImpl() : f_() {}
143
  explicit DereferenceFormatterImpl(Formatter&& f)
144
      : f_(std::forward<Formatter>(f)) {}
145
146
  template <typename T>
147
  void operator()(std::string* out, const T& t) {
148
    f_(out, *t);
149
  }
150
151
  template <typename T>
152
  void operator()(std::string* out, const T& t) const {
153
    f_(out, *t);
154
  }
155
156
 private:
157
  Formatter f_;
158
};
159
160
// DefaultFormatter<T> is a traits class that selects a default Formatter to use
161
// for the given type T. The ::Type member names the Formatter to use. This is
162
// used by the strings::Join() functions that do NOT take a Formatter argument,
163
// in which case a default Formatter must be chosen.
164
//
165
// AlphaNumFormatterImpl is the default in the base template, followed by
166
// specializations for other types.
167
template <typename ValueType>
168
struct DefaultFormatter {
169
  typedef AlphaNumFormatterImpl Type;
170
};
171
template <>
172
struct DefaultFormatter<const char*> {
173
  typedef AlphaNumFormatterImpl Type;
174
};
175
template <>
176
struct DefaultFormatter<char*> {
177
  typedef AlphaNumFormatterImpl Type;
178
};
179
template <>
180
struct DefaultFormatter<std::string> {
181
  typedef NoFormatter Type;
182
};
183
template <>
184
struct DefaultFormatter<absl::string_view> {
185
  typedef NoFormatter Type;
186
};
187
template <typename ValueType>
188
struct DefaultFormatter<ValueType*> {
189
  typedef DereferenceFormatterImpl<typename DefaultFormatter<ValueType>::Type>
190
      Type;
191
};
192
193
template <typename ValueType>
194
struct DefaultFormatter<std::unique_ptr<ValueType>>
195
    : public DefaultFormatter<ValueType*> {};
196
197
//
198
// JoinAlgorithm() functions
199
//
200
201
// The main joining algorithm. This simply joins the elements in the given
202
// iterator range, each separated by the given separator, into an output string,
203
// and formats each element using the provided Formatter object.
204
template <typename Iterator, typename Formatter>
205
std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s,
206
                          Formatter&& f) {
207
  std::string result;
208
  absl::string_view sep("");
209
  for (Iterator it = start; it != end; ++it) {
210
    result.append(sep.data(), sep.size());
211
    f(&result, *it);
212
    sep = s;
213
  }
214
  return result;
215
}
216
217
// A joining algorithm that's optimized for a forward iterator range of
218
// string-like objects that do not need any additional formatting. This is to
219
// optimize the common case of joining, say, a std::vector<string> or a
220
// std::vector<absl::string_view>.
221
//
222
// This is an overload of the previous JoinAlgorithm() function. Here the
223
// Formatter argument is of type NoFormatter. Since NoFormatter is an internal
224
// type, this overload is only invoked when strings::Join() is called with a
225
// range of string-like objects (e.g., std::string, absl::string_view), and an
226
// explicit Formatter argument was NOT specified.
227
//
228
// The optimization is that the needed space will be reserved in the output
229
// string to avoid the need to resize while appending. To do this, the iterator
230
// range will be traversed twice: once to calculate the total needed size, and
231
// then again to copy the elements and delimiters to the output string.
232
template <typename Iterator,
233
          typename = std::enable_if_t<
234
              base_internal::IsAtLeastForwardIterator<Iterator>::value>>
235
std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s,
236
0
                          NoFormatter) {
237
0
  std::string result;
238
0
  if (start != end) {
239
    // Sums size
240
0
    auto&& start_value = *start;
241
    // Use uint64_t to prevent size_t overflow. We assume it is not possible for
242
    // in memory strings to overflow a uint64_t.
243
0
    uint64_t result_size = start_value.size();
244
0
    for (Iterator it = start; ++it != end;) {
245
0
      result_size += s.size();
246
0
      result_size += (*it).size();
247
0
    }
248
249
0
    if (result_size > 0) {
250
0
      constexpr uint64_t kMaxSize =
251
0
          uint64_t{(std::numeric_limits<size_t>::max)()};
252
0
      ABSL_INTERNAL_CHECK(result_size <= kMaxSize, "size_t overflow");
253
254
0
      StringResizeAndOverwrite(
255
0
          result, static_cast<size_t>(result_size),
256
0
          [&start, &end, &start_value, s](char* result_buf,
257
0
                                          size_t result_buf_size) {
258
            // Joins strings
259
0
            memcpy(result_buf, start_value.data(), start_value.size());
260
0
            result_buf += start_value.size();
261
0
            for (Iterator it = start; ++it != end;) {
262
0
              memcpy(result_buf, s.data(), s.size());
263
0
              result_buf += s.size();
264
0
              auto&& value = *it;
265
0
              memcpy(result_buf, value.data(), value.size());
266
0
              result_buf += value.size();
267
0
            }
268
0
            return result_buf_size;
269
0
          });
270
0
    }
271
0
  }
272
0
  return result;
273
0
}
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > absl::strings_internal::JoinAlgorithm<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, void>(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::strings_internal::NoFormatter)
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > absl::strings_internal::JoinAlgorithm<std::__1::basic_string_view<char, std::__1::char_traits<char> > const*, void>(std::__1::basic_string_view<char, std::__1::char_traits<char> > const*, std::__1::basic_string_view<char, std::__1::char_traits<char> > const*, std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::strings_internal::NoFormatter)
274
275
// JoinTupleLoop implements a loop over the elements of a std::tuple, which
276
// are heterogeneous. The primary template matches the tuple interior case. It
277
// continues the iteration after appending a separator (for nonzero indices)
278
// and formatting an element of the tuple. The specialization for the I=N case
279
// matches the end-of-tuple, and terminates the iteration.
280
template <size_t I, size_t N>
281
struct JoinTupleLoop {
282
  template <typename Tup, typename Formatter>
283
  void operator()(std::string* out, const Tup& tup, absl::string_view sep,
284
                  Formatter&& fmt) {
285
    if (I > 0) out->append(sep.data(), sep.size());
286
    fmt(out, std::get<I>(tup));
287
    JoinTupleLoop<I + 1, N>()(out, tup, sep, fmt);
288
  }
289
};
290
template <size_t N>
291
struct JoinTupleLoop<N, N> {
292
  template <typename Tup, typename Formatter>
293
  void operator()(std::string*, const Tup&, absl::string_view, Formatter&&) {}
294
};
295
296
template <typename... T, typename Formatter>
297
std::string JoinAlgorithm(const std::tuple<T...>& tup, absl::string_view sep,
298
                          Formatter&& fmt) {
299
  std::string result;
300
  JoinTupleLoop<0, sizeof...(T)>()(&result, tup, sep, fmt);
301
  return result;
302
}
303
304
template <typename Iterator>
305
std::string JoinRange(Iterator first, Iterator last,
306
0
                      absl::string_view separator) {
307
  // No formatter was explicitly given, so a default must be chosen.
308
0
  typedef typename std::iterator_traits<Iterator>::value_type ValueType;
309
0
  typedef typename DefaultFormatter<ValueType>::Type Formatter;
310
0
  return JoinAlgorithm(first, last, separator, Formatter());
311
0
}
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > absl::strings_internal::JoinRange<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> >(std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, std::__1::basic_string_view<char, std::__1::char_traits<char> >)
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > absl::strings_internal::JoinRange<std::__1::basic_string_view<char, std::__1::char_traits<char> > const*>(std::__1::basic_string_view<char, std::__1::char_traits<char> > const*, std::__1::basic_string_view<char, std::__1::char_traits<char> > const*, std::__1::basic_string_view<char, std::__1::char_traits<char> >)
312
313
template <typename Range, typename Formatter>
314
std::string JoinRange(const Range& range, absl::string_view separator,
315
                      Formatter&& fmt) {
316
  using std::begin;
317
  using std::end;
318
  return JoinAlgorithm(begin(range), end(range), separator, fmt);
319
}
320
321
template <typename Range>
322
0
std::string JoinRange(const Range& range, absl::string_view separator) {
323
0
  using std::begin;
324
0
  using std::end;
325
0
  return JoinRange(begin(range), end(range), separator);
326
0
}
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > absl::strings_internal::JoinRange<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >)
Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > absl::strings_internal::JoinRange<std::initializer_list<std::__1::basic_string_view<char, std::__1::char_traits<char> > > >(std::initializer_list<std::__1::basic_string_view<char, std::__1::char_traits<char> > > const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >)
327
328
template <typename Tuple, std::size_t... I>
329
std::string JoinTuple(const Tuple& value, absl::string_view separator,
330
                      std::index_sequence<I...>) {
331
  return JoinRange(
332
      std::initializer_list<absl::string_view>{
333
          static_cast<const AlphaNum&>(std::get<I>(value)).Piece()...},
334
      separator);
335
}
336
337
}  // namespace strings_internal
338
ABSL_NAMESPACE_END
339
}  // namespace absl
340
341
#endif  // ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_