diff --git a/include/xo/object2/DString.hpp b/include/xo/object2/DString.hpp index bf8f04f..a029da3 100644 --- a/include/xo/object2/DString.hpp +++ b/include/xo/object2/DString.hpp @@ -87,10 +87,10 @@ namespace xo { * @return pointer to newly created DString **/ template - static DString * snprintf(obj mm, - size_type cap, - const char * fmt, - Args&&... args) + static DString * printf(obj mm, + size_type cap, + const char * fmt, + Args&&... args) { DString * result = DString::empty(mm, cap); if (result) { @@ -148,7 +148,12 @@ namespace xo { **/ template size_type sprintf(const char * fmt, Args&&... args) { - int n = std::snprintf(chars_, capacity_, fmt, std::forward(args)...); + int n; + if constexpr (sizeof...(Args) == 0) { + n = std::snprintf(chars_, capacity_, "%s", fmt); + } else { + n = std::snprintf(chars_, capacity_, fmt, std::forward(args)...); + } if (n < 0) { size_ = 0; chars_[0] = '\0'; diff --git a/utest/Printable.test.cpp b/utest/Printable.test.cpp index c1b63dc..f61e06a 100644 --- a/utest/Printable.test.cpp +++ b/utest/Printable.test.cpp @@ -12,6 +12,10 @@ #include #include +#include +#include +#include + #include #include @@ -35,6 +39,7 @@ namespace ut { using xo::scm::ListOps; using xo::scm::DList; using xo::scm::DInteger; + using xo::scm::DString; using xo::mm::AAllocator; using xo::mm::ACollector; using xo::mm::AGCObject; @@ -70,10 +75,10 @@ namespace ut { std::vector s_testcase_v = { testcase_pp(16384, 8192, 0, "()"), - testcase_pp(16384, 8192, 1, "(1000)"), - testcase_pp(16384, 8192, 2, "(1000 1197)"), - testcase_pp(16384, 8192, 5, "(1000 1197 1394 1591 1788)"), - testcase_pp(16384, 8192, 10, "(1000 1197 1394 1591 1788 1985 2182 2379 2576 2773)"), + testcase_pp(16384, 8192, 1, "(01000)"), + testcase_pp(16384, 8192, 2, "(01000 1197)"), + testcase_pp(16384, 8192, 5, "(01000 1197 01394 1591 01788)"), + testcase_pp(16384, 8192, 10, "(01000 1197 01394 1591 01788 1985 02182 2379 02576 2773)"), testcase_pp(16384, 8192, 20, "(...)"), }; } @@ -117,9 +122,17 @@ namespace ut { c_o.add_gc_root(&l0_o); for(int ip1 = tc.list_.size(); ip1 > 0; --ip1) { - auto xi_o = DInteger::box(gc_o, tc.list_[ip1 - 1]); + obj elt; - l0_o = ListOps::cons(gc_o, xi_o, l0_o); + //elt = DInteger::box(gc_o, tc.list_[ip1 - 1]); + + if (ip1 % 2 == 0) { + elt = DInteger::box(gc_o, tc.list_[ip1 - 1]); + } else { + elt = obj(DString::printf(gc_o, 80, "%05d", tc.list_[ip1 - 1])); + } + + l0_o = ListOps::cons(gc_o, elt, l0_o); } // TODO: log_streambuf using DArena