diff --git a/include/libimp/fmt.h b/include/libimp/fmt.h index 26f37a3..dcaa329 100644 --- a/include/libimp/fmt.h +++ b/include/libimp/fmt.h @@ -67,8 +67,6 @@ LIBIMP_NODISCARD std::string fmt(A1 &&a1, A &&...args) { /// @brief Return the string directly. inline char const *to_string(char const *a) noexcept { return (a == nullptr) ? "" : a; } -template -inline char const *to_string(char const (&a)[N]) noexcept { return a; } inline std::string to_string(std::string const &a) noexcept { return a; } inline std::string to_string(std::string &&a) noexcept { return std::move(a); } LIBIMP_EXPORT std::string to_string(char const *a, span fstr) noexcept; diff --git a/src/libimp/fmt.cpp b/src/libimp/fmt.cpp index 5a5a8e1..6177a6b 100644 --- a/src/libimp/fmt.cpp +++ b/src/libimp/fmt.cpp @@ -74,8 +74,12 @@ std::string fmt_of_float(span fstr, span l) { template std::string sprintf(std::string const &sfmt, A a) { - auto sz = std::snprintf(nullptr, 0, sfmt.c_str(), a); + char sbuf[2048]; + auto sz = std::snprintf(sbuf, sizeof(sbuf), sfmt.c_str(), a); if (sz <= 0) return {}; + if (sz < sizeof(sbuf)) { + return sbuf; + } std::string des; des.resize(sz + 1); if (std::snprintf(&des[0], des.size(), sfmt.c_str(), a) < 0) { diff --git a/test/imp/test_imp_fmt.cpp b/test/imp/test_imp_fmt.cpp index 234dfc4..0f3ee95 100644 --- a/test/imp/test_imp_fmt.cpp +++ b/test/imp/test_imp_fmt.cpp @@ -14,8 +14,8 @@ TEST(fmt, operator) { TEST(fmt, to_string) { /// @brief string - EXPECT_EQ(imp::to_string(""), ""); - EXPECT_EQ(imp::to_string("%what%"), "%what%"); + EXPECT_STREQ(imp::to_string(""), ""); + EXPECT_STREQ(imp::to_string("%what%"), "%what%"); EXPECT_EQ(imp::to_string("%what%", "10") , " %what%"); EXPECT_EQ(imp::to_string("%what%", "-10"), "%what% ");