Skip to content

Commit

Permalink
src: add C++-style sprintf utility
Browse files Browse the repository at this point in the history
Add an utility that handles C++-style strings and objects well.
  • Loading branch information
addaleax committed Jan 21, 2020
1 parent 343ddff commit a35bf6b
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 6 deletions.
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@
'src/connect_wrap.h',
'src/connection_wrap.h',
'src/debug_utils.h',
'src/debug_utils-inl.h',
'src/env.h',
'src/env-inl.h',
'src/handle_wrap.h',
Expand Down
86 changes: 86 additions & 0 deletions src/debug_utils-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#ifndef SRC_DEBUG_UTILS_INL_H_
#define SRC_DEBUG_UTILS_INL_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "debug_utils.h"

#include <type_traits>

namespace node {

struct ToStringHelper {
template <typename T>
static std::string Convert(
const T& value,
std::string(T::* to_string)() const = &T::ToString) {
return (value.*to_string)();
}
template <typename T,
typename test_for_number = typename std::
enable_if<std::is_arithmetic<T>::value, bool>::type,
typename dummy = bool>
static std::string Convert(const T& value) { return std::to_string(value); }
static std::string Convert(const char* value) { return value; }
static std::string Convert(const std::string& value) { return value; }
static std::string Convert(bool value) { return value ? "true" : "false"; }
};

template <typename T>
std::string ToString(const T& value) {
return ToStringHelper::Convert(value);
}

inline std::string SPrintFImpl(const char* format) {
return format;
}

template <typename Arg, typename... Args>
std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
const char* format, Arg&& arg, Args&&... args) {
const char* p = strchr(format, '%');
if (p == nullptr) return format;
std::string ret(format, p);
// Ignore long / size_t modifiers
while (strchr("lz", *++p) != nullptr) {}
switch (*p) {
case '%': {
return ret + '%' + SPrintFImpl(p + 1,
std::forward<Arg>(arg),
std::forward<Args>(args)...);
}
default: {
return ret + '%' + SPrintFImpl(p,
std::forward<Arg>(arg),
std::forward<Args>(args)...);
}
case 'd':
case 'i':
case 'u':
case 's': ret += ToString(arg); break;
case 'p': {
CHECK(std::is_pointer<typename std::remove_reference<Arg>::type>::value);
char out[20];
int n = snprintf(out,
sizeof(out),
"%p",
*reinterpret_cast<const void* const*>(&arg));
CHECK_GE(n, 0);
ret += out;
break;
}
}
return ret + SPrintFImpl(p + 1, std::forward<Args>(args)...);
}

template <typename... Args>
std::string COLD_NOINLINE SPrintF( // NOLINT(runtime/string)
const char* format, Args&&... args) {
return SPrintFImpl(format, std::forward<Args>(args)...);
}

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#endif // SRC_DEBUG_UTILS_INL_H_
14 changes: 13 additions & 1 deletion src/debug_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,26 @@

namespace node {

template <typename T>
inline std::string ToString(const T& value);

// C++-style variant of sprintf() that:
// - Returns an std::string
// - Handles \0 bytes correctly
// - Supports %p and %s. %d, %i and %u are aliases for %s.
// - Accepts any class that has a ToString() method for stringification.
template <typename... Args>
inline std::string SPrintF(const char* format, Args&&... args);

template <typename... Args>
inline void FORCE_INLINE Debug(Environment* env,
DebugCategory cat,
const char* format,
Args&&... args) {
if (!UNLIKELY(env->debug_enabled(cat)))
return;
fprintf(stderr, format, std::forward<Args>(args)...);
std::string out = SPrintF(format, std::forward<Args>(args)...);
fwrite(out.data(), out.size(), 1, stderr);
}

inline void FORCE_INLINE Debug(Environment* env,
Expand Down
2 changes: 1 addition & 1 deletion src/inspector_profiler.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "inspector_profiler.h"
#include "base_object-inl.h"
#include "debug_utils.h"
#include "debug_utils-inl.h"
#include "diagnosticfilename-inl.h"
#include "memory_tracker-inl.h"
#include "node_file.h"
Expand Down
2 changes: 1 addition & 1 deletion src/node_http2.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "aliased_buffer.h"
#include "debug_utils.h"
#include "debug_utils-inl.h"
#include "memory_tracker-inl.h"
#include "node.h"
#include "node_buffer.h"
Expand Down
4 changes: 2 additions & 2 deletions src/node_wasi.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "env-inl.h"
#include "base_object-inl.h"
#include "debug_utils.h"
#include "debug_utils-inl.h"
#include "memory_tracker-inl.h"
#include "node_mem-inl.h"
#include "util-inl.h"
Expand Down Expand Up @@ -1063,7 +1063,7 @@ void WASI::PathFilestatGet(const FunctionCallbackInfo<Value>& args) {
CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, buf_ptr);
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
Debug(wasi,
"path_filestat_get(%d, %d, %d, %d, %d)\n",
"path_filestat_get(%d, %d, %d)\n",
fd,
path_ptr,
path_len);
Expand Down
2 changes: 1 addition & 1 deletion src/tls_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "tls_wrap.h"
#include "async_wrap-inl.h"
#include "debug_utils.h"
#include "debug_utils-inl.h"
#include "memory_tracker-inl.h"
#include "node_buffer.h" // Buffer
#include "node_crypto.h" // SecureContext
Expand Down
42 changes: 42 additions & 0 deletions test/cctest/test_util.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "util-inl.h"
#include "debug_utils-inl.h"
#include "env-inl.h"
#include "gtest/gtest.h"

TEST(UtilTest, ListHead) {
Expand Down Expand Up @@ -250,3 +252,43 @@ TEST(UtilTest, MaybeStackBuffer) {
EXPECT_TRUE(buf.IsInvalidated());
}
}

TEST(UtilTest, SPrintF) {
using node::SPrintF;

// %d, %u and %s all do the same thing. The actual C++ type is used to infer
// the right representation.
EXPECT_EQ(SPrintF("%s", false), "false");
EXPECT_EQ(SPrintF("%s", true), "true");
EXPECT_EQ(SPrintF("%d", true), "true");
EXPECT_EQ(SPrintF("%u", true), "true");
EXPECT_EQ(SPrintF("%d", 10000000000LL), "10000000000");
EXPECT_EQ(SPrintF("%d", -10000000000LL), "-10000000000");
EXPECT_EQ(SPrintF("%u", 10000000000LL), "10000000000");
EXPECT_EQ(SPrintF("%u", -10000000000LL), "-10000000000");
EXPECT_EQ(SPrintF("%i", 10), "10");
EXPECT_EQ(SPrintF("%d", 10), "10");

EXPECT_EQ(atof(SPrintF("%s", 0.5).c_str()), 0.5);
EXPECT_EQ(atof(SPrintF("%s", -0.5).c_str()), -0.5);

void (*fn)() = []() {};
void* p = reinterpret_cast<void*>(&fn);
EXPECT_GE(SPrintF("%p", fn).size(), 8u);
EXPECT_GE(SPrintF("%p", p).size(), 8u);

const std::string foo = "foo";
const char* bar = "bar";
EXPECT_EQ(SPrintF("%s %s", foo, "bar"), "foo bar");
EXPECT_EQ(SPrintF("%s %s", foo, bar), "foo bar");

struct HasToString {
std::string ToString() const {
return "meow";
}
};
EXPECT_EQ(SPrintF("%s", HasToString{}), "meow");

const std::string with_zero = std::string("a") + '\0' + 'b';
EXPECT_EQ(SPrintF("%s", with_zero), with_zero);
}

0 comments on commit a35bf6b

Please sign in to comment.