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