GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — src: enhance C++ sprintf utility · nodejs/node@4b5fd24

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • debug_utils-inl.h
    • debug_utils.h
  • test/cctest
    • test_util.cc
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/debug_utils-inl.h‎

Copy file name to clipboardExpand all lines: src/debug_utils-inl.h
+38-1Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,39 @@ struct ToStringHelper {
2727
}
2828
static std::string Convert(const std::string& value) { return value; }
2929
static std::string Convert(bool value) { return value ? "true" : "false"; }
30+
template <unsigned BASE_BITS,
31+
typename T,
32+
typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
33+
static std::string BaseConvert(T value) {
34+
char ret[3 * sizeof(T)];
35+
char* ptr = ret + 3 * sizeof(T) - 1;
36+
*ptr = '\0';
37+
const char* digits = "0123456789abcdef";
38+
do {
39+
unsigned digit = value & ((1 << BASE_BITS) - 1);
40+
*--ptr =
41+
(BASE_BITS < 4 ? static_cast<char>('0' + digit) : digits[digit]);
42+
} while ((value >>= BASE_BITS) != 0);
43+
return ptr;
44+
}
45+
template <unsigned BASE_BITS,
46+
typename T,
47+
typename std::enable_if<!std::is_integral<T>::value, int>::type = 0>
48+
static std::string BaseConvert(T value) {
49+
return Convert(std::forward<T>(value));
50+
}
3051
};
3152

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

58+
template <unsigned BASE_BITS, typename T>
59+
std::string ToBaseString(T&& value) {
60+
return ToStringHelper::BaseConvert<BASE_BITS>(std::forward<T>(value));
61+
}
62+
3763
inline std::string SPrintFImpl(const char* format) {
3864
const char* p = strchr(format, '%');
3965
if (LIKELY(p == nullptr)) return format;
@@ -64,7 +90,18 @@ std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
6490
case 'd':
6591
case 'i':
6692
case 'u':
67-
case 's': ret += ToString(arg); break;
93+
case 's':
94+
ret += ToString(arg);
95+
break;
96+
case 'o':
97+
ret += ToBaseString<3>(arg);
98+
break;
99+
case 'x':
100+
ret += ToBaseString<4>(arg);
101+
break;
102+
case 'X':
103+
ret += node::ToUpper(ToBaseString<4>(arg));
104+
break;
68105
case 'p': {
69106
CHECK(std::is_pointer<typename std::remove_reference<Arg>::type>::value);
70107
char out[20];
Collapse file

‎src/debug_utils.h‎

Copy file name to clipboardExpand all lines: src/debug_utils.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "async_wrap.h"
77

8+
#include <algorithm>
89
#include <sstream>
910
#include <string>
1011

Collapse file

‎test/cctest/test_util.cc‎

Copy file name to clipboardExpand all lines: test/cctest/test_util.cc
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ TEST(UtilTest, SPrintF) {
268268
EXPECT_EQ(SPrintF("%u", -10000000000LL), "-10000000000");
269269
EXPECT_EQ(SPrintF("%i", 10), "10");
270270
EXPECT_EQ(SPrintF("%d", 10), "10");
271+
EXPECT_EQ(SPrintF("%x", 15), "f");
272+
EXPECT_EQ(SPrintF("%x", 16), "10");
273+
EXPECT_EQ(SPrintF("%X", 15), "F");
274+
EXPECT_EQ(SPrintF("%X", 16), "10");
275+
EXPECT_EQ(SPrintF("%o", 7), "7");
276+
EXPECT_EQ(SPrintF("%o", 8), "10");
271277

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

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.