str_join_internal.h
Go to the documentation of this file.
00001 //
00002 // Copyright 2017 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 //
00016 
00017 // This file declares INTERNAL parts of the Join API that are inlined/templated
00018 // or otherwise need to be available at compile time. The main abstractions
00019 // defined in this file are:
00020 //
00021 //   - A handful of default Formatters
00022 //   - JoinAlgorithm() overloads
00023 //   - JoinRange() overloads
00024 //   - JoinTuple()
00025 //
00026 // DO NOT INCLUDE THIS FILE DIRECTLY. Use this file by including
00027 // absl/strings/str_join.h
00028 //
00029 // IWYU pragma: private, include "absl/strings/str_join.h"
00030 
00031 #ifndef ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_
00032 #define ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_
00033 
00034 #include <cstring>
00035 #include <iterator>
00036 #include <memory>
00037 #include <string>
00038 #include <type_traits>
00039 #include <utility>
00040 
00041 #include "absl/strings/internal/ostringstream.h"
00042 #include "absl/strings/internal/resize_uninitialized.h"
00043 #include "absl/strings/str_cat.h"
00044 
00045 namespace absl {
00046 namespace strings_internal {
00047 
00048 //
00049 // Formatter objects
00050 //
00051 // The following are implementation classes for standard Formatter objects. The
00052 // factory functions that users will call to create and use these formatters are
00053 // defined and documented in strings/join.h.
00054 //
00055 
00056 // The default formatter. Converts alpha-numeric types to strings.
00057 struct AlphaNumFormatterImpl {
00058   // This template is needed in order to support passing in a dereferenced
00059   // vector<bool>::iterator
00060   template <typename T>
00061   void operator()(std::string* out, const T& t) const {
00062     StrAppend(out, AlphaNum(t));
00063   }
00064 
00065   void operator()(std::string* out, const AlphaNum& t) const {
00066     StrAppend(out, t);
00067   }
00068 };
00069 
00070 // A type that's used to overload the JoinAlgorithm() function (defined below)
00071 // for ranges that do not require additional formatting (e.g., a range of
00072 // strings).
00073 
00074 struct NoFormatter : public AlphaNumFormatterImpl {};
00075 
00076 // Formats types to strings using the << operator.
00077 class StreamFormatterImpl {
00078  public:
00079   // The method isn't const because it mutates state. Making it const will
00080   // render StreamFormatterImpl thread-hostile.
00081   template <typename T>
00082   void operator()(std::string* out, const T& t) {
00083     // The stream is created lazily to avoid paying the relatively high cost
00084     // of its construction when joining an empty range.
00085     if (strm_) {
00086       strm_->clear();  // clear the bad, fail and eof bits in case they were set
00087       strm_->str(out);
00088     } else {
00089       strm_.reset(new strings_internal::OStringStream(out));
00090     }
00091     *strm_ << t;
00092   }
00093 
00094  private:
00095   std::unique_ptr<strings_internal::OStringStream> strm_;
00096 };
00097 
00098 // Formats a std::pair<>. The 'first' member is formatted using f1_ and the
00099 // 'second' member is formatted using f2_. sep_ is the separator.
00100 template <typename F1, typename F2>
00101 class PairFormatterImpl {
00102  public:
00103   PairFormatterImpl(F1 f1, absl::string_view sep, F2 f2)
00104       : f1_(std::move(f1)), sep_(sep), f2_(std::move(f2)) {}
00105 
00106   template <typename T>
00107   void operator()(std::string* out, const T& p) {
00108     f1_(out, p.first);
00109     out->append(sep_);
00110     f2_(out, p.second);
00111   }
00112 
00113   template <typename T>
00114   void operator()(std::string* out, const T& p) const {
00115     f1_(out, p.first);
00116     out->append(sep_);
00117     f2_(out, p.second);
00118   }
00119 
00120  private:
00121   F1 f1_;
00122   std::string sep_;
00123   F2 f2_;
00124 };
00125 
00126 // Wraps another formatter and dereferences the argument to operator() then
00127 // passes the dereferenced argument to the wrapped formatter. This can be
00128 // useful, for example, to join a std::vector<int*>.
00129 template <typename Formatter>
00130 class DereferenceFormatterImpl {
00131  public:
00132   DereferenceFormatterImpl() : f_() {}
00133   explicit DereferenceFormatterImpl(Formatter&& f)
00134       : f_(std::forward<Formatter>(f)) {}
00135 
00136   template <typename T>
00137   void operator()(std::string* out, const T& t) {
00138     f_(out, *t);
00139   }
00140 
00141   template <typename T>
00142   void operator()(std::string* out, const T& t) const {
00143     f_(out, *t);
00144   }
00145 
00146  private:
00147   Formatter f_;
00148 };
00149 
00150 // DefaultFormatter<T> is a traits class that selects a default Formatter to use
00151 // for the given type T. The ::Type member names the Formatter to use. This is
00152 // used by the strings::Join() functions that do NOT take a Formatter argument,
00153 // in which case a default Formatter must be chosen.
00154 //
00155 // AlphaNumFormatterImpl is the default in the base template, followed by
00156 // specializations for other types.
00157 template <typename ValueType>
00158 struct DefaultFormatter {
00159   typedef AlphaNumFormatterImpl Type;
00160 };
00161 template <>
00162 struct DefaultFormatter<const char*> {
00163   typedef AlphaNumFormatterImpl Type;
00164 };
00165 template <>
00166 struct DefaultFormatter<char*> {
00167   typedef AlphaNumFormatterImpl Type;
00168 };
00169 template <>
00170 struct DefaultFormatter<std::string> {
00171   typedef NoFormatter Type;
00172 };
00173 template <>
00174 struct DefaultFormatter<absl::string_view> {
00175   typedef NoFormatter Type;
00176 };
00177 template <typename ValueType>
00178 struct DefaultFormatter<ValueType*> {
00179   typedef DereferenceFormatterImpl<typename DefaultFormatter<ValueType>::Type>
00180       Type;
00181 };
00182 
00183 template <typename ValueType>
00184 struct DefaultFormatter<std::unique_ptr<ValueType>>
00185     : public DefaultFormatter<ValueType*> {};
00186 
00187 //
00188 // JoinAlgorithm() functions
00189 //
00190 
00191 // The main joining algorithm. This simply joins the elements in the given
00192 // iterator range, each separated by the given separator, into an output string,
00193 // and formats each element using the provided Formatter object.
00194 template <typename Iterator, typename Formatter>
00195 std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s,
00196                           Formatter&& f) {
00197   std::string result;
00198   absl::string_view sep("");
00199   for (Iterator it = start; it != end; ++it) {
00200     result.append(sep.data(), sep.size());
00201     f(&result, *it);
00202     sep = s;
00203   }
00204   return result;
00205 }
00206 
00207 // A joining algorithm that's optimized for a forward iterator range of
00208 // string-like objects that do not need any additional formatting. This is to
00209 // optimize the common case of joining, say, a std::vector<string> or a
00210 // std::vector<absl::string_view>.
00211 //
00212 // This is an overload of the previous JoinAlgorithm() function. Here the
00213 // Formatter argument is of type NoFormatter. Since NoFormatter is an internal
00214 // type, this overload is only invoked when strings::Join() is called with a
00215 // range of string-like objects (e.g., std::string, absl::string_view), and an
00216 // explicit Formatter argument was NOT specified.
00217 //
00218 // The optimization is that the needed space will be reserved in the output
00219 // string to avoid the need to resize while appending. To do this, the iterator
00220 // range will be traversed twice: once to calculate the total needed size, and
00221 // then again to copy the elements and delimiters to the output string.
00222 template <typename Iterator,
00223           typename = typename std::enable_if<std::is_convertible<
00224               typename std::iterator_traits<Iterator>::iterator_category,
00225               std::forward_iterator_tag>::value>::type>
00226 std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s,
00227                           NoFormatter) {
00228   std::string result;
00229   if (start != end) {
00230     // Sums size
00231     size_t result_size = start->size();
00232     for (Iterator it = start; ++it != end;) {
00233       result_size += s.size();
00234       result_size += it->size();
00235     }
00236 
00237     if (result_size > 0) {
00238       STLStringResizeUninitialized(&result, result_size);
00239 
00240       // Joins strings
00241       char* result_buf = &*result.begin();
00242       memcpy(result_buf, start->data(), start->size());
00243       result_buf += start->size();
00244       for (Iterator it = start; ++it != end;) {
00245         memcpy(result_buf, s.data(), s.size());
00246         result_buf += s.size();
00247         memcpy(result_buf, it->data(), it->size());
00248         result_buf += it->size();
00249       }
00250     }
00251   }
00252 
00253   return result;
00254 }
00255 
00256 // JoinTupleLoop implements a loop over the elements of a std::tuple, which
00257 // are heterogeneous. The primary template matches the tuple interior case. It
00258 // continues the iteration after appending a separator (for nonzero indices)
00259 // and formatting an element of the tuple. The specialization for the I=N case
00260 // matches the end-of-tuple, and terminates the iteration.
00261 template <size_t I, size_t N>
00262 struct JoinTupleLoop {
00263   template <typename Tup, typename Formatter>
00264   void operator()(std::string* out, const Tup& tup, absl::string_view sep,
00265                   Formatter&& fmt) {
00266     if (I > 0) out->append(sep.data(), sep.size());
00267     fmt(out, std::get<I>(tup));
00268     JoinTupleLoop<I + 1, N>()(out, tup, sep, fmt);
00269   }
00270 };
00271 template <size_t N>
00272 struct JoinTupleLoop<N, N> {
00273   template <typename Tup, typename Formatter>
00274   void operator()(std::string*, const Tup&, absl::string_view, Formatter&&) {}
00275 };
00276 
00277 template <typename... T, typename Formatter>
00278 std::string JoinAlgorithm(const std::tuple<T...>& tup, absl::string_view sep,
00279                           Formatter&& fmt) {
00280   std::string result;
00281   JoinTupleLoop<0, sizeof...(T)>()(&result, tup, sep, fmt);
00282   return result;
00283 }
00284 
00285 template <typename Iterator>
00286 std::string JoinRange(Iterator first, Iterator last,
00287                       absl::string_view separator) {
00288   // No formatter was explicitly given, so a default must be chosen.
00289   typedef typename std::iterator_traits<Iterator>::value_type ValueType;
00290   typedef typename DefaultFormatter<ValueType>::Type Formatter;
00291   return JoinAlgorithm(first, last, separator, Formatter());
00292 }
00293 
00294 template <typename Range, typename Formatter>
00295 std::string JoinRange(const Range& range, absl::string_view separator,
00296                       Formatter&& fmt) {
00297   using std::begin;
00298   using std::end;
00299   return JoinAlgorithm(begin(range), end(range), separator, fmt);
00300 }
00301 
00302 template <typename Range>
00303 std::string JoinRange(const Range& range, absl::string_view separator) {
00304   using std::begin;
00305   using std::end;
00306   return JoinRange(begin(range), end(range), separator);
00307 }
00308 
00309 }  // namespace strings_internal
00310 }  // namespace absl
00311 
00312 #endif  // ABSL_STRINGS_INTERNAL_STR_JOIN_INTERNAL_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15