Properly using 'long' and 'unsigned long' in tl::sprintf to fit Windows definition of 'long'

This commit is contained in:
Matthias Koefferlein 2025-05-25 19:42:14 +02:00
parent 329ac6c5b7
commit bfe49c22e3
2 changed files with 17 additions and 3 deletions

View File

@ -1975,17 +1975,17 @@ sprintf (const char *f, const std::vector <tl::Variant> &vv, unsigned int a0)
os.setf (std::ios::uppercase);
}
if (a < vv.size ()) {
os << vv [a].to_ulong ();
os << vv [a].to_ulonglong ();
}
} else if (*cp == 'u' || *cp == 'U') {
os.setf (std::ios_base::fmtflags (0), std::ios::basefield);
if (a < vv.size ()) {
os << vv [a].to_ulong ();
os << vv [a].to_ulonglong ();
}
} else if (*cp == 'd' || *cp == 'D') {
os.setf (std::ios_base::fmtflags (0), std::ios::basefield);
if (a < vv.size ()) {
os << vv [a].to_long ();
os << vv [a].to_longlong ();
}
} else if (*cp == 's' || *cp == 'S') {
os.setf (std::ios_base::fmtflags (0), std::ios::basefield);

View File

@ -57,6 +57,20 @@ TEST(1)
EXPECT_EQ (tl::sprintf("%lu %llu %02x", 1, 2, 167), "1 2 a7");
EXPECT_EQ (tl::sprintf("%lu %llu %02X", 1, 2, 761), "1 2 2F9");
EXPECT_EQ (tl::sprintf("%c%c", 'a', 'X'), "aX");
// 64bit numbers
EXPECT_EQ (tl::sprintf("%x", 0x1000000000ll), "1000000000");
EXPECT_EQ (tl::sprintf("%lx", 0x1000000000ll), "1000000000");
EXPECT_EQ (tl::sprintf("%llx", 0x1000000000ll), "1000000000");
EXPECT_EQ (tl::sprintf("%d", 100000000000ll), "100000000000");
EXPECT_EQ (tl::sprintf("%ld", 100000000000ll), "100000000000");
EXPECT_EQ (tl::sprintf("%lld", 100000000000ll), "100000000000");
EXPECT_EQ (tl::sprintf("%d", -100000000000ll), "-100000000000");
EXPECT_EQ (tl::sprintf("%ld", -100000000000ll), "-100000000000");
EXPECT_EQ (tl::sprintf("%lld", -100000000000ll), "-100000000000");
EXPECT_EQ (tl::sprintf("%u", 100000000000ull), "100000000000");
EXPECT_EQ (tl::sprintf("%lu", 100000000000ull), "100000000000");
EXPECT_EQ (tl::sprintf("%llu", 100000000000ull), "100000000000");
}
TEST(1a)