From d9e1c8e18df053ed12fbf5314af2d41424daaba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 1 Dec 2016 22:09:41 -0800 Subject: [PATCH] Added string append. --- include/bx/string.h | 13 ++++++++----- tests/string_test.cpp | 11 +++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/include/bx/string.h b/include/bx/string.h index 655529a..03fce2f 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -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(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(&m_ptr) = ptr; } diff --git a/tests/string_test.cpp b/tests/string_test.cpp index e68a04a..b37e305 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -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() ); }