From f566076f42aea4b4e145c03d4fbcbc2ec653057d Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 24 Nov 2024 09:01:33 +0000 Subject: [PATCH] Added member function resize_and_overwrite --- include/etl/basic_string.h | 24 ++++++-- test/test_string_char.cpp | 64 ++++++++++++++++++- test/test_string_char_external_buffer.cpp | 65 +++++++++++++++++++- test/test_string_u16.cpp | 58 +++++++++++++++++ test/test_string_u16_external_buffer.cpp | 39 +++++++++++- test/test_string_u32.cpp | 58 +++++++++++++++++ test/test_string_u32_external_buffer.cpp | 39 +++++++++++- test/test_string_u8.cpp | 58 +++++++++++++++++ test/test_string_u8_external_buffer.cpp | 65 +++++++++++++++++++- test/test_string_wchar_t.cpp | 58 +++++++++++++++++ test/test_string_wchar_t_external_buffer.cpp | 39 +++++++++++- 11 files changed, 550 insertions(+), 17 deletions(-) diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index 5cc37f10..9146d523 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -496,6 +496,22 @@ namespace etl cleanup(); } + //********************************************************************* + /// Resizes the string and overwrites to data using the operation. + //********************************************************************* + template + void resize_and_overwrite(size_type new_size, TOperation operation) + { + if (new_size > CAPACITY) + { + ETL_ASSERT_FAIL(ETL_ERROR(string_out_of_bounds)); + } + + current_size = operation(p_buffer, new_size); + p_buffer[current_size] = '\0'; + cleanup(); + } + //********************************************************************* /// Resizes the string, but doesn't initialise the free space /// except for a terminator null. @@ -1532,7 +1548,7 @@ namespace etl //********************************************************************* /// Checks that the string is the start of this string //********************************************************************* - bool starts_with(const ibasic_string& str) const + bool starts_with(const etl::ibasic_string& str) const { return compare(0, str.size(), str) == 0; } @@ -1541,7 +1557,7 @@ namespace etl /// Checks that the view is the start of this string //********************************************************************* template - bool starts_with(const basic_string_view& view) const + bool starts_with(const etl::basic_string_view& view) const { return compare(0, view.size(), view) == 0; } @@ -1567,7 +1583,7 @@ namespace etl //********************************************************************* /// Checks that the string is the end of this string //********************************************************************* - bool ends_with(const ibasic_string& str) const + bool ends_with(const etl::ibasic_string& str) const { if (str.size() > size()) { @@ -1581,7 +1597,7 @@ namespace etl /// Checks that the view is the end of this string //********************************************************************* template - bool ends_with(const basic_string_view& view) const + bool ends_with(const etl::basic_string_view& view) const { if (view.size() > size()) { diff --git a/test/test_string_char.cpp b/test/test_string_char.cpp index 5c1e0c99..7c118ff8 100644 --- a/test/test_string_char.cpp +++ b/test/test_string_char.cpp @@ -725,7 +725,7 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); - CHECK_EQUAL(text.size(), NEW_SIZE); + CHECK_EQUAL(NEW_SIZE, text.size()); } //************************************************************************* @@ -738,7 +738,7 @@ namespace text.uninitialized_resize(NEW_SIZE); - CHECK_EQUAL(text.size(), SIZE); + CHECK_EQUAL(SIZE, text.size()); } //************************************************************************* @@ -766,7 +766,65 @@ namespace bool is_equal = Equal(compare_text, text); CHECK(is_equal); - CHECK_EQUAL(text.size(), NEW_SIZE); + CHECK_EQUAL(NEW_SIZE, text.size()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 8UL; + + Text text(initial_text.c_str(), INITIAL_SIZE); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H234567")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_down) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 3UL; + + Text text(initial_text.c_str(), INITIAL_SIZE); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H2")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up_excess) + { + Text text(initial_text.c_str(), initial_text.size()); + + CHECK_THROW(text.resize_and_overwrite(text.capacity() + 1, [](Text::pointer /*p*/, size_t n) { return n; }), etl::string_out_of_bounds); } //************************************************************************* diff --git a/test/test_string_char_external_buffer.cpp b/test/test_string_char_external_buffer.cpp index 4ef70851..3eb26da5 100644 --- a/test/test_string_char_external_buffer.cpp +++ b/test/test_string_char_external_buffer.cpp @@ -919,6 +919,67 @@ namespace CHECK_EQUAL(text.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 8UL; + + TextBuffer buffer{0}; + Text text(initial_text.c_str(), INITIAL_SIZE, buffer.data(), buffer.size()); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H234567")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_down) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 3UL; + + TextBuffer buffer{0}; + Text text(initial_text.c_str(), INITIAL_SIZE, buffer.data(), buffer.size()); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H2")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up_excess) + { + TextBuffer buffer{0}; + Text text(initial_text.c_str(), initial_text.size(), buffer.data(), buffer.size()); + + CHECK_THROW(text.resize_and_overwrite(text.capacity() + 1, [](Text::pointer /*p*/, size_t n) { return n; }), etl::string_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_empty_full) { @@ -1737,7 +1798,7 @@ namespace for (size_t offset = 0UL; offset <= short_text.size(); ++offset) { TextSTD compare_text(short_text.cbegin(), short_text.cend()); - TextBuffer buffer; + TextBuffer buffer{0}; buffer.fill(0); Text text(short_text.begin(), short_text.end(), buffer.data(), buffer.size()); @@ -1762,7 +1823,7 @@ namespace for (size_t offset = 0UL; offset <= short_text.size(); ++offset) { TextSTD compare_text(short_text.cbegin(), short_text.cend()); - TextBuffer buffer; + TextBuffer buffer{0}; buffer.fill(0); Text text(short_text.begin(), short_text.end(), buffer.data(), buffer.size()); View view(insert_text.data(), insert_text.size()); diff --git a/test/test_string_u16.cpp b/test/test_string_u16.cpp index f7565b01..37465e2a 100644 --- a/test/test_string_u16.cpp +++ b/test/test_string_u16.cpp @@ -783,6 +783,64 @@ namespace CHECK_EQUAL(text.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 8UL; + + Text text(initial_text.c_str(), INITIAL_SIZE); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H234567")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_down) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 3UL; + + Text text(initial_text.c_str(), INITIAL_SIZE); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H2")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up_excess) + { + Text text(initial_text.c_str(), initial_text.size()); + + CHECK_THROW(text.resize_and_overwrite(text.capacity() + 1, [](Text::pointer /*p*/, size_t n) { return n; }), etl::string_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_fill) { diff --git a/test/test_string_u16_external_buffer.cpp b/test/test_string_u16_external_buffer.cpp index b15a4772..c94fdad0 100644 --- a/test/test_string_u16_external_buffer.cpp +++ b/test/test_string_u16_external_buffer.cpp @@ -933,6 +933,41 @@ namespace CHECK_EQUAL(text.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_down) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 3UL; + + TextBuffer buffer{0}; + Text text(initial_text.c_str(), INITIAL_SIZE, buffer.data(), buffer.size()); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H2")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up_excess) + { + TextBuffer buffer{0}; + Text text(initial_text.c_str(), initial_text.size(), buffer.data(), buffer.size()); + + CHECK_THROW(text.resize_and_overwrite(text.capacity() + 1, [](Text::pointer /*p*/, size_t n) { return n; }), etl::string_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_empty_full) { @@ -1751,7 +1786,7 @@ namespace for (size_t offset = 0UL; offset <= short_text.size(); ++offset) { TextSTD compare_text(short_text.cbegin(), short_text.cend()); - TextBuffer buffer; + TextBuffer buffer{0}; buffer.fill(0); Text text(short_text.begin(), short_text.end(), buffer.data(), buffer.size()); @@ -1776,7 +1811,7 @@ namespace for (size_t offset = 0UL; offset <= short_text.size(); ++offset) { TextSTD compare_text(short_text.cbegin(), short_text.cend()); - TextBuffer buffer; + TextBuffer buffer{0}; buffer.fill(0); Text text(short_text.begin(), short_text.end(), buffer.data(), buffer.size()); View view(insert_text.data(), insert_text.size()); diff --git a/test/test_string_u32.cpp b/test/test_string_u32.cpp index b27fcaff..b72ef01e 100644 --- a/test/test_string_u32.cpp +++ b/test/test_string_u32.cpp @@ -783,6 +783,64 @@ namespace CHECK_EQUAL(text.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 8UL; + + Text text(initial_text.c_str(), INITIAL_SIZE); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H234567")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_down) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 3UL; + + Text text(initial_text.c_str(), INITIAL_SIZE); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H2")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up_excess) + { + Text text(initial_text.c_str(), initial_text.size()); + + CHECK_THROW(text.resize_and_overwrite(text.capacity() + 1, [](Text::pointer /*p*/, size_t n) { return n; }), etl::string_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_fill) { diff --git a/test/test_string_u32_external_buffer.cpp b/test/test_string_u32_external_buffer.cpp index 11858685..f5e25510 100644 --- a/test/test_string_u32_external_buffer.cpp +++ b/test/test_string_u32_external_buffer.cpp @@ -933,6 +933,41 @@ namespace CHECK_EQUAL(text.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_down) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 3UL; + + TextBuffer buffer{0}; + Text text(initial_text.c_str(), INITIAL_SIZE, buffer.data(), buffer.size()); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H2")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up_excess) + { + TextBuffer buffer{0}; + Text text(initial_text.c_str(), initial_text.size(), buffer.data(), buffer.size()); + + CHECK_THROW(text.resize_and_overwrite(text.capacity() + 1, [](Text::pointer /*p*/, size_t n) { return n; }), etl::string_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_empty_full) { @@ -1751,7 +1786,7 @@ namespace for (size_t offset = 0UL; offset <= short_text.size(); ++offset) { TextSTD compare_text(short_text.cbegin(), short_text.cend()); - TextBuffer buffer; + TextBuffer buffer{0}; buffer.fill(0); Text text(short_text.begin(), short_text.end(), buffer.data(), buffer.size()); @@ -1776,7 +1811,7 @@ namespace for (size_t offset = 0UL; offset <= short_text.size(); ++offset) { TextSTD compare_text(short_text.cbegin(), short_text.cend()); - TextBuffer buffer; + TextBuffer buffer{0}; buffer.fill(0); Text text(short_text.begin(), short_text.end(), buffer.data(), buffer.size()); View view(insert_text.data(), insert_text.size()); diff --git a/test/test_string_u8.cpp b/test/test_string_u8.cpp index d8d9c6a3..e3d5c1fa 100644 --- a/test/test_string_u8.cpp +++ b/test/test_string_u8.cpp @@ -786,6 +786,64 @@ namespace CHECK_EQUAL(text.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 8UL; + + Text text(initial_text.c_str(), INITIAL_SIZE); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H234567")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_down) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 3UL; + + Text text(initial_text.c_str(), INITIAL_SIZE); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H2")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up_excess) + { + Text text(initial_text.c_str(), initial_text.size()); + + CHECK_THROW(text.resize_and_overwrite(text.capacity() + 1, [](Text::pointer /*p*/, size_t n) { return n; }), etl::string_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_fill) { diff --git a/test/test_string_u8_external_buffer.cpp b/test/test_string_u8_external_buffer.cpp index e7613008..5f64fb37 100644 --- a/test/test_string_u8_external_buffer.cpp +++ b/test/test_string_u8_external_buffer.cpp @@ -936,6 +936,67 @@ namespace CHECK_EQUAL(text.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 8UL; + + TextBuffer buffer{0}; + Text text(initial_text.c_str(), INITIAL_SIZE, buffer.data(), buffer.size()); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H234567")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_down) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 3UL; + + TextBuffer buffer{0}; + Text text(initial_text.c_str(), INITIAL_SIZE, buffer.data(), buffer.size()); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H2")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up_excess) + { + TextBuffer buffer{0}; + Text text(initial_text.c_str(), initial_text.size(), buffer.data(), buffer.size()); + + CHECK_THROW(text.resize_and_overwrite(text.capacity() + 1, [](Text::pointer /*p*/, size_t n) { return n; }), etl::string_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_empty_full) { @@ -1754,7 +1815,7 @@ namespace for (size_t offset = 0UL; offset <= short_text.size(); ++offset) { TextSTD compare_text(short_text.cbegin(), short_text.cend()); - TextBuffer buffer; + TextBuffer buffer{0}; buffer.fill(0); Text text(short_text.begin(), short_text.end(), buffer.data(), buffer.size()); @@ -1779,7 +1840,7 @@ namespace for (size_t offset = 0UL; offset <= short_text.size(); ++offset) { TextSTD compare_text(short_text.cbegin(), short_text.cend()); - TextBuffer buffer; + TextBuffer buffer{0}; buffer.fill(0); Text text(short_text.begin(), short_text.end(), buffer.data(), buffer.size()); View view(insert_text.data(), insert_text.size()); diff --git a/test/test_string_wchar_t.cpp b/test/test_string_wchar_t.cpp index 8b3ae997..81b2d8bb 100644 --- a/test/test_string_wchar_t.cpp +++ b/test/test_string_wchar_t.cpp @@ -784,6 +784,64 @@ namespace CHECK_EQUAL(text.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 8UL; + + Text text(initial_text.c_str(), INITIAL_SIZE); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H234567")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_down) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 3UL; + + Text text(initial_text.c_str(), INITIAL_SIZE); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) noexcept + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H2")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up_excess) + { + Text text(initial_text.c_str(), initial_text.size()); + + CHECK_THROW(text.resize_and_overwrite(text.capacity() + 1, [](Text::pointer /*p*/, size_t n) { return n; }), etl::string_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_fill) { diff --git a/test/test_string_wchar_t_external_buffer.cpp b/test/test_string_wchar_t_external_buffer.cpp index 7ce900d0..886c7b70 100644 --- a/test/test_string_wchar_t_external_buffer.cpp +++ b/test/test_string_wchar_t_external_buffer.cpp @@ -936,6 +936,41 @@ namespace CHECK_EQUAL(text.size(), NEW_SIZE); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_down) + { + const size_t INITIAL_SIZE = 5UL; + const size_t NEW_SIZE = 3UL; + + TextBuffer buffer{0}; + Text text(initial_text.c_str(), INITIAL_SIZE, buffer.data(), buffer.size()); + + // Overwrite from index 1 to one less than the new size and set to that size. + text.resize_and_overwrite(NEW_SIZE, [](Text::pointer p, size_t n) + { + size_t i = 1; + while (i < (n - 1)) + { + p[i] = '1' + Text::value_type(i); + ++i; + } + + return i; + }); + + CHECK_EQUAL(NEW_SIZE - 1, text.size()); + CHECK_TRUE(Equal(TextSTD(STR("H2")), text)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_resize_and_overwrite_up_excess) + { + TextBuffer buffer{0}; + Text text(initial_text.c_str(), initial_text.size(), buffer.data(), buffer.size()); + + CHECK_THROW(text.resize_and_overwrite(text.capacity() + 1, [](Text::pointer /*p*/, size_t n) { return n; }), etl::string_out_of_bounds); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_empty_full) { @@ -1754,7 +1789,7 @@ namespace for (size_t offset = 0UL; offset <= short_text.size(); ++offset) { TextSTD compare_text(short_text.cbegin(), short_text.cend()); - TextBuffer buffer; + TextBuffer buffer{0}; buffer.fill(0); Text text(short_text.begin(), short_text.end(), buffer.data(), buffer.size()); @@ -1779,7 +1814,7 @@ namespace for (size_t offset = 0UL; offset <= short_text.size(); ++offset) { TextSTD compare_text(short_text.cbegin(), short_text.cend()); - TextBuffer buffer; + TextBuffer buffer{0}; buffer.fill(0); Text text(short_text.begin(), short_text.end(), buffer.data(), buffer.size()); View view(insert_text.data(), insert_text.size());