diff --git a/include/etl/string.h b/include/etl/string.h index 9f462fdb..6fa1fccf 100644 --- a/include/etl/string.h +++ b/include/etl/string.h @@ -305,6 +305,17 @@ namespace etl this->initialise(); } + //************************************************************************* + /// Constructor, from array buffer. + ///\param buffer The array buffer. + //************************************************************************* + template + 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 + 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 + 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 + string_ext(TPointer text, value_type (&buffer)[BufferSize], + typename etl::enable_if::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 + 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 + 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 + 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 + 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 + string_ext(TIterator first, TIterator last, value_type (&buffer)[BufferSize], typename etl::enable_if::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 + string_ext(std::initializer_list init, value_type (&buffer)[BufferSize]) + : istring(buffer, BufferSize - 1U) + { + this->initialise(); + this->assign(init.begin(), init.end()); + } #endif //************************************************************************* diff --git a/include/etl/u16string.h b/include/etl/u16string.h index 74eddeb2..6cc22299 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -285,6 +285,17 @@ namespace etl this->initialise(); } + //************************************************************************* + /// Constructor, from array buffer. + ///\param buffer The array buffer. + //************************************************************************* + template + 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 + 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 + 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 + u16string_ext(TPointer text, value_type (&buffer)[BufferSize], + typename etl::enable_if::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 + 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 + 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 + 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 + 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 + u16string_ext(TIterator first, TIterator last, value_type (&buffer)[BufferSize], typename etl::enable_if::value, int>::type = 0) + : iu16string(buffer, BufferSize - 1U) + { + if (this->is_within_buffer(reinterpret_cast(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 + u16string_ext(std::initializer_list init, value_type (&buffer)[BufferSize]) + : iu16string(buffer, BufferSize - 1U) + { + this->initialise(); + this->assign(init.begin(), init.end()); + } #endif //************************************************************************* diff --git a/include/etl/u32string.h b/include/etl/u32string.h index 2399b910..c4dc60e5 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -285,6 +285,17 @@ namespace etl this->initialise(); } + //************************************************************************* + /// Constructor, from array buffer. + ///\param buffer The array buffer. + //************************************************************************* + template + 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 + 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 + 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 + u32string_ext(TPointer text, value_type (&buffer)[BufferSize], + typename etl::enable_if::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 + 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 + 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 + 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 + 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 + u32string_ext(TIterator first, TIterator last, value_type (&buffer)[BufferSize], typename etl::enable_if::value, int>::type = 0) + : iu32string(buffer, BufferSize - 1U) + { + if (this->is_within_buffer(reinterpret_cast(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 + u32string_ext(std::initializer_list init, value_type (&buffer)[BufferSize]) + : iu32string(buffer, BufferSize - 1U) + { + this->initialise(); + this->assign(init.begin(), init.end()); + } #endif //************************************************************************* diff --git a/include/etl/u8string.h b/include/etl/u8string.h index f82bbadd..26199773 100644 --- a/include/etl/u8string.h +++ b/include/etl/u8string.h @@ -305,6 +305,17 @@ namespace etl this->initialise(); } + //************************************************************************* + /// Constructor, from array buffer. + ///\param buffer The array buffer. + //************************************************************************* + template + 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 + 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 + 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 + u8string_ext(TPointer text, value_type (&buffer)[BufferSize], + typename etl::enable_if::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 + 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 + 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 + 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 + 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 + u8string_ext(TIterator first, TIterator last, value_type (&buffer)[BufferSize], typename etl::enable_if::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 + u8string_ext(std::initializer_list init, value_type (&buffer)[BufferSize]) + : iu8string(buffer, BufferSize - 1U) + { + this->initialise(); + this->assign(init.begin(), init.end()); + } #endif //************************************************************************* diff --git a/include/etl/wstring.h b/include/etl/wstring.h index d683a160..91405be7 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -285,6 +285,17 @@ namespace etl this->initialise(); } + //************************************************************************* + /// Constructor, from array buffer. + ///\param buffer The array buffer. + //************************************************************************* + template + 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 + 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 + 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 + wstring_ext(TPointer text, value_type (&buffer)[BufferSize], + typename etl::enable_if::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 + 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 + 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 + 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 + 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 + wstring_ext(TIterator first, TIterator last, value_type (&buffer)[BufferSize], typename etl::enable_if::value, int>::type = 0) + : iwstring(buffer, BufferSize - 1U) + { + if (this->is_within_buffer(reinterpret_cast(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 + wstring_ext(std::initializer_list init, value_type (&buffer)[BufferSize]) + : iwstring(buffer, BufferSize - 1U) + { + this->initialise(); + this->assign(init.begin(), init.end()); + } #endif //************************************************************************* diff --git a/test/test_string_char_external_buffer.cpp b/test/test_string_char_external_buffer.cpp index 15790f43..9812a654 100644 --- a/test/test_string_char_external_buffer.cpp +++ b/test/test_string_char_external_buffer.cpp @@ -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) { diff --git a/test/test_string_u16_external_buffer.cpp b/test/test_string_u16_external_buffer.cpp index b22f43dd..311e26f6 100644 --- a/test/test_string_u16_external_buffer.cpp +++ b/test/test_string_u16_external_buffer.cpp @@ -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) { diff --git a/test/test_string_u32_external_buffer.cpp b/test/test_string_u32_external_buffer.cpp index c0b8150b..4ba37523 100644 --- a/test/test_string_u32_external_buffer.cpp +++ b/test/test_string_u32_external_buffer.cpp @@ -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) { diff --git a/test/test_string_u8_external_buffer.cpp b/test/test_string_u8_external_buffer.cpp index e161bc13..41ad7fa1 100644 --- a/test/test_string_u8_external_buffer.cpp +++ b/test/test_string_u8_external_buffer.cpp @@ -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) { diff --git a/test/test_string_wchar_t_external_buffer.cpp b/test/test_string_wchar_t_external_buffer.cpp index 7bc6ca8a..e57b8764 100644 --- a/test/test_string_wchar_t_external_buffer.cpp +++ b/test/test_string_wchar_t_external_buffer.cpp @@ -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) {