Fix fmt function to handle empty strings and update make_prefix template parameters

This commit is contained in:
mutouyun 2025-01-11 18:06:08 +08:00 committed by 木头云
parent e7a8005f58
commit a2da0621e8
3 changed files with 11 additions and 3 deletions

View File

@ -199,6 +199,7 @@ void fmt_context::expend(std::size_t sz) noexcept {
bool fmt_context::append(span<char const> const &str) noexcept {
auto sz = str.size();
if (sz == 0) return true;
if (str.back() == '\0') --sz;
auto sbuf = buffer(sz);
if (sbuf.size() < sz) {

View File

@ -46,9 +46,9 @@ inline std::string make_string(char const *str) {
}
/// \brief Combine prefix from a list of strings.
template <typename... A>
inline std::string make_prefix(std::string prefix, A &&...args) {
return ipc::fmt(prefix, "__IPC_SHM__", std::forward<A>(args)...);
template <typename A1, typename... A>
inline std::string make_prefix(A1 &&prefix, A &&...args) {
return ipc::fmt(std::forward<A1>(prefix), "__IPC_SHM__", std::forward<A>(args)...);
}
} // namespace ipc

View File

@ -110,6 +110,13 @@ TEST(fmt, fmt) {
std::string test(4096, ' ');
std::memcpy(&test[test.size() - sizeof(txt) + 1], txt, sizeof(txt) - 1);
EXPECT_EQ(s, test);
EXPECT_EQ(ipc::fmt("", 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 {