str_join_internal.h 10 KB

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