mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
Fix fmt function to handle null pointers and return empty string
This commit is contained in:
parent
00162b96b9
commit
86d9d868c8
@ -221,8 +221,11 @@ bool to_string(fmt_context &ctx, std::string const &a) noexcept {
|
||||
}
|
||||
|
||||
bool to_string(fmt_context &ctx, char const *a, span<char const> fstr) noexcept {
|
||||
if (a == nullptr) return false;
|
||||
return ipc::sprintf(ctx, fmt_of, fstr, "s", a);
|
||||
if (a == nullptr) {
|
||||
return ipc::sprintf(ctx, fmt_of, fstr, "s", "");
|
||||
} else {
|
||||
return ipc::sprintf(ctx, fmt_of, fstr, "s", a);
|
||||
}
|
||||
}
|
||||
|
||||
bool to_string(fmt_context &ctx, char a) noexcept {
|
||||
|
||||
@ -112,11 +112,10 @@ TEST(fmt, fmt) {
|
||||
EXPECT_EQ(s, test);
|
||||
|
||||
EXPECT_EQ(ipc::fmt("", 1, "", '2', "", 3.0), "123.000000");
|
||||
char const * nc = nullptr;
|
||||
EXPECT_EQ(ipc::fmt(nc, 1, "", '2', "", 3.0), "123.000000");
|
||||
std::string empty;
|
||||
EXPECT_EQ(ipc::fmt(empty, 1, "", '2', "", 3.0), "123.000000");
|
||||
EXPECT_EQ(ipc::fmt(empty, 1, empty, '2', "", 3.0), "123.000000");
|
||||
EXPECT_EQ(ipc::fmt("", 1, empty, '2', empty, 3.0), "123.000000");
|
||||
EXPECT_EQ(ipc::fmt("", 1, "", '2', empty), "12");
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user