Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: enhance C++ sprintf utility #32385

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/debug_utils-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,39 @@ struct ToStringHelper {
}
static std::string Convert(const std::string& value) { return value; }
static std::string Convert(bool value) { return value ? "true" : "false"; }
template <unsigned BASE_BITS,
typename T,
typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
static std::string BaseConvert(T value) {
char ret[3 * sizeof(T)];
char* ptr = ret + 3 * sizeof(T) - 1;
*ptr = '\0';
const char* digits = "0123456789abcdef";
do {
unsigned digit = value & ((1 << BASE_BITS) - 1);
*--ptr =
(BASE_BITS < 4 ? static_cast<char>('0' + digit) : digits[digit]);
} while ((value >>= BASE_BITS) != 0);
return ptr;
}
template <unsigned BASE_BITS,
typename T,
typename std::enable_if<!std::is_integral<T>::value, int>::type = 0>
static std::string BaseConvert(T value) {
return Convert(std::forward<T>(value));
}
};

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

template <unsigned BASE_BITS, typename T>
std::string ToBaseString(T&& value) {
return ToStringHelper::BaseConvert<BASE_BITS>(std::forward<T>(value));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think you need to do any templating for T, any value that’s passed to a function like this will be convertible to unsigned long long anyway.

Copy link
Member Author

@himself65 himself65 Mar 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course ok, but we had code more template for other types if we do this.

btw, I inspired from this https://github.com/fmtlib/fmt/blob/5d32ccfc3130fdd122c344b37087ac7fd8c2b62e/include/fmt/format.h#L901-L913


inline std::string SPrintFImpl(const char* format) {
const char* p = strchr(format, '%');
if (LIKELY(p == nullptr)) return format;
Expand Down Expand Up @@ -64,7 +90,18 @@ std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
case 'd':
case 'i':
case 'u':
case 's': ret += ToString(arg); break;
case 's':
ret += ToString(arg);
break;
case 'o':
ret += ToBaseString<3>(arg);
break;
case 'x':
ret += ToBaseString<4>(arg);
break;
case 'X':
ret += node::ToUpper(ToBaseString<4>(arg));
break;
case 'p': {
CHECK(std::is_pointer<typename std::remove_reference<Arg>::type>::value);
char out[20];
Expand Down
1 change: 1 addition & 0 deletions src/debug_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "async_wrap.h"

#include <algorithm>
#include <sstream>
#include <string>

Expand Down
6 changes: 6 additions & 0 deletions test/cctest/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ TEST(UtilTest, SPrintF) {
EXPECT_EQ(SPrintF("%u", -10000000000LL), "-10000000000");
EXPECT_EQ(SPrintF("%i", 10), "10");
EXPECT_EQ(SPrintF("%d", 10), "10");
EXPECT_EQ(SPrintF("%x", 15), "f");
EXPECT_EQ(SPrintF("%x", 16), "10");
EXPECT_EQ(SPrintF("%X", 15), "F");
EXPECT_EQ(SPrintF("%X", 16), "10");
EXPECT_EQ(SPrintF("%o", 7), "7");
EXPECT_EQ(SPrintF("%o", 8), "10");

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