substitute.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. // File: substitute.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This package contains functions for efficiently performing string
  21. // substitutions using a format string with positional notation:
  22. // `Substitute()` and `SubstituteAndAppend()`.
  23. //
  24. // Unlike printf-style format specifiers, `Substitute()` functions do not need
  25. // to specify the type of the substitution arguments. Supported arguments
  26. // following the format string, such as strings, string_views, ints,
  27. // floats, and bools, are automatically converted to strings during the
  28. // substitution process. (See below for a full list of supported types.)
  29. //
  30. // `Substitute()` does not allow you to specify *how* to format a value, beyond
  31. // the default conversion to string. For example, you cannot format an integer
  32. // in hex.
  33. //
  34. // The format string uses positional identifiers indicated by a dollar sign ($)
  35. // and single digit positional ids to indicate which substitution arguments to
  36. // use at that location within the format string.
  37. //
  38. // A '$$' sequence in the format string causes a literal '$' character to be
  39. // output.
  40. //
  41. // Example 1:
  42. // std::string s = Substitute("$1 purchased $0 $2 for $$10. Thanks $1!",
  43. // 5, "Bob", "Apples");
  44. // EXPECT_EQ("Bob purchased 5 Apples for $10. Thanks Bob!", s);
  45. //
  46. // Example 2:
  47. // std::string s = "Hi. ";
  48. // SubstituteAndAppend(&s, "My name is $0 and I am $1 years old.", "Bob", 5);
  49. // EXPECT_EQ("Hi. My name is Bob and I am 5 years old.", s);
  50. //
  51. // Supported types:
  52. // * absl::string_view, std::string, const char* (null is equivalent to "")
  53. // * int32_t, int64_t, uint32_t, uint64_t
  54. // * float, double
  55. // * bool (Printed as "true" or "false")
  56. // * pointer types other than char* (Printed as "0x<lower case hex string>",
  57. // except that null is printed as "NULL")
  58. // * user-defined types via the `AbslStringify()` customization point. See the
  59. // documentation for `absl::StrCat` for an explanation on how to use this.
  60. //
  61. // If an invalid format string is provided, Substitute returns an empty string
  62. // and SubstituteAndAppend does not change the provided output string.
  63. // A format string is invalid if it:
  64. // * ends in an unescaped $ character,
  65. // e.g. "Hello $", or
  66. // * calls for a position argument which is not provided,
  67. // e.g. Substitute("Hello $2", "world"), or
  68. // * specifies a non-digit, non-$ character after an unescaped $ character,
  69. // e.g. "Hello $f".
  70. // In debug mode, i.e. #ifndef NDEBUG, such errors terminate the program.
  71. #ifndef ABSL_STRINGS_SUBSTITUTE_H_
  72. #define ABSL_STRINGS_SUBSTITUTE_H_
  73. #include <cstring>
  74. #include <string>
  75. #include <type_traits>
  76. #include <vector>
  77. #include "absl/base/macros.h"
  78. #include "absl/base/nullability.h"
  79. #include "absl/base/port.h"
  80. #include "absl/strings/ascii.h"
  81. #include "absl/strings/escaping.h"
  82. #include "absl/strings/internal/stringify_sink.h"
  83. #include "absl/strings/numbers.h"
  84. #include "absl/strings/str_cat.h"
  85. #include "absl/strings/str_split.h"
  86. #include "absl/strings/string_view.h"
  87. #include "absl/strings/strip.h"
  88. namespace absl {
  89. ABSL_NAMESPACE_BEGIN
  90. namespace substitute_internal {
  91. // Arg
  92. //
  93. // This class provides an argument type for `absl::Substitute()` and
  94. // `absl::SubstituteAndAppend()`. `Arg` handles implicit conversion of various
  95. // types to a string. (`Arg` is very similar to the `AlphaNum` class in
  96. // `StrCat()`.)
  97. //
  98. // This class has implicit constructors.
  99. class Arg {
  100. public:
  101. // Overloads for string-y things
  102. //
  103. // Explicitly overload `const char*` so the compiler doesn't cast to `bool`.
  104. Arg(absl::Nullable<const char*> value) // NOLINT(google-explicit-constructor)
  105. : piece_(absl::NullSafeStringView(value)) {}
  106. template <typename Allocator>
  107. Arg( // NOLINT
  108. const std::basic_string<char, std::char_traits<char>, Allocator>&
  109. value) noexcept
  110. : piece_(value) {}
  111. Arg(absl::string_view value) // NOLINT(google-explicit-constructor)
  112. : piece_(value) {}
  113. // Overloads for primitives
  114. //
  115. // No overloads are available for signed and unsigned char because if people
  116. // are explicitly declaring their chars as signed or unsigned then they are
  117. // probably using them as 8-bit integers and would probably prefer an integer
  118. // representation. However, we can't really know, so we make the caller decide
  119. // what to do.
  120. Arg(char value) // NOLINT(google-explicit-constructor)
  121. : piece_(scratch_, 1) {
  122. scratch_[0] = value;
  123. }
  124. Arg(short value) // NOLINT(*)
  125. : piece_(scratch_,
  126. static_cast<size_t>(
  127. numbers_internal::FastIntToBuffer(value, scratch_) -
  128. scratch_)) {}
  129. Arg(unsigned short value) // NOLINT(*)
  130. : piece_(scratch_,
  131. static_cast<size_t>(
  132. numbers_internal::FastIntToBuffer(value, scratch_) -
  133. scratch_)) {}
  134. Arg(int value) // NOLINT(google-explicit-constructor)
  135. : piece_(scratch_,
  136. static_cast<size_t>(
  137. numbers_internal::FastIntToBuffer(value, scratch_) -
  138. scratch_)) {}
  139. Arg(unsigned int value) // NOLINT(google-explicit-constructor)
  140. : piece_(scratch_,
  141. static_cast<size_t>(
  142. numbers_internal::FastIntToBuffer(value, scratch_) -
  143. scratch_)) {}
  144. Arg(long value) // NOLINT(*)
  145. : piece_(scratch_,
  146. static_cast<size_t>(
  147. numbers_internal::FastIntToBuffer(value, scratch_) -
  148. scratch_)) {}
  149. Arg(unsigned long value) // NOLINT(*)
  150. : piece_(scratch_,
  151. static_cast<size_t>(
  152. numbers_internal::FastIntToBuffer(value, scratch_) -
  153. scratch_)) {}
  154. Arg(long long value) // NOLINT(*)
  155. : piece_(scratch_,
  156. static_cast<size_t>(
  157. numbers_internal::FastIntToBuffer(value, scratch_) -
  158. scratch_)) {}
  159. Arg(unsigned long long value) // NOLINT(*)
  160. : piece_(scratch_,
  161. static_cast<size_t>(
  162. numbers_internal::FastIntToBuffer(value, scratch_) -
  163. scratch_)) {}
  164. Arg(float value) // NOLINT(google-explicit-constructor)
  165. : piece_(scratch_, numbers_internal::SixDigitsToBuffer(value, scratch_)) {
  166. }
  167. Arg(double value) // NOLINT(google-explicit-constructor)
  168. : piece_(scratch_, numbers_internal::SixDigitsToBuffer(value, scratch_)) {
  169. }
  170. Arg(bool value) // NOLINT(google-explicit-constructor)
  171. : piece_(value ? "true" : "false") {}
  172. template <typename T, typename = typename std::enable_if<
  173. HasAbslStringify<T>::value>::type>
  174. Arg( // NOLINT(google-explicit-constructor)
  175. const T& v, strings_internal::StringifySink&& sink = {})
  176. : piece_(strings_internal::ExtractStringification(sink, v)) {}
  177. Arg(Hex hex); // NOLINT(google-explicit-constructor)
  178. Arg(Dec dec); // NOLINT(google-explicit-constructor)
  179. // vector<bool>::reference and const_reference require special help to convert
  180. // to `Arg` because it requires two user defined conversions.
  181. template <typename T,
  182. absl::enable_if_t<
  183. std::is_class<T>::value &&
  184. (std::is_same<T, std::vector<bool>::reference>::value ||
  185. std::is_same<T, std::vector<bool>::const_reference>::value)>* =
  186. nullptr>
  187. Arg(T value) // NOLINT(google-explicit-constructor)
  188. : Arg(static_cast<bool>(value)) {}
  189. // `void*` values, with the exception of `char*`, are printed as
  190. // "0x<hex value>". However, in the case of `nullptr`, "NULL" is printed.
  191. Arg( // NOLINT(google-explicit-constructor)
  192. absl::Nullable<const void*> value);
  193. // Normal enums are already handled by the integer formatters.
  194. // This overload matches only scoped enums.
  195. template <typename T,
  196. typename = typename std::enable_if<
  197. std::is_enum<T>{} && !std::is_convertible<T, int>{} &&
  198. !HasAbslStringify<T>::value>::type>
  199. Arg(T value) // NOLINT(google-explicit-constructor)
  200. : Arg(static_cast<typename std::underlying_type<T>::type>(value)) {}
  201. Arg(const Arg&) = delete;
  202. Arg& operator=(const Arg&) = delete;
  203. absl::string_view piece() const { return piece_; }
  204. private:
  205. absl::string_view piece_;
  206. char scratch_[numbers_internal::kFastToBufferSize];
  207. };
  208. // Internal helper function. Don't call this from outside this implementation.
  209. // This interface may change without notice.
  210. void SubstituteAndAppendArray(
  211. absl::Nonnull<std::string*> output, absl::string_view format,
  212. absl::Nullable<const absl::string_view*> args_array, size_t num_args);
  213. #if defined(ABSL_BAD_CALL_IF)
  214. constexpr int CalculateOneBit(absl::Nonnull<const char*> format) {
  215. // Returns:
  216. // * 2^N for '$N' when N is in [0-9]
  217. // * 0 for correct '$' escaping: '$$'.
  218. // * -1 otherwise.
  219. return (*format < '0' || *format > '9') ? (*format == '$' ? 0 : -1)
  220. : (1 << (*format - '0'));
  221. }
  222. constexpr const char* SkipNumber(absl::Nonnull<const char*> format) {
  223. return !*format ? format : (format + 1);
  224. }
  225. constexpr int PlaceholderBitmask(absl::Nonnull<const char*> format) {
  226. return !*format
  227. ? 0
  228. : *format != '$' ? PlaceholderBitmask(format + 1)
  229. : (CalculateOneBit(format + 1) |
  230. PlaceholderBitmask(SkipNumber(format + 1)));
  231. }
  232. #endif // ABSL_BAD_CALL_IF
  233. } // namespace substitute_internal
  234. //
  235. // PUBLIC API
  236. //
  237. // SubstituteAndAppend()
  238. //
  239. // Substitutes variables into a given format string and appends to a given
  240. // output string. See file comments above for usage.
  241. //
  242. // The declarations of `SubstituteAndAppend()` below consist of overloads
  243. // for passing 0 to 10 arguments, respectively.
  244. //
  245. // NOTE: A zero-argument `SubstituteAndAppend()` may be used within variadic
  246. // templates to allow a variable number of arguments.
  247. //
  248. // Example:
  249. // template <typename... Args>
  250. // void VarMsg(std::string* boilerplate, absl::string_view format,
  251. // const Args&... args) {
  252. // absl::SubstituteAndAppend(boilerplate, format, args...);
  253. // }
  254. //
  255. inline void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  256. absl::string_view format) {
  257. substitute_internal::SubstituteAndAppendArray(output, format, nullptr, 0);
  258. }
  259. inline void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  260. absl::string_view format,
  261. const substitute_internal::Arg& a0) {
  262. const absl::string_view args[] = {a0.piece()};
  263. substitute_internal::SubstituteAndAppendArray(output, format, args,
  264. ABSL_ARRAYSIZE(args));
  265. }
  266. inline void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  267. absl::string_view format,
  268. const substitute_internal::Arg& a0,
  269. const substitute_internal::Arg& a1) {
  270. const absl::string_view args[] = {a0.piece(), a1.piece()};
  271. substitute_internal::SubstituteAndAppendArray(output, format, args,
  272. ABSL_ARRAYSIZE(args));
  273. }
  274. inline void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  275. absl::string_view format,
  276. const substitute_internal::Arg& a0,
  277. const substitute_internal::Arg& a1,
  278. const substitute_internal::Arg& a2) {
  279. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece()};
  280. substitute_internal::SubstituteAndAppendArray(output, format, args,
  281. ABSL_ARRAYSIZE(args));
  282. }
  283. inline void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  284. absl::string_view format,
  285. const substitute_internal::Arg& a0,
  286. const substitute_internal::Arg& a1,
  287. const substitute_internal::Arg& a2,
  288. const substitute_internal::Arg& a3) {
  289. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  290. a3.piece()};
  291. substitute_internal::SubstituteAndAppendArray(output, format, args,
  292. ABSL_ARRAYSIZE(args));
  293. }
  294. inline void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  295. absl::string_view format,
  296. const substitute_internal::Arg& a0,
  297. const substitute_internal::Arg& a1,
  298. const substitute_internal::Arg& a2,
  299. const substitute_internal::Arg& a3,
  300. const substitute_internal::Arg& a4) {
  301. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  302. a3.piece(), a4.piece()};
  303. substitute_internal::SubstituteAndAppendArray(output, format, args,
  304. ABSL_ARRAYSIZE(args));
  305. }
  306. inline void SubstituteAndAppend(
  307. absl::Nonnull<std::string*> output, absl::string_view format,
  308. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  309. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  310. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5) {
  311. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  312. a3.piece(), a4.piece(), a5.piece()};
  313. substitute_internal::SubstituteAndAppendArray(output, format, args,
  314. ABSL_ARRAYSIZE(args));
  315. }
  316. inline void SubstituteAndAppend(
  317. absl::Nonnull<std::string*> output, absl::string_view format,
  318. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  319. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  320. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  321. const substitute_internal::Arg& a6) {
  322. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  323. a3.piece(), a4.piece(), a5.piece(),
  324. a6.piece()};
  325. substitute_internal::SubstituteAndAppendArray(output, format, args,
  326. ABSL_ARRAYSIZE(args));
  327. }
  328. inline void SubstituteAndAppend(
  329. absl::Nonnull<std::string*> output, absl::string_view format,
  330. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  331. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  332. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  333. const substitute_internal::Arg& a6, const substitute_internal::Arg& a7) {
  334. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  335. a3.piece(), a4.piece(), a5.piece(),
  336. a6.piece(), a7.piece()};
  337. substitute_internal::SubstituteAndAppendArray(output, format, args,
  338. ABSL_ARRAYSIZE(args));
  339. }
  340. inline void SubstituteAndAppend(
  341. absl::Nonnull<std::string*> output, absl::string_view format,
  342. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  343. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  344. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  345. const substitute_internal::Arg& a6, const substitute_internal::Arg& a7,
  346. const substitute_internal::Arg& a8) {
  347. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  348. a3.piece(), a4.piece(), a5.piece(),
  349. a6.piece(), a7.piece(), a8.piece()};
  350. substitute_internal::SubstituteAndAppendArray(output, format, args,
  351. ABSL_ARRAYSIZE(args));
  352. }
  353. inline void SubstituteAndAppend(
  354. absl::Nonnull<std::string*> output, absl::string_view format,
  355. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  356. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  357. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  358. const substitute_internal::Arg& a6, const substitute_internal::Arg& a7,
  359. const substitute_internal::Arg& a8, const substitute_internal::Arg& a9) {
  360. const absl::string_view args[] = {
  361. a0.piece(), a1.piece(), a2.piece(), a3.piece(), a4.piece(),
  362. a5.piece(), a6.piece(), a7.piece(), a8.piece(), a9.piece()};
  363. substitute_internal::SubstituteAndAppendArray(output, format, args,
  364. ABSL_ARRAYSIZE(args));
  365. }
  366. #if defined(ABSL_BAD_CALL_IF)
  367. // This body of functions catches cases where the number of placeholders
  368. // doesn't match the number of data arguments.
  369. void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  370. absl::Nonnull<const char*> format)
  371. ABSL_BAD_CALL_IF(
  372. substitute_internal::PlaceholderBitmask(format) != 0,
  373. "There were no substitution arguments "
  374. "but this format string either has a $[0-9] in it or contains "
  375. "an unescaped $ character (use $$ instead)");
  376. void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  377. absl::Nonnull<const char*> format,
  378. const substitute_internal::Arg& a0)
  379. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1,
  380. "There was 1 substitution argument given, but "
  381. "this format string is missing its $0, contains "
  382. "one of $1-$9, or contains an unescaped $ character (use "
  383. "$$ instead)");
  384. void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  385. absl::Nonnull<const char*> format,
  386. const substitute_internal::Arg& a0,
  387. const substitute_internal::Arg& a1)
  388. ABSL_BAD_CALL_IF(
  389. substitute_internal::PlaceholderBitmask(format) != 3,
  390. "There were 2 substitution arguments given, but this format string is "
  391. "missing its $0/$1, contains one of $2-$9, or contains an "
  392. "unescaped $ character (use $$ instead)");
  393. void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  394. absl::Nonnull<const char*> format,
  395. const substitute_internal::Arg& a0,
  396. const substitute_internal::Arg& a1,
  397. const substitute_internal::Arg& a2)
  398. ABSL_BAD_CALL_IF(
  399. substitute_internal::PlaceholderBitmask(format) != 7,
  400. "There were 3 substitution arguments given, but "
  401. "this format string is missing its $0/$1/$2, contains one of "
  402. "$3-$9, or contains an unescaped $ character (use $$ instead)");
  403. void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  404. absl::Nonnull<const char*> format,
  405. const substitute_internal::Arg& a0,
  406. const substitute_internal::Arg& a1,
  407. const substitute_internal::Arg& a2,
  408. const substitute_internal::Arg& a3)
  409. ABSL_BAD_CALL_IF(
  410. substitute_internal::PlaceholderBitmask(format) != 15,
  411. "There were 4 substitution arguments given, but "
  412. "this format string is missing its $0-$3, contains one of "
  413. "$4-$9, or contains an unescaped $ character (use $$ instead)");
  414. void SubstituteAndAppend(absl::Nonnull<std::string*> output,
  415. absl::Nonnull<const char*> format,
  416. const substitute_internal::Arg& a0,
  417. const substitute_internal::Arg& a1,
  418. const substitute_internal::Arg& a2,
  419. const substitute_internal::Arg& a3,
  420. const substitute_internal::Arg& a4)
  421. ABSL_BAD_CALL_IF(
  422. substitute_internal::PlaceholderBitmask(format) != 31,
  423. "There were 5 substitution arguments given, but "
  424. "this format string is missing its $0-$4, contains one of "
  425. "$5-$9, or contains an unescaped $ character (use $$ instead)");
  426. void SubstituteAndAppend(
  427. absl::Nonnull<std::string*> output, absl::Nonnull<const char*> format,
  428. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  429. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  430. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5)
  431. ABSL_BAD_CALL_IF(
  432. substitute_internal::PlaceholderBitmask(format) != 63,
  433. "There were 6 substitution arguments given, but "
  434. "this format string is missing its $0-$5, contains one of "
  435. "$6-$9, or contains an unescaped $ character (use $$ instead)");
  436. void SubstituteAndAppend(
  437. absl::Nonnull<std::string*> output, absl::Nonnull<const char*> format,
  438. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  439. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  440. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  441. const substitute_internal::Arg& a6)
  442. ABSL_BAD_CALL_IF(
  443. substitute_internal::PlaceholderBitmask(format) != 127,
  444. "There were 7 substitution arguments given, but "
  445. "this format string is missing its $0-$6, contains one of "
  446. "$7-$9, or contains an unescaped $ character (use $$ instead)");
  447. void SubstituteAndAppend(
  448. absl::Nonnull<std::string*> output, absl::Nonnull<const char*> format,
  449. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  450. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  451. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  452. const substitute_internal::Arg& a6, const substitute_internal::Arg& a7)
  453. ABSL_BAD_CALL_IF(
  454. substitute_internal::PlaceholderBitmask(format) != 255,
  455. "There were 8 substitution arguments given, but "
  456. "this format string is missing its $0-$7, contains one of "
  457. "$8-$9, or contains an unescaped $ character (use $$ instead)");
  458. void SubstituteAndAppend(
  459. absl::Nonnull<std::string*> output, absl::Nonnull<const char*> format,
  460. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  461. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  462. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  463. const substitute_internal::Arg& a6, const substitute_internal::Arg& a7,
  464. const substitute_internal::Arg& a8)
  465. ABSL_BAD_CALL_IF(
  466. substitute_internal::PlaceholderBitmask(format) != 511,
  467. "There were 9 substitution arguments given, but "
  468. "this format string is missing its $0-$8, contains a $9, or "
  469. "contains an unescaped $ character (use $$ instead)");
  470. void SubstituteAndAppend(
  471. absl::Nonnull<std::string*> output, absl::Nonnull<const char*> format,
  472. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  473. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  474. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  475. const substitute_internal::Arg& a6, const substitute_internal::Arg& a7,
  476. const substitute_internal::Arg& a8, const substitute_internal::Arg& a9)
  477. ABSL_BAD_CALL_IF(
  478. substitute_internal::PlaceholderBitmask(format) != 1023,
  479. "There were 10 substitution arguments given, but this "
  480. "format string either doesn't contain all of $0 through $9 or "
  481. "contains an unescaped $ character (use $$ instead)");
  482. #endif // ABSL_BAD_CALL_IF
  483. // Substitute()
  484. //
  485. // Substitutes variables into a given format string. See file comments above
  486. // for usage.
  487. //
  488. // The declarations of `Substitute()` below consist of overloads for passing 0
  489. // to 10 arguments, respectively.
  490. //
  491. // NOTE: A zero-argument `Substitute()` may be used within variadic templates to
  492. // allow a variable number of arguments.
  493. //
  494. // Example:
  495. // template <typename... Args>
  496. // void VarMsg(absl::string_view format, const Args&... args) {
  497. // std::string s = absl::Substitute(format, args...);
  498. ABSL_MUST_USE_RESULT inline std::string Substitute(absl::string_view format) {
  499. std::string result;
  500. SubstituteAndAppend(&result, format);
  501. return result;
  502. }
  503. ABSL_MUST_USE_RESULT inline std::string Substitute(
  504. absl::string_view format, const substitute_internal::Arg& a0) {
  505. std::string result;
  506. SubstituteAndAppend(&result, format, a0);
  507. return result;
  508. }
  509. ABSL_MUST_USE_RESULT inline std::string Substitute(
  510. absl::string_view format, const substitute_internal::Arg& a0,
  511. const substitute_internal::Arg& a1) {
  512. std::string result;
  513. SubstituteAndAppend(&result, format, a0, a1);
  514. return result;
  515. }
  516. ABSL_MUST_USE_RESULT inline std::string Substitute(
  517. absl::string_view format, const substitute_internal::Arg& a0,
  518. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2) {
  519. std::string result;
  520. SubstituteAndAppend(&result, format, a0, a1, a2);
  521. return result;
  522. }
  523. ABSL_MUST_USE_RESULT inline std::string Substitute(
  524. absl::string_view format, const substitute_internal::Arg& a0,
  525. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  526. const substitute_internal::Arg& a3) {
  527. std::string result;
  528. SubstituteAndAppend(&result, format, a0, a1, a2, a3);
  529. return result;
  530. }
  531. ABSL_MUST_USE_RESULT inline std::string Substitute(
  532. absl::string_view format, const substitute_internal::Arg& a0,
  533. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  534. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4) {
  535. std::string result;
  536. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4);
  537. return result;
  538. }
  539. ABSL_MUST_USE_RESULT inline std::string Substitute(
  540. absl::string_view format, const substitute_internal::Arg& a0,
  541. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  542. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  543. const substitute_internal::Arg& a5) {
  544. std::string result;
  545. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5);
  546. return result;
  547. }
  548. ABSL_MUST_USE_RESULT inline std::string Substitute(
  549. absl::string_view format, const substitute_internal::Arg& a0,
  550. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  551. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  552. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6) {
  553. std::string result;
  554. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6);
  555. return result;
  556. }
  557. ABSL_MUST_USE_RESULT inline std::string Substitute(
  558. absl::string_view format, const substitute_internal::Arg& a0,
  559. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  560. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  561. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  562. const substitute_internal::Arg& a7) {
  563. std::string result;
  564. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7);
  565. return result;
  566. }
  567. ABSL_MUST_USE_RESULT inline std::string Substitute(
  568. absl::string_view format, const substitute_internal::Arg& a0,
  569. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  570. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  571. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  572. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8) {
  573. std::string result;
  574. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7, a8);
  575. return result;
  576. }
  577. ABSL_MUST_USE_RESULT inline std::string Substitute(
  578. absl::string_view format, const substitute_internal::Arg& a0,
  579. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  580. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  581. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  582. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
  583. const substitute_internal::Arg& a9) {
  584. std::string result;
  585. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
  586. return result;
  587. }
  588. #if defined(ABSL_BAD_CALL_IF)
  589. // This body of functions catches cases where the number of placeholders
  590. // doesn't match the number of data arguments.
  591. std::string Substitute(absl::Nonnull<const char*> format)
  592. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 0,
  593. "There were no substitution arguments "
  594. "but this format string either has a $[0-9] in it or "
  595. "contains an unescaped $ character (use $$ instead)");
  596. std::string Substitute(absl::Nonnull<const char*> format,
  597. const substitute_internal::Arg& a0)
  598. ABSL_BAD_CALL_IF(
  599. substitute_internal::PlaceholderBitmask(format) != 1,
  600. "There was 1 substitution argument given, but "
  601. "this format string is missing its $0, contains one of $1-$9, "
  602. "or contains an unescaped $ character (use $$ instead)");
  603. std::string Substitute(absl::Nonnull<const char*> format,
  604. const substitute_internal::Arg& a0,
  605. const substitute_internal::Arg& a1)
  606. ABSL_BAD_CALL_IF(
  607. substitute_internal::PlaceholderBitmask(format) != 3,
  608. "There were 2 substitution arguments given, but "
  609. "this format string is missing its $0/$1, contains one of "
  610. "$2-$9, or contains an unescaped $ character (use $$ instead)");
  611. std::string Substitute(absl::Nonnull<const char*> format,
  612. const substitute_internal::Arg& a0,
  613. const substitute_internal::Arg& a1,
  614. const substitute_internal::Arg& a2)
  615. ABSL_BAD_CALL_IF(
  616. substitute_internal::PlaceholderBitmask(format) != 7,
  617. "There were 3 substitution arguments given, but "
  618. "this format string is missing its $0/$1/$2, contains one of "
  619. "$3-$9, or contains an unescaped $ character (use $$ instead)");
  620. std::string Substitute(absl::Nonnull<const char*> format,
  621. const substitute_internal::Arg& a0,
  622. const substitute_internal::Arg& a1,
  623. const substitute_internal::Arg& a2,
  624. const substitute_internal::Arg& a3)
  625. ABSL_BAD_CALL_IF(
  626. substitute_internal::PlaceholderBitmask(format) != 15,
  627. "There were 4 substitution arguments given, but "
  628. "this format string is missing its $0-$3, contains one of "
  629. "$4-$9, or contains an unescaped $ character (use $$ instead)");
  630. std::string Substitute(absl::Nonnull<const char*> format,
  631. const substitute_internal::Arg& a0,
  632. const substitute_internal::Arg& a1,
  633. const substitute_internal::Arg& a2,
  634. const substitute_internal::Arg& a3,
  635. const substitute_internal::Arg& a4)
  636. ABSL_BAD_CALL_IF(
  637. substitute_internal::PlaceholderBitmask(format) != 31,
  638. "There were 5 substitution arguments given, but "
  639. "this format string is missing its $0-$4, contains one of "
  640. "$5-$9, or contains an unescaped $ character (use $$ instead)");
  641. std::string Substitute(absl::Nonnull<const char*> format,
  642. const substitute_internal::Arg& a0,
  643. const substitute_internal::Arg& a1,
  644. const substitute_internal::Arg& a2,
  645. const substitute_internal::Arg& a3,
  646. const substitute_internal::Arg& a4,
  647. const substitute_internal::Arg& a5)
  648. ABSL_BAD_CALL_IF(
  649. substitute_internal::PlaceholderBitmask(format) != 63,
  650. "There were 6 substitution arguments given, but "
  651. "this format string is missing its $0-$5, contains one of "
  652. "$6-$9, or contains an unescaped $ character (use $$ instead)");
  653. std::string Substitute(
  654. absl::Nonnull<const char*> format, const substitute_internal::Arg& a0,
  655. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  656. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  657. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6)
  658. ABSL_BAD_CALL_IF(
  659. substitute_internal::PlaceholderBitmask(format) != 127,
  660. "There were 7 substitution arguments given, but "
  661. "this format string is missing its $0-$6, contains one of "
  662. "$7-$9, or contains an unescaped $ character (use $$ instead)");
  663. std::string Substitute(
  664. absl::Nonnull<const char*> format, const substitute_internal::Arg& a0,
  665. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  666. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  667. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  668. const substitute_internal::Arg& a7)
  669. ABSL_BAD_CALL_IF(
  670. substitute_internal::PlaceholderBitmask(format) != 255,
  671. "There were 8 substitution arguments given, but "
  672. "this format string is missing its $0-$7, contains one of "
  673. "$8-$9, or contains an unescaped $ character (use $$ instead)");
  674. std::string Substitute(
  675. absl::Nonnull<const char*> format, const substitute_internal::Arg& a0,
  676. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  677. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  678. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  679. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8)
  680. ABSL_BAD_CALL_IF(
  681. substitute_internal::PlaceholderBitmask(format) != 511,
  682. "There were 9 substitution arguments given, but "
  683. "this format string is missing its $0-$8, contains a $9, or "
  684. "contains an unescaped $ character (use $$ instead)");
  685. std::string Substitute(
  686. absl::Nonnull<const char*> format, const substitute_internal::Arg& a0,
  687. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  688. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  689. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  690. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
  691. const substitute_internal::Arg& a9)
  692. ABSL_BAD_CALL_IF(
  693. substitute_internal::PlaceholderBitmask(format) != 1023,
  694. "There were 10 substitution arguments given, but this "
  695. "format string either doesn't contain all of $0 through $9 or "
  696. "contains an unescaped $ character (use $$ instead)");
  697. #endif // ABSL_BAD_CALL_IF
  698. ABSL_NAMESPACE_END
  699. } // namespace absl
  700. #endif // ABSL_STRINGS_SUBSTITUTE_H_