Skip to content

Commit 7bdd29f

Browse files
addaleaxcodebytere
authored andcommitted
src: add C++-style sprintf utility
Add an utility that handles C++-style strings and objects well. PR-URL: #31446Fixes: #28761Fixes: #31218 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 393b48e commit 7bdd29f

18 files changed

+165
-18
lines changed

‎node.gyp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@
609609
'src/connect_wrap.h',
610610
'src/connection_wrap.h',
611611
'src/debug_utils.h',
612+
'src/debug_utils-inl.h',
612613
'src/env.h',
613614
'src/env-inl.h',
614615
'src/handle_wrap.h',

‎src/debug_utils-inl.h‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#ifndef SRC_DEBUG_UTILS_INL_H_
2+
#defineSRC_DEBUG_UTILS_INL_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include"debug_utils.h"
7+
8+
#include<type_traits>
9+
10+
namespacenode{
11+
12+
structToStringHelper{
13+
template <typename T>
14+
static std::string Convert(
15+
const T& value,
16+
std::string(T::* to_string)() const = &T::ToString){
17+
return (value.*to_string)();
18+
}
19+
template <typename T,
20+
typename test_for_number = typename std::
21+
enable_if<std::is_arithmetic<T>::value, bool>::type,
22+
typename dummy = bool>
23+
static std::string Convert(const T& value){returnstd::to_string(value)}
24+
static std::string Convert(constchar* value){return value}
25+
static std::string Convert(const std::string& value){return value}
26+
static std::string Convert(bool value){return value ? "true" : "false"}
27+
};
28+
29+
template <typename T>
30+
std::string ToString(const T& value){
31+
returnToStringHelper::Convert(value);
32+
}
33+
34+
inline std::string SPrintFImpl(constchar* format){
35+
constchar* p = strchr(format, '%');
36+
if (LIKELY(p == nullptr)) return format;
37+
CHECK_EQ(p[1], '%'); // Only '%%' allowed when there are no arguments.
38+
39+
returnstd::string(format, p + 1) + SPrintFImpl(p + 2);
40+
}
41+
42+
template <typename Arg, typename... Args>
43+
std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
44+
constchar* format, Arg&& arg, Args&&... args){
45+
constchar* p = strchr(format, '%');
46+
CHECK_NOT_NULL(p); // If you hit this, you passed in too many arguments.
47+
std::string ret(format, p);
48+
// Ignore long / size_t modifiers
49+
while (strchr("lz", *++p) != nullptr){}
50+
switch (*p){
51+
case'%':{
52+
return ret + '%' + SPrintFImpl(p + 1,
53+
std::forward<Arg>(arg),
54+
std::forward<Args>(args)...);
55+
}
56+
default:{
57+
return ret + '%' + SPrintFImpl(p,
58+
std::forward<Arg>(arg),
59+
std::forward<Args>(args)...);
60+
}
61+
case'd':
62+
case'i':
63+
case'u':
64+
case's': ret += ToString(arg); break;
65+
case'p':{
66+
CHECK(std::is_pointer<typename std::remove_reference<Arg>::type>::value);
67+
char out[20];
68+
int n = snprintf(out,
69+
sizeof(out),
70+
"%p",
71+
*reinterpret_cast<constvoid* const*>(&arg));
72+
CHECK_GE(n, 0);
73+
ret += out;
74+
break;
75+
}
76+
}
77+
return ret + SPrintFImpl(p + 1, std::forward<Args>(args)...);
78+
}
79+
80+
template <typename... Args>
81+
std::string COLD_NOINLINE SPrintF( // NOLINT(runtime/string)
82+
constchar* format, Args&&... args){
83+
returnSPrintFImpl(format, std::forward<Args>(args)...);
84+
}
85+
86+
} // namespace node
87+
88+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
89+
90+
#endif // SRC_DEBUG_UTILS_INL_H_

‎src/debug_utils.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include"debug_utils.h"
1+
#include"debug_utils-inl.h"// NOLINT(build/include)
22
#include"env-inl.h"
33

44
#ifdef __POSIX__

‎src/debug_utils.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,26 @@
2222

2323
namespacenode{
2424

25+
template <typename T>
26+
inline std::string ToString(const T& value);
27+
28+
// C++-style variant of sprintf() that:
29+
// - Returns an std::string
30+
// - Handles \0 bytes correctly
31+
// - Supports %p and %s. %d, %i and %u are aliases for %s.
32+
// - Accepts any class that has a ToString() method for stringification.
33+
template <typename... Args>
34+
inline std::string SPrintF(constchar* format, Args&&... args);
35+
2536
template <typename... Args>
2637
inlinevoid FORCE_INLINE Debug(Environment* env,
2738
DebugCategory cat,
2839
constchar* format,
2940
Args&&... args){
3041
if (!UNLIKELY(env->debug_enabled(cat)))
3142
return;
32-
fprintf(stderr, format, std::forward<Args>(args)...);
43+
std::string out = SPrintF(format, std::forward<Args>(args)...);
44+
fwrite(out.data(), out.size(), 1, stderr);
3345
}
3446

3547
inlinevoid FORCE_INLINE Debug(Environment* env,

‎src/inspector_io.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include"inspector/main_thread_interface.h"
55
#include"inspector/node_string.h"
66
#include"base_object-inl.h"
7-
#include"debug_utils.h"
7+
#include"debug_utils-inl.h"
88
#include"node.h"
99
#include"node_crypto.h"
1010
#include"node_internals.h"

‎src/inspector_profiler.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include"inspector_profiler.h"
22
#include"base_object-inl.h"
3-
#include"debug_utils.h"
3+
#include"debug_utils-inl.h"
44
#include"diagnosticfilename-inl.h"
55
#include"memory_tracker-inl.h"
66
#include"node_file.h"

‎src/node.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
// ========== local headers ==========
2525

26-
#include"debug_utils.h"
26+
#include"debug_utils-inl.h"
2727
#include"env-inl.h"
2828
#include"memory_tracker-inl.h"
2929
#include"node_binding.h"

‎src/node_http2.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include"aliased_buffer.h"
2-
#include"debug_utils.h"
2+
#include"debug_utils-inl.h"
33
#include"memory_tracker-inl.h"
44
#include"node.h"
55
#include"node_buffer.h"
@@ -1967,7 +1967,7 @@ std::string Http2Stream::diagnostic_name() const{
19671967

19681968
// Notify the Http2Stream that a new block of HEADERS is being processed.
19691969
voidHttp2Stream::StartHeaders(nghttp2_headers_category category){
1970-
Debug(this, "starting headers, category: %d", id_, category);
1970+
Debug(this, "starting headers, category: %d", category);
19711971
CHECK(!this->IsDestroyed());
19721972
session_->DecrementCurrentSessionMemory(current_headers_length_);
19731973
current_headers_length_ = 0;
@@ -2220,7 +2220,7 @@ int Http2Stream::DoWrite(WriteWrap* req_wrap,
22202220
req_wrap->Done(UV_EOF);
22212221
return0;
22222222
}
2223-
Debug(this, "queuing %d buffers to send", id_, nbufs);
2223+
Debug(this, "queuing %d buffers to send", nbufs);
22242224
for (size_t i = 0; i < nbufs; ++i){
22252225
// Store the req_wrap on the last write info in the queue, so that it is
22262226
// only marked as finished once all buffers associated with it are finished.

‎src/node_messaging.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include"node_messaging.h"
22

33
#include"async_wrap-inl.h"
4-
#include"debug_utils.h"
4+
#include"debug_utils-inl.h"
55
#include"memory_tracker-inl.h"
66
#include"node_contextify.h"
77
#include"node_buffer.h"

‎src/node_platform.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include"node_internals.h"
33

44
#include"env-inl.h"
5-
#include"debug_utils.h"
5+
#include"debug_utils-inl.h"
66
#include<algorithm>// find_if(), find(), move()
77
#include<cmath>// llround()
88
#include<memory>// unique_ptr(), shared_ptr(), make_shared()

0 commit comments

Comments
(0)