You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

substitute.h 34 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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 convert
  155. // to `Arg` 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. // Normal enums are already handled by the integer formatters.
  168. // This overload matches only scoped enums.
  169. template <typename T,
  170. typename = typename std::enable_if<
  171. std::is_enum<T>{} && !std::is_convertible<T, int>{}>::type>
  172. Arg(T value) // NOLINT(google-explicit-constructor)
  173. : Arg(static_cast<typename std::underlying_type<T>::type>(value)) {}
  174. Arg(const Arg&) = delete;
  175. Arg& operator=(const Arg&) = delete;
  176. absl::string_view piece() const { return piece_; }
  177. private:
  178. absl::string_view piece_;
  179. char scratch_[numbers_internal::kFastToBufferSize];
  180. };
  181. // Internal helper function. Don't call this from outside this implementation.
  182. // This interface may change without notice.
  183. void SubstituteAndAppendArray(std::string* output, absl::string_view format,
  184. const absl::string_view* args_array,
  185. size_t num_args);
  186. #if defined(ABSL_BAD_CALL_IF)
  187. constexpr int CalculateOneBit(const char* format) {
  188. // Returns:
  189. // * 2^N for '$N' when N is in [0-9]
  190. // * 0 for correct '$' escaping: '$$'.
  191. // * -1 otherwise.
  192. return (*format < '0' || *format > '9') ? (*format == '$' ? 0 : -1)
  193. : (1 << (*format - '0'));
  194. }
  195. constexpr const char* SkipNumber(const char* format) {
  196. return !*format ? format : (format + 1);
  197. }
  198. constexpr int PlaceholderBitmask(const char* format) {
  199. return !*format
  200. ? 0
  201. : *format != '$' ? PlaceholderBitmask(format + 1)
  202. : (CalculateOneBit(format + 1) |
  203. PlaceholderBitmask(SkipNumber(format + 1)));
  204. }
  205. #endif // ABSL_BAD_CALL_IF
  206. } // namespace substitute_internal
  207. //
  208. // PUBLIC API
  209. //
  210. // SubstituteAndAppend()
  211. //
  212. // Substitutes variables into a given format string and appends to a given
  213. // output string. See file comments above for usage.
  214. //
  215. // The declarations of `SubstituteAndAppend()` below consist of overloads
  216. // for passing 0 to 10 arguments, respectively.
  217. //
  218. // NOTE: A zero-argument `SubstituteAndAppend()` may be used within variadic
  219. // templates to allow a variable number of arguments.
  220. //
  221. // Example:
  222. // template <typename... Args>
  223. // void VarMsg(std::string* boilerplate, absl::string_view format,
  224. // const Args&... args) {
  225. // absl::SubstituteAndAppend(boilerplate, format, args...);
  226. // }
  227. //
  228. inline void SubstituteAndAppend(std::string* output, absl::string_view format) {
  229. substitute_internal::SubstituteAndAppendArray(output, format, nullptr, 0);
  230. }
  231. inline void SubstituteAndAppend(std::string* output, absl::string_view format,
  232. const substitute_internal::Arg& a0) {
  233. const absl::string_view args[] = {a0.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 absl::string_view args[] = {a0.piece(), a1.piece()};
  241. substitute_internal::SubstituteAndAppendArray(output, format, args,
  242. ABSL_ARRAYSIZE(args));
  243. }
  244. inline void SubstituteAndAppend(std::string* output, absl::string_view format,
  245. const substitute_internal::Arg& a0,
  246. const substitute_internal::Arg& a1,
  247. const substitute_internal::Arg& a2) {
  248. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece()};
  249. substitute_internal::SubstituteAndAppendArray(output, format, args,
  250. ABSL_ARRAYSIZE(args));
  251. }
  252. inline void SubstituteAndAppend(std::string* output, absl::string_view format,
  253. const substitute_internal::Arg& a0,
  254. const substitute_internal::Arg& a1,
  255. const substitute_internal::Arg& a2,
  256. const substitute_internal::Arg& a3) {
  257. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  258. a3.piece()};
  259. substitute_internal::SubstituteAndAppendArray(output, format, args,
  260. ABSL_ARRAYSIZE(args));
  261. }
  262. inline void SubstituteAndAppend(std::string* output, absl::string_view format,
  263. const substitute_internal::Arg& a0,
  264. const substitute_internal::Arg& a1,
  265. const substitute_internal::Arg& a2,
  266. const substitute_internal::Arg& a3,
  267. const substitute_internal::Arg& a4) {
  268. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  269. a3.piece(), a4.piece()};
  270. substitute_internal::SubstituteAndAppendArray(output, format, args,
  271. ABSL_ARRAYSIZE(args));
  272. }
  273. inline void SubstituteAndAppend(std::string* output, absl::string_view format,
  274. const substitute_internal::Arg& a0,
  275. const substitute_internal::Arg& a1,
  276. const substitute_internal::Arg& a2,
  277. const substitute_internal::Arg& a3,
  278. const substitute_internal::Arg& a4,
  279. const substitute_internal::Arg& a5) {
  280. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  281. a3.piece(), a4.piece(), a5.piece()};
  282. substitute_internal::SubstituteAndAppendArray(output, format, args,
  283. ABSL_ARRAYSIZE(args));
  284. }
  285. inline void SubstituteAndAppend(std::string* output, absl::string_view format,
  286. const substitute_internal::Arg& a0,
  287. const substitute_internal::Arg& a1,
  288. const substitute_internal::Arg& a2,
  289. const substitute_internal::Arg& a3,
  290. const substitute_internal::Arg& a4,
  291. const substitute_internal::Arg& a5,
  292. const substitute_internal::Arg& a6) {
  293. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  294. a3.piece(), a4.piece(), a5.piece(),
  295. a6.piece()};
  296. substitute_internal::SubstituteAndAppendArray(output, format, args,
  297. ABSL_ARRAYSIZE(args));
  298. }
  299. inline void SubstituteAndAppend(
  300. std::string* output, absl::string_view format,
  301. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  302. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  303. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  304. const substitute_internal::Arg& a6, const substitute_internal::Arg& a7) {
  305. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  306. a3.piece(), a4.piece(), a5.piece(),
  307. a6.piece(), a7.piece()};
  308. substitute_internal::SubstituteAndAppendArray(output, format, args,
  309. ABSL_ARRAYSIZE(args));
  310. }
  311. inline void SubstituteAndAppend(
  312. std::string* output, absl::string_view format,
  313. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  314. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  315. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  316. const substitute_internal::Arg& a6, const substitute_internal::Arg& a7,
  317. const substitute_internal::Arg& a8) {
  318. const absl::string_view args[] = {a0.piece(), a1.piece(), a2.piece(),
  319. a3.piece(), a4.piece(), a5.piece(),
  320. a6.piece(), a7.piece(), a8.piece()};
  321. substitute_internal::SubstituteAndAppendArray(output, format, args,
  322. ABSL_ARRAYSIZE(args));
  323. }
  324. inline void SubstituteAndAppend(
  325. std::string* output, absl::string_view format,
  326. const substitute_internal::Arg& a0, const substitute_internal::Arg& a1,
  327. const substitute_internal::Arg& a2, const substitute_internal::Arg& a3,
  328. const substitute_internal::Arg& a4, const substitute_internal::Arg& a5,
  329. const substitute_internal::Arg& a6, const substitute_internal::Arg& a7,
  330. const substitute_internal::Arg& a8, const substitute_internal::Arg& a9) {
  331. const absl::string_view args[] = {
  332. a0.piece(), a1.piece(), a2.piece(), a3.piece(), a4.piece(),
  333. a5.piece(), a6.piece(), a7.piece(), a8.piece(), a9.piece()};
  334. substitute_internal::SubstituteAndAppendArray(output, format, args,
  335. ABSL_ARRAYSIZE(args));
  336. }
  337. #if defined(ABSL_BAD_CALL_IF)
  338. // This body of functions catches cases where the number of placeholders
  339. // doesn't match the number of data arguments.
  340. void SubstituteAndAppend(std::string* output, const char* format)
  341. ABSL_BAD_CALL_IF(
  342. substitute_internal::PlaceholderBitmask(format) != 0,
  343. "There were no substitution arguments "
  344. "but this format string either has a $[0-9] in it or contains "
  345. "an unescaped $ character (use $$ instead)");
  346. void SubstituteAndAppend(std::string* output, const char* format,
  347. const substitute_internal::Arg& a0)
  348. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1,
  349. "There was 1 substitution argument given, but "
  350. "this format string is missing its $0, contains "
  351. "one of $1-$9, or contains an unescaped $ character (use "
  352. "$$ instead)");
  353. void SubstituteAndAppend(std::string* output, const char* format,
  354. const substitute_internal::Arg& a0,
  355. const substitute_internal::Arg& a1)
  356. ABSL_BAD_CALL_IF(
  357. substitute_internal::PlaceholderBitmask(format) != 3,
  358. "There were 2 substitution arguments given, but this format string is "
  359. "missing its $0/$1, contains one of $2-$9, or contains an "
  360. "unescaped $ character (use $$ instead)");
  361. void SubstituteAndAppend(std::string* output, const char* format,
  362. const substitute_internal::Arg& a0,
  363. const substitute_internal::Arg& a1,
  364. const substitute_internal::Arg& a2)
  365. ABSL_BAD_CALL_IF(
  366. substitute_internal::PlaceholderBitmask(format) != 7,
  367. "There were 3 substitution arguments given, but "
  368. "this format string is missing its $0/$1/$2, contains one of "
  369. "$3-$9, or contains an unescaped $ character (use $$ instead)");
  370. void SubstituteAndAppend(std::string* output, const char* format,
  371. const substitute_internal::Arg& a0,
  372. const substitute_internal::Arg& a1,
  373. const substitute_internal::Arg& a2,
  374. const substitute_internal::Arg& a3)
  375. ABSL_BAD_CALL_IF(
  376. substitute_internal::PlaceholderBitmask(format) != 15,
  377. "There were 4 substitution arguments given, but "
  378. "this format string is missing its $0-$3, contains one of "
  379. "$4-$9, or contains an unescaped $ character (use $$ instead)");
  380. void SubstituteAndAppend(std::string* output, const char* format,
  381. const substitute_internal::Arg& a0,
  382. const substitute_internal::Arg& a1,
  383. const substitute_internal::Arg& a2,
  384. const substitute_internal::Arg& a3,
  385. const substitute_internal::Arg& a4)
  386. ABSL_BAD_CALL_IF(
  387. substitute_internal::PlaceholderBitmask(format) != 31,
  388. "There were 5 substitution arguments given, but "
  389. "this format string is missing its $0-$4, contains one of "
  390. "$5-$9, or contains an unescaped $ character (use $$ instead)");
  391. void SubstituteAndAppend(std::string* output, const char* format,
  392. const substitute_internal::Arg& a0,
  393. const substitute_internal::Arg& a1,
  394. const substitute_internal::Arg& a2,
  395. const substitute_internal::Arg& a3,
  396. const substitute_internal::Arg& a4,
  397. const substitute_internal::Arg& a5)
  398. ABSL_BAD_CALL_IF(
  399. substitute_internal::PlaceholderBitmask(format) != 63,
  400. "There were 6 substitution arguments given, but "
  401. "this format string is missing its $0-$5, contains one of "
  402. "$6-$9, or contains an unescaped $ character (use $$ instead)");
  403. void SubstituteAndAppend(
  404. std::string* output, const char* format, const substitute_internal::Arg& a0,
  405. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  406. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  407. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6)
  408. ABSL_BAD_CALL_IF(
  409. substitute_internal::PlaceholderBitmask(format) != 127,
  410. "There were 7 substitution arguments given, but "
  411. "this format string is missing its $0-$6, contains one of "
  412. "$7-$9, or contains an unescaped $ character (use $$ instead)");
  413. void SubstituteAndAppend(
  414. std::string* output, const char* format, const substitute_internal::Arg& a0,
  415. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  416. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  417. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  418. const substitute_internal::Arg& a7)
  419. ABSL_BAD_CALL_IF(
  420. substitute_internal::PlaceholderBitmask(format) != 255,
  421. "There were 8 substitution arguments given, but "
  422. "this format string is missing its $0-$7, contains one of "
  423. "$8-$9, or contains an unescaped $ character (use $$ instead)");
  424. void SubstituteAndAppend(
  425. std::string* output, const char* format, const substitute_internal::Arg& a0,
  426. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  427. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  428. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  429. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8)
  430. ABSL_BAD_CALL_IF(
  431. substitute_internal::PlaceholderBitmask(format) != 511,
  432. "There were 9 substitution arguments given, but "
  433. "this format string is missing its $0-$8, contains a $9, or "
  434. "contains an unescaped $ character (use $$ instead)");
  435. void SubstituteAndAppend(
  436. std::string* output, const char* format, const substitute_internal::Arg& a0,
  437. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  438. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  439. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  440. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
  441. const substitute_internal::Arg& a9)
  442. ABSL_BAD_CALL_IF(
  443. substitute_internal::PlaceholderBitmask(format) != 1023,
  444. "There were 10 substitution arguments given, but this "
  445. "format string either doesn't contain all of $0 through $9 or "
  446. "contains an unescaped $ character (use $$ instead)");
  447. #endif // ABSL_BAD_CALL_IF
  448. // Substitute()
  449. //
  450. // Substitutes variables into a given format string. See file comments above
  451. // for usage.
  452. //
  453. // The declarations of `Substitute()` below consist of overloads for passing 0
  454. // to 10 arguments, respectively.
  455. //
  456. // NOTE: A zero-argument `Substitute()` may be used within variadic templates to
  457. // allow a variable number of arguments.
  458. //
  459. // Example:
  460. // template <typename... Args>
  461. // void VarMsg(absl::string_view format, const Args&... args) {
  462. // std::string s = absl::Substitute(format, args...);
  463. ABSL_MUST_USE_RESULT inline std::string Substitute(absl::string_view format) {
  464. std::string result;
  465. SubstituteAndAppend(&result, format);
  466. return result;
  467. }
  468. ABSL_MUST_USE_RESULT inline std::string Substitute(
  469. absl::string_view format, const substitute_internal::Arg& a0) {
  470. std::string result;
  471. SubstituteAndAppend(&result, format, a0);
  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) {
  477. std::string result;
  478. SubstituteAndAppend(&result, format, a0, a1);
  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. std::string result;
  485. SubstituteAndAppend(&result, format, a0, a1, a2);
  486. return result;
  487. }
  488. ABSL_MUST_USE_RESULT inline std::string Substitute(
  489. absl::string_view format, const substitute_internal::Arg& a0,
  490. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  491. const substitute_internal::Arg& a3) {
  492. std::string result;
  493. SubstituteAndAppend(&result, format, a0, a1, a2, a3);
  494. return result;
  495. }
  496. ABSL_MUST_USE_RESULT inline std::string Substitute(
  497. absl::string_view format, const substitute_internal::Arg& a0,
  498. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  499. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4) {
  500. std::string result;
  501. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4);
  502. return result;
  503. }
  504. ABSL_MUST_USE_RESULT inline std::string Substitute(
  505. absl::string_view format, const substitute_internal::Arg& a0,
  506. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  507. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  508. const substitute_internal::Arg& a5) {
  509. std::string result;
  510. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5);
  511. return result;
  512. }
  513. ABSL_MUST_USE_RESULT inline std::string Substitute(
  514. absl::string_view format, const substitute_internal::Arg& a0,
  515. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  516. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  517. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6) {
  518. std::string result;
  519. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6);
  520. return result;
  521. }
  522. ABSL_MUST_USE_RESULT inline std::string Substitute(
  523. absl::string_view format, const substitute_internal::Arg& a0,
  524. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  525. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  526. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  527. const substitute_internal::Arg& a7) {
  528. std::string result;
  529. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7);
  530. return result;
  531. }
  532. ABSL_MUST_USE_RESULT inline std::string Substitute(
  533. absl::string_view format, const substitute_internal::Arg& a0,
  534. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  535. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  536. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  537. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8) {
  538. std::string result;
  539. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7, a8);
  540. return result;
  541. }
  542. ABSL_MUST_USE_RESULT inline std::string Substitute(
  543. absl::string_view format, const substitute_internal::Arg& a0,
  544. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  545. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  546. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  547. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
  548. const substitute_internal::Arg& a9) {
  549. std::string result;
  550. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
  551. return result;
  552. }
  553. #if defined(ABSL_BAD_CALL_IF)
  554. // This body of functions catches cases where the number of placeholders
  555. // doesn't match the number of data arguments.
  556. std::string Substitute(const char* format)
  557. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 0,
  558. "There were no substitution arguments "
  559. "but this format string either has a $[0-9] in it or "
  560. "contains an unescaped $ character (use $$ instead)");
  561. std::string Substitute(const char* format, const substitute_internal::Arg& a0)
  562. ABSL_BAD_CALL_IF(
  563. substitute_internal::PlaceholderBitmask(format) != 1,
  564. "There was 1 substitution argument given, but "
  565. "this format string is missing its $0, contains one of $1-$9, "
  566. "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. ABSL_BAD_CALL_IF(
  570. substitute_internal::PlaceholderBitmask(format) != 3,
  571. "There were 2 substitution arguments given, but "
  572. "this format string is missing its $0/$1, contains one of "
  573. "$2-$9, or contains an unescaped $ character (use $$ instead)");
  574. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  575. const substitute_internal::Arg& a1,
  576. const substitute_internal::Arg& a2)
  577. ABSL_BAD_CALL_IF(
  578. substitute_internal::PlaceholderBitmask(format) != 7,
  579. "There were 3 substitution arguments given, but "
  580. "this format string is missing its $0/$1/$2, contains one of "
  581. "$3-$9, or contains an unescaped $ character (use $$ instead)");
  582. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  583. const substitute_internal::Arg& a1,
  584. const substitute_internal::Arg& a2,
  585. const substitute_internal::Arg& a3)
  586. ABSL_BAD_CALL_IF(
  587. substitute_internal::PlaceholderBitmask(format) != 15,
  588. "There were 4 substitution arguments given, but "
  589. "this format string is missing its $0-$3, contains one of "
  590. "$4-$9, or contains an unescaped $ character (use $$ instead)");
  591. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  592. const substitute_internal::Arg& a1,
  593. const substitute_internal::Arg& a2,
  594. const substitute_internal::Arg& a3,
  595. const substitute_internal::Arg& a4)
  596. ABSL_BAD_CALL_IF(
  597. substitute_internal::PlaceholderBitmask(format) != 31,
  598. "There were 5 substitution arguments given, but "
  599. "this format string is missing its $0-$4, contains one of "
  600. "$5-$9, or contains an unescaped $ character (use $$ instead)");
  601. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  602. const substitute_internal::Arg& a1,
  603. const substitute_internal::Arg& a2,
  604. const substitute_internal::Arg& a3,
  605. const substitute_internal::Arg& a4,
  606. const substitute_internal::Arg& a5)
  607. ABSL_BAD_CALL_IF(
  608. substitute_internal::PlaceholderBitmask(format) != 63,
  609. "There were 6 substitution arguments given, but "
  610. "this format string is missing its $0-$5, contains one of "
  611. "$6-$9, or contains an unescaped $ character (use $$ instead)");
  612. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  613. const substitute_internal::Arg& a1,
  614. const substitute_internal::Arg& a2,
  615. const substitute_internal::Arg& a3,
  616. const substitute_internal::Arg& a4,
  617. const substitute_internal::Arg& a5,
  618. const substitute_internal::Arg& a6)
  619. ABSL_BAD_CALL_IF(
  620. substitute_internal::PlaceholderBitmask(format) != 127,
  621. "There were 7 substitution arguments given, but "
  622. "this format string is missing its $0-$6, contains one of "
  623. "$7-$9, or contains an unescaped $ character (use $$ instead)");
  624. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  625. const substitute_internal::Arg& a1,
  626. const substitute_internal::Arg& a2,
  627. const substitute_internal::Arg& a3,
  628. const substitute_internal::Arg& a4,
  629. const substitute_internal::Arg& a5,
  630. const substitute_internal::Arg& a6,
  631. const substitute_internal::Arg& a7)
  632. ABSL_BAD_CALL_IF(
  633. substitute_internal::PlaceholderBitmask(format) != 255,
  634. "There were 8 substitution arguments given, but "
  635. "this format string is missing its $0-$7, contains one of "
  636. "$8-$9, or contains an unescaped $ character (use $$ instead)");
  637. std::string Substitute(
  638. const char* format, const substitute_internal::Arg& a0,
  639. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  640. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  641. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  642. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8)
  643. ABSL_BAD_CALL_IF(
  644. substitute_internal::PlaceholderBitmask(format) != 511,
  645. "There were 9 substitution arguments given, but "
  646. "this format string is missing its $0-$8, contains a $9, or "
  647. "contains an unescaped $ character (use $$ instead)");
  648. std::string Substitute(
  649. const char* format, const substitute_internal::Arg& a0,
  650. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  651. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  652. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  653. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
  654. const substitute_internal::Arg& a9)
  655. ABSL_BAD_CALL_IF(
  656. substitute_internal::PlaceholderBitmask(format) != 1023,
  657. "There were 10 substitution arguments given, but this "
  658. "format string either doesn't contain all of $0 through $9 or "
  659. "contains an unescaped $ character (use $$ instead)");
  660. #endif // ABSL_BAD_CALL_IF
  661. ABSL_NAMESPACE_END
  662. } // namespace absl
  663. #endif // ABSL_STRINGS_SUBSTITUTE_H_