substitute.h 33 KB

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