Deduce underlying storage size when constructing string_ext from char[]. (#1269)

* Deduce underlying storage size when constructing string_ext from char[].

This removes the need for passing sizeof(storage) to the constructor.

* Add array constructors for the other string types.

- u16string_ext
- u32string_ext
- u8string_ext
- wstring_ext

* Add additional constructors to match existing API.

* Fix inconsistent test argument order.

---------

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
This commit is contained in:
Marco Nilsson 2026-02-09 12:01:20 +01:00 committed by John Wellbelove
parent 13c90eb8f2
commit dc0f62cc3f
10 changed files with 1545 additions and 5 deletions

View File

@ -305,6 +305,17 @@ namespace etl
this->initialise();
}
//*************************************************************************
/// Constructor, from array buffer.
///\param buffer The array buffer.
//*************************************************************************
template <size_t Size>
string_ext(value_type (&buffer)[Size])
: istring(buffer, Size - 1U)
{
this->initialise();
}
//*************************************************************************
/// Copy constructor.
///\param other The other string_ext.
@ -341,6 +352,26 @@ namespace etl
}
}
//*************************************************************************
/// From other istring, from array buffer.
///\param other The other istring.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
string_ext(const etl::istring& other, value_type (&buffer)[BufferSize])
: istring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(other.data()))
{
this->current_size = other.size();
}
else
{
this->initialise();
this->assign(other);
}
}
//*************************************************************************
/// From other string_ext, position, length.
///\param other The other string_ext.
@ -363,6 +394,30 @@ namespace etl
}
}
//*************************************************************************
/// From other string_ext, position, length, from array buffer.
///\param other The other string_ext.
///\param buffer The array buffer.
///\param position The position of the first character.
///\param length The number of characters. Default = npos.
//*************************************************************************
template <size_t BufferSize>
string_ext(const etl::istring& other, value_type (&buffer)[BufferSize], size_type position, size_type length = npos)
: istring(buffer, BufferSize - 1U)
{
ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
if (this->is_within_buffer(other.data()))
{
this->current_size = other.size();
}
else
{
this->initialise();
this->assign(other, position, length);
}
}
//*************************************************************************
/// Constructor, from null terminated text.
///\param text The initial text of the string_ext.
@ -383,6 +438,27 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated text, from array buffer.
///\param text The initial text of the string_ext.
///\param buffer The array buffer.
//*************************************************************************
template <typename TPointer, size_t BufferSize>
string_ext(TPointer text, value_type (&buffer)[BufferSize],
typename etl::enable_if<etl::is_same<const value_type*, TPointer>::value, int>::type* = ETL_NULLPTR)
: istring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(text))
{
this->current_size = etl::strlen(buffer);
}
else
{
this->initialise();
this->assign(text, text + etl::strlen(text));
}
}
//*************************************************************************
/// Constructor, from null terminated literal text.
///\param text The initial text of the string_ext.
@ -402,6 +478,26 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated literal text, from array buffer.
///\param literal The initial text of the string_ext.
///\param buffer The array buffer.
//*************************************************************************
template <size_t LiteralSize, size_t BufferSize>
string_ext(const value_type (&literal)[LiteralSize], value_type (&buffer)[BufferSize])
: istring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(literal))
{
this->current_size = etl::strlen(literal);
}
else
{
this->initialise();
this->assign(literal);
}
}
//*************************************************************************
/// Constructor, from null terminated text and count.
///\param text The initial text of the string_ext.
@ -421,6 +517,27 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated text and count, from array buffer.
///\param text The initial text of the string_ext.
///\param count The number of characters to copy.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
string_ext(const value_type* text, size_type count, value_type (&buffer)[BufferSize])
: istring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(text))
{
this->current_size = count;
}
else
{
this->initialise();
this->assign(text, text + count);
}
}
//*************************************************************************
/// Constructor, from initial size and value.
///\param initialSize The initial size of the string_ext.
@ -433,6 +550,20 @@ namespace etl
this->resize(count, c);
}
//*************************************************************************
/// Constructor, from initial size and value, from array buffer.
///\param count The initial size of the string_ext.
///\param c The value to fill the string_ext with.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
string_ext(size_type count, value_type c, value_type (&buffer)[BufferSize])
: istring(buffer, BufferSize - 1U)
{
this->initialise();
this->resize(count, c);
}
//*************************************************************************
/// From string_view.
///\param view The string_view.
@ -451,6 +582,26 @@ namespace etl
}
}
//*************************************************************************
/// From string_view, from array buffer.
///\param view The string_view.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
explicit string_ext(const etl::string_view& view, value_type (&buffer)[BufferSize])
: istring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(view.data()))
{
this->current_size = view.size();
}
else
{
this->initialise();
this->assign(view.begin(), view.end());
}
}
//*************************************************************************
/// Constructor, from an iterator range.
///\tparam TIterator The iterator type.
@ -469,7 +620,29 @@ namespace etl
{
this->initialise();
this->assign(first, last);
}
}
}
//*************************************************************************
/// Constructor, from an iterator range, from array buffer.
///\tparam TIterator The iterator type.
///\param first The iterator to the first element.
///\param last The iterator to the last element + 1.
///\param buffer The array buffer.
//*************************************************************************
template <typename TIterator, size_t BufferSize>
string_ext(TIterator first, TIterator last, value_type (&buffer)[BufferSize], typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
: istring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(etl::addressof(*first)))
{
this->current_size = etl::distance(first, last);
}
else
{
this->initialise();
this->assign(first, last);
}
}
#if ETL_HAS_INITIALIZER_LIST
@ -482,6 +655,19 @@ namespace etl
this->initialise();
this->assign(init.begin(), init.end());
}
//*************************************************************************
/// Construct from initializer_list, from array buffer.
///\param init The initializer_list.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
string_ext(std::initializer_list<value_type> init, value_type (&buffer)[BufferSize])
: istring(buffer, BufferSize - 1U)
{
this->initialise();
this->assign(init.begin(), init.end());
}
#endif
//*************************************************************************

View File

@ -285,6 +285,17 @@ namespace etl
this->initialise();
}
//*************************************************************************
/// Constructor, from array buffer.
///\param buffer The array buffer.
//*************************************************************************
template <size_t Size>
u16string_ext(value_type (&buffer)[Size])
: iu16string(buffer, Size - 1U)
{
this->initialise();
}
//*************************************************************************
/// Copy constructor.
///\param other The other u16string_ext.
@ -321,6 +332,26 @@ namespace etl
}
}
//*************************************************************************
/// From other iu16string, from array buffer.
///\param other The other iu16string.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u16string_ext(const etl::iu16string& other, value_type (&buffer)[BufferSize])
: iu16string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(other.data()))
{
this->current_size = other.size();
}
else
{
this->initialise();
this->assign(other);
}
}
//*************************************************************************
/// From other u16string_ext, position, length.
///\param other The other u16string_ext.
@ -343,6 +374,30 @@ namespace etl
}
}
//*************************************************************************
/// From other u16string_ext, position, length, from array buffer.
///\param other The other u16string_ext.
///\param buffer The array buffer.
///\param position The position of the first character.
///\param length The number of characters. Default = npos.
//*************************************************************************
template <size_t BufferSize>
u16string_ext(const etl::iu16string& other, value_type (&buffer)[BufferSize], size_type position, size_type length = npos)
: iu16string(buffer, BufferSize - 1U)
{
ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
if (this->is_within_buffer(other.data()))
{
this->current_size = other.size();
}
else
{
this->initialise();
this->assign(other, position, length);
}
}
//*************************************************************************
/// Constructor, from null terminated text.
///\param text The initial text of the u16string_ext.
@ -363,6 +418,27 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated text, from array buffer.
///\param text The initial text of the u16string_ext.
///\param buffer The array buffer.
//*************************************************************************
template <typename TPointer, size_t BufferSize>
u16string_ext(TPointer text, value_type (&buffer)[BufferSize],
typename etl::enable_if<etl::is_same<const value_type*, TPointer>::value, int>::type* = ETL_NULLPTR)
: iu16string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(text))
{
this->current_size = etl::strlen(buffer);
}
else
{
this->initialise();
this->assign(text, text + etl::strlen(text));
}
}
//*************************************************************************
/// Constructor, from null terminated literal text.
///\param text The initial text of the u16string_ext.
@ -382,6 +458,26 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated literal text, from array buffer.
///\param literal The initial text of the u16string_ext.
///\param buffer The array buffer.
//*************************************************************************
template <size_t LiteralSize, size_t BufferSize>
u16string_ext(const value_type (&literal)[LiteralSize], value_type (&buffer)[BufferSize])
: iu16string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(literal))
{
this->current_size = etl::strlen(literal);
}
else
{
this->initialise();
this->assign(literal);
}
}
//*************************************************************************
/// Constructor, from null terminated text and count.
///\param text The initial text of the u16string_ext.
@ -401,6 +497,27 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated text and count, from array buffer.
///\param text The initial text of the u16string_ext.
///\param count The number of characters to copy.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u16string_ext(const value_type* text, size_type count, value_type (&buffer)[BufferSize])
: iu16string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(text))
{
this->current_size = count;
}
else
{
this->initialise();
this->assign(text, text + count);
}
}
//*************************************************************************
/// Constructor, from initial size and value.
///\param initialSize The initial size of the u16string_ext.
@ -413,6 +530,20 @@ namespace etl
this->resize(count, c);
}
//*************************************************************************
/// Constructor, from initial size and value, from array buffer.
///\param count The initial size of the u16string_ext.
///\param c The value to fill the u16string_ext with.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u16string_ext(size_type count, value_type c, value_type (&buffer)[BufferSize])
: iu16string(buffer, BufferSize - 1U)
{
this->initialise();
this->resize(count, c);
}
//*************************************************************************
/// From u16string_view.
///\param view The u16string_view.
@ -431,6 +562,26 @@ namespace etl
}
}
//*************************************************************************
/// From u16string_view, from array buffer.
///\param view The u16string_view.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
explicit u16string_ext(const etl::u16string_view& view, value_type (&buffer)[BufferSize])
: iu16string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(view.data()))
{
this->current_size = view.size();
}
else
{
this->initialise();
this->assign(view.begin(), view.end());
}
}
//*************************************************************************
/// Constructor, from an iterator range.
///\tparam TIterator The iterator type.
@ -449,7 +600,29 @@ namespace etl
{
this->initialise();
this->assign(first, last);
}
}
}
//*************************************************************************
/// Constructor, from an iterator range, from array buffer.
///\tparam TIterator The iterator type.
///\param first The iterator to the first element.
///\param last The iterator to the last element + 1.
///\param buffer The array buffer.
//*************************************************************************
template <typename TIterator, size_t BufferSize>
u16string_ext(TIterator first, TIterator last, value_type (&buffer)[BufferSize], typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
: iu16string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(reinterpret_cast<const_iterator>(etl::addressof(*first))))
{
this->current_size = etl::distance(first, last);
}
else
{
this->initialise();
this->assign(first, last);
}
}
#if ETL_HAS_INITIALIZER_LIST
@ -462,6 +635,19 @@ namespace etl
this->initialise();
this->assign(init.begin(), init.end());
}
//*************************************************************************
/// Construct from initializer_list, from array buffer.
///\param init The initializer_list.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u16string_ext(std::initializer_list<value_type> init, value_type (&buffer)[BufferSize])
: iu16string(buffer, BufferSize - 1U)
{
this->initialise();
this->assign(init.begin(), init.end());
}
#endif
//*************************************************************************

View File

@ -285,6 +285,17 @@ namespace etl
this->initialise();
}
//*************************************************************************
/// Constructor, from array buffer.
///\param buffer The array buffer.
//*************************************************************************
template <size_t Size>
u32string_ext(value_type (&buffer)[Size])
: iu32string(buffer, Size - 1U)
{
this->initialise();
}
//*************************************************************************
/// Copy constructor.
///\param other The other u32string_ext.
@ -321,6 +332,26 @@ namespace etl
}
}
//*************************************************************************
/// From other iu32string, from array buffer.
///\param other The other iu32string.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u32string_ext(const etl::iu32string& other, value_type (&buffer)[BufferSize])
: iu32string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(other.data()))
{
this->current_size = other.size();
}
else
{
this->initialise();
this->assign(other);
}
}
//*************************************************************************
/// From other u32string_ext, position, length.
///\param other The other u32string_ext.
@ -343,6 +374,30 @@ namespace etl
}
}
//*************************************************************************
/// From other u32string_ext, position, length, from array buffer.
///\param other The other u32string_ext.
///\param buffer The array buffer.
///\param position The position of the first character.
///\param length The number of characters. Default = npos.
//*************************************************************************
template <size_t BufferSize>
u32string_ext(const etl::iu32string& other, value_type (&buffer)[BufferSize], size_type position, size_type length = npos)
: iu32string(buffer, BufferSize - 1U)
{
ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
if (this->is_within_buffer(other.data()))
{
this->current_size = other.size();
}
else
{
this->initialise();
this->assign(other, position, length);
}
}
//*************************************************************************
/// Constructor, from null terminated text.
///\param text The initial text of the u32string_ext.
@ -363,6 +418,27 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated text, from array buffer.
///\param text The initial text of the u32string_ext.
///\param buffer The array buffer.
//*************************************************************************
template <typename TPointer, size_t BufferSize>
u32string_ext(TPointer text, value_type (&buffer)[BufferSize],
typename etl::enable_if<etl::is_same<const value_type*, TPointer>::value, int>::type* = ETL_NULLPTR)
: iu32string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(text))
{
this->current_size = etl::strlen(buffer);
}
else
{
this->initialise();
this->assign(text, text + etl::strlen(text));
}
}
//*************************************************************************
/// Constructor, from null terminated literal text.
///\param text The initial text of the u32string_ext.
@ -382,6 +458,26 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated literal text, from array buffer.
///\param literal The initial text of the u32string_ext.
///\param buffer The array buffer.
//*************************************************************************
template <size_t LiteralSize, size_t BufferSize>
u32string_ext(const value_type (&literal)[LiteralSize], value_type (&buffer)[BufferSize])
: iu32string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(literal))
{
this->current_size = etl::strlen(literal);
}
else
{
this->initialise();
this->assign(literal);
}
}
//*************************************************************************
/// Constructor, from null terminated text and count.
///\param text The initial text of the u32string_ext.
@ -401,6 +497,27 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated text and count, from array buffer.
///\param text The initial text of the u32string_ext.
///\param count The number of characters to copy.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u32string_ext(const value_type* text, size_type count, value_type (&buffer)[BufferSize])
: iu32string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(text))
{
this->current_size = count;
}
else
{
this->initialise();
this->assign(text, text + count);
}
}
//*************************************************************************
/// Constructor, from initial size and value.
///\param initialSize The initial size of the u32string_ext.
@ -413,6 +530,20 @@ namespace etl
this->resize(count, c);
}
//*************************************************************************
/// Constructor, from initial size and value, from array buffer.
///\param count The initial size of the u32string_ext.
///\param c The value to fill the u32string_ext with.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u32string_ext(size_type count, value_type c, value_type (&buffer)[BufferSize])
: iu32string(buffer, BufferSize - 1U)
{
this->initialise();
this->resize(count, c);
}
//*************************************************************************
/// From u32string_view.
///\param view The u32string_view.
@ -431,6 +562,26 @@ namespace etl
}
}
//*************************************************************************
/// From u32string_view, from array buffer.
///\param view The u32string_view.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
explicit u32string_ext(const etl::u32string_view& view, value_type (&buffer)[BufferSize])
: iu32string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(view.data()))
{
this->current_size = view.size();
}
else
{
this->initialise();
this->assign(view.begin(), view.end());
}
}
//*************************************************************************
/// Constructor, from an iterator range.
///\tparam TIterator The iterator type.
@ -449,7 +600,29 @@ namespace etl
{
this->initialise();
this->assign(first, last);
}
}
}
//*************************************************************************
/// Constructor, from an iterator range, from array buffer.
///\tparam TIterator The iterator type.
///\param first The iterator to the first element.
///\param last The iterator to the last element + 1.
///\param buffer The array buffer.
//*************************************************************************
template <typename TIterator, size_t BufferSize>
u32string_ext(TIterator first, TIterator last, value_type (&buffer)[BufferSize], typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
: iu32string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(reinterpret_cast<const_iterator>(etl::addressof(*first))))
{
this->current_size = etl::distance(first, last);
}
else
{
this->initialise();
this->assign(first, last);
}
}
#if ETL_HAS_INITIALIZER_LIST
@ -462,6 +635,19 @@ namespace etl
this->initialise();
this->assign(init.begin(), init.end());
}
//*************************************************************************
/// Construct from initializer_list, from array buffer.
///\param init The initializer_list.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u32string_ext(std::initializer_list<value_type> init, value_type (&buffer)[BufferSize])
: iu32string(buffer, BufferSize - 1U)
{
this->initialise();
this->assign(init.begin(), init.end());
}
#endif
//*************************************************************************

View File

@ -305,6 +305,17 @@ namespace etl
this->initialise();
}
//*************************************************************************
/// Constructor, from array buffer.
///\param buffer The array buffer.
//*************************************************************************
template <size_t Size>
u8string_ext(value_type (&buffer)[Size])
: iu8string(buffer, Size - 1U)
{
this->initialise();
}
//*************************************************************************
/// Copy constructor.
///\param other The other u8string_ext.
@ -341,6 +352,26 @@ namespace etl
}
}
//*************************************************************************
/// From other iu8string, from array buffer.
///\param other The other iu8string.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u8string_ext(const etl::iu8string& other, value_type (&buffer)[BufferSize])
: iu8string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(other.data()))
{
this->current_size = other.size();
}
else
{
this->initialise();
this->assign(other);
}
}
//*************************************************************************
/// From other u8string_ext, position, length.
///\param other The other u8string_ext.
@ -363,6 +394,30 @@ namespace etl
}
}
//*************************************************************************
/// From other u8string_ext, position, length, from array buffer.
///\param other The other u8string_ext.
///\param buffer The array buffer.
///\param position The position of the first character.
///\param length The number of characters. Default = npos.
//*************************************************************************
template <size_t BufferSize>
u8string_ext(const etl::iu8string& other, value_type (&buffer)[BufferSize], size_type position, size_type length = npos)
: iu8string(buffer, BufferSize - 1U)
{
ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
if (this->is_within_buffer(other.data()))
{
this->current_size = other.size();
}
else
{
this->initialise();
this->assign(other, position, length);
}
}
//*************************************************************************
/// Constructor, from null terminated text.
///\param text The initial text of the u8string_ext.
@ -383,6 +438,27 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated text, from array buffer.
///\param text The initial text of the u8string_ext.
///\param buffer The array buffer.
//*************************************************************************
template <typename TPointer, size_t BufferSize>
u8string_ext(TPointer text, value_type (&buffer)[BufferSize],
typename etl::enable_if<etl::is_same<const value_type*, TPointer>::value, int>::type* = ETL_NULLPTR)
: iu8string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(text))
{
this->current_size = etl::strlen(buffer);
}
else
{
this->initialise();
this->assign(text, text + etl::strlen(text));
}
}
//*************************************************************************
/// Constructor, from null terminated literal text.
///\param text The initial text of the u8string_ext.
@ -402,6 +478,26 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated literal text, from array buffer.
///\param literal The initial text of the u8string_ext.
///\param buffer The array buffer.
//*************************************************************************
template <size_t LiteralSize, size_t BufferSize>
u8string_ext(const value_type (&literal)[LiteralSize], value_type (&buffer)[BufferSize])
: iu8string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(literal))
{
this->current_size = etl::strlen(literal);
}
else
{
this->initialise();
this->assign(literal);
}
}
//*************************************************************************
/// Constructor, from null terminated text and count.
///\param text The initial text of the u8string_ext.
@ -421,6 +517,27 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated text and count, from array buffer.
///\param text The initial text of the u8string_ext.
///\param count The number of characters to copy.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u8string_ext(const value_type* text, size_type count, value_type (&buffer)[BufferSize])
: iu8string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(text))
{
this->current_size = count;
}
else
{
this->initialise();
this->assign(text, text + count);
}
}
//*************************************************************************
/// Constructor, from initial size and value.
///\param initialSize The initial size of the u8string_ext.
@ -433,6 +550,20 @@ namespace etl
this->resize(count, c);
}
//*************************************************************************
/// Constructor, from initial size and value, from array buffer.
///\param count The initial size of the u8string_ext.
///\param c The value to fill the u8string_ext with.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u8string_ext(size_type count, value_type c, value_type (&buffer)[BufferSize])
: iu8string(buffer, BufferSize - 1U)
{
this->initialise();
this->resize(count, c);
}
//*************************************************************************
/// From u8string_view.
///\param view The u8string_view.
@ -451,6 +582,26 @@ namespace etl
}
}
//*************************************************************************
/// From u8string_view, from array buffer.
///\param view The u8string_view.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
explicit u8string_ext(const etl::u8string_view& view, value_type (&buffer)[BufferSize])
: iu8string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(view.data()))
{
this->current_size = view.size();
}
else
{
this->initialise();
this->assign(view.begin(), view.end());
}
}
//*************************************************************************
/// Constructor, from an iterator range.
///\tparam TIterator The iterator type.
@ -469,7 +620,29 @@ namespace etl
{
this->initialise();
this->assign(first, last);
}
}
}
//*************************************************************************
/// Constructor, from an iterator range, from array buffer.
///\tparam TIterator The iterator type.
///\param first The iterator to the first element.
///\param last The iterator to the last element + 1.
///\param buffer The array buffer.
//*************************************************************************
template <typename TIterator, size_t BufferSize>
u8string_ext(TIterator first, TIterator last, value_type (&buffer)[BufferSize], typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
: iu8string(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(etl::addressof(*first)))
{
this->current_size = etl::distance(first, last);
}
else
{
this->initialise();
this->assign(first, last);
}
}
#if ETL_HAS_INITIALIZER_LIST
@ -482,6 +655,19 @@ namespace etl
this->initialise();
this->assign(init.begin(), init.end());
}
//*************************************************************************
/// Construct from initializer_list, from array buffer.
///\param init The initializer_list.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
u8string_ext(std::initializer_list<value_type> init, value_type (&buffer)[BufferSize])
: iu8string(buffer, BufferSize - 1U)
{
this->initialise();
this->assign(init.begin(), init.end());
}
#endif
//*************************************************************************

View File

@ -285,6 +285,17 @@ namespace etl
this->initialise();
}
//*************************************************************************
/// Constructor, from array buffer.
///\param buffer The array buffer.
//*************************************************************************
template <size_t Size>
wstring_ext(value_type (&buffer)[Size])
: iwstring(buffer, Size - 1U)
{
this->initialise();
}
//*************************************************************************
/// Copy constructor.
///\param other The other wstring_ext.
@ -321,6 +332,26 @@ namespace etl
}
}
//*************************************************************************
/// From other iwstring, from array buffer.
///\param other The other iwstring.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
wstring_ext(const etl::iwstring& other, value_type (&buffer)[BufferSize])
: iwstring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(other.data()))
{
this->current_size = other.size();
}
else
{
this->initialise();
this->assign(other);
}
}
//*************************************************************************
/// From other wstring_ext, position, length.
///\param other The other wstring_ext.
@ -343,6 +374,30 @@ namespace etl
}
}
//*************************************************************************
/// From other wstring_ext, position, length, from array buffer.
///\param other The other wstring_ext.
///\param buffer The array buffer.
///\param position The position of the first character.
///\param length The number of characters. Default = npos.
//*************************************************************************
template <size_t BufferSize>
wstring_ext(const etl::iwstring& other, value_type (&buffer)[BufferSize], size_type position, size_type length = npos)
: iwstring(buffer, BufferSize - 1U)
{
ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds));
if (this->is_within_buffer(other.data()))
{
this->current_size = other.size();
}
else
{
this->initialise();
this->assign(other, position, length);
}
}
//*************************************************************************
/// Constructor, from null terminated text.
///\param text The initial text of the wstring_ext.
@ -363,6 +418,27 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated text, from array buffer.
///\param text The initial text of the wstring_ext.
///\param buffer The array buffer.
//*************************************************************************
template <typename TPointer, size_t BufferSize>
wstring_ext(TPointer text, value_type (&buffer)[BufferSize],
typename etl::enable_if<etl::is_same<const value_type*, TPointer>::value, int>::type* = ETL_NULLPTR)
: iwstring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(text))
{
this->current_size = etl::strlen(buffer);
}
else
{
this->initialise();
this->assign(text, text + etl::strlen(text));
}
}
//*************************************************************************
/// Constructor, from null terminated literal text.
///\param text The initial text of the wstring_ext.
@ -382,6 +458,26 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated literal text, from array buffer.
///\param literal The initial text of the wstring_ext.
///\param buffer The array buffer.
//*************************************************************************
template <size_t LiteralSize, size_t BufferSize>
wstring_ext(const value_type (&literal)[LiteralSize], value_type (&buffer)[BufferSize])
: iwstring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(literal))
{
this->current_size = etl::strlen(literal);
}
else
{
this->initialise();
this->assign(literal);
}
}
//*************************************************************************
/// Constructor, from null terminated text and count.
///\param text The initial text of the wstring_ext.
@ -401,6 +497,27 @@ namespace etl
}
}
//*************************************************************************
/// Constructor, from null terminated text and count, from array buffer.
///\param text The initial text of the wstring_ext.
///\param count The number of characters to copy.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
wstring_ext(const value_type* text, size_type count, value_type (&buffer)[BufferSize])
: iwstring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(text))
{
this->current_size = count;
}
else
{
this->initialise();
this->assign(text, text + count);
}
}
//*************************************************************************
/// Constructor, from initial size and value.
///\param initialSize The initial size of the wstring_ext.
@ -413,6 +530,20 @@ namespace etl
this->resize(count, c);
}
//*************************************************************************
/// Constructor, from initial size and value, from array buffer.
///\param count The initial size of the wstring_ext.
///\param c The value to fill the wstring_ext with.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
wstring_ext(size_type count, value_type c, value_type (&buffer)[BufferSize])
: iwstring(buffer, BufferSize - 1U)
{
this->initialise();
this->resize(count, c);
}
//*************************************************************************
/// From wstring_view.
///\param view The wstring_view.
@ -431,6 +562,26 @@ namespace etl
}
}
//*************************************************************************
/// From wstring_view, from array buffer.
///\param view The wstring_view.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
explicit wstring_ext(const etl::wstring_view& view, value_type (&buffer)[BufferSize])
: iwstring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(view.data()))
{
this->current_size = view.size();
}
else
{
this->initialise();
this->assign(view.begin(), view.end());
}
}
//*************************************************************************
/// Constructor, from an iterator range.
///\tparam TIterator The iterator type.
@ -449,7 +600,29 @@ namespace etl
{
this->initialise();
this->assign(first, last);
}
}
}
//*************************************************************************
/// Constructor, from an iterator range, from array buffer.
///\tparam TIterator The iterator type.
///\param first The iterator to the first element.
///\param last The iterator to the last element + 1.
///\param buffer The array buffer.
//*************************************************************************
template <typename TIterator, size_t BufferSize>
wstring_ext(TIterator first, TIterator last, value_type (&buffer)[BufferSize], typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
: iwstring(buffer, BufferSize - 1U)
{
if (this->is_within_buffer(reinterpret_cast<const_iterator>(etl::addressof(*first))))
{
this->current_size = etl::distance(first, last);
}
else
{
this->initialise();
this->assign(first, last);
}
}
#if ETL_HAS_INITIALIZER_LIST
@ -462,6 +635,19 @@ namespace etl
this->initialise();
this->assign(init.begin(), init.end());
}
//*************************************************************************
/// Construct from initializer_list, from array buffer.
///\param init The initializer_list.
///\param buffer The array buffer.
//*************************************************************************
template <size_t BufferSize>
wstring_ext(std::initializer_list<value_type> init, value_type (&buffer)[BufferSize])
: iwstring(buffer, BufferSize - 1U)
{
this->initialise();
this->assign(init.begin(), init.end());
}
#endif
//*************************************************************************

View File

@ -120,6 +120,20 @@ namespace
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_from_array)
{
value_t buffer[SIZE + 1];
Text text(buffer);
CHECK_EQUAL(0U, text.size());
CHECK(text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_EQUAL(SIZE, text.max_size());
CHECK(text.begin() == text.end());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_default_constructor_use_buffer_and_size)
{
@ -173,6 +187,114 @@ namespace
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_array_buffer_text_same_buffer)
{
// Test using the same buffer for source and storage, with size deduced from array
Text text(array_text, array_text);
CHECK_EQUAL(etl::strlen(array_text), text.size());
CHECK(!text.empty());
CHECK_EQUAL(ETL_OR_STD17::size(array_text) - 1, text.capacity());
CHECK_EQUAL(ETL_OR_STD17::size(array_text) - 1, text.max_size());
CHECK(text.begin() != text.end());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_istring_with_array_buffer)
{
value_t buffer[SIZE + 1];
TextBuffer source_buffer{0};
Text source(initial_text.c_str(), source_buffer.data(), source_buffer.size());
Text text(source, buffer);
CHECK_EQUAL(initial_text.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_EQUAL(SIZE, text.max_size());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_text_count_with_array_buffer)
{
value_t buffer[SIZE + 1];
Text text(initial_text.c_str(), 5U, buffer);
CHECK_EQUAL(5U, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_size_value_with_array_buffer)
{
const size_t INITIAL_SIZE = 5UL;
const value_t INITIAL_VALUE = STR('A');
value_t buffer[SIZE + 1];
TextSTD compare_text(INITIAL_SIZE, INITIAL_VALUE);
Text text(INITIAL_SIZE, INITIAL_VALUE, buffer);
CHECK_EQUAL(INITIAL_SIZE, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
bool is_equal = Equal(compare_text, text);
CHECK(is_equal);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_string_view_with_array_buffer)
{
value_t buffer[SIZE + 1];
View view(initial_text.c_str());
Text text(view, buffer);
CHECK_EQUAL(view.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_iterator_range_with_array_buffer)
{
value_t buffer[SIZE + 1];
TextSTD compare_text(initial_text.begin(), initial_text.end());
Text text(initial_text.begin(), initial_text.end(), buffer);
CHECK_EQUAL(initial_text.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
bool is_equal = Equal(compare_text, text);
CHECK(is_equal);
}
#if ETL_HAS_INITIALIZER_LIST
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_initializer_list_with_array_buffer)
{
value_t buffer[SIZE + 1];
Text text({ STR('H'), STR('e'), STR('l'), STR('l'), STR('o') }, buffer);
CHECK_EQUAL(5U, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
#endif
//*************************************************************************
TEST(test_iterator_comparison_empty)
{

View File

@ -134,6 +134,20 @@ namespace
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_from_array)
{
value_t buffer[SIZE + 1];
Text text(buffer);
CHECK_EQUAL(0U, text.size());
CHECK(text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_EQUAL(SIZE, text.max_size());
CHECK(text.begin() == text.end());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_default_constructor_use_buffer_and_size)
{
@ -187,6 +201,114 @@ namespace
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_array_buffer_text_same_buffer)
{
// Test using the same buffer for source and storage, with size deduced from array
Text text(array_text, array_text);
CHECK_EQUAL(etl::strlen(array_text), text.size());
CHECK(!text.empty());
CHECK_EQUAL(ETL_OR_STD17::size(array_text) - 1, text.capacity());
CHECK_EQUAL(ETL_OR_STD17::size(array_text) - 1, text.max_size());
CHECK(text.begin() != text.end());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_istring_with_array_buffer)
{
value_t buffer[SIZE + 1];
TextBuffer source_buffer{0};
Text source(initial_text.c_str(), source_buffer.data(), source_buffer.size());
Text text(source, buffer);
CHECK_EQUAL(initial_text.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_EQUAL(SIZE, text.max_size());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_text_count_with_array_buffer)
{
value_t buffer[SIZE + 1];
Text text(initial_text.c_str(), 5U, buffer);
CHECK_EQUAL(5U, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_size_value_with_array_buffer)
{
const size_t INITIAL_SIZE = 5UL;
const value_t INITIAL_VALUE = STR('A');
value_t buffer[SIZE + 1];
TextSTD compare_text(INITIAL_SIZE, INITIAL_VALUE);
Text text(INITIAL_SIZE, INITIAL_VALUE, buffer);
CHECK_EQUAL(INITIAL_SIZE, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
bool is_equal = Equal(compare_text, text);
CHECK(is_equal);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_string_view_with_array_buffer)
{
value_t buffer[SIZE + 1];
View view(initial_text.c_str());
Text text(view, buffer);
CHECK_EQUAL(view.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_iterator_range_with_array_buffer)
{
value_t buffer[SIZE + 1];
TextSTD compare_text(initial_text.begin(), initial_text.end());
Text text(initial_text.begin(), initial_text.end(), buffer);
CHECK_EQUAL(initial_text.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
bool is_equal = Equal(compare_text, text);
CHECK(is_equal);
}
#if ETL_HAS_INITIALIZER_LIST
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_initializer_list_with_array_buffer)
{
value_t buffer[SIZE + 1];
Text text({ STR('H'), STR('e'), STR('l'), STR('l'), STR('o') }, buffer);
CHECK_EQUAL(5U, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
#endif
//*************************************************************************
TEST(test_iterator_comparison_empty)
{

View File

@ -134,6 +134,20 @@ namespace
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_from_array)
{
value_t buffer[SIZE + 1];
Text text(buffer);
CHECK_EQUAL(0U, text.size());
CHECK(text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_EQUAL(SIZE, text.max_size());
CHECK(text.begin() == text.end());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_default_constructor_use_buffer_and_size)
{
@ -187,6 +201,114 @@ namespace
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_array_buffer_text_same_buffer)
{
// Test using the same buffer for source and storage, with size deduced from array
Text text(array_text, array_text);
CHECK_EQUAL(etl::strlen(array_text), text.size());
CHECK(!text.empty());
CHECK_EQUAL(ETL_OR_STD17::size(array_text) - 1, text.capacity());
CHECK_EQUAL(ETL_OR_STD17::size(array_text) - 1, text.max_size());
CHECK(text.begin() != text.end());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_istring_with_array_buffer)
{
value_t buffer[SIZE + 1];
TextBuffer source_buffer{0};
Text source(initial_text.c_str(), source_buffer.data(), source_buffer.size());
Text text(source, buffer);
CHECK_EQUAL(initial_text.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_EQUAL(SIZE, text.max_size());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_text_count_with_array_buffer)
{
value_t buffer[SIZE + 1];
Text text(initial_text.c_str(), 5U, buffer);
CHECK_EQUAL(5U, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_size_value_with_array_buffer)
{
const size_t INITIAL_SIZE = 5UL;
const value_t INITIAL_VALUE = STR('A');
value_t buffer[SIZE + 1];
TextSTD compare_text(INITIAL_SIZE, INITIAL_VALUE);
Text text(INITIAL_SIZE, INITIAL_VALUE, buffer);
CHECK_EQUAL(INITIAL_SIZE, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
bool is_equal = Equal(compare_text, text);
CHECK(is_equal);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_string_view_with_array_buffer)
{
value_t buffer[SIZE + 1];
View view(initial_text.c_str());
Text text(view, buffer);
CHECK_EQUAL(view.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_iterator_range_with_array_buffer)
{
value_t buffer[SIZE + 1];
TextSTD compare_text(initial_text.begin(), initial_text.end());
Text text(initial_text.begin(), initial_text.end(), buffer);
CHECK_EQUAL(initial_text.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
bool is_equal = Equal(compare_text, text);
CHECK(is_equal);
}
#if ETL_HAS_INITIALIZER_LIST
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_initializer_list_with_array_buffer)
{
value_t buffer[SIZE + 1];
Text text({ STR('H'), STR('e'), STR('l'), STR('l'), STR('o') }, buffer);
CHECK_EQUAL(5U, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
#endif
//*************************************************************************
TEST(test_iterator_comparison_empty)
{

View File

@ -137,6 +137,20 @@ namespace
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_from_array)
{
value_t buffer[SIZE + 1];
Text text(buffer);
CHECK_EQUAL(0U, text.size());
CHECK(text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_EQUAL(SIZE, text.max_size());
CHECK(text.begin() == text.end());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_default_constructor_use_buffer_and_size)
{
@ -190,6 +204,114 @@ namespace
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_array_buffer_text_same_buffer)
{
// Test using the same buffer for source and storage, with size deduced from array
Text text(array_text, array_text);
CHECK_EQUAL(etl::strlen(array_text), text.size());
CHECK(!text.empty());
CHECK_EQUAL(ETL_OR_STD17::size(array_text) - 1, text.capacity());
CHECK_EQUAL(ETL_OR_STD17::size(array_text) - 1, text.max_size());
CHECK(text.begin() != text.end());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_istring_with_array_buffer)
{
value_t buffer[SIZE + 1];
TextBuffer source_buffer{0};
Text source(initial_text.c_str(), source_buffer.data(), source_buffer.size());
Text text(source, buffer);
CHECK_EQUAL(initial_text.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_EQUAL(SIZE, text.max_size());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_text_count_with_array_buffer)
{
value_t buffer[SIZE + 1];
Text text(initial_text.c_str(), 5U, buffer);
CHECK_EQUAL(5U, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_size_value_with_array_buffer)
{
const size_t INITIAL_SIZE = 5UL;
const value_t INITIAL_VALUE = STR('A');
value_t buffer[SIZE + 1];
TextSTD compare_text(INITIAL_SIZE, INITIAL_VALUE);
Text text(INITIAL_SIZE, INITIAL_VALUE, buffer);
CHECK_EQUAL(INITIAL_SIZE, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
bool is_equal = Equal(compare_text, text);
CHECK(is_equal);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_string_view_with_array_buffer)
{
value_t buffer[SIZE + 1];
View view(initial_text.c_str());
Text text(view, buffer);
CHECK_EQUAL(view.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_iterator_range_with_array_buffer)
{
value_t buffer[SIZE + 1];
TextSTD compare_text(initial_text.begin(), initial_text.end());
Text text(initial_text.begin(), initial_text.end(), buffer);
CHECK_EQUAL(initial_text.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
bool is_equal = Equal(compare_text, text);
CHECK(is_equal);
}
#if ETL_HAS_INITIALIZER_LIST
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_initializer_list_with_array_buffer)
{
value_t buffer[SIZE + 1];
Text text({ STR('H'), STR('e'), STR('l'), STR('l'), STR('o') }, buffer);
CHECK_EQUAL(5U, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
#endif
//*************************************************************************
TEST(test_iterator_comparison_empty)
{

View File

@ -137,6 +137,20 @@ namespace
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_from_array)
{
value_t buffer[SIZE + 1];
Text text(buffer);
CHECK_EQUAL(0U, text.size());
CHECK(text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_EQUAL(SIZE, text.max_size());
CHECK(text.begin() == text.end());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_default_constructor_use_buffer_and_size)
{
@ -190,6 +204,114 @@ namespace
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_array_buffer_text_same_buffer)
{
// Test using the same buffer for source and storage, with size deduced from array
Text text(array_text, array_text);
CHECK_EQUAL(etl::strlen(array_text), text.size());
CHECK(!text.empty());
CHECK_EQUAL(ETL_OR_STD17::size(array_text) - 1, text.capacity());
CHECK_EQUAL(ETL_OR_STD17::size(array_text) - 1, text.max_size());
CHECK(text.begin() != text.end());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_istring_with_array_buffer)
{
value_t buffer[SIZE + 1];
TextBuffer source_buffer{0};
Text source(initial_text.c_str(), source_buffer.data(), source_buffer.size());
Text text(source, buffer);
CHECK_EQUAL(initial_text.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_EQUAL(SIZE, text.max_size());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_text_count_with_array_buffer)
{
value_t buffer[SIZE + 1];
Text text(initial_text.c_str(), 5U, buffer);
CHECK_EQUAL(5U, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_size_value_with_array_buffer)
{
const size_t INITIAL_SIZE = 5UL;
const value_t INITIAL_VALUE = STR('A');
value_t buffer[SIZE + 1];
TextSTD compare_text(INITIAL_SIZE, INITIAL_VALUE);
Text text(INITIAL_SIZE, INITIAL_VALUE, buffer);
CHECK_EQUAL(INITIAL_SIZE, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
bool is_equal = Equal(compare_text, text);
CHECK(is_equal);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_string_view_with_array_buffer)
{
value_t buffer[SIZE + 1];
View view(initial_text.c_str());
Text text(view, buffer);
CHECK_EQUAL(view.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_iterator_range_with_array_buffer)
{
value_t buffer[SIZE + 1];
TextSTD compare_text(initial_text.begin(), initial_text.end());
Text text(initial_text.begin(), initial_text.end(), buffer);
CHECK_EQUAL(initial_text.size(), text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
bool is_equal = Equal(compare_text, text);
CHECK(is_equal);
}
#if ETL_HAS_INITIALIZER_LIST
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_initializer_list_with_array_buffer)
{
value_t buffer[SIZE + 1];
Text text({ STR('H'), STR('e'), STR('l'), STR('l'), STR('o') }, buffer);
CHECK_EQUAL(5U, text.size());
CHECK(!text.empty());
CHECK_EQUAL(SIZE, text.capacity());
CHECK_FALSE(text.is_truncated());
}
#endif
//*************************************************************************
TEST(test_iterator_comparison_empty)
{