Merge branch 'feature/c-helper-interface-for-strings' into development

# Conflicts:
#	support/Release notes.txt
This commit is contained in:
John Wellbelove 2021-02-18 13:13:30 +00:00
commit 679bea0311
13 changed files with 442 additions and 90 deletions

View File

@ -582,6 +582,24 @@ namespace etl
return p_buffer;
}
//*********************************************************************
/// Returns a pointer to the beginning of the string data.
///\return A pointer to the beginning of the string data.
//*********************************************************************
pointer data_end()
{
return p_buffer + current_size;
}
//*********************************************************************
/// Returns a const pointer to the beginning of the string data.
///\return A const pointer to the beginning of the string data.
//*********************************************************************
const_pointer data_end() const
{
return p_buffer + current_size;
}
//*********************************************************************
/// Assigns values to the string.
/// Truncates if the string does not have enough free space.
@ -2185,20 +2203,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_to_terminator()
{
#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

@ -7,7 +7,7 @@ Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2018 jwellbelove
Copyright(c) 2021 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
@ -38,8 +38,8 @@ SOFTWARE.
///\ingroup utilities
#define ETL_VERSION_MAJOR 20
#define ETL_VERSION_MINOR 2
#define ETL_VERSION_PATCH 3
#define ETL_VERSION_MINOR 3
#define ETL_VERSION_PATCH 0
#define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) "." ETL_STRINGIFY(ETL_VERSION_MINOR) "." ETL_STRINGIFY(ETL_VERSION_PATCH)
#define ETL_VERSION_W ETL_STRINGIFY(ETL_VERSION_MAJOR) L"." ETL_STRINGIFY(ETL_VERSION_MINOR) L"." ETL_STRINGIFY(ETL_VERSION_PATCH)
#define ETL_VERSION_U16 ETL_STRINGIFY(ETL_VERSION_MAJOR) u"." ETL_STRINGIFY(ETL_VERSION_MINOR) u"." ETL_STRINGIFY(ETL_VERSION_PATCH)

View File

@ -1,6 +1,6 @@
{
"name": "ETL Embedded Template Library",
"version": "20.2.3",
"version": "20.3.0",
"author s": {
"name": "John Wellbelove",
"email": "john.wellbelove@etlcpp.com"

View File

@ -1,5 +1,5 @@
name=Embedded Template Library ETL
version=20.2.3
version=20.3.0
author= John Wellbelove <john.wellbelove@etlcpp.com>
maintainer=John Wellbelove <john.wellbelove@etlcpp.com>
license=MIT

View File

@ -1,6 +1,11 @@
===============================================================================
20.3.0
Added initialize_free_space(), trim_to_terminator() and data_end() functions to
strings for easier integration with C APIs.
===============================================================================
20.2.3
Fixed indexing error in find_next for etl::bitset.
Fixed indexing error in find_next() for etl::bitset.
===============================================================================
20.2.2

View File

@ -874,7 +874,7 @@ namespace
Text text(compare_text.begin(), compare_text.end());
bool is_equal = std::equal(text.data(),
text.data() + text.size(),
text.data_end(),
compare_text.begin());
CHECK(is_equal);
@ -891,7 +891,7 @@ namespace
const Text text(compare_text.begin(), compare_text.end());
bool is_equal = std::equal(text.data(),
text.data() + text.size(),
text.data_end(),
compare_text.begin());
CHECK(is_equal);
@ -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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
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_to_terminator();
CHECK(text.is_truncated());
CHECK_EQUAL(text.max_size(), text.size());
}
};