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