5#ifndef V8_BASE_STRING_FORMAT_H_
6#define V8_BASE_STRING_FORMAT_H_
23template <
const std::string_view&... strs>
26 constexpr size_t kArraySize = (1 + ... + strs.size());
27 std::array<char, kArraySize> arr{};
28 char* ptr = arr.data();
29 for (
auto str : std::initializer_list<std::string_view>{strs...}) {
30 for (
auto c : str) *ptr++ = c;
43 static constexpr std::string_view
string_view = {array.data(),
49 std::is_bounded_array_v<T> &&
51 std::remove_cv_t<std::remove_pointer_t<std::decay_t<T>>>>;
60 requires std::is_integral_v<I>
62 static_assert(
sizeof(
I) == 4 ||
sizeof(
I) == 8);
63 constexpr static bool kIs64Bit =
sizeof(
I) == 8;
64 constexpr static bool kIsSigned = std::is_signed_v<I>;
65 static constexpr int kMaxLen = (kIs64Bit ? 20 : 10) + (kIsSigned ? 1 : 0);
66 static constexpr std::string_view kFormats[2][2]{{
"%" PRIu32,
"%" PRId32},
67 {
"%" PRIu64,
"%" PRId64}};
68 static constexpr std::string_view kFormatPart = kFormats[kIs64Bit][kIsSigned];
71 std::conditional_t<kIs64Bit,
72 std::conditional_t<kIsSigned, int64_t, uint64_t>,
73 std::conditional_t<kIsSigned, int32_t, uint32_t>>;
80 static constexpr size_t kCharArraySize = std::extent_v<S>;
82 static_assert(kCharArraySize >= 1,
"Do not print (static) empty strings");
83 static_assert(kCharArraySize <= 128,
"Do not include huge strings");
84 static constexpr int kMaxLen = kCharArraySize - 1;
85 static constexpr std::string_view kFormatPart =
"%s";
90template <
const std::string_view& kFormat,
int kMaxLen,
typename... Parts>
92 std::array<char, kMaxLen> message;
94 static_assert(kMaxLen > 0);
97 "Don't generate overly large strings; this limit can be increased, but "
98 "consider that the array lives on the stack of the caller.");
102 static_assert((kFormat.size() == 0) == (
sizeof...(Parts) == 0));
103 if constexpr (kFormat.size() == 0) {
106 int characters = base::OS::SNPrintF(message.data(), kMaxLen, kFormat.data(),
108 CHECK(characters >= 0 && characters < kMaxLen);
136template <
typename... Ts>
138 template <
typename T>
141 static_assert(std::conjunction_v<std::is_trivial<Part<Ts>>...>,
142 "All parts needs to be trivial to guarantee optimal code");
150 static_assert(
sizeof...(Ts) == 0,
151 "Only explicitly construct empty FormattedString, use "
152 "operator<< to appending");
158 template <
typename T>
160 using PlainT = std::remove_cv_t<std::remove_reference_t<T>>;
175 template <
typename... Us>
std::array< char, kMaxLen > PrintFormattedStringToArray(Parts... parts)
FormattedString() -> FormattedString<>
#define I(name, number_of_args, result_size)
#define DCHECK_EQ(v1, v2)
static constexpr auto JoinIntoNullTerminatedArray() noexcept
static constexpr std::string_view string_view
#define V8_WARN_UNUSED_RESULT