substitute.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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(substitute_internal::PlaceholderBitmask(format) != 0,
  335. "There were no substitution arguments "
  336. "but this format string has a $[0-9] in it");
  337. void SubstituteAndAppend(std::string* output, const char* format,
  338. const substitute_internal::Arg& a0)
  339. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1,
  340. "There was 1 substitution argument given, but "
  341. "this format string is either missing its $0, or "
  342. "contains one of $1-$9");
  343. void SubstituteAndAppend(std::string* output, const char* format,
  344. const substitute_internal::Arg& a0,
  345. const substitute_internal::Arg& a1)
  346. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 3,
  347. "There were 2 substitution arguments given, but "
  348. "this format string is either missing its $0/$1, or "
  349. "contains one of $2-$9");
  350. void SubstituteAndAppend(std::string* output, const char* format,
  351. const substitute_internal::Arg& a0,
  352. const substitute_internal::Arg& a1,
  353. const substitute_internal::Arg& a2)
  354. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 7,
  355. "There were 3 substitution arguments given, but "
  356. "this format string is either missing its $0/$1/$2, or "
  357. "contains one of $3-$9");
  358. void SubstituteAndAppend(std::string* output, const char* format,
  359. const substitute_internal::Arg& a0,
  360. const substitute_internal::Arg& a1,
  361. const substitute_internal::Arg& a2,
  362. const substitute_internal::Arg& a3)
  363. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 15,
  364. "There were 4 substitution arguments given, but "
  365. "this format string is either missing its $0-$3, or "
  366. "contains one of $4-$9");
  367. void SubstituteAndAppend(std::string* output, const char* format,
  368. const substitute_internal::Arg& a0,
  369. const substitute_internal::Arg& a1,
  370. const substitute_internal::Arg& a2,
  371. const substitute_internal::Arg& a3,
  372. const substitute_internal::Arg& a4)
  373. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 31,
  374. "There were 5 substitution arguments given, but "
  375. "this format string is either missing its $0-$4, or "
  376. "contains one of $5-$9");
  377. void SubstituteAndAppend(std::string* output, const char* format,
  378. const substitute_internal::Arg& a0,
  379. const substitute_internal::Arg& a1,
  380. const substitute_internal::Arg& a2,
  381. const substitute_internal::Arg& a3,
  382. const substitute_internal::Arg& a4,
  383. const substitute_internal::Arg& a5)
  384. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 63,
  385. "There were 6 substitution arguments given, but "
  386. "this format string is either missing its $0-$5, or "
  387. "contains one of $6-$9");
  388. void SubstituteAndAppend(
  389. std::string* output, const char* format, const substitute_internal::Arg& a0,
  390. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  391. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  392. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6)
  393. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 127,
  394. "There were 7 substitution arguments given, but "
  395. "this format string is either missing its $0-$6, or "
  396. "contains one of $7-$9");
  397. void SubstituteAndAppend(
  398. std::string* output, const char* format, const substitute_internal::Arg& a0,
  399. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  400. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  401. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  402. const substitute_internal::Arg& a7)
  403. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 255,
  404. "There were 8 substitution arguments given, but "
  405. "this format string is either missing its $0-$7, or "
  406. "contains one of $8-$9");
  407. void SubstituteAndAppend(
  408. std::string* output, const char* format, const substitute_internal::Arg& a0,
  409. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  410. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  411. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  412. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8)
  413. ABSL_BAD_CALL_IF(
  414. substitute_internal::PlaceholderBitmask(format) != 511,
  415. "There were 9 substitution arguments given, but "
  416. "this format string is either missing its $0-$8, or contains a $9");
  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. const substitute_internal::Arg& a9)
  424. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1023,
  425. "There were 10 substitution arguments given, but this "
  426. "format string doesn't contain all of $0 through $9");
  427. #endif // ABSL_BAD_CALL_IF
  428. // Substitute()
  429. //
  430. // Substitutes variables into a given format string. See file comments above
  431. // for usage.
  432. //
  433. // The declarations of `Substitute()` below consist of overloads for passing 0
  434. // to 10 arguments, respectively.
  435. //
  436. // NOTE: A zero-argument `Substitute()` may be used within variadic templates to
  437. // allow a variable number of arguments.
  438. //
  439. // Example:
  440. // template <typename... Args>
  441. // void VarMsg(absl::string_view format, const Args&... args) {
  442. // std::string s = absl::Substitute(format, args...);
  443. ABSL_MUST_USE_RESULT inline std::string Substitute(absl::string_view format) {
  444. std::string result;
  445. SubstituteAndAppend(&result, format);
  446. return result;
  447. }
  448. ABSL_MUST_USE_RESULT inline std::string Substitute(
  449. absl::string_view format, const substitute_internal::Arg& a0) {
  450. std::string result;
  451. SubstituteAndAppend(&result, format, a0);
  452. return result;
  453. }
  454. ABSL_MUST_USE_RESULT inline std::string Substitute(
  455. absl::string_view format, const substitute_internal::Arg& a0,
  456. const substitute_internal::Arg& a1) {
  457. std::string result;
  458. SubstituteAndAppend(&result, format, a0, a1);
  459. return result;
  460. }
  461. ABSL_MUST_USE_RESULT inline std::string Substitute(
  462. absl::string_view format, const substitute_internal::Arg& a0,
  463. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2) {
  464. std::string result;
  465. SubstituteAndAppend(&result, format, a0, a1, a2);
  466. return result;
  467. }
  468. ABSL_MUST_USE_RESULT inline std::string Substitute(
  469. absl::string_view format, const substitute_internal::Arg& a0,
  470. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  471. const substitute_internal::Arg& a3) {
  472. std::string result;
  473. SubstituteAndAppend(&result, format, a0, a1, a2, a3);
  474. return result;
  475. }
  476. ABSL_MUST_USE_RESULT inline std::string Substitute(
  477. absl::string_view format, const substitute_internal::Arg& a0,
  478. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  479. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4) {
  480. std::string result;
  481. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4);
  482. return result;
  483. }
  484. ABSL_MUST_USE_RESULT inline std::string Substitute(
  485. absl::string_view format, const substitute_internal::Arg& a0,
  486. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  487. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  488. const substitute_internal::Arg& a5) {
  489. std::string result;
  490. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5);
  491. return result;
  492. }
  493. ABSL_MUST_USE_RESULT inline std::string Substitute(
  494. absl::string_view format, const substitute_internal::Arg& a0,
  495. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  496. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  497. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6) {
  498. std::string result;
  499. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6);
  500. return result;
  501. }
  502. ABSL_MUST_USE_RESULT inline std::string Substitute(
  503. absl::string_view format, const substitute_internal::Arg& a0,
  504. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  505. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  506. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  507. const substitute_internal::Arg& a7) {
  508. std::string result;
  509. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7);
  510. return result;
  511. }
  512. ABSL_MUST_USE_RESULT inline std::string Substitute(
  513. absl::string_view format, const substitute_internal::Arg& a0,
  514. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  515. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  516. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  517. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8) {
  518. std::string result;
  519. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7, a8);
  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, const substitute_internal::Arg& a8,
  528. const substitute_internal::Arg& a9) {
  529. std::string result;
  530. SubstituteAndAppend(&result, format, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
  531. return result;
  532. }
  533. #if defined(ABSL_BAD_CALL_IF)
  534. // This body of functions catches cases where the number of placeholders
  535. // doesn't match the number of data arguments.
  536. std::string Substitute(const char* format)
  537. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 0,
  538. "There were no substitution arguments "
  539. "but this format string has a $[0-9] in it");
  540. std::string Substitute(const char* format, const substitute_internal::Arg& a0)
  541. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1,
  542. "There was 1 substitution argument given, but "
  543. "this format string is either missing its $0, or "
  544. "contains one of $1-$9");
  545. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  546. const substitute_internal::Arg& a1)
  547. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 3,
  548. "There were 2 substitution arguments given, but "
  549. "this format string is either missing its $0/$1, or "
  550. "contains one of $2-$9");
  551. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  552. const substitute_internal::Arg& a1,
  553. const substitute_internal::Arg& a2)
  554. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 7,
  555. "There were 3 substitution arguments given, but "
  556. "this format string is either missing its $0/$1/$2, or "
  557. "contains one of $3-$9");
  558. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  559. const substitute_internal::Arg& a1,
  560. const substitute_internal::Arg& a2,
  561. const substitute_internal::Arg& a3)
  562. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 15,
  563. "There were 4 substitution arguments given, but "
  564. "this format string is either missing its $0-$3, or "
  565. "contains one of $4-$9");
  566. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  567. const substitute_internal::Arg& a1,
  568. const substitute_internal::Arg& a2,
  569. const substitute_internal::Arg& a3,
  570. const substitute_internal::Arg& a4)
  571. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 31,
  572. "There were 5 substitution arguments given, but "
  573. "this format string is either missing its $0-$4, or "
  574. "contains one of $5-$9");
  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. const substitute_internal::Arg& a4,
  580. const substitute_internal::Arg& a5)
  581. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 63,
  582. "There were 6 substitution arguments given, but "
  583. "this format string is either missing its $0-$5, or "
  584. "contains one of $6-$9");
  585. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  586. const substitute_internal::Arg& a1,
  587. const substitute_internal::Arg& a2,
  588. const substitute_internal::Arg& a3,
  589. const substitute_internal::Arg& a4,
  590. const substitute_internal::Arg& a5,
  591. const substitute_internal::Arg& a6)
  592. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 127,
  593. "There were 7 substitution arguments given, but "
  594. "this format string is either missing its $0-$6, or "
  595. "contains one of $7-$9");
  596. std::string Substitute(const char* format, const substitute_internal::Arg& a0,
  597. const substitute_internal::Arg& a1,
  598. const substitute_internal::Arg& a2,
  599. const substitute_internal::Arg& a3,
  600. const substitute_internal::Arg& a4,
  601. const substitute_internal::Arg& a5,
  602. const substitute_internal::Arg& a6,
  603. const substitute_internal::Arg& a7)
  604. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 255,
  605. "There were 8 substitution arguments given, but "
  606. "this format string is either missing its $0-$7, or "
  607. "contains one of $8-$9");
  608. std::string Substitute(
  609. const char* format, const substitute_internal::Arg& a0,
  610. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  611. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  612. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  613. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8)
  614. ABSL_BAD_CALL_IF(
  615. substitute_internal::PlaceholderBitmask(format) != 511,
  616. "There were 9 substitution arguments given, but "
  617. "this format string is either missing its $0-$8, or contains a $9");
  618. std::string Substitute(
  619. const char* format, const substitute_internal::Arg& a0,
  620. const substitute_internal::Arg& a1, const substitute_internal::Arg& a2,
  621. const substitute_internal::Arg& a3, const substitute_internal::Arg& a4,
  622. const substitute_internal::Arg& a5, const substitute_internal::Arg& a6,
  623. const substitute_internal::Arg& a7, const substitute_internal::Arg& a8,
  624. const substitute_internal::Arg& a9)
  625. ABSL_BAD_CALL_IF(substitute_internal::PlaceholderBitmask(format) != 1023,
  626. "There were 10 substitution arguments given, but this "
  627. "format string doesn't contain all of $0 through $9");
  628. #endif // ABSL_BAD_CALL_IF
  629. ABSL_NAMESPACE_END
  630. } // namespace absl
  631. #endif // ABSL_STRINGS_SUBSTITUTE_H_