From 25e353538a1dd40faeaa4b43f8517daea1e7afb0 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 26 Oct 2020 19:32:48 +0000 Subject: [PATCH 1/3] Initial string reduction code --- include/etl/basic_string.h | 250 +++++---- include/etl/platform.h | 14 + include/etl/string.h | 33 +- include/etl/string_utilities.h | 50 +- include/etl/u16string.h | 36 +- include/etl/u32string.h | 38 +- include/etl/wstring.h | 37 +- test/codeblocks/ETL.cbp | 24 + test/test_make_string.cpp | 16 + test/test_string_char.cpp | 340 +++++++++++- test/test_string_char_external_buffer.cpp | 346 +++++++++++++ test/test_string_u16.cpp | 450 ++++++++++++++-- test/test_string_u16_external_buffer.cpp | 512 ++++++++++++++++--- test/test_string_u32.cpp | 450 ++++++++++++++-- test/test_string_u32_external_buffer.cpp | 512 ++++++++++++++++--- test/test_string_utilities.cpp | 5 +- test/test_string_utilities_u16.cpp | 4 +- test/test_string_utilities_u32.cpp | 4 +- test/test_string_utilities_wchar_t.cpp | 4 +- test/test_string_wchar_t.cpp | 453 +++++++++++++--- test/test_string_wchar_t_external_buffer.cpp | 512 ++++++++++++++++--- test/vs2019/etl.sln | 6 + test/vs2019/etl.vcxproj | 97 ++++ 23 files changed, 3581 insertions(+), 612 deletions(-) diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index efadab47..b455e819 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -50,6 +50,7 @@ SOFTWARE. #include "integral_limits.h" #include "exception.h" #include "memory.h" +#include "exception.h" #undef ETL_FILE #define ETL_FILE "27" @@ -149,10 +150,7 @@ namespace etl typedef size_t size_type; - enum - { - npos = etl::integral_limits::max - }; + static const size_type npos = etl::integral_limits::max; //************************************************************************* /// Gets the current size of the string. @@ -212,16 +210,17 @@ namespace etl /// Returns the remaining capacity. ///\return The remaining capacity. //************************************************************************* - size_t available() const + size_type available() const { return max_size() - size(); } +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED //************************************************************************* /// Returns whether the string was truncated by the last operation. ///\return Whether the string was truncated by the last operation. //************************************************************************* - size_t truncated() const + bool truncated() const { return is_truncated; } @@ -233,7 +232,9 @@ namespace etl { is_truncated = false; } +#endif +#if ETL_STRING_CLEAR_AFTER_USE_ENABLED //************************************************************************* /// Sets the 'secure' flag to the requested state. //************************************************************************* @@ -249,18 +250,24 @@ namespace etl { return clear_afer_use; } +#endif protected: //************************************************************************* /// Constructor. //************************************************************************* - string_base(size_t max_size_) - : is_truncated(false) - , clear_afer_use(false) - , current_size(0) + string_base(size_type max_size_) + : current_size(0) , CAPACITY(max_size_) { +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED + is_truncated = false; +#endif + +#if ETL_STRING_CLEAR_AFTER_USE_ENABLED + clear_afer_use = false; +#endif } //************************************************************************* @@ -270,10 +277,16 @@ namespace etl { } - bool is_truncated; ///< Set to true if the operation truncated the string. - bool clear_afer_use; ///< Set to true if the string must be cleared after use. size_type current_size; ///< The current number of elements in the string. const size_type CAPACITY; ///< The maximum number of elements in the string. + +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED + bool is_truncated; ///< Set to true if the operation truncated the string. +#endif + +#if ETL_STRING_CLEAR_AFTER_USE_ENABLED + bool clear_afer_use; ///< Set to true if the string must be cleared after use. +#endif }; //*************************************************************************** @@ -297,7 +310,6 @@ namespace etl typedef const T* const_iterator; typedef ETL_OR_STD::reverse_iterator reverse_iterator; typedef ETL_OR_STD::reverse_iterator const_reverse_iterator; - typedef size_t size_type; typedef typename etl::iterator_traits::difference_type difference_type; @@ -414,7 +426,7 @@ namespace etl /// If asserts or exceptions are enabled and the new size is larger than the ///\param new_size The new size. //********************************************************************* - void resize(size_t new_size) + void resize(size_type new_size) { resize(new_size, 0); } @@ -424,14 +436,16 @@ namespace etl ///\param new_size The new size. ///\param value The value to fill new elements with. Default = default constructed value. //********************************************************************* - void resize(size_t new_size, T value) + void resize(size_type new_size, T value) { if (new_size > CAPACITY) { +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = true; #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); +#endif #endif } @@ -453,7 +467,7 @@ namespace etl ///\param i The index. ///\return A reference to the value at index 'i' //********************************************************************* - reference operator [](size_t i) + reference operator [](size_type i) { return p_buffer[i]; } @@ -463,7 +477,7 @@ namespace etl ///\param i The index. ///\return A const reference to the value at index 'i' //********************************************************************* - const_reference operator [](size_t i) const + const_reference operator [](size_type i) const { return p_buffer[i]; } @@ -474,7 +488,7 @@ namespace etl ///\param i The index. ///\return A reference to the value at index 'i' //********************************************************************* - reference at(size_t i) + reference at(size_type i) { ETL_ASSERT(i < size(), ETL_ERROR(string_out_of_bounds)); return p_buffer[i]; @@ -486,7 +500,7 @@ namespace etl ///\param i The index. ///\return A const reference to the value at index 'i' //********************************************************************* - const_reference at(size_t i) const + const_reference at(size_type i) const { ETL_ASSERT(i < size(), ETL_ERROR(string_out_of_bounds)); return p_buffer[i]; @@ -555,6 +569,7 @@ namespace etl { assign(other.begin(), other.end()); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (other.truncated()) { is_truncated = true; @@ -563,6 +578,7 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif cleanup(); } @@ -574,7 +590,7 @@ namespace etl ///\param subposition The position to start from. ///\param sublength The length to copy. //********************************************************************* - void assign(const etl::ibasic_string& other, size_t subposition, size_t sublength) + void assign(const etl::ibasic_string& other, size_type subposition, size_type sublength) { if (sublength == npos) { @@ -600,10 +616,12 @@ namespace etl p_buffer[current_size++] = *other++; } +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = (*other != 0); #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ASSERT(is_truncated == false, ETL_ERROR(string_truncation)) +#endif #endif p_buffer[current_size] = 0; @@ -615,14 +633,16 @@ namespace etl ///\param other The other string. ///\param length The length to copy. //********************************************************************* - void assign(const_pointer other, size_t length_) + void assign(const_pointer other, size_type length_) { initialise(); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = (length_ > CAPACITY); #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ASSERT(is_truncated == false, ETL_ERROR(string_truncation)) +#endif #endif length_ = etl::min(length_, CAPACITY); @@ -657,10 +677,12 @@ namespace etl p_buffer[current_size] = 0; +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = (first != last); #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ASSERT(is_truncated == false, ETL_ERROR(string_truncation)) +#endif #endif } @@ -670,14 +692,16 @@ namespace etl ///\param n The number of elements to add. ///\param value The value to insert for each element. //********************************************************************* - void assign(size_t n, T value) + void assign(size_type n, T value) { initialise(); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = (n > CAPACITY); #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ASSERT(is_truncated == false, ETL_ERROR(string_truncation)) +#endif #endif n = etl::min(n, CAPACITY); @@ -709,10 +733,12 @@ namespace etl } else { +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = true; #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); +#endif #endif } } @@ -737,6 +763,7 @@ namespace etl { insert(end(), str.begin(), str.end()); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (str.truncated()) { is_truncated = true; @@ -745,6 +772,7 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif return *this; } @@ -755,7 +783,7 @@ namespace etl ///\param subposition The position in str. ///\param sublength The number of characters. //********************************************************************* - ibasic_string& append(const ibasic_string& str, size_t subposition, size_t sublength = npos) + ibasic_string& append(const ibasic_string& str, size_type subposition, size_type sublength = npos) { ETL_ASSERT(subposition <= str.size(), ETL_ERROR(string_out_of_bounds)); @@ -779,7 +807,7 @@ namespace etl ///\param str The string to append. ///\param n The number of characters. //********************************************************************* - ibasic_string& append(const T* str, size_t n) + ibasic_string& append(const T* str, size_type n) { insert(size(), str, n); return *this; @@ -790,7 +818,7 @@ namespace etl ///\param n The number of characters. ///\param c The character. //********************************************************************* - ibasic_string& append(size_t n, T c) + ibasic_string& append(size_type n, T c) { insert(size(), n, c); return *this; @@ -845,10 +873,12 @@ namespace etl *insert_position = value; } +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = true; #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); +#endif #endif } @@ -863,7 +893,7 @@ namespace etl ///\param n The number of elements to add. ///\param value The value to insert. //********************************************************************* - void insert(const_iterator position, size_t n, T value) + void insert(const_iterator position, size_type n, T value) { if (n == 0) { @@ -872,15 +902,17 @@ namespace etl // Quick hack, as iterators are pointers. iterator insert_position = const_cast(position); - const size_t start = etl::distance(cbegin(), position); + const size_type start = etl::distance(cbegin(), position); // No effect. if (start >= CAPACITY) { +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = true; #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); +#endif #endif return; } @@ -890,10 +922,12 @@ namespace etl { if ((current_size + n) > CAPACITY) { +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = true; #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); +#endif #endif } @@ -903,20 +937,23 @@ namespace etl else { // Lets do some shifting. - const size_t shift_amount = n; - const size_t to_position = start + shift_amount; - const size_t remaining_characters = current_size - start; - const size_t max_shift_characters = CAPACITY - start - shift_amount; - const size_t characters_to_shift = etl::min(max_shift_characters, remaining_characters); + const size_type shift_amount = n; + const size_type to_position = start + shift_amount; + const size_type remaining_characters = current_size - start; + const size_type max_shift_characters = CAPACITY - start - shift_amount; + const size_type characters_to_shift = etl::min(max_shift_characters, remaining_characters); // Will the string truncate? if ((start + shift_amount + remaining_characters) > CAPACITY) { current_size = CAPACITY; + +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = true; #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); +#endif #endif } else @@ -946,16 +983,18 @@ namespace etl return; } - const size_t start = etl::distance(begin(), position); - const size_t n = etl::distance(first, last); + const size_type start = etl::distance(begin(), position); + const size_type n = etl::distance(first, last); // No effect. if (start >= CAPACITY) { +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = true; #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); +#endif #endif return; } @@ -965,10 +1004,12 @@ namespace etl { if (((current_size + n) > CAPACITY)) { +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = true; #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); +#endif #endif } @@ -982,20 +1023,23 @@ namespace etl else { // Lets do some shifting. - const size_t shift_amount = n; - const size_t to_position = start + shift_amount; - const size_t remaining_characters = current_size - start; - const size_t max_shift_characters = CAPACITY - start - shift_amount; - const size_t characters_to_shift = etl::min(max_shift_characters, remaining_characters); + const size_type shift_amount = n; + const size_type to_position = start + shift_amount; + const size_type remaining_characters = current_size - start; + const size_type max_shift_characters = CAPACITY - start - shift_amount; + const size_type characters_to_shift = etl::min(max_shift_characters, remaining_characters); // Will the string truncate? if ((start + shift_amount + remaining_characters) > CAPACITY) { current_size = CAPACITY; + +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = true; #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); +#endif #endif } else @@ -1019,12 +1063,13 @@ namespace etl ///\param position The position to insert before. ///\param str The string to insert. //********************************************************************* - etl::ibasic_string& insert(size_t position, const etl::ibasic_string& str) + etl::ibasic_string& insert(size_type position, const etl::ibasic_string& str) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); insert(begin() + position, str.cbegin(), str.cend()); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (str.truncated()) { is_truncated = true; @@ -1033,6 +1078,7 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif return *this; } @@ -1044,7 +1090,7 @@ namespace etl ///\param subposition The subposition to start from. ///\param sublength The number of characters to insert. //********************************************************************* - etl::ibasic_string& insert(size_t position, const etl::ibasic_string& str, size_t subposition, size_t sublength) + etl::ibasic_string& insert(size_type position, const etl::ibasic_string& str, size_type subposition, size_type sublength) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); ETL_ASSERT(subposition <= str.size(), ETL_ERROR(string_out_of_bounds)); @@ -1056,6 +1102,7 @@ namespace etl insert(begin() + position, str.cbegin() + subposition, str.cbegin() + subposition + sublength); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (str.truncated()) { is_truncated = true; @@ -1064,6 +1111,7 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif return *this; } @@ -1073,7 +1121,7 @@ namespace etl ///\param position The position to insert before. ///\param s The string to insert. //********************************************************************* - etl::ibasic_string& insert(size_t position, const_pointer s) + etl::ibasic_string& insert(size_type position, const_pointer s) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); @@ -1087,7 +1135,7 @@ namespace etl ///\param s The string to insert. ///\param n The number of characters to insert. //********************************************************************* - etl::ibasic_string& insert(size_t position, const_pointer s, size_t n) + etl::ibasic_string& insert(size_type position, const_pointer s, size_type n) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); @@ -1101,7 +1149,7 @@ namespace etl ///\param n The number of characters to insert. ///\param c The character to insert. //********************************************************************* - etl::ibasic_string& insert(size_t position, size_t n, value_type c) + etl::ibasic_string& insert(size_type position, size_type n, value_type c) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); @@ -1115,7 +1163,7 @@ namespace etl ///\param length Number of characters. ///\return A refernce to this string. //********************************************************************* - etl::ibasic_string& erase(size_t position, size_t length_ = npos) + etl::ibasic_string& erase(size_type position, size_type length_ = npos) { // Limit the length. length_ = etl::min(length_, size() - position); @@ -1154,7 +1202,7 @@ namespace etl } etl::copy(last, end(), first); - size_t n_delete = etl::distance(first, last); + size_type n_delete = etl::distance(first, last); current_size -= n_delete; p_buffer[current_size] = 0; @@ -1177,20 +1225,22 @@ namespace etl ///\param len The number of characters to copy. ///\param pos The position to start copying from. //********************************************************************* - size_t copy(pointer s, size_t len, size_t pos = 0) + size_type copy(pointer s, size_type len, size_type pos = 0) { if ((pos + len > size())) { +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = true; #if defined(ETL_STRING_TRUNCATION_IS_ERROR) ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); +#endif #endif } - size_t endpos = etl::min(pos + len, size()); + size_type endpos = etl::min(pos + len, size()); - for (size_t i = pos; i < endpos; ++i) + for (size_type i = pos; i < endpos; ++i) { *s++ = p_buffer[i]; } @@ -1203,7 +1253,7 @@ namespace etl ///\param str The content to find ///\param pos The position to start searching from. //********************************************************************* - size_t find(const ibasic_string& str, size_t pos = 0) const + size_type find(const ibasic_string& str, size_type pos = 0) const { if ((pos + str.size()) > size()) { @@ -1227,7 +1277,7 @@ namespace etl ///\param s Pointer to the content to find ///\param pos The position to start searching from. //********************************************************************* - size_t find(const_pointer s, size_t pos = 0) const + size_type find(const_pointer s, size_type pos = 0) const { #if defined(ETL_DEBUG) if ((pos + etl::strlen(s)) > size()) @@ -1254,7 +1304,7 @@ namespace etl ///\param pos The position to start searching from. ///\param n The number of characters to search for. //********************************************************************* - size_t find(const_pointer s, size_t pos, size_t n) const + size_type find(const_pointer s, size_type pos, size_type n) const { #if defined(ETL_DEBUG) if ((pos + etl::strlen(s) - n) > size()) @@ -1280,7 +1330,7 @@ namespace etl ///\param c The character to find. ///\param position The position to start searching from. //********************************************************************* - size_t find(T c, size_t position = 0) const + size_type find(T c, size_type position = 0) const { const_iterator i = etl::find(begin() + position, end(), c); @@ -1299,7 +1349,7 @@ namespace etl ///\param str The content to find ///\param pos The position to start searching from. //********************************************************************* - size_t rfind(const ibasic_string& str, size_t position = npos) const + size_type rfind(const ibasic_string& str, size_type position = npos) const { if ((str.size()) > size()) { @@ -1330,9 +1380,9 @@ namespace etl ///\param str The content to find ///\param pos The position to start searching from. //********************************************************************* - size_t rfind(const_pointer s, size_t position = npos) const + size_type rfind(const_pointer s, size_type position = npos) const { - size_t len = etl::strlen(s); + size_type len = etl::strlen(s); if (len > size()) { @@ -1366,7 +1416,7 @@ namespace etl ///\param str The content to find ///\param pos The position to start searching from. //********************************************************************* - size_t rfind(const_pointer s, size_t position, size_t length_) const + size_type rfind(const_pointer s, size_type position, size_type length_) const { if (length_ > size()) { @@ -1400,7 +1450,7 @@ namespace etl ///\param c The character to find ///\param pos The position to start searching from. //********************************************************************* - size_t rfind(T c, size_t position = npos) const + size_type rfind(T c, size_type position = npos) const { if (position >= size()) { @@ -1427,7 +1477,7 @@ namespace etl ///\param length The number of characters to replace. ///\param str The string to replace it with. //********************************************************************* - ibasic_string& replace(size_t position, size_t length_, const ibasic_string& str) + ibasic_string& replace(size_type position, size_type length_, const ibasic_string& str) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); @@ -1461,6 +1511,7 @@ namespace etl // Insert the new stuff. insert(first_, str.begin(), str.end()); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (str.truncated()) { is_truncated = true; @@ -1469,6 +1520,7 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif return *this; } @@ -1476,7 +1528,7 @@ namespace etl //********************************************************************* /// Replace characters from 'position' of 'length' with 'str' from 'subpsotion' of 'sublength'. //********************************************************************* - ibasic_string& replace(size_t position, size_t length_, const ibasic_string& str, size_t subposition, size_t sublength) + ibasic_string& replace(size_type position, size_type length_, const ibasic_string& str, size_type subposition, size_type sublength) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); ETL_ASSERT(subposition <= str.size(), ETL_ERROR(string_out_of_bounds)); @@ -1491,6 +1543,7 @@ namespace etl // Insert the new stuff. insert(position, str, subposition, sublength); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (str.truncated()) { is_truncated = true; @@ -1499,6 +1552,7 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif return *this; } @@ -1506,7 +1560,7 @@ namespace etl //********************************************************************* /// Replace characters from 'position' of 'length' with pointed to string. //********************************************************************* - ibasic_string& replace(size_t position, size_t length_, const_pointer s) + ibasic_string& replace(size_type position, size_type length_, const_pointer s) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); @@ -1543,7 +1597,7 @@ namespace etl //********************************************************************* /// Replace characters from 'position' of 'length' with 'n' characters from pointed to string. //********************************************************************* - ibasic_string& replace(size_t position, size_t length_, const_pointer s, size_t n) + ibasic_string& replace(size_type position, size_type length_, const_pointer s, size_type n) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); @@ -1562,7 +1616,7 @@ namespace etl //********************************************************************* /// Replace characters from 'first' to 'last' with 'n' characters from pointed to string. //********************************************************************* - ibasic_string& replace(const_iterator first, const_iterator last, const_pointer s, size_t n) + ibasic_string& replace(const_iterator first, const_iterator last, const_pointer s, size_type n) { // Quick hack, as iterators are pointers. iterator first_ = const_cast(first); @@ -1580,7 +1634,7 @@ namespace etl //********************************************************************* /// Replace characters from 'position' of 'length' with 'n' copies of 'c'. //********************************************************************* - ibasic_string& replace(size_t position, size_t length_, size_t n, value_type c) + ibasic_string& replace(size_type position, size_type length_, size_type n, value_type c) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); @@ -1599,7 +1653,7 @@ namespace etl //********************************************************************* /// Replace characters from 'first' of 'last' with 'n' copies of 'c'. //********************************************************************* - ibasic_string& replace(const_iterator first, const_iterator last, size_t n, value_type c) + ibasic_string& replace(const_iterator first, const_iterator last, size_type n, value_type c) { // Quick hack, as iterators are pointers. iterator first_ = const_cast(first); @@ -1647,7 +1701,7 @@ namespace etl //************************************************************************* /// Compare position / length with string. //************************************************************************* - int compare(size_t position, size_t length_, const ibasic_string& str) const + int compare(size_type position, size_type length_, const ibasic_string& str) const { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); @@ -1663,7 +1717,7 @@ namespace etl //************************************************************************* /// Compare position / length with string / subposition / sublength. //************************************************************************* - int compare(size_t position, size_t length_, const ibasic_string& str, size_t subposition, size_t sublength) const + int compare(size_type position, size_type length_, const ibasic_string& str, size_type subposition, size_type sublength) const { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); ETL_ASSERT(subposition <= str.size(), ETL_ERROR(string_out_of_bounds)); @@ -1692,7 +1746,7 @@ namespace etl //************************************************************************* /// Compare position / length with C string. //************************************************************************* - int compare(size_t position, size_t length_, const_pointer s) const + int compare(size_type position, size_type length_, const_pointer s) const { return compare(p_buffer + position, p_buffer + position + length_, @@ -1703,7 +1757,7 @@ namespace etl //************************************************************************* /// Compare position / length with C string / n. //************************************************************************* - int compare(size_t position, size_t length_, const_pointer s, size_t n) const + int compare(size_type position, size_type length_, const_pointer s, size_type n) const { return compare(p_buffer + position, p_buffer + position + length_, @@ -1716,7 +1770,7 @@ namespace etl ///\param str The content to find ///\param pos The position to start searching from. //********************************************************************* - size_t find_first_of(const ibasic_string& str, size_t position = 0) const + size_type find_first_of(const ibasic_string& str, size_type position = 0) const { return find_first_of(str.c_str(), position, str.size()); } @@ -1726,7 +1780,7 @@ namespace etl ///\param s Pointer to the content to find ///\param pos The position to start searching from. //********************************************************************* - size_t find_first_of(const_pointer s, size_t position = 0) const + size_type find_first_of(const_pointer s, size_type position = 0) const { return find_first_of(s, position, etl::strlen(s)); } @@ -1737,13 +1791,13 @@ namespace etl ///\param pos The position to start searching from. ///\param n The number of characters to search for. //********************************************************************* - size_t find_first_of(const_pointer s, size_t position, size_t n) const + size_type find_first_of(const_pointer s, size_type position, size_type n) const { if (position < size()) { - for (size_t i = position; i < size(); ++i) + for (size_type i = position; i < size(); ++i) { - for (size_t j = 0; j < n; ++j) + for (size_type j = 0; j < n; ++j) { if (p_buffer[i] == s[j]) { @@ -1761,11 +1815,11 @@ namespace etl ///\param c The character to find ///\param pos The position to start searching from. //********************************************************************* - size_t find_first_of(value_type c, size_t position = 0) const + size_type find_first_of(value_type c, size_type position = 0) const { if (position < size()) { - for (size_t i = position; i < size(); ++i) + for (size_type i = position; i < size(); ++i) { if (p_buffer[i] == c) { @@ -1782,7 +1836,7 @@ namespace etl ///\param str The content to find ///\param pos The position to start searching from. //********************************************************************* - size_t find_last_of(const ibasic_string& str, size_t position = npos) const + size_type find_last_of(const ibasic_string& str, size_type position = npos) const { return find_last_of(str.c_str(), position, str.size()); } @@ -1792,7 +1846,7 @@ namespace etl ///\param s Pointer to the content to find ///\param pos The position to start searching from. //********************************************************************* - size_t find_last_of(const_pointer s, size_t position = npos) const + size_type find_last_of(const_pointer s, size_type position = npos) const { return find_last_of(s, position, etl::strlen(s)); } @@ -1803,7 +1857,7 @@ namespace etl ///\param pos The position to start searching from. ///\param n The number of characters to search for. //********************************************************************* - size_t find_last_of(const_pointer s, size_t position, size_t n) const + size_type find_last_of(const_pointer s, size_type position, size_type n) const { if (empty()) { @@ -1816,7 +1870,7 @@ namespace etl while (it != rend()) { - for (size_t j = 0; j < n; ++j) + for (size_type j = 0; j < n; ++j) { if (p_buffer[position] == s[j]) { @@ -1836,7 +1890,7 @@ namespace etl ///\param c The character to find ///\param pos The position to start searching from. //********************************************************************* - size_t find_last_of(value_type c, size_t position = npos) const + size_type find_last_of(value_type c, size_type position = npos) const { if (empty()) { @@ -1866,7 +1920,7 @@ namespace etl ///\param str The content to find ///\param pos The position to start searching from. //********************************************************************* - size_t find_first_not_of(const ibasic_string& str, size_t position = 0) const + size_type find_first_not_of(const ibasic_string& str, size_type position = 0) const { return find_first_not_of(str.c_str(), position, str.size()); } @@ -1876,7 +1930,7 @@ namespace etl ///\param s Pointer to the content to not find ///\param pos The position to start searching from. //********************************************************************* - size_t find_first_not_of(const_pointer s, size_t position = 0) const + size_type find_first_not_of(const_pointer s, size_type position = 0) const { return find_first_not_of(s, position, etl::strlen(s)); } @@ -1887,15 +1941,15 @@ namespace etl ///\param pos The position to start searching from. ///\param n The number of characters to search for. //********************************************************************* - size_t find_first_not_of(const_pointer s, size_t position, size_t n) const + size_type find_first_not_of(const_pointer s, size_type position, size_type n) const { if (position < size()) { - for (size_t i = position; i < size(); ++i) + for (size_type i = position; i < size(); ++i) { bool found = false; - for (size_t j = 0; j < n; ++j) + for (size_type j = 0; j < n; ++j) { if (p_buffer[i] == s[j]) { @@ -1918,11 +1972,11 @@ namespace etl ///\param c The character to not find ///\param pos The position to start searching from. //********************************************************************* - size_t find_first_not_of(value_type c, size_t position = 0) const + size_type find_first_not_of(value_type c, size_type position = 0) const { if (position < size()) { - for (size_t i = position; i < size(); ++i) + for (size_type i = position; i < size(); ++i) { if (p_buffer[i] != c) { @@ -1939,7 +1993,7 @@ namespace etl ///\param str The content to find ///\param pos The position to start searching from. //********************************************************************* - size_t find_last_not_of(const ibasic_string& str, size_t position = npos) const + size_type find_last_not_of(const ibasic_string& str, size_type position = npos) const { return find_last_not_of(str.c_str(), position, str.size()); } @@ -1949,7 +2003,7 @@ namespace etl ///\param s The pointer to the content to find ///\param pos The position to start searching from. //********************************************************************* - size_t find_last_not_of(const_pointer s, size_t position = npos) const + size_type find_last_not_of(const_pointer s, size_type position = npos) const { return find_last_not_of(s, position, etl::strlen(s)); } @@ -1960,7 +2014,7 @@ namespace etl ///\param pos The position to start searching from. ///\param n The number of characters to use. //********************************************************************* - size_t find_last_not_of(const_pointer s, size_t position, size_t n) const + size_type find_last_not_of(const_pointer s, size_type position, size_type n) const { if (empty()) { @@ -1975,7 +2029,7 @@ namespace etl { bool found = false; - for (size_t j = 0; j < n; ++j) + for (size_type j = 0; j < n; ++j) { if (p_buffer[position] == s[j]) { @@ -1998,7 +2052,7 @@ namespace etl //********************************************************************* // //********************************************************************* - size_t find_last_not_of(value_type c, size_t position = npos) const + size_type find_last_not_of(value_type c, size_type position = npos) const { if (empty()) { @@ -2071,7 +2125,7 @@ namespace etl //************************************************************************* ibasic_string& operator += (T rhs) { - append(size_t(1), rhs); + append(size_type(1), rhs); return *this; } @@ -2088,7 +2142,7 @@ namespace etl //********************************************************************* /// Constructor. //********************************************************************* - ibasic_string(T* p_buffer_, size_t MAX_SIZE_) + ibasic_string(T* p_buffer_, size_type MAX_SIZE_) : string_base(MAX_SIZE_), p_buffer(p_buffer_) { @@ -2102,7 +2156,9 @@ namespace etl current_size = 0; cleanup(); p_buffer[0] = 0; +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED is_truncated = false; +#endif } //************************************************************************* @@ -2160,10 +2216,12 @@ namespace etl //************************************************************************* void cleanup() { +#if ETL_STRING_CLEAR_AFTER_USE_ENABLED if (is_secure()) { etl::memory_clear_range(&p_buffer[current_size], &p_buffer[CAPACITY]); } +#endif } //************************************************************************* @@ -2187,10 +2245,12 @@ namespace etl #endif ~ibasic_string() { +#if ETL_STRING_CLEAR_AFTER_USE_ENABLED if (is_secure()) { initialise(); } +#endif } }; diff --git a/include/etl/platform.h b/include/etl/platform.h index 48d47665..e97a840e 100644 --- a/include/etl/platform.h +++ b/include/etl/platform.h @@ -101,6 +101,20 @@ SOFTWARE. #define ETL_EXPLICIT_STRING_FROM_CHAR #endif +// Option to disable truncation checks for strings. +#if defined(ETL_DISABLE_STRING_TRUNCATION_CHECKS) + #define ETL_STRING_TRUNCATION_CHECKS_ENABLED 0 +#else + #define ETL_STRING_TRUNCATION_CHECKS_ENABLED 1 +#endif + +// Option to disable clear-after-use functionality for strings. +#if defined(ETL_DISABLE_STRING_CLEAR_AFTER_USE) + #define ETL_STRING_CLEAR_AFTER_USE_ENABLED 0 +#else + #define ETL_STRING_CLEAR_AFTER_USE_ENABLED 1 +#endif + // The macros below are dependent on the profile. // C++11 #if ETL_CPP11_SUPPORTED && !defined(ETL_FORCE_NO_ADVANCED_CPP) diff --git a/include/etl/string.h b/include/etl/string.h index be996692..4f3b67bc 100644 --- a/include/etl/string.h +++ b/include/etl/string.h @@ -105,6 +105,7 @@ namespace etl this->assign(other.begin() + position, other.begin() + position + length_); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (other.truncated()) { this->is_truncated = true; @@ -113,6 +114,7 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif } //************************************************************************* @@ -141,7 +143,7 @@ namespace etl ///\param initialSize The initial size of the string. ///\param value The value to fill the string with. //************************************************************************* - string(size_t count, value_type c) + string(size_type count, value_type c) : istring(reinterpret_cast(&buffer), MAX_SIZE) { this->initialise(); @@ -187,7 +189,7 @@ namespace etl ///\param position The position of the first character. Default = 0. ///\param length The number of characters. Default = npos. //************************************************************************* - etl::string substr(size_t position = 0, size_t length_ = npos) const + etl::string substr(size_type position = 0, size_type length_ = npos) const { etl::string new_string; @@ -270,11 +272,12 @@ namespace etl typedef istring interface_type; typedef istring::value_type value_type; + typedef istring::size_type size_type; //************************************************************************* /// Constructor. //************************************************************************* - string(value_type* buffer, size_t buffer_size) + string(value_type* buffer, size_type buffer_size) : istring(buffer, buffer_size - 1U) { this->initialise(); @@ -284,7 +287,7 @@ namespace etl /// Copy constructor. ///\param other The other string. //************************************************************************* - string(const etl::string<0U>& other, value_type* buffer, size_t buffer_size) + string(const etl::string<0U>& other, value_type* buffer, size_type buffer_size) : istring(buffer, buffer_size - 1U) { this->assign(other); @@ -294,7 +297,7 @@ namespace etl /// From other istring. ///\param other The other istring. //************************************************************************* - string(const etl::istring& other, value_type* buffer, size_t buffer_size) + string(const etl::istring& other, value_type* buffer, size_type buffer_size) : istring(buffer, buffer_size - 1U) { this->assign(other); @@ -306,13 +309,14 @@ namespace etl ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - string(const etl::istring& other, value_type* buffer, size_t buffer_size, size_t position, size_t length_ = npos) + string(const etl::istring& other, value_type* buffer, size_type buffer_size, size_type position, size_type length_ = npos) : istring(buffer, buffer_size - 1U) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (other.truncated()) { this->is_truncated = true; @@ -321,13 +325,14 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif } //************************************************************************* /// Constructor, from null terminated text. ///\param text The initial text of the string. //************************************************************************* - string(const value_type* text, value_type* buffer, size_t buffer_size) + string(const value_type* text, value_type* buffer, size_type buffer_size) : istring(buffer, buffer_size - 1U) { // Is the initial text at the same address as the buffer? @@ -346,7 +351,7 @@ namespace etl ///\param text The initial text of the string. ///\param count The number of characters to copy. //************************************************************************* - string(const value_type* text, size_t count, value_type* buffer, size_t buffer_size) + string(const value_type* text, size_type count, value_type* buffer, size_type buffer_size) : istring(buffer, buffer_size - 1U) { this->assign(text, text + count); @@ -357,7 +362,7 @@ namespace etl ///\param initialSize The initial size of the string. ///\param value The value to fill the string with. //************************************************************************* - string(size_t count, value_type c, value_type* buffer, size_t buffer_size) + string(size_type count, value_type c, value_type* buffer, size_type buffer_size) : istring(buffer, buffer_size - 1U) { this->initialise(); @@ -371,7 +376,7 @@ namespace etl ///\param last The iterator to the last element + 1. //************************************************************************* template - string(TIterator first, TIterator last, value_type* buffer, size_t buffer_size) + string(TIterator first, TIterator last, value_type* buffer, size_type buffer_size) : istring(buffer, buffer_size - 1U) { this->assign(first, last); @@ -381,7 +386,7 @@ namespace etl //************************************************************************* /// Construct from initializer_list. //************************************************************************* - string(std::initializer_list init, value_type* buffer, size_t buffer_size) + string(std::initializer_list init, value_type* buffer, size_type buffer_size) : istring(buffer, buffer_size - 1U) { this->assign(init.begin(), init.end()); @@ -392,7 +397,7 @@ namespace etl /// From string_view. ///\param view The string_view. //************************************************************************* - explicit string(const etl::string_view& view, value_type* buffer, size_t buffer_size) + explicit string(const etl::string_view& view, value_type* buffer, size_type buffer_size) : istring(buffer, buffer_size - 1U) { this->assign(view.begin(), view.end()); @@ -456,7 +461,7 @@ namespace etl size_t operator()(const etl::istring& text) const { return etl::private_hash::generic_hash(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + reinterpret_cast(&text[text.size()])); } }; @@ -466,7 +471,7 @@ namespace etl size_t operator()(const etl::string& text) const { return etl::private_hash::generic_hash(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + reinterpret_cast(&text[text.size()])); } }; #endif diff --git a/include/etl/string_utilities.h b/include/etl/string_utilities.h index 6701b579..d17c9a5b 100644 --- a/include/etl/string_utilities.h +++ b/include/etl/string_utilities.h @@ -113,7 +113,7 @@ namespace etl template void trim_from_left(TIString& s, typename TIString::const_pointer trim_characters) { - size_t position = s.find_first_not_of(trim_characters); + typename TIString::size_type position = s.find_first_not_of(trim_characters); s.erase(0U, position); } @@ -134,7 +134,7 @@ namespace etl template TStringView trim_from_view_left(const TStringView& view, typename TStringView::const_pointer trim_characters) { - size_t first = view.find_first_not_of(trim_characters); + typename TStringView::size_type first = view.find_first_not_of(trim_characters); typename TStringView::const_pointer pbegin = view.data() + view.size(); @@ -163,7 +163,7 @@ namespace etl template void trim_left(TIString& s, typename TIString::const_pointer delimiters) { - size_t p = s.find_first_of(delimiters); + typename TIString::size_type p = s.find_first_of(delimiters); if (p == TIString::npos) { @@ -182,7 +182,7 @@ namespace etl template TStringView trim_view_left(const TStringView& view, typename TStringView::const_pointer delimiters) { - size_t first = view.find_first_of(delimiters); + typename TStringView::size_type first = view.find_first_of(delimiters); typename TStringView::const_pointer pbegin = view.data(); @@ -193,7 +193,7 @@ namespace etl } else { - return TStringView(pbegin, size_t(0U)); + return TStringView(pbegin, typename TStringView::size_type(0U)); } } @@ -224,7 +224,7 @@ namespace etl template TStringView trim_from_view_right(const TStringView& view, typename TStringView::const_pointer trim_characters) { - size_t last = view.find_last_not_of(trim_characters) + 1; + typename TStringView::size_type last = view.find_last_not_of(trim_characters) + 1; typename TStringView::const_pointer pend = view.data(); @@ -252,7 +252,7 @@ namespace etl template void trim_right(TIString& s, typename TIString::const_pointer delimiters) { - size_t p = s.find_last_of(delimiters); + typename TIString::size_type p = s.find_last_of(delimiters); if (p == TIString::npos) { @@ -275,7 +275,7 @@ namespace etl template TStringView trim_view_right(const TStringView& view, typename TStringView::const_pointer delimiters) { - size_t last = view.find_last_of(delimiters) + 1; + typename TStringView::size_type last = view.find_last_of(delimiters) + 1; typename TStringView::const_pointer pend = view.data(); @@ -286,7 +286,7 @@ namespace etl } else { - return TStringView(view.data(), size_t(0U)); + return TStringView(view.data(), typename TStringView::size_type(0U)); } } @@ -318,8 +318,8 @@ namespace etl template TStringView trim_from_view(const TStringView& view, typename TStringView::const_pointer trim_characters) { - size_t first = view.find_first_not_of(trim_characters); - size_t last = view.find_last_not_of(trim_characters) + 1; + typename TStringView::size_type first = view.find_first_not_of(trim_characters); + typename TStringView::size_type last = view.find_last_not_of(trim_characters) + 1; typename TStringView::const_pointer pbegin = view.data(); typename TStringView::const_pointer pend = view.data(); @@ -365,8 +365,8 @@ namespace etl template TStringView trim_view(const TStringView& view, typename TStringView::const_pointer delimiters) { - size_t first = view.find_first_of(delimiters); - size_t last = view.find_last_of(delimiters) + 1; + typename TStringView::size_type first = view.find_first_of(delimiters); + typename TStringView::size_type last = view.find_last_of(delimiters) + 1; typename TStringView::const_pointer pbegin = view.data(); typename TStringView::const_pointer pend = view.data(); @@ -388,7 +388,7 @@ namespace etl /// Get up to the first n characters. //*************************************************************************** template - void left_n(TIString& s, size_t n) + void left_n(TIString& s, typename TIString::size_type n) { n = (n > s.size()) ? s.size() : n; @@ -399,7 +399,7 @@ namespace etl /// Get a view of up to the first n characters. //*************************************************************************** template - TStringView left_n_view(const TStringView& view, size_t n) + TStringView left_n_view(const TStringView& view, typename TStringView::size_type n) { n = (n > view.size()) ? view.size() : n; @@ -410,7 +410,7 @@ namespace etl /// Get up to the last n characters. //*************************************************************************** template - void right_n(TIString& s, size_t n) + void right_n(TIString& s, typename TIString::size_type n) { n = (n > s.size()) ? s.size() : n; @@ -421,7 +421,7 @@ namespace etl /// Get a view of up to the last n characters. //*************************************************************************** template - TStringView right_n_view(const TStringView& view, size_t n) + TStringView right_n_view(const TStringView& view, typename TStringView::size_type n) { n = (n > view.size()) ? view.size() : n; @@ -466,15 +466,15 @@ namespace etl const typename TIString::value_type* p_old = pairsbegin->first; const typename TIString::value_type* p_new = pairsbegin->second; - size_t position = 0U; + typename TIString::size_type position = 0U; do { position = s.find(p_old, position); if (position != TIString::npos) { - s.replace(position, etl::strlen(p_old), p_new, etl::strlen(p_new)); - position += etl::strlen(p_new); + s.replace(position, typename TIString::size_type(etl::strlen(p_old)), p_new, typename TIString::size_type(etl::strlen(p_new))); + position += typename TIString::size_type(etl::strlen(p_new)); } } while (position != TIString::npos); @@ -703,7 +703,7 @@ namespace etl typedef typename TInput::const_pointer const_pointer; bool token_found = false; - size_t position = 0U; + typename TStringView::size_type position = 0U; TStringView view = last_view.value_or(TStringView()); const_pointer begin_ptr = input.data(); @@ -744,14 +744,14 @@ namespace etl /// pad_left //*************************************************************************** template - void pad_left(TIString& s, size_t required_size, typename TIString::value_type pad_char) + void pad_left(TIString& s, typename TIString::size_type required_size, typename TIString::value_type pad_char) { required_size = etl::min(required_size, s.max_size()); if (required_size > s.size()) { required_size -= s.size(); - s.insert(size_t(0U), required_size, pad_char); + s.insert(typename TIString::size_type(0U), required_size, pad_char); } } @@ -759,7 +759,7 @@ namespace etl /// pad_right //*************************************************************************** template - void pad_right(TIString& s, size_t required_size, typename TIString::value_type pad_char) + void pad_right(TIString& s, typename TIString::size_type required_size, typename TIString::value_type pad_char) { required_size = etl::min(required_size, s.max_size()); @@ -774,7 +774,7 @@ namespace etl /// pad //*************************************************************************** template - void pad(TIString& s, size_t required_size, string_pad_direction pad_direction, typename TIString::value_type pad_char) + void pad(TIString& s, typename TIString::size_type required_size, string_pad_direction pad_direction, typename TIString::value_type pad_char) { switch (int(pad_direction)) { diff --git a/include/etl/u16string.h b/include/etl/u16string.h index c2a7db6e..0c3d3470 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -98,13 +98,14 @@ namespace etl ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - u16string(const etl::iu16string& other, size_t position, size_t length_ = npos) + u16string(const etl::iu16string& other, size_type position, size_type length_ = npos) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (other.truncated()) { this->is_truncated = true; @@ -113,6 +114,7 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif } //************************************************************************* @@ -130,7 +132,7 @@ namespace etl ///\param text The initial text of the u16string. ///\param count The number of characters to copy. //************************************************************************* - u16string(const value_type* text, size_t count) + u16string(const value_type* text, size_type count) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { this->assign(text, text + count); @@ -141,7 +143,7 @@ namespace etl ///\param initialSize The initial size of the u16string. ///\param value The value to fill the u16string with. //************************************************************************* - u16string(size_t count, value_type c) + u16string(size_type count, value_type c) : iu16string(reinterpret_cast(&buffer), MAX_SIZE) { this->initialise(); @@ -187,7 +189,7 @@ namespace etl ///\param position The position of the first character. Default = 0. ///\param length The number of characters. Default = npos. //************************************************************************* - etl::u16string substr(size_t position = 0, size_t length_ = npos) const + etl::u16string substr(size_type position = 0, size_type length_ = npos) const { etl::u16string new_string; @@ -260,7 +262,7 @@ namespace etl //************************************************************************* /// Constructor. //************************************************************************* - u16string(value_type* buffer, size_t buffer_size) + u16string(value_type* buffer, size_type buffer_size) : iu16string(buffer, buffer_size - 1U) { this->initialise(); @@ -270,7 +272,7 @@ namespace etl /// Copy constructor. ///\param other The other u16string. //************************************************************************* - u16string(const etl::u16string<0U>& other, value_type* buffer, size_t buffer_size) + u16string(const etl::u16string<0U>& other, value_type* buffer, size_type buffer_size) : iu16string(buffer, buffer_size - 1U) { this->assign(other); @@ -280,7 +282,7 @@ namespace etl /// From other iu16string. ///\param other The other iu16string. //************************************************************************* - u16string(const etl::iu16string& other, value_type* buffer, size_t buffer_size) + u16string(const etl::iu16string& other, value_type* buffer, size_type buffer_size) : iu16string(buffer, buffer_size - 1U) { this->assign(other); @@ -292,13 +294,14 @@ namespace etl ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - u16string(const etl::iu16string& other, value_type* buffer, size_t buffer_size, size_t position, size_t length_ = npos) + u16string(const etl::iu16string& other, value_type* buffer, size_type buffer_size, size_type position, size_type length_ = npos) : iu16string(buffer, buffer_size - 1U) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (other.truncated()) { this->is_truncated = true; @@ -307,13 +310,14 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(u16string_truncation)); #endif } +#endif } //************************************************************************* /// Constructor, from null terminated text. ///\param text The initial text of the u16string. //************************************************************************* - u16string(const value_type* text, value_type* buffer, size_t buffer_size) + u16string(const value_type* text, value_type* buffer, size_type buffer_size) : iu16string(buffer, buffer_size - 1U) { // Is the initial text at the same address as the buffer? @@ -332,7 +336,7 @@ namespace etl ///\param text The initial text of the u16string. ///\param count The number of characters to copy. //************************************************************************* - u16string(const value_type* text, size_t count, value_type* buffer, size_t buffer_size) + u16string(const value_type* text, size_type count, value_type* buffer, size_type buffer_size) : iu16string(buffer, buffer_size - 1U) { this->assign(text, text + count); @@ -343,7 +347,7 @@ namespace etl ///\param initialSize The initial size of the u16string. ///\param value The value to fill the u16string with. //************************************************************************* - u16string(size_t count, value_type c, value_type* buffer, size_t buffer_size) + u16string(size_type count, value_type c, value_type* buffer, size_type buffer_size) : iu16string(buffer, buffer_size - 1U) { this->initialise(); @@ -357,7 +361,7 @@ namespace etl ///\param last The iterator to the last element + 1. //************************************************************************* template - u16string(TIterator first, TIterator last, value_type* buffer, size_t buffer_size) + u16string(TIterator first, TIterator last, value_type* buffer, size_type buffer_size) : iu16string(buffer, buffer_size - 1U) { this->assign(first, last); @@ -367,7 +371,7 @@ namespace etl //************************************************************************* /// Construct from initializer_list. //************************************************************************* - u16string(std::initializer_list init, value_type* buffer, size_t buffer_size) + u16string(std::initializer_list init, value_type* buffer, size_type buffer_size) : iu16string(buffer, buffer_size - 1U) { this->assign(init.begin(), init.end()); @@ -378,7 +382,7 @@ namespace etl /// From u16string_view. ///\param view The u16string_view. //************************************************************************* - explicit u16string(const etl::u16string_view& view, value_type* buffer, size_t buffer_size) + explicit u16string(const etl::u16string_view& view, value_type* buffer, size_type buffer_size) : iu16string(buffer, buffer_size - 1U) { this->assign(view.begin(), view.end()); @@ -442,7 +446,7 @@ namespace etl size_t operator()(const etl::iu16string& text) const { return etl::private_hash::generic_hash(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + reinterpret_cast(&text[text.size()])); } }; @@ -452,7 +456,7 @@ namespace etl size_t operator()(const etl::u16string& text) const { return etl::private_hash::generic_hash(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + reinterpret_cast(&text[text.size()])); } }; #endif diff --git a/include/etl/u32string.h b/include/etl/u32string.h index 7e729260..ac31751c 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -98,13 +98,14 @@ namespace etl ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - u32string(const etl::iu32string& other, size_t position, size_t length_ = npos) + u32string(const etl::iu32string& other, size_type position, size_type length_ = npos) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (other.truncated()) { this->is_truncated = true; @@ -113,6 +114,7 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif } //************************************************************************* @@ -130,7 +132,7 @@ namespace etl ///\param text The initial text of the u32string. ///\param count The number of characters to copy. //************************************************************************* - u32string(const value_type* text, size_t count) + u32string(const value_type* text, size_type count) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { this->assign(text, text + count); @@ -141,7 +143,7 @@ namespace etl ///\param initialSize The initial size of the u32string. ///\param value The value to fill the u32string with. //************************************************************************* - u32string(size_t count, value_type c) + u32string(size_type count, value_type c) : iu32string(reinterpret_cast(&buffer), MAX_SIZE) { this->initialise(); @@ -187,7 +189,7 @@ namespace etl ///\param position The position of the first character. Default = 0. ///\param length The number of characters. Default = npos. //************************************************************************* - etl::u32string substr(size_t position = 0, size_t length_ = npos) const + etl::u32string substr(size_type position = 0, size_type length_ = npos) const { etl::u32string new_string; @@ -260,7 +262,7 @@ namespace etl //************************************************************************* /// Constructor. //************************************************************************* - u32string(value_type* buffer, size_t buffer_size) + u32string(value_type* buffer, size_type buffer_size) : iu32string(buffer, buffer_size - 1U) { this->initialise(); @@ -270,7 +272,7 @@ namespace etl /// Copy constructor. ///\param other The other u32string. //************************************************************************* - u32string(const etl::u32string<0U>& other, value_type* buffer, size_t buffer_size) + u32string(const etl::u32string<0U>& other, value_type* buffer, size_type buffer_size) : iu32string(buffer, buffer_size - 1U) { this->assign(other); @@ -280,7 +282,7 @@ namespace etl /// From other iu32string. ///\param other The other iu32string. //************************************************************************* - u32string(const etl::iu32string& other, value_type* buffer, size_t buffer_size) + u32string(const etl::iu32string& other, value_type* buffer, size_type buffer_size) : iu32string(buffer, buffer_size - 1U) { this->assign(other); @@ -292,13 +294,14 @@ namespace etl ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - u32string(const etl::iu32string& other, value_type* buffer, size_t buffer_size, size_t position, size_t length_ = npos) + u32string(const etl::iu32string& other, value_type* buffer, size_type buffer_size, size_type position, size_type length_ = npos) : iu32string(buffer, buffer_size - 1U) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (other.truncated()) { this->is_truncated = true; @@ -307,19 +310,20 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(u32string_truncation)); #endif } +#endif } //************************************************************************* /// Constructor, from null terminated text. ///\param text The initial text of the u32string. //************************************************************************* - ETL_EXPLICIT_STRING_FROM_CHAR u32string(const value_type* text, value_type* buffer, size_t buffer_size) + ETL_EXPLICIT_STRING_FROM_CHAR u32string(const value_type* text, value_type* buffer, size_type buffer_size) : iu32string(buffer, buffer_size - 1U) { // Is the initial text at the same address as the buffer? if (text == buffer) { - this->current_size = etl::strlen(buffer); + this->current_size = etl::strlen(buffer);; } else { @@ -332,7 +336,7 @@ namespace etl ///\param text The initial text of the u32string. ///\param count The number of characters to copy. //************************************************************************* - u32string(const value_type* text, size_t count, value_type* buffer, size_t buffer_size) + u32string(const value_type* text, size_type count, value_type* buffer, size_type buffer_size) : iu32string(buffer, buffer_size - 1U) { this->assign(text, text + count); @@ -343,7 +347,7 @@ namespace etl ///\param initialSize The initial size of the u32string. ///\param value The value to fill the u32string with. //************************************************************************* - u32string(size_t count, value_type c, value_type* buffer, size_t buffer_size) + u32string(size_type count, value_type c, value_type* buffer, size_type buffer_size) : iu32string(buffer, buffer_size - 1U) { this->initialise(); @@ -357,7 +361,7 @@ namespace etl ///\param last The iterator to the last element + 1. //************************************************************************* template - u32string(TIterator first, TIterator last, value_type* buffer, size_t buffer_size) + u32string(TIterator first, TIterator last, value_type* buffer, size_type buffer_size) : iu32string(buffer, buffer_size - 1U) { this->assign(first, last); @@ -367,7 +371,7 @@ namespace etl //************************************************************************* /// Construct from initializer_list. //************************************************************************* - u32string(std::initializer_list init, value_type* buffer, size_t buffer_size) + u32string(std::initializer_list init, value_type* buffer, size_type buffer_size) : iu32string(buffer, buffer_size - 1U) { this->assign(init.begin(), init.end()); @@ -378,7 +382,7 @@ namespace etl /// From u32string_view. ///\param view The u32string_view. //************************************************************************* - explicit u32string(const etl::u32string_view& view, value_type* buffer, size_t buffer_size) + explicit u32string(const etl::u32string_view& view, value_type* buffer, size_type buffer_size) : iu32string(buffer, buffer_size - 1U) { this->assign(view.begin(), view.end()); @@ -442,7 +446,7 @@ namespace etl size_t operator()(const etl::iu32string& text) const { return etl::private_hash::generic_hash(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + reinterpret_cast(&text[text.size()])); } }; @@ -452,7 +456,7 @@ namespace etl size_t operator()(const etl::u32string& text) const { return etl::private_hash::generic_hash(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + reinterpret_cast(&text[text.size()])); } }; #endif diff --git a/include/etl/wstring.h b/include/etl/wstring.h index 22403529..2e66dc40 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -92,20 +92,20 @@ namespace etl this->assign(other); } - //************************************************************************* /// From other string, position, length. ///\param other The other string. ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - wstring(const etl::iwstring& other, size_t position, size_t length_ = npos) + wstring(const etl::iwstring& other, size_type position, size_type length_ = npos) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (other.truncated()) { this->is_truncated = true; @@ -114,6 +114,7 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(string_truncation)); #endif } +#endif } //************************************************************************* @@ -131,7 +132,7 @@ namespace etl ///\param text The initial text of the wstring. ///\param count The number of characters to copy. //************************************************************************* - wstring(const value_type* text, size_t count) + wstring(const value_type* text, size_type count) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { this->assign(text, text + count); @@ -142,7 +143,7 @@ namespace etl ///\param initialSize The initial size of the wstring. ///\param value The value to fill the wstring with. //************************************************************************* - wstring(size_t count, value_type c) + wstring(size_type count, value_type c) : iwstring(reinterpret_cast(&buffer), MAX_SIZE) { this->initialise(); @@ -188,7 +189,7 @@ namespace etl ///\param position The position of the first character. Default = 0. ///\param length The number of characters. Default = npos. //************************************************************************* - etl::wstring substr(size_t position = 0, size_t length_ = npos) const + etl::wstring substr(size_type position = 0, size_type length_ = npos) const { etl::wstring new_string; @@ -261,7 +262,7 @@ namespace etl //************************************************************************* /// Constructor. //************************************************************************* - wstring(value_type* buffer, size_t buffer_size) + wstring(value_type* buffer, size_type buffer_size) : iwstring(buffer, buffer_size - 1U) { this->initialise(); @@ -271,7 +272,7 @@ namespace etl /// Copy constructor. ///\param other The other wstring. //************************************************************************* - wstring(const etl::wstring<0U>& other, value_type* buffer, size_t buffer_size) + wstring(const etl::wstring<0U>& other, value_type* buffer, size_type buffer_size) : iwstring(buffer, buffer_size - 1U) { this->assign(other); @@ -281,7 +282,7 @@ namespace etl /// From other iwstring. ///\param other The other iwstring. //************************************************************************* - wstring(const etl::iwstring& other, value_type* buffer, size_t buffer_size) + wstring(const etl::iwstring& other, value_type* buffer, size_type buffer_size) : iwstring(buffer, buffer_size - 1U) { this->assign(other); @@ -293,13 +294,14 @@ namespace etl ///\param position The position of the first character. ///\param length The number of characters. Default = npos. //************************************************************************* - wstring(const etl::iwstring& other, value_type* buffer, size_t buffer_size, size_t position, size_t length_ = npos) + wstring(const etl::iwstring& other, value_type* buffer, size_type buffer_size, size_type position, size_type length_ = npos) : iwstring(buffer, buffer_size - 1U) { ETL_ASSERT(position < other.size(), ETL_ERROR(string_out_of_bounds)); this->assign(other.begin() + position, other.begin() + position + length_); +#if ETL_STRING_TRUNCATION_CHECKS_ENABLED if (other.truncated()) { this->is_truncated = true; @@ -308,13 +310,14 @@ namespace etl ETL_ALWAYS_ASSERT(ETL_ERROR(wstring_truncation)); #endif } +#endif } //************************************************************************* /// Constructor, from null terminated text. ///\param text The initial text of the wstring. //************************************************************************* - wstring(const value_type* text, value_type* buffer, size_t buffer_size) + wstring(const value_type* text, value_type* buffer, size_type buffer_size) : iwstring(buffer, buffer_size - 1U) { // Is the initial text at the same address as the buffer? @@ -333,7 +336,7 @@ namespace etl ///\param text The initial text of the wstring. ///\param count The number of characters to copy. //************************************************************************* - wstring(const value_type* text, size_t count, value_type* buffer, size_t buffer_size) + wstring(const value_type* text, size_type count, value_type* buffer, size_type buffer_size) : iwstring(buffer, buffer_size - 1U) { this->assign(text, text + count); @@ -344,7 +347,7 @@ namespace etl ///\param initialSize The initial size of the wstring. ///\param value The value to fill the wstring with. //************************************************************************* - wstring(size_t count, value_type c, value_type* buffer, size_t buffer_size) + wstring(size_type count, value_type c, value_type* buffer, size_type buffer_size) : iwstring(buffer, buffer_size - 1U) { this->initialise(); @@ -358,7 +361,7 @@ namespace etl ///\param last The iterator to the last element + 1. //************************************************************************* template - wstring(TIterator first, TIterator last, value_type* buffer, size_t buffer_size) + wstring(TIterator first, TIterator last, value_type* buffer, size_type buffer_size) : iwstring(buffer, buffer_size - 1U) { this->assign(first, last); @@ -368,7 +371,7 @@ namespace etl //************************************************************************* /// Construct from initializer_list. //************************************************************************* - wstring(std::initializer_list init, value_type* buffer, size_t buffer_size) + wstring(std::initializer_list init, value_type* buffer, size_type buffer_size) : iwstring(buffer, buffer_size - 1U) { this->assign(init.begin(), init.end()); @@ -379,7 +382,7 @@ namespace etl /// From wstring_view. ///\param view The wstring_view. //************************************************************************* - explicit wstring(const etl::wstring_view& view, value_type* buffer, size_t buffer_size) + explicit wstring(const etl::wstring_view& view, value_type* buffer, size_type buffer_size) : iwstring(buffer, buffer_size - 1U) { this->assign(view.begin(), view.end()); @@ -443,7 +446,7 @@ namespace etl size_t operator()(const etl::iwstring& text) const { return etl::private_hash::generic_hash(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + reinterpret_cast(&text[text.size()])); } }; @@ -453,7 +456,7 @@ namespace etl size_t operator()(const etl::wstring& text) const { return etl::private_hash::generic_hash(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + reinterpret_cast(&text[text.size()])); } }; #endif diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index 52f76a84..65b731dd 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -127,6 +127,29 @@ + + @@ -211,6 +234,7 @@