Interface for C string handling.

This commit is contained in:
John Wellbelove 2021-02-17 09:02:04 +00:00
parent 0ee3574a14
commit 7eaf3e13a7
9 changed files with 411 additions and 82 deletions

View File

@ -2185,20 +2185,29 @@ namespace etl
#endif
//*********************************************************************
/// Update the size to the distnace to the first null, or max size.
/// Clears the free space to string terminator value.
//*********************************************************************
void update_size()
void initialize_free_space()
{
p_buffer[max_size()] = 0; // Ensure a terminating null.
#if ETL_STRING_TRUNCATION_CHECKS_ENABLED
set_truncated(false);
#endif
etl::fill(&p_buffer[current_size], &p_buffer[CAPACITY + 1U], T(0));
}
size_t i = 0U;
//*********************************************************************
/// Trim the size to the distance to the first null terminator.
/// If the last buffer position has a non-null value then the truncated
/// flag will be set.
//*********************************************************************
void trim()
{
#if ETL_STRING_TRUNCATION_CHECKS_ENABLED
set_truncated(p_buffer[CAPACITY] != T(0));
#endif
while ((i != max_size()) && (p_buffer[i] != 0))
{
++i;
}
current_size = i;
p_buffer[CAPACITY] = T(0); // Ensure a terminating null.
current_size = etl::strlen(p_buffer);
}
protected:

View File

@ -4158,15 +4158,51 @@ namespace
}
#endif
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_empty_string)
{
Text empty;
Text text;
text.initialize_free_space();
CHECK(text.empty());
CHECK(text == empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_part_filled_string)
{
Text empty;
Text initial = STR("ABC");
Text text(initial);
text.initialize_free_space();
CHECK(text == initial);
CHECK(text != empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
@ -4175,10 +4211,11 @@ namespace
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size() - 1, text.size());
}
@ -4187,10 +4224,11 @@ namespace
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
text.update_size();
text.trim();
CHECK(text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
};

View File

@ -4520,42 +4520,84 @@ namespace
}
#endif
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_empty_string)
{
TextBuffer buffer1;
TextBuffer buffer2;
Text text(buffer1.data(), buffer1.size());
Text empty(buffer2.data(), buffer2.size());
text.initialize_free_space();
CHECK(text.empty());
CHECK(text == empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_part_filled_string)
{
TextBuffer buffer1;
TextBuffer buffer2;
Text initial(STR("ABC"), buffer1.data(), buffer1.size());
Text empty(buffer2.data(), buffer2.size());
Text text(initial, buffer2.data(), buffer2.size());
text.initialize_free_space();
CHECK(text == initial);
CHECK(text != empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size() - 1, text.size());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
text.update_size();
text.trim();
CHECK(text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
};

View File

@ -4158,15 +4158,51 @@ namespace
}
#endif
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_empty_string)
{
Text empty;
Text text;
text.initialize_free_space();
CHECK(text.empty());
CHECK(text == empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_part_filled_string)
{
Text empty;
Text initial = STR("ABC");
Text text(initial);
text.initialize_free_space();
CHECK(text == initial);
CHECK(text != empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
@ -4175,10 +4211,11 @@ namespace
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size() - 1, text.size());
}
@ -4187,10 +4224,11 @@ namespace
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
text.update_size();
text.trim();
CHECK(text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
};

View File

@ -4520,42 +4520,84 @@ namespace
}
#endif
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_empty_string)
{
TextBuffer buffer1;
TextBuffer buffer2;
Text text(buffer1.data(), buffer1.size());
Text empty(buffer2.data(), buffer2.size());
text.initialize_free_space();
CHECK(text.empty());
CHECK(text == empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_part_filled_string)
{
TextBuffer buffer1;
TextBuffer buffer2;
Text initial(STR("ABC"), buffer1.data(), buffer1.size());
Text empty(buffer2.data(), buffer2.size());
Text text(initial, buffer2.data(), buffer2.size());
text.initialize_free_space();
CHECK(text == initial);
CHECK(text != empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size() - 1, text.size());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
text.update_size();
text.trim();
CHECK(text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
};

View File

@ -4158,15 +4158,51 @@ namespace
}
#endif
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_empty_string)
{
Text empty;
Text text;
text.initialize_free_space();
CHECK(text.empty());
CHECK(text == empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_part_filled_string)
{
Text empty;
Text initial = STR("ABC");
Text text(initial);
text.initialize_free_space();
CHECK(text == initial);
CHECK(text != empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
@ -4175,10 +4211,11 @@ namespace
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size() - 1, text.size());
}
@ -4187,10 +4224,11 @@ namespace
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
text.update_size();
text.trim();
CHECK(text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
};

View File

@ -4503,42 +4503,84 @@ namespace
}
#endif
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_empty_string)
{
TextBuffer buffer1;
TextBuffer buffer2;
Text text(buffer1.data(), buffer1.size());
Text empty(buffer2.data(), buffer2.size());
text.initialize_free_space();
CHECK(text.empty());
CHECK(text == empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_part_filled_string)
{
TextBuffer buffer1;
TextBuffer buffer2;
Text initial(STR("ABC"), buffer1.data(), buffer1.size());
Text empty(buffer2.data(), buffer2.size());
Text text(initial, buffer2.data(), buffer2.size());
text.initialize_free_space();
CHECK(text == initial);
CHECK(text != empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size() - 1, text.size());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
text.update_size();
text.trim();
CHECK(text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
};

View File

@ -4158,15 +4158,51 @@ namespace
}
#endif
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_empty_string)
{
Text empty;
Text text;
text.initialize_free_space();
CHECK(text.empty());
CHECK(text == empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_part_filled_string)
{
Text empty;
Text initial = STR("ABC");
Text text(initial);
text.initialize_free_space();
CHECK(text == initial);
CHECK(text != empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
@ -4175,10 +4211,11 @@ namespace
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size() - 1, text.size());
}
@ -4187,10 +4224,11 @@ namespace
{
Text text;
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
text.update_size();
text.trim();
CHECK(text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
};

View File

@ -4503,42 +4503,84 @@ namespace
}
#endif
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_empty_string)
{
TextBuffer buffer1;
TextBuffer buffer2;
Text text(buffer1.data(), buffer1.size());
Text empty(buffer2.data(), buffer2.size());
text.initialize_free_space();
CHECK(text.empty());
CHECK(text == empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_initialize_free_space_part_filled_string)
{
TextBuffer buffer1;
TextBuffer buffer2;
Text initial(STR("ABC"), buffer1.data(), buffer1.size());
Text empty(buffer2.data(), buffer2.size());
Text text(initial, buffer2.data(), buffer2.size());
text.initialize_free_space();
CHECK(text == initial);
CHECK(text != empty);
for (size_t i = text.size(); i < text.max_size(); ++i)
{
CHECK_EQUAL(0, text[i]);
}
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_max_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size(), STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_shorter_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() - 1, STR('A'));
text.update_size();
text.trim();
CHECK(!text.is_truncated());
CHECK_EQUAL(text.max_size() - 1, text.size());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_update_after_c_string_greater_size)
{
TextBuffer buffer;
Text text(buffer.data(), buffer.size());
TextBuffer buffer1;
Text text(buffer1.data(), buffer1.size());
text.resize(text.max_size());
text.initialize_free_space();
std::fill(text.data(), text.data() + text.max_size() + 1, STR('A')); // Overwrites to terminating null.
text.update_size();
text.trim();
CHECK(text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
};