diff --git a/Foundation/include/Poco/Format.h b/Foundation/include/Poco/Format.h index 515df47e9..e76326c6e 100644 --- a/Foundation/include/Poco/Format.h +++ b/Foundation/include/Poco/Format.h @@ -106,20 +106,26 @@ std::string Foundation_API format(const std::string& fmt, const Any& value); /// std::string s2 = format("second: %[1]d, first: %[0]d", 1, 2); void Foundation_API format(std::string& result, const char *fmt, const std::vector& values); - /// Supports a variable number of arguments and is used by - /// all other variants of format(). + /// Supports a variable number of arguments. void Foundation_API format(std::string& result, const std::string& fmt, const std::vector& values); + /// Supports a variable number of arguments. + +inline void formatAny(std::string& result, const std::string& fmt, const std::vector& values) /// Supports a variable number of arguments and is used by /// all other variants of format(). +{ + format(result, fmt, values); +} +inline void formatAny(std::string& result, const char *fmt, const std::vector& values) + /// Supports a variable number of arguments and is used by + /// all other variants of format(). +{ + format(result, fmt, values); +} -template < - typename T, -#ifdef __cpp_lib_remove_cvref - typename std::enable_if_t, std::vector>>, -#endif - typename... Args> +template void format(std::string& result, const std::string& fmt, T arg1, Args... args) /// Appends the formatted string to result. { @@ -127,16 +133,11 @@ void format(std::string& result, const std::string& fmt, T arg1, Args... args) values.reserve(sizeof...(Args) + 1); values.emplace_back(arg1); values.insert(values.end(), { args... }); - format(result, fmt, values); + formatAny(result, fmt, values); } -template < - typename T, -#ifdef __cpp_lib_remove_cvref - typename std::enable_if_t, std::vector>>, -#endif - typename... Args> +template void format(std::string& result, const char* fmt, T arg1, Args... args) /// Appends the formatted string to result. { @@ -144,16 +145,11 @@ void format(std::string& result, const char* fmt, T arg1, Args... args) values.reserve(sizeof...(Args) + 1); values.emplace_back(arg1); values.insert(values.end(), { args... }); - format(result, fmt, values); + formatAny(result, fmt, values); } -template < - typename T, -#ifdef __cpp_lib_remove_cvref - typename std::enable_if_t, std::vector>>, -#endif - typename... Args> +template std::string format(const std::string& fmt, T arg1, Args... args) /// Returns the formatted string. { @@ -162,17 +158,12 @@ std::string format(const std::string& fmt, T arg1, Args... args) values.emplace_back(arg1); values.insert(values.end(), { args... }); std::string result; - format(result, fmt, values); + formatAny(result, fmt, values); return result; } -template < - typename T, -#ifdef __cpp_lib_remove_cvref - typename std::enable_if_t, std::vector>>, -#endif - typename... Args> +template std::string format(const char* fmt, T arg1, Args... args) /// Returns the formatted string. { @@ -181,7 +172,7 @@ std::string format(const char* fmt, T arg1, Args... args) values.emplace_back(arg1); values.insert(values.end(), { args... }); std::string result; - format(result, fmt, values); + formatAny(result, fmt, values); return result; } diff --git a/Foundation/testsuite/src/FormatTest.cpp b/Foundation/testsuite/src/FormatTest.cpp index 229ecc0a2..b852f1736 100644 --- a/Foundation/testsuite/src/FormatTest.cpp +++ b/Foundation/testsuite/src/FormatTest.cpp @@ -20,6 +20,7 @@ using Poco::format; using Poco::BadCastException; using Poco::Int64; using Poco::UInt64; +using Poco::Any; FormatTest::FormatTest(const std::string& name): CppUnit::TestCase(name) @@ -453,6 +454,27 @@ void FormatTest::testIndex() } +void FormatTest::testAny() +{ + Any a = 42; + std::string s(format("%d", a)); + assertTrue (s == "42"); + + a = std::string("42"); + s = format("%s", a); + assertTrue (s == "42"); + + a = 42.; + s = format("%f", a); + assertTrue (s.find("42.0") == 0); + + s.clear(); + std::vector av{ 42, std::string("42"), 42. }; + format(s, "%d '%s' %f", av); + assertTrue (s.find("42 '42' 42.0") == 0); +} + + void FormatTest::setUp() { } @@ -476,6 +498,7 @@ CppUnit::Test* FormatTest::suite() CppUnit_addTest(pSuite, FormatTest, testString); CppUnit_addTest(pSuite, FormatTest, testMultiple); CppUnit_addTest(pSuite, FormatTest, testIndex); + CppUnit_addTest(pSuite, FormatTest, testAny); return pSuite; } diff --git a/Foundation/testsuite/src/FormatTest.h b/Foundation/testsuite/src/FormatTest.h index cf3885d14..0dc326358 100644 --- a/Foundation/testsuite/src/FormatTest.h +++ b/Foundation/testsuite/src/FormatTest.h @@ -30,6 +30,7 @@ public: void testString(); void testMultiple(); void testIndex(); + void testAny(); void setUp(); void tearDown();