Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c3be122

Browse filesBrowse files
addaleaxaduh95
authored andcommitted
src: allow std::string_view arguments to SPrintF() and friends
Modernize the code so there is no need to work with raw C strings anymore. PR-URL: #60058 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 764d356 commit c3be122
Copy full SHA for c3be122

File tree

Expand file treeCollapse file tree

3 files changed

+44
-32
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+44
-32
lines changed
Open diff view settings
Collapse file

‎src/debug_utils-inl.h‎

Copy file name to clipboardExpand all lines: src/debug_utils-inl.h
+34-21Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,42 @@ std::string ToBaseString(const T& value) {
6565
return ToStringHelper::BaseConvert<BASE_BITS>(value);
6666
}
6767

68-
inline std::string SPrintFImpl(const char* format) {
69-
const char* p = strchr(format, '%');
70-
if (p == nullptr) [[unlikely]]
71-
return format;
72-
CHECK_EQ(p[1], '%'); // Only '%%' allowed when there are no arguments.
68+
inline std::string SPrintFImpl(std::string_view format) {
69+
auto offset = format.find('%');
70+
if (offset == std::string_view::npos) return std::string(format);
71+
CHECK_LT(offset + 1, format.size());
72+
CHECK_EQ(format[offset + 1],
73+
'%'); // Only '%%' allowed when there are no arguments.
7374

74-
return std::string(format, p + 1) + SPrintFImpl(p + 2);
75+
return std::string(format.substr(0, offset + 1)) +
76+
SPrintFImpl(format.substr(offset + 2));
7577
}
7678

7779
template <typename Arg, typename... Args>
7880
std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
79-
const char* format, Arg&& arg, Args&&... args) {
80-
const char* p = strchr(format, '%');
81-
CHECK_NOT_NULL(p); // If you hit this, you passed in too many arguments.
82-
std::string ret(format, p);
81+
std::string_view format,
82+
Arg&& arg,
83+
Args&&... args) {
84+
auto offset = format.find('%');
85+
CHECK_NE(offset, std::string_view::npos); // If you hit this, you passed in
86+
// too many arguments.
87+
std::string ret(format.substr(0, offset));
8388
// Ignore long / size_t modifiers
84-
while (strchr("lz", *++p) != nullptr) {}
85-
switch (*p) {
89+
while (++offset < format.size() &&
90+
(format[offset] == 'l' || format[offset] == 'z')) {
91+
}
92+
switch (offset == format.size() ? '\0' : format[offset]) {
8693
case '%': {
87-
return ret + '%' + SPrintFImpl(p + 1,
88-
std::forward<Arg>(arg),
89-
std::forward<Args>(args)...);
94+
return ret + '%' +
95+
SPrintFImpl(format.substr(offset + 1),
96+
std::forward<Arg>(arg),
97+
std::forward<Args>(args)...);
9098
}
9199
default: {
92-
return ret + '%' + SPrintFImpl(p,
93-
std::forward<Arg>(arg),
94-
std::forward<Args>(args)...);
100+
return ret + '%' +
101+
SPrintFImpl(format.substr(offset),
102+
std::forward<Arg>(arg),
103+
std::forward<Args>(args)...);
95104
}
96105
case 'd':
97106
case 'i':
@@ -120,17 +129,21 @@ std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
120129
break;
121130
}
122131
}
123-
return ret + SPrintFImpl(p + 1, std::forward<Args>(args)...);
132+
return ret +
133+
SPrintFImpl(format.substr(offset + 1), std::forward<Args>(args)...);
124134
}
125135

126136
template <typename... Args>
127137
std::string COLD_NOINLINE SPrintF( // NOLINT(runtime/string)
128-
const char* format, Args&&... args) {
138+
std::string_view format,
139+
Args&&... args) {
129140
return SPrintFImpl(format, std::forward<Args>(args)...);
130141
}
131142

132143
template <typename... Args>
133-
void COLD_NOINLINE FPrintF(FILE* file, const char* format, Args&&... args) {
144+
void COLD_NOINLINE FPrintF(FILE* file,
145+
std::string_view format,
146+
Args&&... args) {
134147
FWrite(file, SPrintF(format, std::forward<Args>(args)...));
135148
}
136149

Collapse file

‎src/debug_utils.h‎

Copy file name to clipboardExpand all lines: src/debug_utils.h
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ inline std::string ToString(const T& value);
3333
// - Supports %p and %s. %d, %i and %u are aliases for %s.
3434
// - Accepts any class that has a ToString() method for stringification.
3535
template <typename... Args>
36-
inline std::string SPrintF(const char* format, Args&&... args);
36+
inline std::string SPrintF(std::string_view format, Args&&... args);
3737
template <typename... Args>
38-
inline void FPrintF(FILE* file, const char* format, Args&&... args);
38+
inline void FPrintF(FILE* file, std::string_view format, Args&&... args);
3939
void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str);
4040

4141
// Listing the AsyncWrap provider types first enables us to cast directly
Collapse file

‎src/node_errors.h‎

Copy file name to clipboardExpand all lines: src/node_errors.h
+8-9Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details);
134134
#define V(code, type) \
135135
template <typename... Args> \
136136
inline v8::Local<v8::Object> code( \
137-
v8::Isolate* isolate, const char* format, Args&&... args) { \
137+
v8::Isolate* isolate, std::string_view format, Args&&... args) { \
138138
std::string message; \
139139
if (sizeof...(Args) == 0) { \
140140
message = format; \
@@ -159,17 +159,18 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details);
159159
} \
160160
template <typename... Args> \
161161
inline void THROW_##code( \
162-
v8::Isolate* isolate, const char* format, Args&&... args) { \
162+
v8::Isolate* isolate, std::string_view format, Args&&... args) { \
163163
isolate->ThrowException( \
164164
code(isolate, format, std::forward<Args>(args)...)); \
165165
} \
166166
template <typename... Args> \
167167
inline void THROW_##code( \
168-
Environment* env, const char* format, Args&&... args) { \
168+
Environment* env, std::string_view format, Args&&... args) { \
169169
THROW_##code(env->isolate(), format, std::forward<Args>(args)...); \
170170
} \
171171
template <typename... Args> \
172-
inline void THROW_##code(Realm* realm, const char* format, Args&&... args) { \
172+
inline void THROW_##code( \
173+
Realm* realm, std::string_view format, Args&&... args) { \
173174
THROW_##code(realm->isolate(), format, std::forward<Args>(args)...); \
174175
}
175176
ERRORS_WITH_CODE(V)
@@ -250,10 +251,8 @@ PREDEFINED_ERROR_MESSAGES(V)
250251
// Errors with predefined non-static messages
251252
inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env,
252253
int64_t timeout) {
253-
std::ostringstream message;
254-
message << "Script execution timed out after ";
255-
message << timeout << "ms";
256-
THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, message.str().c_str());
254+
THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(
255+
env, "Script execution timed out after %dms", timeout);
257256
}
258257

259258
inline void THROW_ERR_REQUIRE_ASYNC_MODULE(
@@ -275,7 +274,7 @@ inline void THROW_ERR_REQUIRE_ASYNC_MODULE(
275274
message += "\n Requiring ";
276275
message += utf8.ToStringView();
277276
}
278-
THROW_ERR_REQUIRE_ASYNC_MODULE(env, message.c_str());
277+
THROW_ERR_REQUIRE_ASYNC_MODULE(env, message);
279278
}
280279

281280
inline v8::Local<v8::Object> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.