1*6777b538SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_STRINGS_STRINGPRINTF_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_STRINGS_STRINGPRINTF_H_
7*6777b538SAndroid Build Coastguard Worker
8*6777b538SAndroid Build Coastguard Worker #include <stdarg.h> // va_list
9*6777b538SAndroid Build Coastguard Worker
10*6777b538SAndroid Build Coastguard Worker #include <string>
11*6777b538SAndroid Build Coastguard Worker #include <string_view>
12*6777b538SAndroid Build Coastguard Worker
13*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
14*6777b538SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
15*6777b538SAndroid Build Coastguard Worker
16*6777b538SAndroid Build Coastguard Worker namespace base {
17*6777b538SAndroid Build Coastguard Worker
18*6777b538SAndroid Build Coastguard Worker // Returns a C++ string given `printf()`-like input. The format string should be
19*6777b538SAndroid Build Coastguard Worker // a compile-time constant (like with `std::format()`).
20*6777b538SAndroid Build Coastguard Worker // TODO(crbug.com/1371963): Implement in terms of `std::format()`,
21*6777b538SAndroid Build Coastguard Worker // `absl::StrFormat()`, or similar.
22*6777b538SAndroid Build Coastguard Worker [[nodiscard]] BASE_EXPORT std::string StringPrintf(const char* format, ...)
23*6777b538SAndroid Build Coastguard Worker PRINTF_FORMAT(1, 2);
24*6777b538SAndroid Build Coastguard Worker
25*6777b538SAndroid Build Coastguard Worker // Returns a C++ string given `printf()`-like input. The format string must be a
26*6777b538SAndroid Build Coastguard Worker // run-time value (like with `std::vformat()`), or this will not compile.
27*6777b538SAndroid Build Coastguard Worker // Because this does not check arguments at compile-time, prefer
28*6777b538SAndroid Build Coastguard Worker // `StringPrintf()` whenever possible.
29*6777b538SAndroid Build Coastguard Worker template <typename... Args>
StringPrintfNonConstexpr(std::string_view format,const Args &...args)30*6777b538SAndroid Build Coastguard Worker [[nodiscard]] std::string StringPrintfNonConstexpr(std::string_view format,
31*6777b538SAndroid Build Coastguard Worker const Args&... args) {
32*6777b538SAndroid Build Coastguard Worker // TODO(crbug.com/1371963): Implement in terms of `std::vformat()`,
33*6777b538SAndroid Build Coastguard Worker // `absl::FormatUntyped()`, or similar.
34*6777b538SAndroid Build Coastguard Worker return StringPrintf(std::string(format).c_str(), args...);
35*6777b538SAndroid Build Coastguard Worker }
36*6777b538SAndroid Build Coastguard Worker
37*6777b538SAndroid Build Coastguard Worker // If possible, guide users to use `StringPrintf()` instead of
38*6777b538SAndroid Build Coastguard Worker // `StringPrintfNonConstexpr()` when the format string is constexpr.
39*6777b538SAndroid Build Coastguard Worker //
40*6777b538SAndroid Build Coastguard Worker // It would be nice to do this with `std::enable_if`, but I don't know of a way;
41*6777b538SAndroid Build Coastguard Worker // whether a string constant's value is available at compile time is not
42*6777b538SAndroid Build Coastguard Worker // something easily obtained from the type system, and trying to pass various
43*6777b538SAndroid Build Coastguard Worker // forms of string constant to non-type template parameters produces a variety
44*6777b538SAndroid Build Coastguard Worker // of compile errors.
45*6777b538SAndroid Build Coastguard Worker #if HAS_ATTRIBUTE(enable_if)
46*6777b538SAndroid Build Coastguard Worker // Disable calling with a constexpr `std::string_view`.
47*6777b538SAndroid Build Coastguard Worker template <typename... Args>
48*6777b538SAndroid Build Coastguard Worker [[nodiscard]] std::string StringPrintfNonConstexpr(std::string_view format,
49*6777b538SAndroid Build Coastguard Worker const Args&... args)
50*6777b538SAndroid Build Coastguard Worker __attribute__((enable_if(
51*6777b538SAndroid Build Coastguard Worker [](std::string_view s) { return s.empty() || s[0] == s[0]; }(format),
52*6777b538SAndroid Build Coastguard Worker "Use StringPrintf() for constexpr format strings"))) = delete;
53*6777b538SAndroid Build Coastguard Worker // Disable calling with a constexpr `char[]` or `char*`.
54*6777b538SAndroid Build Coastguard Worker template <typename... Args>
55*6777b538SAndroid Build Coastguard Worker [[nodiscard]] std::string StringPrintfNonConstexpr(const char* format,
56*6777b538SAndroid Build Coastguard Worker const Args&... args)
57*6777b538SAndroid Build Coastguard Worker __attribute__((
58*6777b538SAndroid Build Coastguard Worker enable_if([](const char* s) { return !!s; }(format),
59*6777b538SAndroid Build Coastguard Worker "Use StringPrintf() for constexpr format strings"))) = delete;
60*6777b538SAndroid Build Coastguard Worker #endif
61*6777b538SAndroid Build Coastguard Worker
62*6777b538SAndroid Build Coastguard Worker // Returns a C++ string given `vprintf()`-like input.
63*6777b538SAndroid Build Coastguard Worker [[nodiscard]] BASE_EXPORT std::string StringPrintV(const char* format,
64*6777b538SAndroid Build Coastguard Worker va_list ap)
65*6777b538SAndroid Build Coastguard Worker PRINTF_FORMAT(1, 0);
66*6777b538SAndroid Build Coastguard Worker
67*6777b538SAndroid Build Coastguard Worker // Like `StringPrintf()`, but appends result to a supplied string.
68*6777b538SAndroid Build Coastguard Worker // TODO(crbug.com/1371963): Implement in terms of `std::format_to()`,
69*6777b538SAndroid Build Coastguard Worker // `absl::StrAppendFormat()`, or similar.
70*6777b538SAndroid Build Coastguard Worker BASE_EXPORT void StringAppendF(std::string* dst, const char* format, ...)
71*6777b538SAndroid Build Coastguard Worker PRINTF_FORMAT(2, 3);
72*6777b538SAndroid Build Coastguard Worker
73*6777b538SAndroid Build Coastguard Worker // Like `StringPrintV()`, but appends result to a supplied string.
74*6777b538SAndroid Build Coastguard Worker BASE_EXPORT void StringAppendV(std::string* dst, const char* format, va_list ap)
75*6777b538SAndroid Build Coastguard Worker PRINTF_FORMAT(2, 0);
76*6777b538SAndroid Build Coastguard Worker
77*6777b538SAndroid Build Coastguard Worker } // namespace base
78*6777b538SAndroid Build Coastguard Worker
79*6777b538SAndroid Build Coastguard Worker #endif // BASE_STRINGS_STRINGPRINTF_H_
80