Added string append.

This commit is contained in:
Branimir Karadžić
2016-12-01 22:09:41 -08:00
parent 743b896bfd
commit d9e1c8e18d
2 changed files with 19 additions and 5 deletions

View File

@@ -669,15 +669,18 @@ namespace bx
void set(const char* _ptr, uint32_t _len = UINT32_MAX)
{
clear();
append(_ptr, _len);
}
void append(const char* _ptr, uint32_t _len = UINT32_MAX)
{
if (0 != _len)
{
uint32_t len = uint32_t(strnlen(_ptr, _len) );
uint32_t old = m_len;
uint32_t len = m_len + uint32_t(strnlen(_ptr, _len) );
char* ptr = (char*)BX_REALLOC(*AllocatorT, 0 != m_len ? const_cast<char*>(m_ptr) : NULL, len+1);
m_len = len;
char* ptr = (char*)BX_ALLOC(*AllocatorT, len+1);
memcpy(ptr, _ptr, len);
ptr[len] = '\0';
strlncpy(ptr + old, len-old+1, _ptr, _len);
*const_cast<char**>(&m_ptr) = ptr;
}

View File

@@ -53,10 +53,21 @@ TEST_CASE("StringView", "")
String st(sv);
REQUIRE(4 == st.getLength() );
st.append("test");
REQUIRE(8 == st.getLength() );
st.append("test", 2);
REQUIRE(10 == st.getLength() );
REQUIRE(0 == strcmp(st.getPtr(), "testtestte") );
st.clear();
REQUIRE(0 == st.getLength() );
REQUIRE(4 == sv.getLength() );
st.append("test");
REQUIRE(4 == st.getLength() );
sv.clear();
REQUIRE(0 == sv.getLength() );
}