substitute.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 // File: substitute.h
00018 // -----------------------------------------------------------------------------
00019 //
00020 // This package contains functions for efficiently performing string
00021 // substitutions using a format string with positional notation:
00022 // `Substitute()` and `SubstituteAndAppend()`.
00023 //
00024 // Unlike printf-style format specifiers, `Substitute()` functions do not need
00025 // to specify the type of the substitution arguments. Supported arguments
00026 // following the format string, such as strings, string_views, ints,
00027 // floats, and bools, are automatically converted to strings during the
00028 // substitution process. (See below for a full list of supported types.)
00029 //
00030 // `Substitute()` does not allow you to specify *how* to format a value, beyond
00031 // the default conversion to string. For example, you cannot format an integer
00032 // in hex.
00033 //
00034 // The format string uses positional identifiers indicated by a dollar sign ($)
00035 // and single digit positional ids to indicate which substitution arguments to
00036 // use at that location within the format string.
00037 //
00038 // Example 1:
00039 //   std::string s = Substitute("$1 purchased $0 $2. Thanks $1!",
00040 //                              5, "Bob", "Apples");
00041 //   EXPECT_EQ("Bob purchased 5 Apples. Thanks Bob!", s);
00042 //
00043 // Example 2:
00044 //   std::string s = "Hi. ";
00045 //   SubstituteAndAppend(&s, "My name is $0 and I am $1 years old.", "Bob", 5);
00046 //   EXPECT_EQ("Hi. My name is Bob and I am 5 years old.", s);
00047 //
00048 // Supported types:
00049 //   * absl::string_view, std::string, const char* (null is equivalent to "")
00050 //   * int32_t, int64_t, uint32_t, uint64
00051 //   * float, double
00052 //   * bool (Printed as "true" or "false")
00053 //   * pointer types other than char* (Printed as "0x<lower case hex string>",
00054 //     except that null is printed as "NULL")
00055 //
00056 // If an invalid format string is provided, Substitute returns an empty string
00057 // and SubstituteAndAppend does not change the provided output string.
00058 // A format string is invalid if it:
00059 //   * ends in an unescaped $ character,
00060 //     e.g. "Hello $", or
00061 //   * calls for a position argument which is not provided,
00062 //     e.g. Substitute("Hello $2", "world"), or
00063 //   * specifies a non-digit, non-$ character after an unescaped $ character,
00064 //     e.g. "Hello $f".
00065 // In debug mode, i.e. #ifndef NDEBUG, such errors terminate the program.
00066 
00067 #ifndef ABSL_STRINGS_SUBSTITUTE_H_
00068 #define ABSL_STRINGS_SUBSTITUTE_H_
00069 
00070 #include <cstring>
00071 #include <string>
00072 #include <type_traits>
00073 #include <vector>
00074 
00075 #include "absl/base/macros.h"
00076 #include "absl/base/port.h"
00077 #include "absl/strings/ascii.h"
00078 #include "absl/strings/escaping.h"
00079 #include "absl/strings/numbers.h"
00080 #include "absl/strings/str_cat.h"
00081 #include "absl/strings/str_split.h"
00082 #include "absl/strings/string_view.h"
00083 #include "absl/strings/strip.h"
00084 
00085 namespace absl {
00086 namespace substitute_internal {
00087 
00088 // Arg
00089 //
00090 // This class provides an argument type for `absl::Substitute()` and
00091 // `absl::SubstituteAndAppend()`. `Arg` handles implicit conversion of various
00092 // types to a string. (`Arg` is very similar to the `AlphaNum` class in
00093 // `StrCat()`.)
00094 //
00095 // This class has implicit constructors.
00096 class Arg {
00097  public:
00098   // Overloads for std::string-y things
00099   //
00100   // Explicitly overload `const char*` so the compiler doesn't cast to `bool`.
00101   Arg(const char* value)  // NOLINT(runtime/explicit)
00102       : piece_(absl::NullSafeStringView(value)) {}
00103   template <typename Allocator>
00104   Arg(  // NOLINT
00105       const std::basic_string<char, std::char_traits<char>, Allocator>&
00106           value) noexcept
00107       : piece_(value) {}
00108   Arg(absl::string_view value)  // NOLINT(runtime/explicit)
00109       : piece_(value) {}
00110 
00111   // Overloads for primitives
00112   //
00113   // No overloads are available for signed and unsigned char because if people
00114   // are explicitly declaring their chars as signed or unsigned then they are
00115   // probably using them as 8-bit integers and would probably prefer an integer
00116   // representation. However, we can't really know, so we make the caller decide
00117   // what to do.
00118   Arg(char value)  // NOLINT(runtime/explicit)
00119       : piece_(scratch_, 1) { scratch_[0] = value; }
00120   Arg(short value)  // NOLINT(*)
00121       : piece_(scratch_,
00122                numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
00123   Arg(unsigned short value)  // NOLINT(*)
00124       : piece_(scratch_,
00125                numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
00126   Arg(int value)  // NOLINT(runtime/explicit)
00127       : piece_(scratch_,
00128                numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
00129   Arg(unsigned int value)  // NOLINT(runtime/explicit)
00130       : piece_(scratch_,
00131                numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
00132   Arg(long value)  // NOLINT(*)
00133       : piece_(scratch_,
00134                numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
00135   Arg(unsigned long value)  // NOLINT(*)
00136       : piece_(scratch_,
00137                numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
00138   Arg(long long value)  // NOLINT(*)
00139       : piece_(scratch_,
00140                numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
00141   Arg(unsigned long long value)  // NOLINT(*)
00142       : piece_(scratch_,
00143                numbers_internal::FastIntToBuffer(value, scratch_) - scratch_) {}
00144   Arg(float value)  // NOLINT(runtime/explicit)
00145       : piece_(scratch_, numbers_internal::SixDigitsToBuffer(value, scratch_)) {
00146   }
00147   Arg(double value)  // NOLINT(runtime/explicit)
00148       : piece_(scratch_, numbers_internal::SixDigitsToBuffer(value, scratch_)) {
00149   }
00150   Arg(bool value)  // NOLINT(runtime/explicit)
00151       : piece_(value ? "true" : "false") {}
00152 
00153   Arg(Hex hex);  // NOLINT(runtime/explicit)
00154   Arg(Dec dec);  // NOLINT(runtime/explicit)
00155 
00156   // vector<bool>::reference and const_reference require special help to
00157   // convert to `AlphaNum` because it requires two user defined conversions.
00158   template <typename T,
00159             absl::enable_if_t<
00160                 std::is_class<T>::value &&
00161                 (std::is_same<T, std::vector<bool>::reference>::value ||
00162                  std::is_same<T, std::vector<bool>::const_reference>::value)>* =
00163                 nullptr>
00164   Arg(T value)  // NOLINT(google-explicit-constructor)
00165       : Arg(static_cast<bool>(value)) {}
00166 
00167   // `void*` values, with the exception of `char*`, are printed as
00168   // "0x<hex value>". However, in the case of `nullptr`, "NULL" is printed.
00169   Arg(const void* value);  // NOLINT(runtime/explicit)
00170 
00171   Arg(const Arg&) = delete;
00172   Arg& operator=(const Arg&) = delete;
00173 
00174   absl::string_view piece() const { return piece_; }
00175 
00176  private:
00177   absl::string_view piece_;
00178   char scratch_[numbers_internal::kFastToBufferSize];
00179 };
00180 
00181 // Internal helper function. Don't call this from outside this implementation.
00182 // This interface may change without notice.
00183 void SubstituteAndAppendArray(std::string* output, absl::string_view format,
00184                               const absl::string_view* args_array,
00185                               size_t num_args);
00186 
00187 #if defined(ABSL_BAD_CALL_IF)
00188 constexpr int CalculateOneBit(const char* format) {
00189   return (*format < '0' || *format > '9') ? 0 : (1 << (*format - '0'));
00190 }
00191 
00192 constexpr const char* SkipNumber(const char* format) {
00193   return !*format ? format : (format + 1);
00194 }
00195 
00196 constexpr int PlaceholderBitmask(const char* format) {
00197   return !*format ? 0 : *format != '$'
00198                              ? PlaceholderBitmask(format + 1)
00199                              : (CalculateOneBit(format + 1) |
00200                                    PlaceholderBitmask(SkipNumber(format + 1)));
00201 }
00202 #endif  // ABSL_BAD_CALL_IF
00203 
00204 }  // namespace substitute_internal
00205 
00206 //
00207 // PUBLIC API
00208 //
00209 
00210 // SubstituteAndAppend()
00211 //
00212 // Substitutes variables into a given format string and appends to a given
00213 // output string. See file comments above for usage.
00214 //
00215 // The declarations of `SubstituteAndAppend()` below consist of overloads
00216 // for passing 0 to 10 arguments, respectively.
00217 //
00218 // NOTE: A zero-argument `SubstituteAndAppend()` may be used within variadic
00219 // templates to allow a variable number of arguments.
00220 //
00221 // Example:
00222 //  template <typename... Args>
00223 //  void VarMsg(std::string* boilerplate, absl::string_view format,
00224 //      const Args&... args) {
00225 //    absl::SubstituteAndAppend(boilerplate, format, args...);
00226 //  }
00227 //
00228 inline void SubstituteAndAppend(std::string* output, absl::string_view format) {
00229   substitute_internal::SubstituteAndAppendArray(output, format, nullptr, 0);
00230 }
00231 
00232 inline void SubstituteAndAppend(std::string* output, absl::string_view format,
00233                                 const substitute_internal::Arg& a0) {
00234   const absl::string_view args[] = {a0.piece()};
00235   substitute_internal::SubstituteAndAppendArray(output, format, args,
00236                                                 ABSL_ARRAYSIZE(args));
00237 }
00238 
00239 inline void SubstituteAndAppend(std::string* output, absl::string_view format,
00240                                 const substitute_internal::Arg& a0,
00241                                 const substitute_internal::Arg& a1) {
00242   const absl::string_view args[] = {a0.piece(), a1.piece()};
00243   substitute_internal::SubstituteAndAppendArray(output, format, args,
00244                                                 ABSL_ARRAYSIZE(args));
00245 }
00246 
00247 inline void SubstituteAndAppend(std::string* output, absl::string_view format,
00248                                 const substitute_internal::Arg& a0,
00249                                 const substitute_internal::Arg& a1,
00250                                 const substitute_internal::Arg& a2) {
00251   const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece()};
00252   substitute_internal::SubstituteAndAppendArray(output, format, args,
00253                                                 ABSL_ARRAYSIZE(args));
00254 }
00255 
00256 inline void SubstituteAndAppend(std::string* output, absl::string_view format,
00257                                 const substitute_internal::Arg& a0,
00258                                 const substitute_internal::Arg& a1,
00259                                 const substitute_internal::Arg& a2,
00260                                 const substitute_internal::Arg& a3) {
00261   const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
00262                                     a3.piece()};
00263   substitute_internal::SubstituteAndAppendArray(output, format, args,
00264                                                 ABSL_ARRAYSIZE(args));
00265 }
00266 
00267 inline void SubstituteAndAppend(std::string* output, absl::string_view format,
00268                                 const substitute_internal::Arg& a0,
00269                                 const substitute_internal::Arg& a1,
00270                                 const substitute_internal::Arg& a2,
00271                                 const substitute_internal::Arg& a3,
00272                                 const substitute_internal::Arg& a4) {
00273   const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
00274                                     a3.piece(), a4.piece()};
00275   substitute_internal::SubstituteAndAppendArray(output, format, args,
00276                                                 ABSL_ARRAYSIZE(args));
00277 }
00278 
00279 inline void SubstituteAndAppend(std::string* output, absl::string_view format,
00280                                 const substitute_internal::Arg& a0,
00281                                 const substitute_internal::Arg& a1,
00282                                 const substitute_internal::Arg& a2,
00283                                 const substitute_internal::Arg& a3,
00284                                 const substitute_internal::Arg& a4,
00285                                 const substitute_internal::Arg& a5) {
00286   const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
00287                                     a3.piece(), a4.piece(), a5.piece()};
00288   substitute_internal::SubstituteAndAppendArray(output, format, args,
00289                                                 ABSL_ARRAYSIZE(args));
00290 }
00291 
00292 inline void SubstituteAndAppend(std::string* output, absl::string_view format,
00293                                 const substitute_internal::Arg& a0,
00294                                 const substitute_internal::Arg& a1,
00295                                 const substitute_internal::Arg& a2,
00296                                 const substitute_internal::Arg& a3,
00297                                 const substitute_internal::Arg& a4,
00298                                 const substitute_internal::Arg& a5,
00299                                 const substitute_internal::Arg& a6) {
00300   const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
00301                                     a3.piece(), a4.piece(), a5.piece(),
00302                                     a6.piece()};
00303   substitute_internal::SubstituteAndAppendArray(output, format, args,
00304                                                 ABSL_ARRAYSIZE(args));
00305 }
00306 
00307 inline void SubstituteAndAppend(
00308     std::string* output, absl::string_view format,
00309     const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
00310     const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
00311     const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
00312     const substitute_internal::Arg& a6, const substitute_internal::Arg& a7) {
00313   const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
00314                                     a3.piece(), a4.piece(), a5.piece(),
00315                                     a6.piece(), a7.piece()};
00316   substitute_internal::SubstituteAndAppendArray(output, format, args,
00317                                                 ABSL_ARRAYSIZE(args));
00318 }
00319 
00320 inline void SubstituteAndAppend(
00321     std::string* output, absl::string_view format,
00322     const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
00323     const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
00324     const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
00325     const substitute_internal::Arg& a6, const substitute_internal::Arg& a7,
00326     const substitute_internal::Arg& a8) {
00327   const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
00328                                     a3.piece(), a4.piece(), a5.piece(),
00329                                     a6.piece(), a7.piece(), a8.piece()};
00330   substitute_internal::SubstituteAndAppendArray(output, format, args,
00331                                                 ABSL_ARRAYSIZE(args));
00332 }
00333 
00334 inline void SubstituteAndAppend(
00335     std::string* output, absl::string_view format,
00336     const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
00337     const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
00338     const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
00339     const substitute_internal::Arg& a6, const substitute_internal::Arg& a7,
00340     const substitute_internal::Arg& a8, const substitute_internal::Arg& a9) {
00341   const absl::string_view args[] = {
00342       a0.piece(), a1.piece(), a2.piece(), a3.piece(), a4.piece(),
00343       a5.piece(), a6.piece(), a7.piece(), a8.piece(), a9.piece()};
00344   substitute_internal::SubstituteAndAppendArray(output, format, args,
00345                                                 ABSL_ARRAYSIZE(args));
00346 }
00347 
00348 #if defined(ABSL_BAD_CALL_IF)
00349 // This body of functions catches cases where the number of placeholders
00350 // doesn't match the number of data arguments.
00351 void SubstituteAndAppend(std::string* output, const char* format)
00352     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 0,
00353                      "There were no substitution arguments "
00354                      "but this format std::string has a $[0-9] in it");
00355 
00356 void SubstituteAndAppend(std::string* output, const char* format,
00357                          const substitute_internal::Arg& a0)
00358     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1,
00359                      "There was 1 substitution argument given, but "
00360                      "this format std::string is either missing its $0, or "
00361                      "contains one of $1-$9");
00362 
00363 void SubstituteAndAppend(std::string* output, const char* format,
00364                          const substitute_internal::Arg& a0,
00365                          const substitute_internal::Arg& a1)
00366     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 3,
00367                      "There were 2 substitution arguments given, but "
00368                      "this format std::string is either missing its $0/$1, or "
00369                      "contains one of $2-$9");
00370 
00371 void SubstituteAndAppend(std::string* output, const char* format,
00372                          const substitute_internal::Arg& a0,
00373                          const substitute_internal::Arg& a1,
00374                          const substitute_internal::Arg& a2)
00375     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 7,
00376                      "There were 3 substitution arguments given, but "
00377                      "this format std::string is either missing its $0/$1/$2, or "
00378                      "contains one of $3-$9");
00379 
00380 void SubstituteAndAppend(std::string* output, const char* format,
00381                          const substitute_internal::Arg& a0,
00382                          const substitute_internal::Arg& a1,
00383                          const substitute_internal::Arg& a2,
00384                          const substitute_internal::Arg& a3)
00385     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 15,
00386                      "There were 4 substitution arguments given, but "
00387                      "this format std::string is either missing its $0-$3, or "
00388                      "contains one of $4-$9");
00389 
00390 void SubstituteAndAppend(std::string* output, const char* format,
00391                          const substitute_internal::Arg& a0,
00392                          const substitute_internal::Arg& a1,
00393                          const substitute_internal::Arg& a2,
00394                          const substitute_internal::Arg& a3,
00395                          const substitute_internal::Arg& a4)
00396     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 31,
00397                      "There were 5 substitution arguments given, but "
00398                      "this format std::string is either missing its $0-$4, or "
00399                      "contains one of $5-$9");
00400 
00401 void SubstituteAndAppend(std::string* output, const char* format,
00402                          const substitute_internal::Arg& a0,
00403                          const substitute_internal::Arg& a1,
00404                          const substitute_internal::Arg& a2,
00405                          const substitute_internal::Arg& a3,
00406                          const substitute_internal::Arg& a4,
00407                          const substitute_internal::Arg& a5)
00408     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 63,
00409                      "There were 6 substitution arguments given, but "
00410                      "this format std::string is either missing its $0-$5, or "
00411                      "contains one of $6-$9");
00412 
00413 void SubstituteAndAppend(
00414     std::string* output, const char* format, const substitute_internal::Arg& a0,
00415     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00416     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00417     const substitute_internal::Arg& a5, const substitute_internal::Arg& a6)
00418     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 127,
00419                      "There were 7 substitution arguments given, but "
00420                      "this format std::string is either missing its $0-$6, or "
00421                      "contains one of $7-$9");
00422 
00423 void SubstituteAndAppend(
00424     std::string* output, const char* format, const substitute_internal::Arg& a0,
00425     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00426     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00427     const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
00428     const substitute_internal::Arg& a7)
00429     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 255,
00430                      "There were 8 substitution arguments given, but "
00431                      "this format std::string is either missing its $0-$7, or "
00432                      "contains one of $8-$9");
00433 
00434 void SubstituteAndAppend(
00435     std::string* output, const char* format, const substitute_internal::Arg& a0,
00436     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00437     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00438     const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
00439     const substitute_internal::Arg& a7, const substitute_internal::Arg& a8)
00440     ABSL_BAD_CALL_IF(
00441         substitute_internal::PlaceholderBitmask(format) != 511,
00442         "There were 9 substitution arguments given, but "
00443         "this format std::string is either missing its $0-$8, or contains a $9");
00444 
00445 void SubstituteAndAppend(
00446     std::string* output, const char* format, const substitute_internal::Arg& a0,
00447     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00448     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00449     const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
00450     const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
00451     const substitute_internal::Arg& a9)
00452     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1023,
00453                      "There were 10 substitution arguments given, but this "
00454                      "format std::string doesn't contain all of $0 through $9");
00455 #endif  // ABSL_BAD_CALL_IF
00456 
00457 // Substitute()
00458 //
00459 // Substitutes variables into a given format string. See file comments above
00460 // for usage.
00461 //
00462 // The declarations of `Substitute()` below consist of overloads for passing 0
00463 // to 10 arguments, respectively.
00464 //
00465 // NOTE: A zero-argument `Substitute()` may be used within variadic templates to
00466 // allow a variable number of arguments.
00467 //
00468 // Example:
00469 //  template <typename... Args>
00470 //  void VarMsg(absl::string_view format, const Args&... args) {
00471 //    std::string s = absl::Substitute(format, args...);
00472 
00473 ABSL_MUST_USE_RESULT inline std::string Substitute(absl::string_view format) {
00474   std::string result;
00475   SubstituteAndAppend(&result, format);
00476   return result;
00477 }
00478 
00479 ABSL_MUST_USE_RESULT inline std::string Substitute(
00480     absl::string_view format, const substitute_internal::Arg& a0) {
00481   std::string result;
00482   SubstituteAndAppend(&result, format, a0);
00483   return result;
00484 }
00485 
00486 ABSL_MUST_USE_RESULT inline std::string Substitute(
00487     absl::string_view format, const substitute_internal::Arg& a0,
00488     const substitute_internal::Arg& a1) {
00489   std::string result;
00490   SubstituteAndAppend(&result, format, a0, a1);
00491   return result;
00492 }
00493 
00494 ABSL_MUST_USE_RESULT inline std::string Substitute(
00495     absl::string_view format, const substitute_internal::Arg& a0,
00496     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2) {
00497   std::string result;
00498   SubstituteAndAppend(&result, format, a0, a1, a2);
00499   return result;
00500 }
00501 
00502 ABSL_MUST_USE_RESULT inline std::string Substitute(
00503     absl::string_view format, const substitute_internal::Arg& a0,
00504     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00505     const substitute_internal::Arg& a3) {
00506   std::string result;
00507   SubstituteAndAppend(&result, format, a0, a1, a2, a3);
00508   return result;
00509 }
00510 
00511 ABSL_MUST_USE_RESULT inline std::string Substitute(
00512     absl::string_view format, const substitute_internal::Arg& a0,
00513     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00514     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4) {
00515   std::string result;
00516   SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4);
00517   return result;
00518 }
00519 
00520 ABSL_MUST_USE_RESULT inline std::string Substitute(
00521     absl::string_view format, const substitute_internal::Arg& a0,
00522     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00523     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00524     const substitute_internal::Arg& a5) {
00525   std::string result;
00526   SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5);
00527   return result;
00528 }
00529 
00530 ABSL_MUST_USE_RESULT inline std::string Substitute(
00531     absl::string_view format, const substitute_internal::Arg& a0,
00532     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00533     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00534     const substitute_internal::Arg& a5, const substitute_internal::Arg& a6) {
00535   std::string result;
00536   SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6);
00537   return result;
00538 }
00539 
00540 ABSL_MUST_USE_RESULT inline std::string Substitute(
00541     absl::string_view format, const substitute_internal::Arg& a0,
00542     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00543     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00544     const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
00545     const substitute_internal::Arg& a7) {
00546   std::string result;
00547   SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7);
00548   return result;
00549 }
00550 
00551 ABSL_MUST_USE_RESULT inline std::string Substitute(
00552     absl::string_view format, const substitute_internal::Arg& a0,
00553     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00554     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00555     const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
00556     const substitute_internal::Arg& a7, const substitute_internal::Arg& a8) {
00557   std::string result;
00558   SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7, a8);
00559   return result;
00560 }
00561 
00562 ABSL_MUST_USE_RESULT inline std::string Substitute(
00563     absl::string_view format, const substitute_internal::Arg& a0,
00564     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00565     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00566     const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
00567     const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
00568     const substitute_internal::Arg& a9) {
00569   std::string result;
00570   SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
00571   return result;
00572 }
00573 
00574 #if defined(ABSL_BAD_CALL_IF)
00575 // This body of functions catches cases where the number of placeholders
00576 // doesn't match the number of data arguments.
00577 std::string Substitute(const char* format)
00578     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 0,
00579                      "There were no substitution arguments "
00580                      "but this format std::string has a $[0-9] in it");
00581 
00582 std::string Substitute(const char* format, const substitute_internal::Arg& a0)
00583     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1,
00584                      "There was 1 substitution argument given, but "
00585                      "this format std::string is either missing its $0, or "
00586                      "contains one of $1-$9");
00587 
00588 std::string Substitute(const char* format, const substitute_internal::Arg& a0,
00589                        const substitute_internal::Arg& a1)
00590     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 3,
00591                      "There were 2 substitution arguments given, but "
00592                      "this format std::string is either missing its $0/$1, or "
00593                      "contains one of $2-$9");
00594 
00595 std::string Substitute(const char* format, const substitute_internal::Arg& a0,
00596                        const substitute_internal::Arg& a1,
00597                        const substitute_internal::Arg& a2)
00598     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 7,
00599                      "There were 3 substitution arguments given, but "
00600                      "this format std::string is either missing its $0/$1/$2, or "
00601                      "contains one of $3-$9");
00602 
00603 std::string Substitute(const char* format, const substitute_internal::Arg& a0,
00604                        const substitute_internal::Arg& a1,
00605                        const substitute_internal::Arg& a2,
00606                        const substitute_internal::Arg& a3)
00607     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 15,
00608                      "There were 4 substitution arguments given, but "
00609                      "this format std::string is either missing its $0-$3, or "
00610                      "contains one of $4-$9");
00611 
00612 std::string Substitute(const char* format, const substitute_internal::Arg& a0,
00613                        const substitute_internal::Arg& a1,
00614                        const substitute_internal::Arg& a2,
00615                        const substitute_internal::Arg& a3,
00616                        const substitute_internal::Arg& a4)
00617     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 31,
00618                      "There were 5 substitution arguments given, but "
00619                      "this format std::string is either missing its $0-$4, or "
00620                      "contains one of $5-$9");
00621 
00622 std::string Substitute(const char* format, const substitute_internal::Arg& a0,
00623                        const substitute_internal::Arg& a1,
00624                        const substitute_internal::Arg& a2,
00625                        const substitute_internal::Arg& a3,
00626                        const substitute_internal::Arg& a4,
00627                        const substitute_internal::Arg& a5)
00628     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 63,
00629                      "There were 6 substitution arguments given, but "
00630                      "this format std::string is either missing its $0-$5, or "
00631                      "contains one of $6-$9");
00632 
00633 std::string Substitute(const char* format, const substitute_internal::Arg& a0,
00634                        const substitute_internal::Arg& a1,
00635                        const substitute_internal::Arg& a2,
00636                        const substitute_internal::Arg& a3,
00637                        const substitute_internal::Arg& a4,
00638                        const substitute_internal::Arg& a5,
00639                        const substitute_internal::Arg& a6)
00640     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 127,
00641                      "There were 7 substitution arguments given, but "
00642                      "this format std::string is either missing its $0-$6, or "
00643                      "contains one of $7-$9");
00644 
00645 std::string Substitute(const char* format, const substitute_internal::Arg& a0,
00646                        const substitute_internal::Arg& a1,
00647                        const substitute_internal::Arg& a2,
00648                        const substitute_internal::Arg& a3,
00649                        const substitute_internal::Arg& a4,
00650                        const substitute_internal::Arg& a5,
00651                        const substitute_internal::Arg& a6,
00652                        const substitute_internal::Arg& a7)
00653     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 255,
00654                      "There were 8 substitution arguments given, but "
00655                      "this format std::string is either missing its $0-$7, or "
00656                      "contains one of $8-$9");
00657 
00658 std::string Substitute(
00659     const char* format, const substitute_internal::Arg& a0,
00660     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00661     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00662     const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
00663     const substitute_internal::Arg& a7, const substitute_internal::Arg& a8)
00664     ABSL_BAD_CALL_IF(
00665         substitute_internal::PlaceholderBitmask(format) != 511,
00666         "There were 9 substitution arguments given, but "
00667         "this format std::string is either missing its $0-$8, or contains a $9");
00668 
00669 std::string Substitute(
00670     const char* format, const substitute_internal::Arg& a0,
00671     const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
00672     const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
00673     const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
00674     const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
00675     const substitute_internal::Arg& a9)
00676     ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1023,
00677                      "There were 10 substitution arguments given, but this "
00678                      "format std::string doesn't contain all of $0 through $9");
00679 #endif  // ABSL_BAD_CALL_IF
00680 
00681 }  // namespace absl
00682 
00683 #endif  // ABSL_STRINGS_SUBSTITUTE_H_


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