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