Added member function resize_and_overwrite

This commit is contained in:
John Wellbelove 2024-11-24 09:01:33 +00:00
parent 89123357a6
commit f566076f42
11 changed files with 550 additions and 17 deletions

View File

@ -496,6 +496,22 @@ namespace etl
cleanup();
}
//*********************************************************************
/// Resizes the string and overwrites to data using the operation.
//*********************************************************************
template <typename TOperation>
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<T>& str) const
bool starts_with(const etl::ibasic_string<T>& 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 <typename TTraits>
bool starts_with(const basic_string_view<T, TTraits>& view) const
bool starts_with(const etl::basic_string_view<T, TTraits>& 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<T>& str) const
bool ends_with(const etl::ibasic_string<T>& str) const
{
if (str.size() > size())
{
@ -1581,7 +1597,7 @@ namespace etl
/// Checks that the view is the end of this string
//*********************************************************************
template <typename TTraits>
bool ends_with(const basic_string_view<T, TTraits>& view) const
bool ends_with(const etl::basic_string_view<T, TTraits>& view) const
{
if (view.size() > size())
{

View File

@ -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);
}
//*************************************************************************

View File

@ -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());

View File

@ -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)
{

View File

@ -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());

View File

@ -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)
{

View File

@ -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());

View File

@ -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)
{

View File

@ -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());

View File

@ -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)
{

View File

@ -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());