mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-17 20:52:37 +01:00
Added string append.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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() );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user