Added STL string to bx::StringView conversion.

This commit is contained in:
Branimir Karadžić
2018-02-20 11:14:17 -08:00
parent cfc6b32d10
commit 9987d5cc5a
3 changed files with 33 additions and 6 deletions

View File

@@ -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 <typename Ty>
@@ -86,6 +85,12 @@ namespace bx
set(_ptr, _term);
}
template<typename Ty>
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<typename Ty>
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);

View File

@@ -41,6 +41,10 @@ namespace bx
///
StringView(const char* _ptr, const char* _term);
///
template<typename Ty>
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<typename Ty>
void set(const Ty& _container);
///
void clear();

View File

@@ -8,9 +8,17 @@
#include <bx/string.h>
#include <bx/handlealloc.h>
#include <bx/sort.h>
#include <string>
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)