Skip to content

Commit 9a5c8ef

Browse files
committed
src: Add enum handle for ToStringHelper + formatting
Fixes: #56666
1 parent 304bb9c commit 9a5c8ef

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/debug_utils-inl.h

+24-21
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ namespace node {
1313

1414
struct ToStringHelper {
1515
template <typename T>
16-
static std::string Convert(
17-
const T& value,
18-
std::string(T::* to_string)() const = &T::ToString) {
16+
static std::string Convert(const T& value,
17+
std::string (T::*to_string)()
18+
const = &T::ToString) {
1919
return (value.*to_string)();
2020
}
2121
template <typename T,
2222
typename test_for_number = typename std::
23-
enable_if<std::is_arithmetic<T>::value, bool>::type,
23+
enable_if_t<std::is_arithmetic_v<T> || std::is_enum_v<T>, bool>,
2424
typename dummy = bool>
25-
static std::string Convert(const T& value) { return std::to_string(value); }
25+
static std::string Convert(const T& value) {
26+
return std::to_string(value);
27+
}
2628
static std::string Convert(const char* value) {
2729
return value != nullptr ? value : "(null)";
2830
}
@@ -42,8 +44,7 @@ struct ToStringHelper {
4244
const char* digits = "0123456789abcdef";
4345
do {
4446
unsigned digit = v & ((1 << BASE_BITS) - 1);
45-
*--ptr =
46-
(BASE_BITS < 4 ? static_cast<char>('0' + digit) : digits[digit]);
47+
*--ptr = (BASE_BITS < 4 ? static_cast<char>('0' + digit) : digits[digit]);
4748
} while ((v >>= BASE_BITS) != 0);
4849
return ptr;
4950
}
@@ -76,22 +77,25 @@ inline std::string SPrintFImpl(const char* format) {
7677

7778
template <typename Arg, typename... Args>
7879
std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
79-
const char* format, Arg&& arg, Args&&... args) {
80+
const char* format,
81+
Arg&& arg,
82+
Args&&... args) {
8083
const char* p = strchr(format, '%');
8184
CHECK_NOT_NULL(p); // If you hit this, you passed in too many arguments.
8285
std::string ret(format, p);
8386
// Ignore long / size_t modifiers
84-
while (strchr("lz", *++p) != nullptr) {}
87+
while (strchr("lz", *++p) != nullptr) {
88+
}
8589
switch (*p) {
8690
case '%': {
87-
return ret + '%' + SPrintFImpl(p + 1,
88-
std::forward<Arg>(arg),
89-
std::forward<Args>(args)...);
91+
return ret + '%' +
92+
SPrintFImpl(
93+
p + 1, std::forward<Arg>(arg), std::forward<Args>(args)...);
9094
}
9195
default: {
92-
return ret + '%' + SPrintFImpl(p,
93-
std::forward<Arg>(arg),
94-
std::forward<Args>(args)...);
96+
return ret + '%' +
97+
SPrintFImpl(
98+
p, std::forward<Arg>(arg), std::forward<Args>(args)...);
9599
}
96100
case 'd':
97101
case 'i':
@@ -109,12 +113,10 @@ std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
109113
ret += node::ToUpper(ToBaseString<4>(arg));
110114
break;
111115
case 'p': {
112-
CHECK(std::is_pointer<typename std::remove_reference<Arg>::type>::value);
116+
CHECK(std::is_pointer_v<typename std::remove_reference_t<Arg>>);
113117
char out[20];
114-
int n = snprintf(out,
115-
sizeof(out),
116-
"%p",
117-
*reinterpret_cast<const void* const*>(&arg));
118+
int n = snprintf(
119+
out, sizeof(out), "%p", *reinterpret_cast<const void* const*>(&arg));
118120
CHECK_GE(n, 0);
119121
ret += out;
120122
break;
@@ -125,7 +127,8 @@ std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
125127

126128
template <typename... Args>
127129
std::string COLD_NOINLINE SPrintF( // NOLINT(runtime/string)
128-
const char* format, Args&&... args) {
130+
const char* format,
131+
Args&&... args) {
129132
return SPrintFImpl(format, std::forward<Args>(args)...);
130133
}
131134

0 commit comments

Comments
 (0)