diff --git a/include/bx/inline/string.inl b/include/bx/inline/string.inl index 330f73b..15cdc25 100644 --- a/include/bx/inline/string.inl +++ b/include/bx/inline/string.inl @@ -19,14 +19,13 @@ namespace bx char temp[2048]; char* out = temp; - int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList); - if ( (int32_t)sizeof(temp) < len) + int32_t len = vsnprintf(out, sizeof(temp), _format, _argList); + if (int32_t(sizeof(temp) ) < len) { - out = (char*)alloca(len+1); - len = bx::vsnprintf(out, len, _format, _argList); + out = (char*)alloca(len); + len = vsnprintf(out, len, _format, _argList); } - out[len] = '\0'; - _out.append(out); + _out.append(out, out+len); } template @@ -86,6 +85,12 @@ namespace bx set(_ptr, _term); } + template + inline StringView::StringView(const Ty& _container) + { + set(_container); + } + inline void StringView::set(const char* _ptr, int32_t _len) { clear(); @@ -106,6 +111,12 @@ namespace bx set(_ptr, int32_t(_term-_ptr) ); } + template + inline void StringView::set(const Ty& _container) + { + set(_container.data(), _container.length() ); + } + inline void StringView::set(const StringView& _str) { set(_str.m_ptr, _str.m_len); diff --git a/include/bx/string.h b/include/bx/string.h index 39c3631..3a253ef 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -41,6 +41,10 @@ namespace bx /// StringView(const char* _ptr, const char* _term); + /// + template + explicit StringView(const Ty& _container); + /// void set(const char* _ptr, int32_t _len = INT32_MAX); @@ -50,6 +54,10 @@ namespace bx /// void set(const StringView& _str); + /// + template + void set(const Ty& _container); + /// void clear(); diff --git a/tests/string_test.cpp b/tests/string_test.cpp index cc96acf..db5e3e2 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -8,9 +8,17 @@ #include #include #include +#include bx::AllocatorI* g_allocator; +TEST_CASE("stringPrintfTy", "") +{ + std::string test; + bx::stringPrintf(test, "printf into std::string."); + REQUIRE(0 == bx::strCmp(bx::StringView(test), "printf into std::string.") ); +} + TEST_CASE("chars", "") { for (char ch = 'A'; ch <= 'Z'; ++ch)